| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <comphelper/errcode.hxx> |
| 21 |
#include <svl/svdde.hxx> |
| 22 |
#include "ddectrl.hxx" |
| 23 |
#include <basic/sberrors.hxx> |
| 24 |
|
| 25 |
#define DDE_FIRSTERR 0x4000 |
| 26 |
#define DDE_LASTERR 0x4011 |
| 27 |
|
| 28 |
const ErrCode nDdeErrMap[] = |
| 29 |
{ |
| 30 |
/* DMLERR_ADVACKTIMEOUT */ ErrCode(0x4000), ERRCODE_BASIC_DDE_TIMEOUT, |
| 31 |
/* DMLERR_BUSY */ ErrCode(0x4001), ERRCODE_BASIC_DDE_BUSY, |
| 32 |
/* DMLERR_DATAACKTIMEOUT */ ErrCode(0x4002), ERRCODE_BASIC_DDE_TIMEOUT, |
| 33 |
/* DMLERR_DLL_NOT_INITIALIZED */ ErrCode(0x4003), ERRCODE_BASIC_DDE_ERROR, |
| 34 |
/* DMLERR_DLL_USAGE */ ErrCode(0x4004), ERRCODE_BASIC_DDE_ERROR, |
| 35 |
/* DMLERR_EXECACKTIMEOUT */ ErrCode(0x4005), ERRCODE_BASIC_DDE_TIMEOUT, |
| 36 |
/* DMLERR_INVALIDPARAMETER */ ErrCode(0x4006), ERRCODE_BASIC_DDE_ERROR, |
| 37 |
/* DMLERR_LOW_MEMORY */ ErrCode(0x4007), ERRCODE_BASIC_DDE_ERROR, |
| 38 |
/* DMLERR_MEMORY_ERROR */ ErrCode(0x4008), ERRCODE_BASIC_DDE_ERROR, |
| 39 |
/* DMLERR_NOTPROCESSED */ ErrCode(0x4009), ERRCODE_BASIC_DDE_NOTPROCESSED, |
| 40 |
/* DMLERR_NO_CONV_ESTABLISHED */ ErrCode(0x400a), ERRCODE_BASIC_DDE_NO_CHANNEL, |
| 41 |
/* DMLERR_POKEACKTIMEOUT */ ErrCode(0x400b), ERRCODE_BASIC_DDE_TIMEOUT, |
| 42 |
/* DMLERR_POSTMSG_FAILED */ ErrCode(0x400c), ERRCODE_BASIC_DDE_QUEUE_OVERFLOW, |
| 43 |
/* DMLERR_REENTRANCY */ ErrCode(0x400d), ERRCODE_BASIC_DDE_ERROR, |
| 44 |
/* DMLERR_SERVER_DIED */ ErrCode(0x400e), ERRCODE_BASIC_DDE_PARTNER_QUIT, |
| 45 |
/* DMLERR_SYS_ERROR */ ErrCode(0x400f), ERRCODE_BASIC_DDE_ERROR, |
| 46 |
/* DMLERR_UNADVACKTIMEOUT */ ErrCode(0x4010), ERRCODE_BASIC_DDE_TIMEOUT, |
| 47 |
/* DMLERR_UNFOUND_QUEUE_ID */ ErrCode(0x4011), ERRCODE_BASIC_DDE_NO_CHANNEL |
| 48 |
}; |
| 49 |
|
| 50 |
ErrCode SbiDdeControl::GetLastErr( const DdeConnection* pConv ) |
| 51 |
{ |
| 52 |
if( !pConv ) |
| 53 |
{ |
| 54 |
return ERRCODE_NONE; |
| 55 |
} |
| 56 |
tools::Long nErr = pConv->GetError(); |
| 57 |
if( !nErr ) |
| 58 |
{ |
| 59 |
return ERRCODE_NONE; |
| 60 |
} |
| 61 |
if( nErr < DDE_FIRSTERR || nErr > DDE_LASTERR ) |
| 62 |
{ |
| 63 |
return ERRCODE_BASIC_DDE_ERROR; |
| 64 |
} |
| 65 |
return nDdeErrMap[ 2 * (nErr - DDE_FIRSTERR) + 1 ]; |
| 66 |
} |
| 67 |
|
| 68 |
IMPL_LINK( SbiDdeControl, Data, const DdeData*, pData, void ) |
| 69 |
{ |
| 70 |
aData = OUString::createFromAscii( static_cast<const char*>(pData->getData()) ); |
| 71 |
} |
| 72 |
|
| 73 |
SbiDdeControl::SbiDdeControl() |
| 74 |
{ |
| 75 |
} |
| 76 |
|
| 77 |
SbiDdeControl::~SbiDdeControl() |
| 78 |
{ |
| 79 |
TerminateAll(); |
| 80 |
} |
| 81 |
|
| 82 |
size_t SbiDdeControl::GetFreeChannel() |
| 83 |
{ |
| 84 |
size_t nChannel = 0; |
| 85 |
size_t nListSize = aConvList.size(); |
| 86 |
|
| 87 |
for (; nChannel < nListSize; ++nChannel) |
| 88 |
{ |
| 89 |
if (!aConvList[nChannel]) |
| 90 |
{ |
| 91 |
return nChannel+1; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
aConvList.push_back(nullptr); |
| 96 |
return nChannel+1; |
| 97 |
} |
| 98 |
|
| 99 |
ErrCode SbiDdeControl::Initiate( const OUString& rService, const OUString& rTopic, |
| 100 |
size_t& rnHandle ) |
| 101 |
{ |
| 102 |
ErrCode nErr; |
| 103 |
auto pConv = std::make_unique<DdeConnection> ( rService, rTopic ); |
| 104 |
nErr = GetLastErr( pConv.get() ); |
| 105 |
if( nErr ) |
| 106 |
{ |
| 107 |
rnHandle = 0; |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
size_t nChannel = GetFreeChannel(); |
| 112 |
aConvList[nChannel-1] = std::move(pConv); |
| 113 |
rnHandle = nChannel; |
| 114 |
} |
| 115 |
return ERRCODE_NONE; |
| 116 |
} |
| 117 |
|
| 118 |
ErrCode SbiDdeControl::Terminate( size_t nChannel ) |
| 119 |
{ |
| 120 |
if (!nChannel || nChannel > aConvList.size()) |
| 121 |
{ |
| 122 |
return ERRCODE_BASIC_DDE_NO_CHANNEL; |
| 123 |
} |
| 124 |
DdeConnection* pConv = aConvList[nChannel-1].get(); |
| 125 |
|
| 126 |
if( !pConv ) |
| 127 |
{ |
| 128 |
return ERRCODE_BASIC_DDE_NO_CHANNEL; |
| 129 |
} |
| 130 |
aConvList[nChannel-1].reset(); |
| 131 |
|
| 132 |
return ERRCODE_NONE; |
| 133 |
} |
| 134 |
|
| 135 |
ErrCode SbiDdeControl::TerminateAll() |
| 136 |
{ |
| 137 |
aConvList.clear(); |
| 138 |
return ERRCODE_NONE; |
| 139 |
} |
| 140 |
|
| 141 |
ErrCode SbiDdeControl::Request( size_t nChannel, const OUString& rItem, OUString& rResult ) |
| 142 |
{ |
| 143 |
if (!nChannel || nChannel > aConvList.size()) |
| 144 |
{ |
| 145 |
return ERRCODE_BASIC_DDE_NO_CHANNEL; |
| 146 |
} |
| 147 |
|
| 148 |
DdeConnection* pConv = aConvList[nChannel-1].get(); |
| 149 |
|
| 150 |
if( !pConv ) |
| 151 |
{ |
| 152 |
return ERRCODE_BASIC_DDE_NO_CHANNEL; |
| 153 |
} |
| 154 |
|
| 155 |
DdeRequest aRequest( *pConv, rItem, 30000 ); |
| 156 |
aRequest.SetDataHdl( LINK( this, SbiDdeControl, Data ) ); |
| 157 |
aRequest.Execute(); |
| 158 |
rResult = aData; |
| 159 |
return GetLastErr( pConv ); |
| 160 |
} |
| 161 |
|
| 162 |
ErrCode SbiDdeControl::Execute( size_t nChannel, const OUString& rCommand ) |
| 163 |
{ |
| 164 |
if (!nChannel || nChannel > aConvList.size()) |
| 165 |
{ |
| 166 |
return ERRCODE_BASIC_DDE_NO_CHANNEL; |
| 167 |
} |
| 168 |
|
| 169 |
DdeConnection* pConv = aConvList[nChannel-1].get(); |
| 170 |
|
| 171 |
if( !pConv ) |
| 172 |
{ |
| 173 |
return ERRCODE_BASIC_DDE_NO_CHANNEL; |
| 174 |
} |
| 175 |
DdeExecute aRequest( *pConv, rCommand, 30000 ); |
| 176 |
aRequest.Execute(); |
| 177 |
return GetLastErr( pConv ); |
| 178 |
} |
| 179 |
|
| 180 |
ErrCode SbiDdeControl::Poke( size_t nChannel, const OUString& rItem, const OUString& rData ) |
| 181 |
{ |
| 182 |
if (!nChannel || nChannel > aConvList.size()) |
| 183 |
{ |
| 184 |
return ERRCODE_BASIC_DDE_NO_CHANNEL; |
| 185 |
} |
| 186 |
DdeConnection* pConv = aConvList[nChannel-1].get(); |
| 187 |
|
| 188 |
if( !pConv ) |
| 189 |
{ |
| 190 |
return ERRCODE_BASIC_DDE_NO_CHANNEL; |
| 191 |
} |
| 192 |
DdePoke aRequest( *pConv, rItem, DdeData(rData), 30000 ); |
| 193 |
aRequest.Execute(); |
| 194 |
return GetLastErr( pConv ); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |