LCOV - code coverage report
Current view: top level - sfx2/source/appl - impldde.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 163 0.6 %
Date: 2014-11-03 Functions: 2 21 9.5 %
Legend: Lines: hit not hit

          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 <comphelper/string.hxx>
      29             : #include <vcl/svapp.hxx>
      30             : #include <vcl/fixed.hxx>
      31             : #include <vcl/edit.hxx>
      32             : #include <vcl/button.hxx>
      33             : #include <vcl/msgbox.hxx>
      34             : #include <sot/exchange.hxx>
      35             : #include <rtl/strbuf.hxx>
      36             : #include <rtl/ustring.hxx>
      37             : 
      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             : #include <unotools/securityoptions.hxx>
      49             : 
      50             : #define DDELINK_ERROR_APP   1
      51             : #define DDELINK_ERROR_DATA  2
      52             : 
      53             : using namespace ::com::sun::star::uno;
      54             : 
      55             : namespace sfx2
      56             : {
      57             : 
      58           0 : class SvDDELinkEditDialog : public ModalDialog
      59             : {
      60             :     Edit            *m_pEdDdeApp;
      61             :     Edit            *m_pEdDdeTopic;
      62             :     Edit            *m_pEdDdeItem;
      63             :     OKButton        *m_pOKButton;
      64             : 
      65             :     DECL_STATIC_LINK( SvDDELinkEditDialog, EditHdl_Impl, Edit* );
      66             : public:
      67             :     SvDDELinkEditDialog( vcl::Window* pParent, SvBaseLink* );
      68             :     OUString GetCmd() const;
      69             : };
      70             : 
      71           0 : SvDDELinkEditDialog::SvDDELinkEditDialog( vcl::Window* pParent, SvBaseLink* pLink )
      72           0 :     : ModalDialog( pParent, "LinkEditDialog", "sfx/ui/linkeditdialog.ui" )
      73             : {
      74           0 :     get(m_pOKButton, "ok");
      75           0 :     get(m_pEdDdeApp, "app");
      76           0 :     get(m_pEdDdeTopic, "file");
      77           0 :     get(m_pEdDdeItem, "category");
      78             : 
      79           0 :     OUString sServer, sTopic, sItem;
      80           0 :     pLink->GetLinkManager()->GetDisplayNames( pLink, &sServer, &sTopic, &sItem );
      81             : 
      82           0 :     m_pEdDdeApp->SetText( sServer );
      83           0 :     m_pEdDdeTopic->SetText( sTopic );
      84           0 :     m_pEdDdeItem->SetText( sItem );
      85             : 
      86           0 :     m_pEdDdeApp->SetModifyHdl( STATIC_LINK( this, SvDDELinkEditDialog, EditHdl_Impl));
      87           0 :     m_pEdDdeTopic->SetModifyHdl( STATIC_LINK( this, SvDDELinkEditDialog, EditHdl_Impl));
      88           0 :     m_pEdDdeItem->SetModifyHdl( STATIC_LINK( this, SvDDELinkEditDialog, EditHdl_Impl));
      89             : 
      90           0 :     m_pOKButton->Enable( !sServer.isEmpty() && !sTopic.isEmpty() && !sItem.isEmpty() );
      91           0 : }
      92             : 
      93           0 : OUString SvDDELinkEditDialog::GetCmd() const
      94             : {
      95           0 :     OUString sCmd( m_pEdDdeApp->GetText() ), sRet;
      96           0 :     ::sfx2::MakeLnkName( sRet, &sCmd, m_pEdDdeTopic->GetText(), m_pEdDdeItem->GetText() );
      97           0 :     return sRet;
      98             : }
      99             : 
     100           0 : IMPL_STATIC_LINK( SvDDELinkEditDialog, EditHdl_Impl, Edit *, pEdit )
     101             : {
     102             :     (void)pEdit; // unused variable
     103           0 :     pThis->m_pOKButton->Enable( !pThis->m_pEdDdeApp->GetText().isEmpty() &&
     104           0 :                               !pThis->m_pEdDdeTopic->GetText().isEmpty() &&
     105           0 :                               !pThis->m_pEdDdeItem->GetText().isEmpty() );
     106           0 :     return 0;
     107             : }
     108             : 
     109           0 : SvDDEObject::SvDDEObject()
     110           0 :     : pConnection( 0 ), pLink( 0 ), pRequest( 0 ), pGetData( 0 ), nError( 0 )
     111             : {
     112           0 :     SetUpdateTimeout( 100 );
     113           0 :     bWaitForData = sal_False;
     114           0 : }
     115             : 
     116           0 : SvDDEObject::~SvDDEObject()
     117             : {
     118           0 :     delete pLink;
     119           0 :     delete pRequest;
     120           0 :     delete pConnection;
     121           0 : }
     122             : 
     123           0 : bool SvDDEObject::GetData( ::com::sun::star::uno::Any & rData /*out param*/,
     124             :                             const OUString & rMimeType,
     125             :                             bool bSynchron )
     126             : {
     127           0 :     if( !pConnection )
     128           0 :         return false;
     129             : 
     130           0 :     if( pConnection->GetError() )  // then we try once more
     131             :     {
     132           0 :         OUString sServer( pConnection->GetServiceName() );
     133           0 :         OUString sTopic( pConnection->GetTopicName() );
     134             : 
     135           0 :         delete pConnection;
     136           0 :         pConnection = new DdeConnection( sServer, sTopic );
     137           0 :         if( pConnection->GetError() )
     138           0 :             nError = DDELINK_ERROR_APP;
     139             :     }
     140             : 
     141           0 :     if( bWaitForData ) // we are in an rekursive loop, get out again
     142           0 :         return false;
     143             : 
     144             :     // Lock against Reentrance
     145           0 :     bWaitForData = sal_True;
     146             : 
     147             :     // if you want to print, we'll wait until the data is available
     148           0 :     if( bSynchron )
     149             :     {
     150           0 :         DdeRequest aReq( *pConnection, sItem, 5000 );
     151           0 :         aReq.SetDataHdl( LINK( this, SvDDEObject, ImplGetDDEData ) );
     152           0 :         aReq.SetFormat( SotExchange::GetFormatIdFromMimeType( rMimeType ));
     153             : 
     154           0 :         pGetData = &rData;
     155             : 
     156           0 :         do {
     157           0 :             aReq.Execute();
     158           0 :         } while( aReq.GetError() && ImplHasOtherFormat( aReq ) );
     159             : 
     160           0 :         if( pConnection->GetError() )
     161           0 :             nError = DDELINK_ERROR_DATA;
     162             : 
     163           0 :         bWaitForData = sal_False;
     164             :     }
     165             :     else
     166             :     {
     167             :         // otherwise it will be executed asynchronously
     168             :         {
     169           0 :             if( pRequest )
     170           0 :                 delete pRequest;
     171             : 
     172           0 :             pRequest = new DdeRequest( *pConnection, sItem );
     173           0 :             pRequest->SetDataHdl( LINK( this, SvDDEObject, ImplGetDDEData ) );
     174           0 :             pRequest->SetDoneHdl( LINK( this, SvDDEObject, ImplDoneDDEData ) );
     175             :             pRequest->SetFormat( SotExchange::GetFormatIdFromMimeType(
     176           0 :                                     rMimeType ) );
     177           0 :             pRequest->Execute();
     178             :         }
     179             : 
     180           0 :         OUString aEmptyStr;
     181           0 :         rData <<= aEmptyStr;
     182             :     }
     183           0 :     return 0 == pConnection->GetError();
     184             : }
     185             : 
     186             : 
     187           0 : bool SvDDEObject::Connect( SvBaseLink * pSvLink )
     188             : {
     189           0 :     sal_uInt16 nLinkType = pSvLink->GetUpdateMode();
     190           0 :     if( pConnection )           // Connection is already made
     191             :     {
     192             :         // well, then just add it as dependent
     193             :         AddDataAdvise( pSvLink,
     194             :                 SotExchange::GetFormatMimeType( pSvLink->GetContentType()),
     195             :                 LINKUPDATE_ONCALL == nLinkType
     196             :                         ? ADVISEMODE_ONLYONCE
     197           0 :                         : 0 );
     198           0 :         AddConnectAdvise( pSvLink );
     199             : 
     200           0 :         return true;
     201             :     }
     202             : 
     203           0 :     if( !pSvLink->GetLinkManager() )
     204           0 :         return false;
     205             : 
     206           0 :     OUString sServer, sTopic;
     207           0 :     pSvLink->GetLinkManager()->GetDisplayNames( pSvLink, &sServer, &sTopic, &sItem );
     208             : 
     209           0 :     if( sServer.isEmpty() || sTopic.isEmpty() || sItem.isEmpty() )
     210           0 :         return false;
     211             : 
     212           0 :     pConnection = new DdeConnection( sServer, sTopic );
     213           0 :     if( pConnection->GetError() )
     214             :     {
     215             :         // check if the DDE server knows the "SYSTEM" topic
     216           0 :         bool bSysTopic = false;
     217           0 :         if (!sTopic.equalsIgnoreAsciiCase("SYSTEM"))
     218             :         {
     219           0 :             DdeConnection aTmp(sServer, OUString("SYSTEM"));
     220           0 :             bSysTopic = !aTmp.GetError();
     221             :         }
     222             : 
     223           0 :         if( bSysTopic )
     224             :         {
     225             :             // if the system topic works then the server is up but just doesn't know the original topic
     226           0 :             nError = DDELINK_ERROR_DATA;
     227           0 :             return false;
     228             :         }
     229             : 
     230           0 :         nError = DDELINK_ERROR_APP;
     231             :     }
     232             : 
     233           0 :     if( LINKUPDATE_ALWAYS == nLinkType && !pLink && !pConnection->GetError() )
     234             :     {
     235             :         // Setting up Hot Link, Data will be available at some point later on
     236           0 :         pLink = new DdeHotLink( *pConnection, sItem );
     237           0 :         pLink->SetDataHdl( LINK( this, SvDDEObject, ImplGetDDEData ) );
     238           0 :         pLink->SetDoneHdl( LINK( this, SvDDEObject, ImplDoneDDEData ) );
     239           0 :         pLink->SetFormat( pSvLink->GetContentType() );
     240           0 :         pLink->Execute();
     241             :     }
     242             : 
     243           0 :     if( pConnection->GetError() )
     244           0 :         return false;
     245             : 
     246             :     AddDataAdvise( pSvLink,
     247             :                 SotExchange::GetFormatMimeType( pSvLink->GetContentType()),
     248             :                 LINKUPDATE_ONCALL == nLinkType
     249             :                         ? ADVISEMODE_ONLYONCE
     250           0 :                         : 0 );
     251           0 :     AddConnectAdvise( pSvLink );
     252           0 :     SetUpdateTimeout( 0 );
     253           0 :     return true;
     254             : }
     255             : 
     256           0 : void SvDDEObject::Edit( vcl::Window* pParent, sfx2::SvBaseLink* pBaseLink, const Link& rEndEditHdl )
     257             : {
     258           0 :     SvDDELinkEditDialog aDlg( pParent, pBaseLink );
     259           0 :     if ( RET_OK == aDlg.Execute() && rEndEditHdl.IsSet() )
     260             :     {
     261           0 :         OUString sCommand = aDlg.GetCmd();
     262           0 :         rEndEditHdl.Call( &sCommand );
     263           0 :     }
     264           0 : }
     265             : 
     266           0 : bool SvDDEObject::ImplHasOtherFormat( DdeTransaction& rReq )
     267             : {
     268           0 :     sal_uInt16 nFmt = 0;
     269           0 :     switch( rReq.GetFormat() )
     270             :     {
     271             :     case FORMAT_RTF:
     272           0 :         nFmt = FORMAT_STRING;
     273           0 :         break;
     274             : 
     275             :     case SOT_FORMATSTR_ID_HTML_SIMPLE:
     276             :     case SOT_FORMATSTR_ID_HTML:
     277           0 :         nFmt = FORMAT_RTF;
     278           0 :         break;
     279             : 
     280             :     case FORMAT_GDIMETAFILE:
     281           0 :         nFmt = FORMAT_BITMAP;
     282           0 :         break;
     283             : 
     284             :     case SOT_FORMATSTR_ID_SVXB:
     285           0 :         nFmt = FORMAT_GDIMETAFILE;
     286           0 :         break;
     287             : 
     288             :     // something else?
     289             :     }
     290           0 :     if( nFmt )
     291           0 :         rReq.SetFormat( nFmt );         // try it once more
     292           0 :     return 0 != nFmt;
     293             : }
     294             : 
     295           0 : bool SvDDEObject::IsPending() const
     296             : /*  [Description]
     297             : 
     298             :     The method determines whether the data-object can be read from a DDE.
     299             : 
     300             :     Returned is the following:
     301             :         ERRCODE_NONE                    if it has been completely read
     302             :         ERRCODE_SO_PENDING              if it has not been completely read
     303             :         ERRCODE_SO_FALSE                otherwise
     304             : */
     305             : {
     306           0 :     return bWaitForData;
     307             : }
     308             : 
     309           0 : bool SvDDEObject::IsDataComplete() const
     310             : {
     311           0 :     return bWaitForData;
     312             : }
     313             : 
     314           0 : IMPL_LINK( SvDDEObject, ImplGetDDEData, DdeData*, pData )
     315             : {
     316           0 :     sal_uIntPtr nFmt = pData->GetFormat();
     317           0 :     switch( nFmt )
     318             :     {
     319             :     case FORMAT_GDIMETAFILE:
     320           0 :         break;
     321             : 
     322             :     case FORMAT_BITMAP:
     323           0 :         break;
     324             : 
     325             :     default:
     326             :         {
     327           0 :             const sal_Char* p = (sal_Char*)( pData->operator const void*() );
     328           0 :             long nLen = FORMAT_STRING == nFmt ? (p ? strlen( p ) : 0) : (long)*pData;
     329             : 
     330           0 :             Sequence< sal_Int8 > aSeq( (const sal_Int8*)p, nLen );
     331           0 :             if( pGetData )
     332             :             {
     333           0 :                 *pGetData <<= aSeq;  // Copy Data
     334           0 :                 pGetData = 0;        // reset the pointer here
     335             :             }
     336             :             else
     337             :             {
     338           0 :                 Any aVal;
     339           0 :                 aVal <<= aSeq;
     340             :                 DataChanged( SotExchange::GetFormatMimeType(
     341           0 :                                                 pData->GetFormat() ), aVal );
     342           0 :                 bWaitForData = sal_False;
     343           0 :             }
     344             :         }
     345             :     }
     346             : 
     347           0 :     return 0;
     348             : }
     349             : 
     350           0 : IMPL_LINK( SvDDEObject, ImplDoneDDEData, void*, pData )
     351             : {
     352           0 :     bool bValid = (bool)reinterpret_cast<sal_uIntPtr>(pData);
     353           0 :     if( !bValid && ( pRequest || pLink ))
     354             :     {
     355           0 :         DdeTransaction* pReq = 0;
     356           0 :         if( !pLink || ( pLink && pLink->IsBusy() ))
     357           0 :             pReq = pRequest;  // only the one that is ready
     358           0 :         else if( pRequest && pRequest->IsBusy() )
     359           0 :             pReq = pLink;  // only the one that is ready
     360             : 
     361           0 :         if( pReq )
     362             :         {
     363           0 :             if( ImplHasOtherFormat( *pReq ) )
     364             :             {
     365           0 :                 pReq->Execute();
     366             :             }
     367           0 :             else if( pReq == pRequest )
     368             :             {
     369           0 :                 bWaitForData = sal_False;
     370             :             }
     371           0 :         }
     372             :     }
     373             :     else
     374             :         // End waiting
     375           0 :         bWaitForData = sal_False;
     376             : 
     377           0 :     return 0;
     378             : }
     379             : 
     380         951 : }
     381             : 
     382             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10