LCOV - code coverage report
Current view: top level - sw/source/core/edit - edfld.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 37 222 16.7 %
Date: 2014-11-03 Functions: 10 27 37.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 <config_features.h>
      21             : 
      22             : #include <unotools/charclass.hxx>
      23             : #include <editsh.hxx>
      24             : #include <fldbas.hxx>
      25             : #include <doc.hxx>
      26             : #include <IDocumentFieldsAccess.hxx>
      27             : #include <IDocumentState.hxx>
      28             : #include <docary.hxx>
      29             : #include <fmtfld.hxx>
      30             : #include <txtfld.hxx>
      31             : #include <edimp.hxx>
      32             : #include <dbfld.hxx>
      33             : #include <expfld.hxx>
      34             : #include <flddat.hxx>
      35             : #include <swundo.hxx>
      36             : #include <dbmgr.hxx>
      37             : #include <swddetbl.hxx>
      38             : #include <hints.hxx>
      39             : #include <switerator.hxx>
      40             : #include <fieldhint.hxx>
      41             : #include <DocumentSettingManager.hxx>
      42             : #include <IDocumentContentOperations.hxx>
      43             : 
      44             : /// count field types with a ResId, if 0 count all
      45           0 : sal_uInt16 SwEditShell::GetFldTypeCount(sal_uInt16 nResId, bool bUsed ) const
      46             : {
      47           0 :     const SwFldTypes* pFldTypes = GetDoc()->getIDocumentFieldsAccess().GetFldTypes();
      48           0 :     const sal_uInt16 nSize = pFldTypes->size();
      49             : 
      50           0 :     if(nResId == USHRT_MAX)
      51             :     {
      52           0 :         if(!bUsed)
      53           0 :             return nSize;
      54             :         else
      55             :         {
      56           0 :             sal_uInt16 nUsed = 0;
      57           0 :             for ( sal_uInt16 i = 0; i < nSize; i++ )
      58             :             {
      59           0 :                 if(IsUsed(*(*pFldTypes)[i]))
      60           0 :                     nUsed++;
      61             :             }
      62           0 :             return nUsed;
      63             :         }
      64             :     }
      65             : 
      66             :     // all types with the same ResId
      67           0 :     sal_uInt16 nIdx  = 0;
      68           0 :     for(sal_uInt16 i = 0; i < nSize; ++i)
      69             :     {   // same ResId -> increment index
      70           0 :         SwFieldType& rFldType = *((*pFldTypes)[i]);
      71           0 :         if(rFldType.Which() == nResId)
      72           0 :             nIdx++;
      73             :     }
      74           0 :     return nIdx;
      75             : }
      76             : 
      77             : /// get field types with a ResId, if 0 get all
      78           2 : SwFieldType* SwEditShell::GetFldType(sal_uInt16 nFld, sal_uInt16 nResId, bool bUsed ) const
      79             : {
      80           2 :     const SwFldTypes* pFldTypes = GetDoc()->getIDocumentFieldsAccess().GetFldTypes();
      81           2 :     const sal_uInt16 nSize = pFldTypes->size();
      82             : 
      83           2 :     if(nResId == USHRT_MAX && nFld < nSize)
      84             :     {
      85           0 :         if(!bUsed)
      86           0 :             return (*pFldTypes)[nFld];
      87             :         else
      88             :         {
      89           0 :             sal_uInt16 i, nUsed = 0;
      90           0 :             for ( i = 0; i < nSize; i++ )
      91             :             {
      92           0 :                 if(IsUsed(*(*pFldTypes)[i]))
      93             :                 {
      94           0 :                     if(nUsed == nFld)
      95           0 :                         break;
      96           0 :                     nUsed++;
      97             :                 }
      98             :             }
      99           0 :             return i < nSize ? (*pFldTypes)[i] : 0;
     100             :         }
     101             :     }
     102             : 
     103           2 :     sal_uInt16 nIdx = 0;
     104          26 :     for(sal_uInt16 i = 0; i < nSize; ++i)
     105             :     {   // same ResId -> increment index
     106          26 :         SwFieldType* pFldType = (*pFldTypes)[i];
     107          26 :         if(pFldType->Which() == nResId)
     108             :         {
     109           2 :             if (!bUsed || IsUsed(*pFldType))
     110             :             {
     111           2 :                 if(nIdx == nFld)
     112           2 :                     return pFldType;
     113           0 :                 nIdx++;
     114             :             }
     115             :         }
     116             :     }
     117           0 :     return 0;
     118             : }
     119             : 
     120             : /// get first type with given ResId and name
     121           0 : SwFieldType* SwEditShell::GetFldType(sal_uInt16 nResId, const OUString& rName) const
     122             : {
     123           0 :     return GetDoc()->getIDocumentFieldsAccess().GetFldType( nResId, rName, false );
     124             : }
     125             : 
     126             : /// delete field type
     127           0 : void SwEditShell::RemoveFldType(sal_uInt16 nFld, sal_uInt16 nResId)
     128             : {
     129           0 :     if( USHRT_MAX == nResId )
     130             :     {
     131           0 :         GetDoc()->getIDocumentFieldsAccess().RemoveFldType(nFld);
     132           0 :         return;
     133             :     }
     134             : 
     135           0 :     const SwFldTypes* pFldTypes = GetDoc()->getIDocumentFieldsAccess().GetFldTypes();
     136           0 :     const sal_uInt16 nSize = pFldTypes->size();
     137           0 :     sal_uInt16 nIdx = 0;
     138           0 :     for( sal_uInt16 i = 0; i < nSize; ++i )
     139             :         // Gleiche ResId -> Index erhoehen
     140           0 :         if( (*pFldTypes)[i]->Which() == nResId &&
     141           0 :             nIdx++ == nFld )
     142             :         {
     143           0 :             GetDoc()->getIDocumentFieldsAccess().RemoveFldType( i );
     144           0 :             return;
     145             :         }
     146             : }
     147             : 
     148             : /// delete field type based on its name
     149           0 : void SwEditShell::RemoveFldType(sal_uInt16 nResId, const OUString& rStr)
     150             : {
     151           0 :     const SwFldTypes* pFldTypes = GetDoc()->getIDocumentFieldsAccess().GetFldTypes();
     152           0 :     const sal_uInt16 nSize = pFldTypes->size();
     153           0 :     const CharClass& rCC = GetAppCharClass();
     154             : 
     155           0 :     OUString aTmp( rCC.lowercase( rStr ));
     156             : 
     157           0 :     for(sal_uInt16 i = 0; i < nSize; ++i)
     158             :     {
     159             :         // same ResId -> increment index
     160           0 :         SwFieldType* pFldType = (*pFldTypes)[i];
     161           0 :         if( pFldType->Which() == nResId )
     162             :         {
     163           0 :             if( aTmp == rCC.lowercase( pFldType->GetName() ) )
     164             :             {
     165           0 :                 GetDoc()->getIDocumentFieldsAccess().RemoveFldType(i);
     166           0 :                 return;
     167             :             }
     168             :         }
     169           0 :     }
     170             : }
     171             : 
     172           0 : void SwEditShell::FieldToText( SwFieldType* pType )
     173             : {
     174           0 :     if( !pType->GetDepends() )
     175           0 :         return;
     176             : 
     177           0 :     SET_CURR_SHELL( this );
     178           0 :     StartAllAction();
     179           0 :     StartUndo( UNDO_DELETE );
     180           0 :     Push();
     181           0 :     SwPaM* pPaM = GetCrsr();
     182             : 
     183           0 :     SwFieldHint aHint( pPaM );
     184           0 :     SwClientIter aIter( *pType );
     185           0 :     for ( SwClient* pClient = aIter.GoStart(); pClient; pClient = ++aIter )
     186             :     {
     187           0 :         pPaM->DeleteMark();
     188           0 :         pClient->SwClientNotifyCall( *pType, aHint );
     189             :      }
     190             : 
     191           0 :     Pop( false );
     192           0 :     EndAllAction();
     193           0 :     EndUndo( UNDO_DELETE );
     194             : }
     195             : 
     196             : /// add a field at the cursor position
     197           2 : void SwEditShell::Insert2(SwField& rFld, const bool bForceExpandHints)
     198             : {
     199           2 :     SET_CURR_SHELL( this );
     200           2 :     StartAllAction();
     201           4 :     SwFmtFld aFld( rFld );
     202             : 
     203             :     const SetAttrMode nInsertFlags = (bForceExpandHints)
     204             :         ? nsSetAttrMode::SETATTR_FORCEHINTEXPAND
     205           2 :         : nsSetAttrMode::SETATTR_DEFAULT;
     206             : 
     207           4 :     FOREACHPAM_START(GetCrsr()) // for each PaM
     208           2 :         const bool bSuccess(GetDoc()->getIDocumentContentOperations().InsertPoolItem(*PCURCRSR, aFld, nInsertFlags));
     209             :         OSL_ENSURE( bSuccess, "Doc->Insert(Field) failed");
     210             :         (void) bSuccess;
     211           2 :     FOREACHPAM_END()
     212             : 
     213           4 :     EndAllAction();
     214           2 : }
     215             : 
     216             : /// Are the PaMs positioned on fields?
     217           0 : static SwTxtFld* lcl_FindInputFld( SwDoc* pDoc, SwField& rFld )
     218             : {
     219             :     // Search field via its address. For input fields this needs to be done in protected fields.
     220           0 :     SwTxtFld* pTFld = 0;
     221           0 :     if( RES_INPUTFLD == rFld.Which() )
     222             :     {
     223           0 :         const SfxPoolItem* pItem = NULL;
     224             :         const sal_uInt32 nMaxItems =
     225           0 :             pDoc->GetAttrPool().GetItemCount2( RES_TXTATR_INPUTFIELD );
     226           0 :         for( sal_uInt32 n = 0; n < nMaxItems; ++n )
     227           0 :             if( 0 != (pItem = pDoc->GetAttrPool().GetItem2( RES_TXTATR_INPUTFIELD, n ) )
     228           0 :                 && ((SwFmtFld*)pItem)->GetField() == &rFld )
     229             :             {
     230           0 :                 pTFld = ((SwFmtFld*)pItem)->GetTxtFld();
     231           0 :                 break;
     232             :             }
     233             :     }
     234           0 :     else if( RES_SETEXPFLD == rFld.Which()
     235           0 :         && ((SwSetExpField&)rFld).GetInputFlag() )
     236             :     {
     237           0 :         const SfxPoolItem* pItem = NULL;
     238             :         const sal_uInt32 nMaxItems =
     239           0 :             pDoc->GetAttrPool().GetItemCount2( RES_TXTATR_FIELD );
     240           0 :         for( sal_uInt32 n = 0; n < nMaxItems; ++n )
     241           0 :             if( 0 != (pItem = pDoc->GetAttrPool().GetItem2( RES_TXTATR_FIELD, n ) )
     242           0 :                 && ((SwFmtFld*)pItem)->GetField() == &rFld )
     243             :             {
     244           0 :                 pTFld = ((SwFmtFld*)pItem)->GetTxtFld();
     245           0 :                 break;
     246             :             }
     247             :     }
     248           0 :     return pTFld;
     249             : }
     250             : 
     251           0 : void SwEditShell::UpdateFlds( SwField &rFld )
     252             : {
     253           0 :     SET_CURR_SHELL( this );
     254           0 :     StartAllAction();
     255             :     {
     256             :         // // If there are no selections so take the value of the current cursor position.
     257           0 :         SwMsgPoolItem* pMsgHnt = 0;
     258           0 :         SwRefMarkFldUpdate aRefMkHt( GetOut() );
     259           0 :         sal_uInt16 nFldWhich = rFld.GetTyp()->Which();
     260           0 :         if( RES_GETREFFLD == nFldWhich )
     261           0 :             pMsgHnt = &aRefMkHt;
     262             : 
     263           0 :         SwPaM* pCrsr = GetCrsr();
     264             :         SwTxtFld *pTxtFld;
     265             :         SwFmtFld *pFmtFld;
     266             : 
     267           0 :         if ( pCrsr->GetNext() == pCrsr && !pCrsr->HasMark())
     268             :         {
     269           0 :             pTxtFld = GetTxtFldAtPos( pCrsr->Start(), true );
     270             : 
     271           0 :             if (!pTxtFld) // #i30221#
     272           0 :                 pTxtFld = lcl_FindInputFld( GetDoc(), rFld);
     273             : 
     274           0 :             if (pTxtFld != 0)
     275           0 :                 GetDoc()->getIDocumentFieldsAccess().UpdateFld(pTxtFld, rFld, pMsgHnt, true);
     276             :         }
     277             : 
     278             :         // bOkay (instead of return because of EndAllAction) becomes false,
     279             :         // 1) if only one PaM has more than one field or
     280             :         // 2) if there are mixed field types
     281           0 :         bool bOkay = true;
     282           0 :         bool bTblSelBreak = false;
     283             : 
     284           0 :         SwMsgPoolItem aFldHint( RES_TXTATR_FIELD );  // Search-Hint
     285           0 :         SwMsgPoolItem aAnnotationFldHint( RES_TXTATR_ANNOTATION );
     286           0 :         SwMsgPoolItem aInputFldHint( RES_TXTATR_INPUTFIELD );
     287           0 :         FOREACHPAM_START(GetCrsr())               // for each PaM
     288           0 :             if( PCURCRSR->HasMark() && bOkay )    // ... with selection
     289             :             {
     290             :                 // copy of the PaM
     291           0 :                 SwPaM aCurPam( *PCURCRSR->GetMark(), *PCURCRSR->GetPoint() );
     292           0 :                 SwPaM aPam( *PCURCRSR->GetPoint() );
     293             : 
     294           0 :                 SwPosition *pCurStt = aCurPam.Start(), *pCurEnd =
     295           0 :                     aCurPam.End();
     296             :                 /*
     297             :                  * In case that there are two contiguous fields in a PaM, the aPam goes step by step
     298             :                  * to the end. aCurPam is reduced in each loop. If aCurPam was searched completely,
     299             :                  * the loop terminates because Start = End.
     300             :                  */
     301             : 
     302             :                 // Search for SwTxtFld ...
     303           0 :                 while(  bOkay
     304           0 :                      && pCurStt->nContent != pCurEnd->nContent
     305           0 :                      && ( aPam.Find( aFldHint, false, fnMoveForward, &aCurPam )
     306           0 :                           || aPam.Find( aAnnotationFldHint, false, fnMoveForward, &aCurPam )
     307           0 :                           || aPam.Find( aInputFldHint, false, fnMoveForward, &aCurPam ) ) )
     308             :                 {
     309             :                     // if only one PaM has more than one field  ...
     310           0 :                     if( aPam.Start()->nContent != pCurStt->nContent )
     311           0 :                         bOkay = false;
     312             : 
     313           0 :                     if( 0 != (pTxtFld = GetTxtFldAtPos( pCurStt, true )) )
     314             :                     {
     315           0 :                         pFmtFld = (SwFmtFld*)&pTxtFld->GetFmtFld();
     316           0 :                         SwField *pCurFld = pFmtFld->GetField();
     317             : 
     318             :                         // if there are mixed field types
     319           0 :                         if( pCurFld->GetTyp()->Which() !=
     320           0 :                             rFld.GetTyp()->Which() )
     321           0 :                             bOkay = false;
     322             : 
     323           0 :                         bTblSelBreak = GetDoc()->getIDocumentFieldsAccess().UpdateFld(pTxtFld, rFld,
     324           0 :                                                            pMsgHnt, false);
     325             :                     }
     326             :                     // The search area is reduced by the found area:
     327           0 :                     pCurStt->nContent++;
     328           0 :                 }
     329             :             }
     330             : 
     331           0 :             if( bTblSelBreak ) // If table section and table formula are updated -> finish
     332           0 :                 break;
     333             : 
     334           0 :         FOREACHPAM_END()
     335             :     }
     336           0 :     GetDoc()->getIDocumentState().SetModified();
     337           0 :     EndAllAction();
     338           0 : }
     339             : 
     340           0 : SwDBData SwEditShell::GetDBData() const
     341             : {
     342           0 :     return GetDoc()->GetDBData();
     343             : }
     344             : 
     345           4 : const SwDBData& SwEditShell::GetDBDesc() const
     346             : {
     347           4 :     return GetDoc()->GetDBDesc();
     348             : }
     349             : 
     350           4 : void SwEditShell::ChgDBData(const SwDBData& rNewData)
     351             : {
     352           4 :     GetDoc()->ChgDBData(rNewData);
     353           4 : }
     354             : 
     355           0 : void SwEditShell::GetAllUsedDB( std::vector<OUString>& rDBNameList,
     356             :                                 std::vector<OUString>* pAllDBNames )
     357             : {
     358           0 :     GetDoc()->GetAllUsedDB( rDBNameList, pAllDBNames );
     359           0 : }
     360             : 
     361           0 : void SwEditShell::ChangeDBFields( const std::vector<OUString>& rOldNames,
     362             :                                   const OUString& rNewName )
     363             : {
     364           0 :     GetDoc()->ChangeDBFields( rOldNames, rNewName );
     365           0 : }
     366             : 
     367             : /// Update all expression fields
     368           0 : void SwEditShell::UpdateExpFlds(bool bCloseDB)
     369             : {
     370           0 :     SET_CURR_SHELL( this );
     371           0 :     StartAllAction();
     372           0 :     GetDoc()->getIDocumentFieldsAccess().UpdateExpFlds(NULL, true);
     373           0 :     if (bCloseDB)
     374             :     {
     375             : #if HAVE_FEATURE_DBCONNECTIVITY
     376           0 :         GetDoc()->GetDBManager()->CloseAll(); // close all database connections
     377             : #endif
     378             :     }
     379           0 :     EndAllAction();
     380           0 : }
     381             : 
     382           4 : SwDBManager* SwEditShell::GetDBManager() const
     383             : {
     384             : #if HAVE_FEATURE_DBCONNECTIVITY
     385           4 :     return GetDoc()->GetDBManager();
     386             : #else
     387             :     return NULL;
     388             : #endif
     389             : }
     390             : 
     391             : /// insert field type
     392           0 : SwFieldType* SwEditShell::InsertFldType(const SwFieldType& rFldType)
     393             : {
     394           0 :     return GetDoc()->getIDocumentFieldsAccess().InsertFldType(rFldType);
     395             : }
     396             : 
     397          10 : void SwEditShell::LockExpFlds()
     398             : {
     399          10 :     GetDoc()->getIDocumentFieldsAccess().LockExpFlds();
     400          10 : }
     401             : 
     402          10 : void SwEditShell::UnlockExpFlds()
     403             : {
     404          10 :     GetDoc()->getIDocumentFieldsAccess().UnlockExpFlds();
     405          10 : }
     406             : 
     407           0 : void SwEditShell::SetFldUpdateFlags( SwFldUpdateFlags eFlags )
     408             : {
     409           0 :     getIDocumentSettingAccess()->setFieldUpdateFlags( eFlags );
     410           0 : }
     411             : 
     412           0 : SwFldUpdateFlags SwEditShell::GetFldUpdateFlags(bool bDocSettings) const
     413             : {
     414           0 :     return getIDocumentSettingAccess()->getFieldUpdateFlags( !bDocSettings );
     415             : }
     416             : 
     417           0 : void SwEditShell::SetLabelDoc( bool bFlag )
     418             : {
     419           0 :     GetDoc()->GetDocumentSettingManager().set(IDocumentSettingAccess::LABEL_DOCUMENT, bFlag );
     420           0 : }
     421             : 
     422       10278 : bool SwEditShell::IsLabelDoc() const
     423             : {
     424       10278 :     return getIDocumentSettingAccess()->get(IDocumentSettingAccess::LABEL_DOCUMENT);
     425             : }
     426             : 
     427           0 : void SwEditShell::ChangeAuthorityData(const SwAuthEntry* pNewData)
     428             : {
     429           0 :     GetDoc()->ChangeAuthorityData(pNewData);
     430           0 : }
     431             : 
     432           0 : bool SwEditShell::IsAnyDatabaseFieldInDoc()const
     433             : {
     434           0 :     const SwFldTypes * pFldTypes = GetDoc()->getIDocumentFieldsAccess().GetFldTypes();
     435           0 :     const sal_uInt16 nSize = pFldTypes->size();
     436           0 :     for(sal_uInt16 i = 0; i < nSize; ++i)
     437             :     {
     438           0 :         SwFieldType& rFldType = *((*pFldTypes)[i]);
     439           0 :         sal_uInt16 nWhich = rFldType.Which();
     440           0 :         if(IsUsed(rFldType))
     441             :         {
     442           0 :             switch(nWhich)
     443             :             {
     444             :                 case RES_DBFLD:
     445             :                 case RES_DBNEXTSETFLD:
     446             :                 case RES_DBNUMSETFLD:
     447             :                 case RES_DBSETNUMBERFLD:
     448             :                 {
     449           0 :                     SwIterator<SwFmtFld,SwFieldType> aIter( rFldType );
     450           0 :                     SwFmtFld* pFld = aIter.First();
     451           0 :                     while(pFld)
     452             :                     {
     453           0 :                         if(pFld->IsFldInDoc())
     454           0 :                             return true;
     455           0 :                         pFld = aIter.Next();
     456           0 :                     }
     457             :                 }
     458           0 :                 break;
     459             :             }
     460             :         }
     461             :     }
     462           0 :     return false;
     463         270 : }
     464             : 
     465             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10