LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / basic / source / runtime / ddectrl.cxx
blobbb2c4c5548f1607c5dfb6e4249c72bb09b7784e0
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 <vcl/errcode.hxx>
21 #include <svl/svdde.hxx>
22 #include "ddectrl.hxx"
23 #include <basic/sberrors.hxx>
25 #define DDE_FIRSTERR 0x4000
26 #define DDE_LASTERR 0x4011
28 const ErrCode nDdeErrMap[] =
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
50 ErrCode SbiDdeControl::GetLastErr( const DdeConnection* pConv )
52 if( !pConv )
54 return ERRCODE_NONE;
56 tools::Long nErr = pConv->GetError();
57 if( !nErr )
59 return ERRCODE_NONE;
61 if( nErr < DDE_FIRSTERR || nErr > DDE_LASTERR )
63 return ERRCODE_BASIC_DDE_ERROR;
65 return nDdeErrMap[ 2 * (nErr - DDE_FIRSTERR) + 1 ];
68 IMPL_LINK( SbiDdeControl, Data, const DdeData*, pData, void )
70 aData = OUString::createFromAscii( static_cast<const char*>(pData->getData()) );
73 SbiDdeControl::SbiDdeControl()
77 SbiDdeControl::~SbiDdeControl()
79 TerminateAll();
82 size_t SbiDdeControl::GetFreeChannel()
84 size_t nChannel = 0;
85 size_t nListSize = aConvList.size();
87 for (; nChannel < nListSize; ++nChannel)
89 if (!aConvList[nChannel])
91 return nChannel+1;
95 aConvList.push_back(nullptr);
96 return nChannel+1;
99 ErrCode SbiDdeControl::Initiate( const OUString& rService, const OUString& rTopic,
100 size_t& rnHandle )
102 ErrCode nErr;
103 auto pConv = std::make_unique<DdeConnection> ( rService, rTopic );
104 nErr = GetLastErr( pConv.get() );
105 if( nErr )
107 rnHandle = 0;
109 else
111 size_t nChannel = GetFreeChannel();
112 aConvList[nChannel-1] = std::move(pConv);
113 rnHandle = nChannel;
115 return ERRCODE_NONE;
118 ErrCode SbiDdeControl::Terminate( size_t nChannel )
120 if (!nChannel || nChannel > aConvList.size())
122 return ERRCODE_BASIC_DDE_NO_CHANNEL;
124 DdeConnection* pConv = aConvList[nChannel-1].get();
126 if( !pConv )
128 return ERRCODE_BASIC_DDE_NO_CHANNEL;
130 aConvList[nChannel-1].reset();
132 return ERRCODE_NONE;
135 ErrCode SbiDdeControl::TerminateAll()
137 aConvList.clear();
138 return ERRCODE_NONE;
141 ErrCode SbiDdeControl::Request( size_t nChannel, const OUString& rItem, OUString& rResult )
143 if (!nChannel || nChannel > aConvList.size())
145 return ERRCODE_BASIC_DDE_NO_CHANNEL;
148 DdeConnection* pConv = aConvList[nChannel-1].get();
150 if( !pConv )
152 return ERRCODE_BASIC_DDE_NO_CHANNEL;
155 DdeRequest aRequest( *pConv, rItem, 30000 );
156 aRequest.SetDataHdl( LINK( this, SbiDdeControl, Data ) );
157 aRequest.Execute();
158 rResult = aData;
159 return GetLastErr( pConv );
162 ErrCode SbiDdeControl::Execute( size_t nChannel, const OUString& rCommand )
164 if (!nChannel || nChannel > aConvList.size())
166 return ERRCODE_BASIC_DDE_NO_CHANNEL;
169 DdeConnection* pConv = aConvList[nChannel-1].get();
171 if( !pConv )
173 return ERRCODE_BASIC_DDE_NO_CHANNEL;
175 DdeExecute aRequest( *pConv, rCommand, 30000 );
176 aRequest.Execute();
177 return GetLastErr( pConv );
180 ErrCode SbiDdeControl::Poke( size_t nChannel, const OUString& rItem, const OUString& rData )
182 if (!nChannel || nChannel > aConvList.size())
184 return ERRCODE_BASIC_DDE_NO_CHANNEL;
186 DdeConnection* pConv = aConvList[nChannel-1].get();
188 if( !pConv )
190 return ERRCODE_BASIC_DDE_NO_CHANNEL;
192 DdePoke aRequest( *pConv, rItem, DdeData(rData), 30000 );
193 aRequest.Execute();
194 return GetLastErr( pConv );
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */