bump product version to 5.0.4.1
[LibreOffice.git] / basic / source / runtime / ddectrl.cxx
blob9f7149cf332dd0ee52cd848a37ac067d83788f74
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/errcode.hxx>
21 #include <svl/svdde.hxx>
22 #include "ddectrl.hxx"
23 #include <basic/sberrors.hxx>
25 #define DDE_FREECHANNEL (reinterpret_cast<DdeConnection*>(sal_IntPtr(-1)))
27 #define DDE_FIRSTERR 0x4000
28 #define DDE_LASTERR 0x4011
30 static const SbError nDdeErrMap[] =
32 /* DMLERR_ADVACKTIMEOUT */ 0x4000, SbERR_DDE_TIMEOUT,
33 /* DMLERR_BUSY */ 0x4001, SbERR_DDE_BUSY,
34 /* DMLERR_DATAACKTIMEOUT */ 0x4002, SbERR_DDE_TIMEOUT,
35 /* DMLERR_DLL_NOT_INITIALIZED */ 0x4003, SbERR_DDE_ERROR,
36 /* DMLERR_DLL_USAGE */ 0x4004, SbERR_DDE_ERROR,
37 /* DMLERR_EXECACKTIMEOUT */ 0x4005, SbERR_DDE_TIMEOUT,
38 /* DMLERR_INVALIDPARAMETER */ 0x4006, SbERR_DDE_ERROR,
39 /* DMLERR_LOW_MEMORY */ 0x4007, SbERR_DDE_ERROR,
40 /* DMLERR_MEMORY_ERROR */ 0x4008, SbERR_DDE_ERROR,
41 /* DMLERR_NOTPROCESSED */ 0x4009, SbERR_DDE_NOTPROCESSED,
42 /* DMLERR_NO_CONV_ESTABLISHED */ 0x400a, SbERR_DDE_NO_CHANNEL,
43 /* DMLERR_POKEACKTIMEOUT */ 0x400b, SbERR_DDE_TIMEOUT,
44 /* DMLERR_POSTMSG_FAILED */ 0x400c, SbERR_DDE_QUEUE_OVERFLOW,
45 /* DMLERR_REENTRANCY */ 0x400d, SbERR_DDE_ERROR,
46 /* DMLERR_SERVER_DIED */ 0x400e, SbERR_DDE_PARTNER_QUIT,
47 /* DMLERR_SYS_ERROR */ 0x400f, SbERR_DDE_ERROR,
48 /* DMLERR_UNADVACKTIMEOUT */ 0x4010, SbERR_DDE_TIMEOUT,
49 /* DMLERR_UNFOUND_QUEUE_ID */ 0x4011, SbERR_DDE_NO_CHANNEL
52 SbError SbiDdeControl::GetLastErr( DdeConnection* pConv )
54 if( !pConv )
56 return 0;
58 long nErr = pConv->GetError();
59 if( !nErr )
61 return 0;
63 if( nErr < DDE_FIRSTERR || nErr > DDE_LASTERR )
65 return SbERR_DDE_ERROR;
67 return nDdeErrMap[ 2 * (nErr - DDE_FIRSTERR) + 1 ];
70 IMPL_LINK( SbiDdeControl,Data , DdeData*, pData )
72 aData = OUString::createFromAscii( static_cast<const char*>((const void*)*pData) );
73 return 1;
76 SbiDdeControl::SbiDdeControl()
80 SbiDdeControl::~SbiDdeControl()
82 TerminateAll();
85 size_t SbiDdeControl::GetFreeChannel()
87 size_t nChannel = 0;
88 size_t nListSize = aConvList.size();
90 for (; nChannel < nListSize; ++nChannel)
92 if (aConvList[nChannel] == DDE_FREECHANNEL)
94 return nChannel+1;
98 aConvList.push_back(DDE_FREECHANNEL);
99 return nChannel+1;
102 SbError SbiDdeControl::Initiate( const OUString& rService, const OUString& rTopic,
103 size_t& rnHandle )
105 SbError nErr;
106 DdeConnection* pConv = new DdeConnection( rService, rTopic );
107 nErr = GetLastErr( pConv );
108 if( nErr )
110 delete pConv;
111 rnHandle = 0;
113 else
115 size_t nChannel = GetFreeChannel();
116 aConvList[nChannel-1] = pConv;
117 rnHandle = nChannel;
119 return 0;
122 SbError SbiDdeControl::Terminate( size_t nChannel )
124 if (!nChannel || nChannel > aConvList.size())
126 return SbERR_DDE_NO_CHANNEL;
128 DdeConnection* pConv = aConvList[nChannel-1];
130 if( pConv == DDE_FREECHANNEL )
132 return SbERR_DDE_NO_CHANNEL;
134 delete pConv;
135 aConvList[nChannel-1] = DDE_FREECHANNEL;
137 return 0L;
140 SbError SbiDdeControl::TerminateAll()
142 for (size_t nChannel = 0; nChannel < aConvList.size(); ++nChannel)
144 DdeConnection *conv = aConvList[nChannel];
146 if (conv != DDE_FREECHANNEL)
148 delete conv;
152 aConvList.clear();
154 return 0;
157 SbError SbiDdeControl::Request( size_t nChannel, const OUString& rItem, OUString& rResult )
159 if (!nChannel || nChannel > aConvList.size())
161 return SbERR_DDE_NO_CHANNEL;
164 DdeConnection* pConv = aConvList[nChannel-1];
166 if( pConv == DDE_FREECHANNEL )
168 return SbERR_DDE_NO_CHANNEL;
171 DdeRequest aRequest( *pConv, rItem, 30000 );
172 aRequest.SetDataHdl( LINK( this, SbiDdeControl, Data ) );
173 aRequest.Execute();
174 rResult = aData;
175 return GetLastErr( pConv );
178 SbError SbiDdeControl::Execute( size_t nChannel, const OUString& rCommand )
180 if (!nChannel || nChannel > aConvList.size())
182 return SbERR_DDE_NO_CHANNEL;
185 DdeConnection* pConv = aConvList[nChannel-1];
187 if( pConv == DDE_FREECHANNEL )
189 return SbERR_DDE_NO_CHANNEL;
191 DdeExecute aRequest( *pConv, rCommand, 30000 );
192 aRequest.Execute();
193 return GetLastErr( pConv );
196 SbError SbiDdeControl::Poke( size_t nChannel, const OUString& rItem, const OUString& rData )
198 if (!nChannel || nChannel > aConvList.size())
200 return SbERR_DDE_NO_CHANNEL;
202 DdeConnection* pConv = aConvList[nChannel-1];
204 if( pConv == DDE_FREECHANNEL )
206 return SbERR_DDE_NO_CHANNEL;
208 DdePoke aRequest( *pConv, rItem, DdeData(rData), 30000 );
209 aRequest.Execute();
210 return GetLastErr( pConv );
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */