LCOV - code coverage report
Current view: top level - sw/source/core/fields - ddefld.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 0 213 0.0 %
Date: 2014-04-11 Functions: 0 26 0.0 %
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             : #include <osl/thread.h>
      21             : #include <sfx2/linkmgr.hxx>
      22             : #include <doc.hxx>
      23             : #include <editsh.hxx>
      24             : #include <ndtxt.hxx>
      25             : #include <fmtfld.hxx>
      26             : #include <txtfld.hxx>
      27             : #include <ddefld.hxx>
      28             : #include <swtable.hxx>
      29             : #include <swbaslnk.hxx>
      30             : #include <swddetbl.hxx>
      31             : #include <unofldmid.h>
      32             : #include <hints.hxx>
      33             : 
      34             : using namespace ::com::sun::star;
      35             : 
      36             : #define DDE_TXT_ENCODING    osl_getThreadTextEncoding()
      37             : 
      38           0 : class SwIntrnlRefLink : public SwBaseLink
      39             : {
      40             :     SwDDEFieldType& rFldType;
      41             : public:
      42           0 :     SwIntrnlRefLink( SwDDEFieldType& rType, sal_uInt16 nUpdateType, sal_uInt16 nFmt )
      43             :         : SwBaseLink( nUpdateType, nFmt ),
      44           0 :         rFldType( rType )
      45           0 :     {}
      46             : 
      47             :     virtual void Closed() SAL_OVERRIDE;
      48             :     virtual ::sfx2::SvBaseLink::UpdateResult DataChanged(
      49             :         const OUString& rMimeType, const ::com::sun::star::uno::Any & rValue ) SAL_OVERRIDE;
      50             : 
      51             :     virtual const SwNode* GetAnchor() const SAL_OVERRIDE;
      52             :     virtual bool IsInRange( sal_uLong nSttNd, sal_uLong nEndNd, sal_Int32 nStt = 0,
      53             :                             sal_Int32 nEnd = -1 ) const SAL_OVERRIDE;
      54             : };
      55             : 
      56           0 : ::sfx2::SvBaseLink::UpdateResult SwIntrnlRefLink::DataChanged( const OUString& rMimeType,
      57             :                                 const uno::Any & rValue )
      58             : {
      59           0 :     switch( SotExchange::GetFormatIdFromMimeType( rMimeType ) )
      60             :     {
      61             :     case FORMAT_STRING:
      62           0 :         if( !IsNoDataFlag() )
      63             :         {
      64           0 :             uno::Sequence< sal_Int8 > aSeq;
      65           0 :             rValue >>= aSeq;
      66           0 :             OUString sStr( (sal_Char*)aSeq.getConstArray(), aSeq.getLength(), DDE_TXT_ENCODING );
      67             : 
      68             :             // remove not needed CR-LF at the end
      69           0 :             sal_Int32 n = sStr.getLength();
      70           0 :             while( n && 0 == sStr[ n-1 ] )
      71           0 :                 --n;
      72           0 :             if( n && 0x0a == sStr[ n-1 ] )
      73           0 :                 --n;
      74           0 :             if( n && 0x0d == sStr[ n-1 ] )
      75           0 :                 --n;
      76             : 
      77           0 :             sal_Bool bDel = n != sStr.getLength();
      78           0 :             if( bDel )
      79           0 :                 sStr = sStr.copy( 0, n );
      80             : 
      81           0 :             rFldType.SetExpansion( sStr );
      82             :             // set Expansion first! (otherwise this flag will be deleted)
      83           0 :             rFldType.SetCRLFDelFlag( bDel );
      84             :         }
      85           0 :         break;
      86             : 
      87             :     // other formats
      88             :     default:
      89           0 :         return SUCCESS;
      90             :     }
      91             : 
      92             :     OSL_ENSURE( rFldType.GetDoc(), "no pDoc" );
      93             : 
      94             :     // no dependencies left?
      95           0 :     if( rFldType.GetDepends() && !rFldType.IsModifyLocked() && !ChkNoDataFlag() )
      96             :     {
      97             :         SwViewShell* pSh;
      98           0 :         SwEditShell* pESh = rFldType.GetDoc()->GetEditShell( &pSh );
      99             : 
     100             :         // Search for fields. If no valid found, disconnect.
     101           0 :         SwMsgPoolItem aUpdateDDE( RES_UPDATEDDETBL );
     102           0 :         int bCallModify = sal_False;
     103           0 :         rFldType.LockModify();
     104             : 
     105           0 :         SwClientIter aIter( rFldType );     // TODO
     106           0 :         SwClient * pLast = aIter.GoStart();
     107           0 :         if( pLast )     // Could we jump to beginning?
     108           0 :             do {
     109             :                 // a DDE table or a DDE field attribute in the text
     110           0 :                 if( !pLast->IsA( TYPE( SwFmtFld ) ) ||
     111           0 :                     ((SwFmtFld*)pLast)->GetTxtFld() )
     112             :                 {
     113           0 :                     if( !bCallModify )
     114             :                     {
     115           0 :                         if( pESh )
     116           0 :                             pESh->StartAllAction();
     117           0 :                         else if( pSh )
     118           0 :                             pSh->StartAction();
     119             :                     }
     120           0 :                     pLast->ModifyNotification( 0, &aUpdateDDE );
     121           0 :                     bCallModify = sal_True;
     122             :                 }
     123             :             } while( 0 != ( pLast = ++aIter ));
     124             : 
     125           0 :         rFldType.UnlockModify();
     126             : 
     127           0 :         if( bCallModify )
     128             :         {
     129           0 :             if( pESh )
     130           0 :                 pESh->EndAllAction();
     131           0 :             else if( pSh )
     132           0 :                 pSh->EndAction();
     133             : 
     134           0 :             if( pSh )
     135           0 :                 pSh->GetDoc()->SetModified();
     136           0 :         }
     137             :     }
     138             : 
     139           0 :     return SUCCESS;
     140             : }
     141             : 
     142           0 : void SwIntrnlRefLink::Closed()
     143             : {
     144           0 :     if( rFldType.GetDoc() && !rFldType.GetDoc()->IsInDtor() )
     145             :     {
     146             :         // advise goes, convert all fields into text?
     147             :         SwViewShell* pSh;
     148           0 :         SwEditShell* pESh = rFldType.GetDoc()->GetEditShell( &pSh );
     149           0 :         if( pESh )
     150             :         {
     151           0 :             pESh->StartAllAction();
     152           0 :             pESh->FieldToText( &rFldType );
     153           0 :             pESh->EndAllAction();
     154             :         }
     155             :         else
     156             :         {
     157           0 :             pSh->StartAction();
     158             :             // am Doc aufrufen ??
     159           0 :             pSh->EndAction();
     160             :         }
     161             :     }
     162           0 :     SvBaseLink::Closed();
     163           0 : }
     164             : 
     165           0 : const SwNode* SwIntrnlRefLink::GetAnchor() const
     166             : {
     167             :     // here, any anchor of the normal NodesArray should be sufficient
     168           0 :     const SwNode* pNd = 0;
     169           0 :     SwClientIter aIter( rFldType );     // TODO
     170           0 :     SwClient * pLast = aIter.GoStart();
     171           0 :     if( pLast )     // Could we jump to beginning?
     172           0 :         do {
     173             :             // a DDE table or a DDE field attribute in the text
     174           0 :             if( !pLast->IsA( TYPE( SwFmtFld ) ))
     175             :             {
     176           0 :                 SwDepend* pDep = (SwDepend*)pLast;
     177           0 :                 SwDDETable* pDDETbl = (SwDDETable*)pDep->GetToTell();
     178           0 :                 pNd = pDDETbl->GetTabSortBoxes()[0]->GetSttNd();
     179             :             }
     180           0 :             else if( ((SwFmtFld*)pLast)->GetTxtFld() )
     181           0 :                 pNd = ((SwFmtFld*)pLast)->GetTxtFld()->GetpTxtNode();
     182             : 
     183           0 :             if( pNd && &rFldType.GetDoc()->GetNodes() == &pNd->GetNodes() )
     184           0 :                 break;
     185           0 :             pNd = 0;
     186             :         } while( 0 != ( pLast = ++aIter ));
     187             : 
     188           0 :     return pNd;
     189             : }
     190             : 
     191           0 : bool SwIntrnlRefLink::IsInRange( sal_uLong nSttNd, sal_uLong nEndNd,
     192             :                                 sal_Int32 nStt, sal_Int32 nEnd ) const
     193             : {
     194             :     // here, any anchor of the normal NodesArray should be sufficient
     195           0 :     SwNodes* pNds = &rFldType.GetDoc()->GetNodes();
     196           0 :     SwClientIter aIter( rFldType );         // TODO
     197           0 :     SwClient * pLast = aIter.GoStart();
     198           0 :     if( pLast )     // Could we jump to beginning?
     199           0 :         do {
     200             :             // a DDE table or a DDE field attribute in the text
     201           0 :             if( !pLast->IsA( TYPE( SwFmtFld ) ))
     202             :             {
     203           0 :                 SwDepend* pDep = (SwDepend*)pLast;
     204           0 :                 SwDDETable* pDDETbl = (SwDDETable*)pDep->GetToTell();
     205           0 :                 const SwTableNode* pTblNd = pDDETbl->GetTabSortBoxes()[0]->
     206           0 :                                 GetSttNd()->FindTableNode();
     207           0 :                 if( pTblNd->GetNodes().IsDocNodes() &&
     208           0 :                     nSttNd < pTblNd->EndOfSectionIndex() &&
     209           0 :                     nEndNd > pTblNd->GetIndex() )
     210           0 :                     return true;
     211             :             }
     212           0 :             else if( ((SwFmtFld*)pLast)->GetTxtFld() )
     213             :             {
     214           0 :                 const SwTxtFld* pTFld = ((SwFmtFld*)pLast)->GetTxtFld();
     215           0 :                 const SwTxtNode* pNd = pTFld->GetpTxtNode();
     216           0 :                 if( pNd && pNds == &pNd->GetNodes() )
     217             :                 {
     218           0 :                     sal_uLong nNdPos = pNd->GetIndex();
     219           0 :                     if( nSttNd <= nNdPos && nNdPos <= nEndNd &&
     220           0 :                         ( nNdPos != nSttNd || *pTFld->GetStart() >= nStt ) &&
     221           0 :                         ( nNdPos != nEndNd || *pTFld->GetStart() < nEnd ))
     222           0 :                         return true;
     223             :                 }
     224             :             }
     225             :         } while( 0 != ( pLast = ++aIter ));
     226             : 
     227           0 :     return false;
     228             : }
     229             : 
     230           0 : SwDDEFieldType::SwDDEFieldType(const OUString& rName,
     231             :                                const OUString& rCmd, sal_uInt16 nUpdateType )
     232             :     : SwFieldType( RES_DDEFLD ),
     233           0 :     aName( rName ), pDoc( 0 ), nRefCnt( 0 )
     234             : {
     235           0 :     bCRLFFlag = bDeleted = sal_False;
     236           0 :     refLink = new SwIntrnlRefLink( *this, nUpdateType, FORMAT_STRING );
     237           0 :     SetCmd( rCmd );
     238           0 : }
     239             : 
     240           0 : SwDDEFieldType::~SwDDEFieldType()
     241             : {
     242           0 :     if( pDoc && !pDoc->IsInDtor() )
     243           0 :         pDoc->GetLinkManager().Remove( refLink );
     244           0 :     refLink->Disconnect();
     245           0 : }
     246             : 
     247           0 : SwFieldType* SwDDEFieldType::Copy() const
     248             : {
     249           0 :     SwDDEFieldType* pType = new SwDDEFieldType( aName, GetCmd(), GetType() );
     250           0 :     pType->aExpansion = aExpansion;
     251           0 :     pType->bCRLFFlag = bCRLFFlag;
     252           0 :     pType->bDeleted = bDeleted;
     253           0 :     pType->SetDoc( pDoc );
     254           0 :     return pType;
     255             : }
     256             : 
     257           0 : OUString SwDDEFieldType::GetName() const
     258             : {
     259           0 :     return aName;
     260             : }
     261             : 
     262           0 : void SwDDEFieldType::SetCmd( const OUString& _aStr )
     263             : {
     264           0 :     OUString aStr = _aStr;
     265           0 :     sal_Int32 nIndex = 0;
     266           0 :     do
     267             :     {
     268           0 :         aStr = aStr.replaceFirst("  ", " ", &nIndex);
     269           0 :     } while (nIndex>=0);
     270           0 :     refLink->SetLinkSourceName( aStr );
     271           0 : }
     272             : 
     273           0 : OUString SwDDEFieldType::GetCmd() const
     274             : {
     275           0 :     return refLink->GetLinkSourceName();
     276             : }
     277             : 
     278           0 : void SwDDEFieldType::SetDoc( SwDoc* pNewDoc )
     279             : {
     280           0 :     if( pNewDoc == pDoc )
     281           0 :         return;
     282             : 
     283           0 :     if( pDoc && refLink.Is() )
     284             :     {
     285             :         OSL_ENSURE( !nRefCnt, "How do we get the references?" );
     286           0 :         pDoc->GetLinkManager().Remove( refLink );
     287             :     }
     288             : 
     289           0 :     pDoc = pNewDoc;
     290           0 :     if( pDoc && nRefCnt )
     291             :     {
     292           0 :         refLink->SetVisible( pDoc->IsVisibleLinks() );
     293           0 :         pDoc->GetLinkManager().InsertDDELink( refLink );
     294             :     }
     295             : }
     296             : 
     297           0 : void SwDDEFieldType::_RefCntChgd()
     298             : {
     299           0 :     if( nRefCnt )
     300             :     {
     301           0 :         refLink->SetVisible( pDoc->IsVisibleLinks() );
     302           0 :         pDoc->GetLinkManager().InsertDDELink( refLink );
     303           0 :         if( pDoc->GetCurrentViewShell() )
     304           0 :             UpdateNow();
     305             :     }
     306             :     else
     307             :     {
     308           0 :         Disconnect();
     309           0 :         pDoc->GetLinkManager().Remove( refLink );
     310             :     }
     311           0 : }
     312             : 
     313           0 : bool SwDDEFieldType::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
     314             : {
     315           0 :     sal_Int32 nPart = -1;
     316           0 :     switch( nWhichId )
     317             :     {
     318           0 :     case FIELD_PROP_PAR2:      nPart = 2; break;
     319           0 :     case FIELD_PROP_PAR4:      nPart = 1; break;
     320           0 :     case FIELD_PROP_SUBTYPE:   nPart = 0; break;
     321             :     case FIELD_PROP_BOOL1:
     322             :         {
     323           0 :             sal_Bool bSet = GetType() == sfx2::LINKUPDATE_ALWAYS ? sal_True : sal_False;
     324           0 :             rVal.setValue(&bSet, ::getBooleanCppuType());
     325             :         }
     326           0 :         break;
     327             :     case FIELD_PROP_PAR5:
     328           0 :         rVal <<= aExpansion;
     329           0 :         break;
     330             :     default:
     331             :         OSL_FAIL("illegal property");
     332             :     }
     333           0 :     if ( nPart>=0 )
     334           0 :         rVal <<= GetCmd().getToken(nPart, sfx2::cTokenSeparator);
     335           0 :     return true;
     336             : }
     337             : 
     338           0 : bool SwDDEFieldType::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
     339             : {
     340           0 :     sal_Int32 nPart = -1;
     341           0 :     switch( nWhichId )
     342             :     {
     343           0 :     case FIELD_PROP_PAR2:      nPart = 2; break;
     344           0 :     case FIELD_PROP_PAR4:      nPart = 1; break;
     345           0 :     case FIELD_PROP_SUBTYPE:   nPart = 0; break;
     346             :     case FIELD_PROP_BOOL1:
     347           0 :         SetType( static_cast<sal_uInt16>(*(sal_Bool*)rVal.getValue() ?
     348             :                                      sfx2::LINKUPDATE_ALWAYS :
     349           0 :                                      sfx2::LINKUPDATE_ONCALL ) );
     350           0 :         break;
     351             :     case FIELD_PROP_PAR5:
     352           0 :         rVal >>= aExpansion;
     353           0 :         break;
     354             :     default:
     355             :         OSL_FAIL("illegal property");
     356             :     }
     357           0 :     if( nPart>=0 )
     358             :     {
     359           0 :         const OUString sOldCmd( GetCmd() );
     360           0 :         OUString sNewCmd;
     361           0 :         sal_Int32 nIndex = 0;
     362           0 :         for (sal_Int32 i=0; i<3; ++i)
     363             :         {
     364           0 :             OUString sToken = sOldCmd.getToken(0, sfx2::cTokenSeparator, nIndex);
     365           0 :             if (i==nPart)
     366             :             {
     367           0 :                 rVal >>= sToken;
     368             :             }
     369           0 :             sNewCmd += sToken + OUString(sfx2::cTokenSeparator);
     370           0 :         }
     371           0 :         SetCmd( sNewCmd );
     372             :     }
     373           0 :     return true;
     374             : }
     375             : 
     376           0 : SwDDEField::SwDDEField( SwDDEFieldType* pInitType )
     377           0 :     : SwField(pInitType)
     378             : {
     379           0 : }
     380             : 
     381           0 : SwDDEField::~SwDDEField()
     382             : {
     383           0 :     if( GetTyp()->IsLastDepend() )
     384           0 :         ((SwDDEFieldType*)GetTyp())->Disconnect();
     385           0 : }
     386             : 
     387           0 : OUString SwDDEField::Expand() const
     388             : {
     389           0 :     OUString aStr = ((SwDDEFieldType*)GetTyp())->GetExpansion();
     390           0 :     aStr = aStr.replaceAll("\r", OUString());
     391           0 :     aStr = aStr.replaceAll("\t", " ");
     392           0 :     aStr = aStr.replaceAll("\n", "|");
     393           0 :     if (aStr.endsWith("|"))
     394             :     {
     395           0 :         return aStr.copy(0, aStr.getLength()-1);
     396             :     }
     397           0 :     return aStr;
     398             : }
     399             : 
     400           0 : SwField* SwDDEField::Copy() const
     401             : {
     402           0 :     return new SwDDEField((SwDDEFieldType*)GetTyp());
     403             : }
     404             : 
     405             : /// get field type name
     406           0 : OUString SwDDEField::GetPar1() const
     407             : {
     408           0 :     return ((const SwDDEFieldType*)GetTyp())->GetName();
     409             : }
     410             : 
     411             : /// get field type command
     412           0 : OUString SwDDEField::GetPar2() const
     413             : {
     414           0 :     return ((const SwDDEFieldType*)GetTyp())->GetCmd();
     415             : }
     416             : 
     417             : /// set field type command
     418           0 : void SwDDEField::SetPar2(const OUString& rStr)
     419             : {
     420           0 :     ((SwDDEFieldType*)GetTyp())->SetCmd(rStr);
     421           0 : }
     422             : 
     423             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10