Branch data Line data Source code
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 : :
21 : : #if defined(WNT)
22 : : #include <prewin.h>
23 : : #include <postwin.h>
24 : : #endif
25 : :
26 : : #include "impldde.hxx"
27 : :
28 : : #include <vcl/svapp.hxx>
29 : : #include <vcl/fixed.hxx>
30 : : #include <vcl/edit.hxx>
31 : : #include <vcl/button.hxx>
32 : : #include <vcl/msgbox.hxx>
33 : : #include <sot/exchange.hxx>
34 : : #include <rtl/strbuf.hxx>
35 : : #include <rtl/ustring.hxx>
36 : :
37 : : #include "dde.hrc"
38 : : #include <sfx2/lnkbase.hxx>
39 : : #include <sfx2/linkmgr.hxx>
40 : : #include "sfx2/sfxresid.hxx"
41 : :
42 : : #include <com/sun/star/uno/Any.hxx>
43 : : #include <com/sun/star/uno/Sequence.hxx>
44 : :
45 : : #include <svl/svdde.hxx>
46 : : #include <sot/formats.hxx>
47 : :
48 : : #define DDELINK_ERROR_APP 1
49 : : #define DDELINK_ERROR_DATA 2
50 : : #define DDELINK_ERROR_LINK 3
51 : :
52 : : using namespace ::com::sun::star::uno;
53 : :
54 : : namespace sfx2
55 : : {
56 : :
57 [ # # ][ # # ]: 0 : class SvDDELinkEditDialog : public ModalDialog
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
58 : : {
59 : : FixedText aFtDdeApp;
60 : : Edit aEdDdeApp;
61 : : FixedText aFtDdeTopic;
62 : : Edit aEdDdeTopic;
63 : : FixedText aFtDdeItem;
64 : : Edit aEdDdeItem;
65 : : FixedLine aGroupDdeChg;
66 : : OKButton aOKButton1;
67 : : CancelButton aCancelButton1;
68 : :
69 : : DECL_STATIC_LINK( SvDDELinkEditDialog, EditHdl_Impl, Edit* );
70 : : public:
71 : : SvDDELinkEditDialog( Window* pParent, SvBaseLink* );
72 : : String GetCmd() const;
73 : : };
74 : :
75 : 0 : SvDDELinkEditDialog::SvDDELinkEditDialog( Window* pParent, SvBaseLink* pLink )
76 : : : ModalDialog( pParent, SfxResId( MD_DDE_LINKEDIT ) ),
77 : : aFtDdeApp( this, SfxResId( FT_DDE_APP ) ),
78 : : aEdDdeApp( this, SfxResId( ED_DDE_APP ) ),
79 : : aFtDdeTopic( this, SfxResId( FT_DDE_TOPIC ) ),
80 : : aEdDdeTopic( this, SfxResId( ED_DDE_TOPIC ) ),
81 : : aFtDdeItem( this, SfxResId( FT_DDE_ITEM ) ),
82 : : aEdDdeItem( this, SfxResId( ED_DDE_ITEM ) ),
83 : : aGroupDdeChg( this, SfxResId( GROUP_DDE_CHG ) ),
84 : : aOKButton1( this, SfxResId( 1 ) ),
85 [ # # ][ # # ]: 0 : aCancelButton1( this, SfxResId( 1 ) )
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
86 : : {
87 [ # # ]: 0 : FreeResource();
88 : :
89 [ # # ][ # # ]: 0 : String sServer, sTopic, sItem;
[ # # ]
90 [ # # ][ # # ]: 0 : pLink->GetLinkManager()->GetDisplayNames( pLink, &sServer, &sTopic, &sItem );
91 : :
92 [ # # ]: 0 : aEdDdeApp.SetText( sServer );
93 [ # # ]: 0 : aEdDdeTopic.SetText( sTopic );
94 [ # # ]: 0 : aEdDdeItem.SetText( sItem );
95 : :
96 [ # # ]: 0 : aEdDdeApp.SetModifyHdl( STATIC_LINK( this, SvDDELinkEditDialog, EditHdl_Impl));
97 [ # # ]: 0 : aEdDdeTopic.SetModifyHdl( STATIC_LINK( this, SvDDELinkEditDialog, EditHdl_Impl));
98 [ # # ]: 0 : aEdDdeItem.SetModifyHdl( STATIC_LINK( this, SvDDELinkEditDialog, EditHdl_Impl));
99 : :
100 [ # # ][ # # ]: 0 : aOKButton1.Enable( sServer.Len() && sTopic.Len() && sItem.Len() );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
101 : 0 : }
102 : :
103 : 0 : String SvDDELinkEditDialog::GetCmd() const
104 : : {
105 [ # # ][ # # ]: 0 : String sCmd( aEdDdeApp.GetText() ), sRet;
106 [ # # ][ # # ]: 0 : ::sfx2::MakeLnkName( sRet, &sCmd, aEdDdeTopic.GetText(), aEdDdeItem.GetText() );
[ # # ][ # # ]
[ # # ]
107 [ # # ]: 0 : return sRet;
108 : : }
109 : :
110 : 0 : IMPL_STATIC_LINK( SvDDELinkEditDialog, EditHdl_Impl, Edit *, pEdit )
111 : : {
112 : : (void)pEdit; // unused variable
113 [ # # ][ # # ]: 0 : pThis->aOKButton1.Enable( pThis->aEdDdeApp.GetText().Len() &&
[ # # ]
114 [ # # ][ # # ]: 0 : pThis->aEdDdeTopic.GetText().Len() &&
[ # # ][ # # ]
115 [ # # ][ # # ]: 0 : pThis->aEdDdeItem.GetText().Len() );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # # # ]
116 : 0 : return 0;
117 : : }
118 : :
119 : 2 : SvDDEObject::SvDDEObject()
120 [ + - ]: 2 : : pConnection( 0 ), pLink( 0 ), pRequest( 0 ), pGetData( 0 ), nError( 0 )
121 : : {
122 [ + - ]: 2 : SetUpdateTimeout( 100 );
123 : 2 : bWaitForData = sal_False;
124 : 2 : }
125 : :
126 [ + - ]: 2 : SvDDEObject::~SvDDEObject()
127 : : {
128 [ - + ][ # # ]: 2 : delete pLink;
129 [ - + ][ # # ]: 2 : delete pRequest;
130 [ - + ][ # # ]: 2 : delete pConnection;
131 [ - + ]: 4 : }
132 : :
133 : 0 : sal_Bool SvDDEObject::GetData( ::com::sun::star::uno::Any & rData /*out param*/,
134 : : const String & rMimeType,
135 : : sal_Bool bSynchron )
136 : : {
137 [ # # ]: 0 : if( !pConnection )
138 : 0 : return sal_False;
139 : :
140 [ # # ]: 0 : if( pConnection->GetError() ) // then we try once more
141 : : {
142 [ # # ][ # # ]: 0 : String sServer( pConnection->GetServiceName() );
143 [ # # ][ # # ]: 0 : String sTopic( pConnection->GetTopicName() );
144 : :
145 [ # # ][ # # ]: 0 : delete pConnection;
146 [ # # ][ # # ]: 0 : pConnection = new DdeConnection( sServer, sTopic );
147 [ # # ][ # # ]: 0 : if( pConnection->GetError() )
148 [ # # ][ # # ]: 0 : nError = DDELINK_ERROR_APP;
149 : : }
150 : :
151 [ # # ]: 0 : if( bWaitForData ) // we are in an rekursive loop, get out again
152 : 0 : return sal_False;
153 : :
154 : : // Lock against Reentrance
155 : 0 : bWaitForData = sal_True;
156 : :
157 : : // if you want to print, we'll wait until the data is available
158 [ # # ]: 0 : if( bSynchron )
159 : : {
160 [ # # ]: 0 : DdeRequest aReq( *pConnection, sItem, 5000 );
161 [ # # ]: 0 : aReq.SetDataHdl( LINK( this, SvDDEObject, ImplGetDDEData ) );
162 [ # # ][ # # ]: 0 : aReq.SetFormat( SotExchange::GetFormatIdFromMimeType( rMimeType ));
163 : :
164 : 0 : pGetData = &rData;
165 : :
166 [ # # ][ # # ]: 0 : do {
[ # # ]
167 [ # # ]: 0 : aReq.Execute();
168 [ # # ][ # # ]: 0 : } while( aReq.GetError() && ImplHasOtherFormat( aReq ) );
169 : :
170 [ # # ][ # # ]: 0 : if( pConnection->GetError() )
171 : 0 : nError = DDELINK_ERROR_DATA;
172 : :
173 [ # # ]: 0 : bWaitForData = sal_False;
174 : : }
175 : : else
176 : : {
177 : : // otherwise it will be executed asynchronously
178 : : {
179 [ # # ]: 0 : if( pRequest )
180 [ # # ][ # # ]: 0 : delete pRequest;
181 : :
182 [ # # ][ # # ]: 0 : pRequest = new DdeRequest( *pConnection, sItem );
183 [ # # ]: 0 : pRequest->SetDataHdl( LINK( this, SvDDEObject, ImplGetDDEData ) );
184 [ # # ]: 0 : pRequest->SetDoneHdl( LINK( this, SvDDEObject, ImplDoneDDEData ) );
185 : : pRequest->SetFormat( SotExchange::GetFormatIdFromMimeType(
186 [ # # ][ # # ]: 0 : rMimeType ) );
187 [ # # ]: 0 : pRequest->Execute();
188 : : }
189 : :
190 : 0 : ::rtl::OUString aEmptyStr;
191 [ # # ]: 0 : rData <<= aEmptyStr;
192 : : }
193 : 0 : return 0 == pConnection->GetError();
194 : : }
195 : :
196 : :
197 : 2 : sal_Bool SvDDEObject::Connect( SvBaseLink * pSvLink )
198 : : {
199 : : #if defined(WNT)
200 : : static sal_Bool bInWinExec = sal_False;
201 : : #endif
202 [ + - ]: 2 : sal_uInt16 nLinkType = pSvLink->GetUpdateMode();
203 [ - + ]: 2 : if( pConnection ) // Connection is already made
204 : : {
205 : : // well, then just add it as dependent
206 : : AddDataAdvise( pSvLink,
207 : : SotExchange::GetFormatMimeType( pSvLink->GetContentType()),
208 : : LINKUPDATE_ONCALL == nLinkType
209 : : ? ADVISEMODE_ONLYONCE
210 [ # # ][ # # ]: 0 : : 0 );
[ # # ][ # # ]
[ # # ]
211 [ # # ]: 0 : AddConnectAdvise( pSvLink );
212 : :
213 : 0 : return sal_True;
214 : : }
215 : :
216 [ + - ][ - + ]: 2 : if( !pSvLink->GetLinkManager() )
217 : 0 : return sal_False;
218 : :
219 [ + - ][ + - ]: 2 : String sServer, sTopic;
220 [ + - ][ + - ]: 2 : pSvLink->GetLinkManager()->GetDisplayNames( pSvLink, &sServer, &sTopic, &sItem );
221 : :
222 [ + - ][ - + ]: 2 : if( !sServer.Len() || !sTopic.Len() || !sItem.Len() )
[ # # ][ + - ]
223 : 2 : return sal_False;
224 : :
225 [ # # ][ # # ]: 0 : pConnection = new DdeConnection( sServer, sTopic );
226 [ # # ][ # # ]: 0 : if( pConnection->GetError() )
227 : : {
228 : : // Is it possible to address the system-Topic?
229 : : // then the server is up, it just does not know the topic!
230 [ # # ][ # # ]: 0 : if( sTopic.EqualsIgnoreCaseAscii( "SYSTEM" ) )
231 : : {
232 : : sal_Bool bSysTopic;
233 : : {
234 [ # # ][ # # ]: 0 : DdeConnection aTmp(sServer, rtl::OUString("SYSTEM"));
[ # # ]
235 [ # # ][ # # ]: 0 : bSysTopic = !aTmp.GetError();
236 : : }
237 : :
238 [ # # ]: 0 : if( bSysTopic )
239 : : {
240 : 0 : nError = DDELINK_ERROR_DATA;
241 : 0 : return sal_False;
242 : : }
243 : : // otherwise in Win/WinNT, start the Application directly
244 : : }
245 : :
246 : : #if defined(WNT)
247 : :
248 : : // Server not up, try once more to start it.
249 : : if( !bInWinExec )
250 : : {
251 : : rtl::OStringBuffer aCmdLine(rtl::OUStringToOString(sServer, RTL_TEXTENCODING_ASCII_US));
252 : : aCmdLine.append(RTL_CONSTASCII_STRINGPARAM(".exe "));
253 : : aCmdLine.append(rtl::OUStringToOString(sTopic, RTL_TEXTENCODING_ASCII_US));
254 : :
255 : : if( WinExec( aCmdLine.getStr(), SW_SHOWMINIMIZED ) < 32 )
256 : : nError = DDELINK_ERROR_APP;
257 : : else
258 : : {
259 : : sal_uInt16 i;
260 : : for( i=0; i<5; i++ )
261 : : {
262 : : bInWinExec = sal_True;
263 : : Application::Reschedule();
264 : : bInWinExec = sal_False;
265 : :
266 : : delete pConnection;
267 : : pConnection = new DdeConnection( sServer, sTopic );
268 : : if( !pConnection->GetError() )
269 : : break;
270 : : }
271 : :
272 : : if( i == 5 )
273 : : {
274 : : nError = DDELINK_ERROR_APP;
275 : : }
276 : : }
277 : : }
278 : : else
279 : : #endif // WNT
280 : : {
281 : 0 : nError = DDELINK_ERROR_APP;
282 : : }
283 : : }
284 : :
285 [ # # ][ # # ]: 0 : if( LINKUPDATE_ALWAYS == nLinkType && !pLink && !pConnection->GetError() )
[ # # ][ # # ]
[ # # ]
286 : : {
287 : : // Setting up Hot Link, Data will be available at some point later on
288 [ # # ][ # # ]: 0 : pLink = new DdeHotLink( *pConnection, sItem );
289 [ # # ]: 0 : pLink->SetDataHdl( LINK( this, SvDDEObject, ImplGetDDEData ) );
290 [ # # ]: 0 : pLink->SetDoneHdl( LINK( this, SvDDEObject, ImplDoneDDEData ) );
291 [ # # ][ # # ]: 0 : pLink->SetFormat( pSvLink->GetContentType() );
292 [ # # ]: 0 : pLink->Execute();
293 : : }
294 : :
295 [ # # ][ # # ]: 0 : if( pConnection->GetError() )
296 : 0 : return sal_False;
297 : :
298 : : AddDataAdvise( pSvLink,
299 : : SotExchange::GetFormatMimeType( pSvLink->GetContentType()),
300 : : LINKUPDATE_ONCALL == nLinkType
301 : : ? ADVISEMODE_ONLYONCE
302 [ # # ][ # # ]: 0 : : 0 );
[ # # ][ # # ]
[ # # ]
303 [ # # ]: 0 : AddConnectAdvise( pSvLink );
304 [ # # ]: 0 : SetUpdateTimeout( 0 );
305 [ + - ][ + - ]: 2 : return sal_True;
306 : : }
307 : :
308 : 0 : void SvDDEObject::Edit( Window* pParent, sfx2::SvBaseLink* pBaseLink, const Link& rEndEditHdl )
309 : : {
310 [ # # ]: 0 : SvDDELinkEditDialog aDlg( pParent, pBaseLink );
311 [ # # ][ # # ]: 0 : if ( RET_OK == aDlg.Execute() && rEndEditHdl.IsSet() )
[ # # ][ # # ]
[ # # ]
312 : : {
313 [ # # ]: 0 : String sCommand = aDlg.GetCmd();
314 [ # # ][ # # ]: 0 : rEndEditHdl.Call( &sCommand );
315 [ # # ]: 0 : }
316 : 0 : }
317 : :
318 : 0 : sal_Bool SvDDEObject::ImplHasOtherFormat( DdeTransaction& rReq )
319 : : {
320 : 0 : sal_uInt16 nFmt = 0;
321 [ # # # # : 0 : switch( rReq.GetFormat() )
# ]
322 : : {
323 : : case FORMAT_RTF:
324 : 0 : nFmt = FORMAT_STRING;
325 : 0 : break;
326 : :
327 : : case SOT_FORMATSTR_ID_HTML_SIMPLE:
328 : : case SOT_FORMATSTR_ID_HTML:
329 : 0 : nFmt = FORMAT_RTF;
330 : 0 : break;
331 : :
332 : : case FORMAT_GDIMETAFILE:
333 : 0 : nFmt = FORMAT_BITMAP;
334 : 0 : break;
335 : :
336 : : case SOT_FORMATSTR_ID_SVXB:
337 : 0 : nFmt = FORMAT_GDIMETAFILE;
338 : 0 : break;
339 : :
340 : : // something else?
341 : : }
342 [ # # ]: 0 : if( nFmt )
343 : 0 : rReq.SetFormat( nFmt ); // try it once more
344 : 0 : return 0 != nFmt;
345 : : }
346 : :
347 : 0 : sal_Bool SvDDEObject::IsPending() const
348 : : /* [Description]
349 : :
350 : : The method determines whether the data-object can be read from a DDE.
351 : :
352 : : Returned is the following:
353 : : ERRCODE_NONE if it has been completely read
354 : : ERRCODE_SO_PENDING if it has not been completely read
355 : : ERRCODE_SO_FALSE otherwise
356 : : */
357 : : {
358 : 0 : return bWaitForData;
359 : : }
360 : :
361 : 0 : sal_Bool SvDDEObject::IsDataComplete() const
362 : : {
363 : 0 : return bWaitForData;
364 : : }
365 : :
366 : 0 : IMPL_LINK( SvDDEObject, ImplGetDDEData, DdeData*, pData )
367 : : {
368 : 0 : sal_uIntPtr nFmt = pData->GetFormat();
369 [ # # # ]: 0 : switch( nFmt )
370 : : {
371 : : case FORMAT_GDIMETAFILE:
372 : 0 : break;
373 : :
374 : : case FORMAT_BITMAP:
375 : 0 : break;
376 : :
377 : : default:
378 : : {
379 [ # # ]: 0 : const sal_Char* p = (sal_Char*)( pData->operator const void*() );
380 [ # # ][ # # ]: 0 : long nLen = FORMAT_STRING == nFmt ? (p ? strlen( p ) : 0) : (long)*pData;
[ # # ]
381 : :
382 [ # # ]: 0 : Sequence< sal_Int8 > aSeq( (const sal_Int8*)p, nLen );
383 [ # # ]: 0 : if( pGetData )
384 : : {
385 [ # # ]: 0 : *pGetData <<= aSeq; // Copy Data
386 : 0 : pGetData = 0; // reset the pointer here
387 : : }
388 : : else
389 : : {
390 : 0 : Any aVal;
391 [ # # ]: 0 : aVal <<= aSeq;
392 : : DataChanged( SotExchange::GetFormatMimeType(
393 [ # # ][ # # ]: 0 : pData->GetFormat() ), aVal );
[ # # ][ # # ]
394 : 0 : bWaitForData = sal_False;
395 [ # # ]: 0 : }
396 : : }
397 : : }
398 : :
399 : 0 : return 0;
400 : : }
401 : :
402 : 0 : IMPL_LINK( SvDDEObject, ImplDoneDDEData, void*, pData )
403 : : {
404 : 0 : sal_Bool bValid = (sal_Bool)(sal_uIntPtr)pData;
405 [ # # ][ # # ]: 0 : if( !bValid && ( pRequest || pLink ))
[ # # ]
406 : : {
407 : 0 : DdeTransaction* pReq = 0;
408 [ # # ][ # # ]: 0 : if( !pLink || ( pLink && pLink->IsBusy() ))
[ # # ][ # # ]
409 : 0 : pReq = pRequest; // only the one that is ready
410 [ # # ][ # # ]: 0 : else if( pRequest && pRequest->IsBusy() )
[ # # ]
411 : 0 : pReq = pLink; // only the one that is ready
412 : :
413 [ # # ]: 0 : if( pReq )
414 : : {
415 [ # # ]: 0 : if( ImplHasOtherFormat( *pReq ) )
416 : : {
417 : 0 : pReq->Execute();
418 : : }
419 [ # # ]: 0 : else if( pReq == pRequest )
420 : : {
421 : 0 : bWaitForData = sal_False;
422 : : }
423 : 0 : }
424 : : }
425 : : else
426 : : // End waiting
427 : 0 : bWaitForData = sal_False;
428 : :
429 : 0 : return 0;
430 : : }
431 : :
432 : : }
433 : :
434 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|