xref: /aoo41x/main/basic/source/runtime/ddectrl.cxx (revision e1f63238)
1*e1f63238SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e1f63238SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e1f63238SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e1f63238SAndrew Rist  * distributed with this work for additional information
6*e1f63238SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e1f63238SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e1f63238SAndrew Rist  * "License"); you may not use this file except in compliance
9*e1f63238SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e1f63238SAndrew Rist  *
11*e1f63238SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e1f63238SAndrew Rist  *
13*e1f63238SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e1f63238SAndrew Rist  * software distributed under the License is distributed on an
15*e1f63238SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e1f63238SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e1f63238SAndrew Rist  * specific language governing permissions and limitations
18*e1f63238SAndrew Rist  * under the License.
19*e1f63238SAndrew Rist  *
20*e1f63238SAndrew Rist  *************************************************************/
21*e1f63238SAndrew Rist 
22*e1f63238SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_basic.hxx"
26cdf0e10cSrcweir #include <tools/errcode.hxx>
27cdf0e10cSrcweir #include <svl/svdde.hxx>
28cdf0e10cSrcweir #include "ddectrl.hxx"
29cdf0e10cSrcweir #ifndef _SBERRORS_HXX
30cdf0e10cSrcweir #include <basic/sberrors.hxx>
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #define DDE_FREECHANNEL	((DdeConnection*)0xffffffff)
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #define DDE_FIRSTERR	0x4000
36cdf0e10cSrcweir #define DDE_LASTERR		0x4011
37cdf0e10cSrcweir 
38cdf0e10cSrcweir static const SbError nDdeErrMap[] =
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 	/* DMLERR_ADVACKTIMEOUT 	  */  0x4000, SbERR_DDE_TIMEOUT,
41cdf0e10cSrcweir 	/* DMLERR_BUSY 				  */  0x4001, SbERR_DDE_BUSY,
42cdf0e10cSrcweir 	/* DMLERR_DATAACKTIMEOUT 	  */  0x4002, SbERR_DDE_TIMEOUT,
43cdf0e10cSrcweir 	/* DMLERR_DLL_NOT_INITIALIZED */  0x4003, SbERR_DDE_ERROR,
44cdf0e10cSrcweir 	/* DMLERR_DLL_USAGE           */  0x4004, SbERR_DDE_ERROR,
45cdf0e10cSrcweir 	/* DMLERR_EXECACKTIMEOUT      */  0x4005, SbERR_DDE_TIMEOUT,
46cdf0e10cSrcweir 	/* DMLERR_INVALIDPARAMETER    */  0x4006, SbERR_DDE_ERROR,
47cdf0e10cSrcweir 	/* DMLERR_LOW_MEMORY          */  0x4007, SbERR_DDE_ERROR,
48cdf0e10cSrcweir 	/* DMLERR_MEMORY_ERROR        */  0x4008, SbERR_DDE_ERROR,
49cdf0e10cSrcweir 	/* DMLERR_NOTPROCESSED        */  0x4009, SbERR_DDE_NOTPROCESSED,
50cdf0e10cSrcweir 	/* DMLERR_NO_CONV_ESTABLISHED */  0x400a, SbERR_DDE_NO_CHANNEL,
51cdf0e10cSrcweir 	/* DMLERR_POKEACKTIMEOUT      */  0x400b, SbERR_DDE_TIMEOUT,
52cdf0e10cSrcweir 	/* DMLERR_POSTMSG_FAILED      */  0x400c, SbERR_DDE_QUEUE_OVERFLOW,
53cdf0e10cSrcweir 	/* DMLERR_REENTRANCY          */  0x400d, SbERR_DDE_ERROR,
54cdf0e10cSrcweir 	/* DMLERR_SERVER_DIED         */  0x400e, SbERR_DDE_PARTNER_QUIT,
55cdf0e10cSrcweir 	/* DMLERR_SYS_ERROR           */  0x400f, SbERR_DDE_ERROR,
56cdf0e10cSrcweir 	/* DMLERR_UNADVACKTIMEOUT     */  0x4010, SbERR_DDE_TIMEOUT,
57cdf0e10cSrcweir 	/* DMLERR_UNFOUND_QUEUE_ID    */  0x4011, SbERR_DDE_NO_CHANNEL
58cdf0e10cSrcweir };
59cdf0e10cSrcweir 
GetLastErr(DdeConnection * pConv)60cdf0e10cSrcweir SbError SbiDdeControl::GetLastErr( DdeConnection* pConv )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 	if( !pConv )
63cdf0e10cSrcweir 		return 0;
64cdf0e10cSrcweir 	long nErr = pConv->GetError();
65cdf0e10cSrcweir 	if( !nErr )
66cdf0e10cSrcweir 		return 0;
67cdf0e10cSrcweir 	if( nErr < DDE_FIRSTERR || nErr > DDE_LASTERR )
68cdf0e10cSrcweir 		return SbERR_DDE_ERROR;
69cdf0e10cSrcweir 	return nDdeErrMap[ 2*(nErr - DDE_FIRSTERR) + 1 ];
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir IMPL_LINK_INLINE( SbiDdeControl,Data , DdeData*, pData,
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     aData = String::CreateFromAscii( (char*)(const void*)*pData );
75cdf0e10cSrcweir 	return 1;
76cdf0e10cSrcweir }
77cdf0e10cSrcweir )
78cdf0e10cSrcweir 
SbiDdeControl()79cdf0e10cSrcweir SbiDdeControl::SbiDdeControl()
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	pConvList = new DdeConnections;
82cdf0e10cSrcweir 	DdeConnection* pPtr = DDE_FREECHANNEL;
83cdf0e10cSrcweir 	pConvList->Insert( pPtr );
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
~SbiDdeControl()86cdf0e10cSrcweir SbiDdeControl::~SbiDdeControl()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	TerminateAll();
89cdf0e10cSrcweir 	delete pConvList;
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
GetFreeChannel()92cdf0e10cSrcweir sal_Int16 SbiDdeControl::GetFreeChannel()
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	sal_Int16 nListSize = (sal_Int16)pConvList->Count();
95cdf0e10cSrcweir 	DdeConnection* pPtr = pConvList->First();
96cdf0e10cSrcweir 	pPtr = pConvList->Next(); // nullten eintrag ueberspringen
97cdf0e10cSrcweir 	sal_Int16 nChannel;
98cdf0e10cSrcweir 	for( nChannel = 1; nChannel < nListSize; nChannel++ )
99cdf0e10cSrcweir 	{
100cdf0e10cSrcweir 		if( pPtr == DDE_FREECHANNEL )
101cdf0e10cSrcweir 			return nChannel;
102cdf0e10cSrcweir 		pPtr = pConvList->Next();
103cdf0e10cSrcweir 	}
104cdf0e10cSrcweir 	pPtr = DDE_FREECHANNEL;
105cdf0e10cSrcweir 	pConvList->Insert( pPtr, LIST_APPEND );
106cdf0e10cSrcweir 	return nChannel;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
Initiate(const String & rService,const String & rTopic,sal_Int16 & rnHandle)109cdf0e10cSrcweir SbError SbiDdeControl::Initiate( const String& rService, const String& rTopic,
110cdf0e10cSrcweir 			sal_Int16& rnHandle )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	SbError nErr;
113cdf0e10cSrcweir 	DdeConnection* pConv = new DdeConnection( rService, rTopic );
114cdf0e10cSrcweir 	nErr = GetLastErr( pConv );
115cdf0e10cSrcweir 	if( nErr )
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir 		delete pConv;
118cdf0e10cSrcweir 		rnHandle = 0;
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir 	else
121cdf0e10cSrcweir 	{
122cdf0e10cSrcweir 		sal_Int16 nChannel = GetFreeChannel();
123cdf0e10cSrcweir 		pConvList->Replace( pConv, (sal_uIntPtr)nChannel );
124cdf0e10cSrcweir 		rnHandle = nChannel;
125cdf0e10cSrcweir 	}
126cdf0e10cSrcweir 	return 0;
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
Terminate(sal_Int16 nChannel)129cdf0e10cSrcweir SbError SbiDdeControl::Terminate( sal_Int16 nChannel )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir 	DdeConnection* pConv = pConvList->GetObject( (sal_uIntPtr)nChannel );
132cdf0e10cSrcweir 	if( !nChannel || !pConv || pConv == DDE_FREECHANNEL )
133cdf0e10cSrcweir 		return SbERR_DDE_NO_CHANNEL;
134cdf0e10cSrcweir 	pConvList->Replace( DDE_FREECHANNEL, (sal_uIntPtr)nChannel );
135cdf0e10cSrcweir 	delete pConv;
136cdf0e10cSrcweir 	return 0L;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
TerminateAll()139cdf0e10cSrcweir SbError SbiDdeControl::TerminateAll()
140cdf0e10cSrcweir {
141cdf0e10cSrcweir 	sal_Int16 nChannel = (sal_Int16)pConvList->Count();
142cdf0e10cSrcweir 	while( nChannel )
143cdf0e10cSrcweir 	{
144cdf0e10cSrcweir 		nChannel--;
145cdf0e10cSrcweir 		Terminate( nChannel );
146cdf0e10cSrcweir 	}
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	pConvList->Clear();
149cdf0e10cSrcweir 	DdeConnection* pPtr = DDE_FREECHANNEL;
150cdf0e10cSrcweir 	pConvList->Insert( pPtr );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 	return 0;
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
Request(sal_Int16 nChannel,const String & rItem,String & rResult)155cdf0e10cSrcweir SbError SbiDdeControl::Request( sal_Int16 nChannel, const String& rItem, String& rResult )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	DdeConnection* pConv = pConvList->GetObject( (sal_uIntPtr)nChannel );
158cdf0e10cSrcweir 	if( !nChannel || !pConv || pConv == DDE_FREECHANNEL )
159cdf0e10cSrcweir 		return SbERR_DDE_NO_CHANNEL;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	DdeRequest aRequest( *pConv, rItem, 30000 );
162cdf0e10cSrcweir 	aRequest.SetDataHdl( LINK( this, SbiDdeControl, Data ) );
163cdf0e10cSrcweir 	aRequest.Execute();
164cdf0e10cSrcweir 	rResult = aData;
165cdf0e10cSrcweir 	return GetLastErr( pConv );
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
Execute(sal_Int16 nChannel,const String & rCommand)168cdf0e10cSrcweir SbError SbiDdeControl::Execute( sal_Int16 nChannel, const String& rCommand )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir 	DdeConnection* pConv = pConvList->GetObject( (sal_uIntPtr)nChannel );
171cdf0e10cSrcweir 	if( !nChannel || !pConv || pConv == DDE_FREECHANNEL )
172cdf0e10cSrcweir 		return SbERR_DDE_NO_CHANNEL;
173cdf0e10cSrcweir 	DdeExecute aRequest( *pConv, rCommand, 30000 );
174cdf0e10cSrcweir 	aRequest.Execute();
175cdf0e10cSrcweir 	return GetLastErr( pConv );
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
Poke(sal_Int16 nChannel,const String & rItem,const String & rData)178cdf0e10cSrcweir SbError SbiDdeControl::Poke( sal_Int16 nChannel, const String& rItem, const String& rData )
179cdf0e10cSrcweir {
180cdf0e10cSrcweir 	DdeConnection* pConv = pConvList->GetObject( (sal_uIntPtr)nChannel );
181cdf0e10cSrcweir 	if( !nChannel || !pConv || pConv == DDE_FREECHANNEL )
182cdf0e10cSrcweir 		return SbERR_DDE_NO_CHANNEL;
183cdf0e10cSrcweir 	DdePoke aRequest( *pConv, rItem, DdeData(rData), 30000 );
184cdf0e10cSrcweir 	aRequest.Execute();
185cdf0e10cSrcweir 	return GetLastErr( pConv );
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 
189