xref: /aoo42x/main/basic/source/runtime/ddectrl.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basic.hxx"
30*cdf0e10cSrcweir #include <tools/errcode.hxx>
31*cdf0e10cSrcweir #include <svl/svdde.hxx>
32*cdf0e10cSrcweir #include "ddectrl.hxx"
33*cdf0e10cSrcweir #ifndef _SBERRORS_HXX
34*cdf0e10cSrcweir #include <basic/sberrors.hxx>
35*cdf0e10cSrcweir #endif
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #define DDE_FREECHANNEL	((DdeConnection*)0xffffffff)
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #define DDE_FIRSTERR	0x4000
40*cdf0e10cSrcweir #define DDE_LASTERR		0x4011
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir static const SbError nDdeErrMap[] =
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir 	/* DMLERR_ADVACKTIMEOUT 	  */  0x4000, SbERR_DDE_TIMEOUT,
45*cdf0e10cSrcweir 	/* DMLERR_BUSY 				  */  0x4001, SbERR_DDE_BUSY,
46*cdf0e10cSrcweir 	/* DMLERR_DATAACKTIMEOUT 	  */  0x4002, SbERR_DDE_TIMEOUT,
47*cdf0e10cSrcweir 	/* DMLERR_DLL_NOT_INITIALIZED */  0x4003, SbERR_DDE_ERROR,
48*cdf0e10cSrcweir 	/* DMLERR_DLL_USAGE           */  0x4004, SbERR_DDE_ERROR,
49*cdf0e10cSrcweir 	/* DMLERR_EXECACKTIMEOUT      */  0x4005, SbERR_DDE_TIMEOUT,
50*cdf0e10cSrcweir 	/* DMLERR_INVALIDPARAMETER    */  0x4006, SbERR_DDE_ERROR,
51*cdf0e10cSrcweir 	/* DMLERR_LOW_MEMORY          */  0x4007, SbERR_DDE_ERROR,
52*cdf0e10cSrcweir 	/* DMLERR_MEMORY_ERROR        */  0x4008, SbERR_DDE_ERROR,
53*cdf0e10cSrcweir 	/* DMLERR_NOTPROCESSED        */  0x4009, SbERR_DDE_NOTPROCESSED,
54*cdf0e10cSrcweir 	/* DMLERR_NO_CONV_ESTABLISHED */  0x400a, SbERR_DDE_NO_CHANNEL,
55*cdf0e10cSrcweir 	/* DMLERR_POKEACKTIMEOUT      */  0x400b, SbERR_DDE_TIMEOUT,
56*cdf0e10cSrcweir 	/* DMLERR_POSTMSG_FAILED      */  0x400c, SbERR_DDE_QUEUE_OVERFLOW,
57*cdf0e10cSrcweir 	/* DMLERR_REENTRANCY          */  0x400d, SbERR_DDE_ERROR,
58*cdf0e10cSrcweir 	/* DMLERR_SERVER_DIED         */  0x400e, SbERR_DDE_PARTNER_QUIT,
59*cdf0e10cSrcweir 	/* DMLERR_SYS_ERROR           */  0x400f, SbERR_DDE_ERROR,
60*cdf0e10cSrcweir 	/* DMLERR_UNADVACKTIMEOUT     */  0x4010, SbERR_DDE_TIMEOUT,
61*cdf0e10cSrcweir 	/* DMLERR_UNFOUND_QUEUE_ID    */  0x4011, SbERR_DDE_NO_CHANNEL
62*cdf0e10cSrcweir };
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir SbError SbiDdeControl::GetLastErr( DdeConnection* pConv )
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir 	if( !pConv )
67*cdf0e10cSrcweir 		return 0;
68*cdf0e10cSrcweir 	long nErr = pConv->GetError();
69*cdf0e10cSrcweir 	if( !nErr )
70*cdf0e10cSrcweir 		return 0;
71*cdf0e10cSrcweir 	if( nErr < DDE_FIRSTERR || nErr > DDE_LASTERR )
72*cdf0e10cSrcweir 		return SbERR_DDE_ERROR;
73*cdf0e10cSrcweir 	return nDdeErrMap[ 2*(nErr - DDE_FIRSTERR) + 1 ];
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir IMPL_LINK_INLINE( SbiDdeControl,Data , DdeData*, pData,
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir     aData = String::CreateFromAscii( (char*)(const void*)*pData );
79*cdf0e10cSrcweir 	return 1;
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir )
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir SbiDdeControl::SbiDdeControl()
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir 	pConvList = new DdeConnections;
86*cdf0e10cSrcweir 	DdeConnection* pPtr = DDE_FREECHANNEL;
87*cdf0e10cSrcweir 	pConvList->Insert( pPtr );
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir SbiDdeControl::~SbiDdeControl()
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	TerminateAll();
93*cdf0e10cSrcweir 	delete pConvList;
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir sal_Int16 SbiDdeControl::GetFreeChannel()
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir 	sal_Int16 nListSize = (sal_Int16)pConvList->Count();
99*cdf0e10cSrcweir 	DdeConnection* pPtr = pConvList->First();
100*cdf0e10cSrcweir 	pPtr = pConvList->Next(); // nullten eintrag ueberspringen
101*cdf0e10cSrcweir 	sal_Int16 nChannel;
102*cdf0e10cSrcweir 	for( nChannel = 1; nChannel < nListSize; nChannel++ )
103*cdf0e10cSrcweir 	{
104*cdf0e10cSrcweir 		if( pPtr == DDE_FREECHANNEL )
105*cdf0e10cSrcweir 			return nChannel;
106*cdf0e10cSrcweir 		pPtr = pConvList->Next();
107*cdf0e10cSrcweir 	}
108*cdf0e10cSrcweir 	pPtr = DDE_FREECHANNEL;
109*cdf0e10cSrcweir 	pConvList->Insert( pPtr, LIST_APPEND );
110*cdf0e10cSrcweir 	return nChannel;
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir SbError SbiDdeControl::Initiate( const String& rService, const String& rTopic,
114*cdf0e10cSrcweir 			sal_Int16& rnHandle )
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir 	SbError nErr;
117*cdf0e10cSrcweir 	DdeConnection* pConv = new DdeConnection( rService, rTopic );
118*cdf0e10cSrcweir 	nErr = GetLastErr( pConv );
119*cdf0e10cSrcweir 	if( nErr )
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 		delete pConv;
122*cdf0e10cSrcweir 		rnHandle = 0;
123*cdf0e10cSrcweir 	}
124*cdf0e10cSrcweir 	else
125*cdf0e10cSrcweir 	{
126*cdf0e10cSrcweir 		sal_Int16 nChannel = GetFreeChannel();
127*cdf0e10cSrcweir 		pConvList->Replace( pConv, (sal_uIntPtr)nChannel );
128*cdf0e10cSrcweir 		rnHandle = nChannel;
129*cdf0e10cSrcweir 	}
130*cdf0e10cSrcweir 	return 0;
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir SbError SbiDdeControl::Terminate( sal_Int16 nChannel )
134*cdf0e10cSrcweir {
135*cdf0e10cSrcweir 	DdeConnection* pConv = pConvList->GetObject( (sal_uIntPtr)nChannel );
136*cdf0e10cSrcweir 	if( !nChannel || !pConv || pConv == DDE_FREECHANNEL )
137*cdf0e10cSrcweir 		return SbERR_DDE_NO_CHANNEL;
138*cdf0e10cSrcweir 	pConvList->Replace( DDE_FREECHANNEL, (sal_uIntPtr)nChannel );
139*cdf0e10cSrcweir 	delete pConv;
140*cdf0e10cSrcweir 	return 0L;
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir SbError SbiDdeControl::TerminateAll()
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	sal_Int16 nChannel = (sal_Int16)pConvList->Count();
146*cdf0e10cSrcweir 	while( nChannel )
147*cdf0e10cSrcweir 	{
148*cdf0e10cSrcweir 		nChannel--;
149*cdf0e10cSrcweir 		Terminate( nChannel );
150*cdf0e10cSrcweir 	}
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	pConvList->Clear();
153*cdf0e10cSrcweir 	DdeConnection* pPtr = DDE_FREECHANNEL;
154*cdf0e10cSrcweir 	pConvList->Insert( pPtr );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 	return 0;
157*cdf0e10cSrcweir }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir SbError SbiDdeControl::Request( sal_Int16 nChannel, const String& rItem, String& rResult )
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir 	DdeConnection* pConv = pConvList->GetObject( (sal_uIntPtr)nChannel );
162*cdf0e10cSrcweir 	if( !nChannel || !pConv || pConv == DDE_FREECHANNEL )
163*cdf0e10cSrcweir 		return SbERR_DDE_NO_CHANNEL;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 	DdeRequest aRequest( *pConv, rItem, 30000 );
166*cdf0e10cSrcweir 	aRequest.SetDataHdl( LINK( this, SbiDdeControl, Data ) );
167*cdf0e10cSrcweir 	aRequest.Execute();
168*cdf0e10cSrcweir 	rResult = aData;
169*cdf0e10cSrcweir 	return GetLastErr( pConv );
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir SbError SbiDdeControl::Execute( sal_Int16 nChannel, const String& rCommand )
173*cdf0e10cSrcweir {
174*cdf0e10cSrcweir 	DdeConnection* pConv = pConvList->GetObject( (sal_uIntPtr)nChannel );
175*cdf0e10cSrcweir 	if( !nChannel || !pConv || pConv == DDE_FREECHANNEL )
176*cdf0e10cSrcweir 		return SbERR_DDE_NO_CHANNEL;
177*cdf0e10cSrcweir 	DdeExecute aRequest( *pConv, rCommand, 30000 );
178*cdf0e10cSrcweir 	aRequest.Execute();
179*cdf0e10cSrcweir 	return GetLastErr( pConv );
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir SbError SbiDdeControl::Poke( sal_Int16 nChannel, const String& rItem, const String& rData )
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	DdeConnection* pConv = pConvList->GetObject( (sal_uIntPtr)nChannel );
185*cdf0e10cSrcweir 	if( !nChannel || !pConv || pConv == DDE_FREECHANNEL )
186*cdf0e10cSrcweir 		return SbERR_DDE_NO_CHANNEL;
187*cdf0e10cSrcweir 	DdePoke aRequest( *pConv, rItem, DdeData(rData), 30000 );
188*cdf0e10cSrcweir 	aRequest.Execute();
189*cdf0e10cSrcweir 	return GetLastErr( pConv );
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 
193