1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 ((DdeConnection*)0xffffffff)
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
)
58 long nErr
= pConv
->GetError();
63 if( nErr
< DDE_FIRSTERR
|| nErr
> DDE_LASTERR
)
65 return SbERR_DDE_ERROR
;
67 return nDdeErrMap
[ 2 * (nErr
- DDE_FIRSTERR
) + 1 ];
70 IMPL_LINK_INLINE( SbiDdeControl
,Data
, DdeData
*, pData
,
72 aData
= OUString::createFromAscii( (const char*)(const void*)*pData
);
77 SbiDdeControl::SbiDdeControl()
81 SbiDdeControl::~SbiDdeControl()
86 size_t SbiDdeControl::GetFreeChannel()
89 size_t nListSize
= aConvList
.size();
91 for (; nChannel
< nListSize
; ++nChannel
)
93 if (aConvList
[nChannel
] == DDE_FREECHANNEL
)
99 aConvList
.push_back(DDE_FREECHANNEL
);
103 SbError
SbiDdeControl::Initiate( const OUString
& rService
, const OUString
& rTopic
,
107 DdeConnection
* pConv
= new DdeConnection( rService
, rTopic
);
108 nErr
= GetLastErr( pConv
);
116 size_t nChannel
= GetFreeChannel();
117 aConvList
[nChannel
-1] = pConv
;
123 SbError
SbiDdeControl::Terminate( size_t nChannel
)
125 if (!nChannel
|| nChannel
> aConvList
.size())
127 return SbERR_DDE_NO_CHANNEL
;
129 DdeConnection
* pConv
= aConvList
[nChannel
-1];
131 if( pConv
== DDE_FREECHANNEL
)
133 return SbERR_DDE_NO_CHANNEL
;
136 pConv
= DDE_FREECHANNEL
;
141 SbError
SbiDdeControl::TerminateAll()
144 for (size_t nChannel
= 0; nChannel
< aConvList
.size(); ++nChannel
)
146 conv
= aConvList
[nChannel
];
148 if (conv
!= DDE_FREECHANNEL
)
159 SbError
SbiDdeControl::Request( size_t nChannel
, const OUString
& rItem
, OUString
& rResult
)
161 if (!nChannel
|| nChannel
> aConvList
.size())
163 return SbERR_DDE_NO_CHANNEL
;
166 DdeConnection
* pConv
= aConvList
[nChannel
-1];
168 if( pConv
== DDE_FREECHANNEL
)
170 return SbERR_DDE_NO_CHANNEL
;
173 DdeRequest
aRequest( *pConv
, rItem
, 30000 );
174 aRequest
.SetDataHdl( LINK( this, SbiDdeControl
, Data
) );
177 return GetLastErr( pConv
);
180 SbError
SbiDdeControl::Execute( size_t nChannel
, const OUString
& rCommand
)
182 if (!nChannel
|| nChannel
> aConvList
.size())
184 return SbERR_DDE_NO_CHANNEL
;
187 DdeConnection
* pConv
= aConvList
[nChannel
-1];
189 if( pConv
== DDE_FREECHANNEL
)
191 return SbERR_DDE_NO_CHANNEL
;
193 DdeExecute
aRequest( *pConv
, rCommand
, 30000 );
195 return GetLastErr( pConv
);
198 SbError
SbiDdeControl::Poke( size_t nChannel
, const OUString
& rItem
, const OUString
& rData
)
200 if (!nChannel
|| nChannel
> aConvList
.size())
202 return SbERR_DDE_NO_CHANNEL
;
204 DdeConnection
* pConv
= aConvList
[nChannel
-1];
206 if( pConv
== DDE_FREECHANNEL
)
208 return SbERR_DDE_NO_CHANNEL
;
210 DdePoke
aRequest( *pConv
, rItem
, DdeData(rData
), 30000 );
212 return GetLastErr( pConv
);
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */