LCOV - code coverage report
Current view: top level - sw/source/core/uibase/dochdl - gloshdl.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 17 380 4.5 %
Date: 2014-04-11 Functions: 5 33 15.2 %
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 <hintids.hxx>
      21             : #include <editeng/wghtitem.hxx>
      22             : #include <editeng/adjustitem.hxx>
      23             : #ifndef __RSC //autogen
      24             : #include <tools/errinf.hxx>
      25             : #endif
      26             : #include <vcl/msgbox.hxx>
      27             : #include <svl/macitem.hxx>
      28             : #include <sfx2/fcontnr.hxx>
      29             : #include <sfx2/docfile.hxx>
      30             : #include <svl/urihelper.hxx>
      31             : #include <unotools/transliterationwrapper.hxx>
      32             : #include <poolfmt.hxx>
      33             : #include <fmtcol.hxx>
      34             : #include <docary.hxx>
      35             : #include <wrtsh.hxx>
      36             : #include <uitool.hxx>
      37             : #include <view.hxx>
      38             : #include <swevent.hxx>
      39             : #include <gloshdl.hxx>
      40             : #include <glosdoc.hxx>
      41             : #include <shellio.hxx>
      42             : #include <swundo.hxx>
      43             : #include <expfld.hxx>
      44             : #include <initui.hxx>
      45             : #include <gloslst.hxx>
      46             : #include <swdtflvr.hxx>
      47             : #include <docsh.hxx>
      48             : #include <crsskip.hxx>
      49             : 
      50             : #include <dochdl.hrc>
      51             : #include <swerror.h>
      52             : #include <frmmgr.hxx>
      53             : #include <vcl/lstbox.hxx>
      54             : 
      55             : #include <editeng/acorrcfg.hxx>
      56             : #include "swabstdlg.hxx"
      57             : #include <misc.hrc>
      58             : 
      59             : #include <IDocumentFieldsAccess.hxx>
      60             : 
      61             : using namespace ::com::sun::star;
      62             : 
      63             : const short RET_EDIT = 100;
      64             : 
      65             : // PUBLIC METHODS -------------------------------------------------------
      66           0 : struct TextBlockInfo_Impl
      67             : {
      68             :     OUString sTitle;
      69             :     OUString sLongName;
      70             :     OUString sGroupName;
      71             : };
      72             : typedef boost::ptr_vector<TextBlockInfo_Impl> TextBlockInfoArr;
      73             : 
      74             : // Dialog for edit templates
      75           0 : void SwGlossaryHdl::GlossaryDlg()
      76             : {
      77           0 :     SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
      78             :     OSL_ENSURE(pFact, "Dialogdiet fail!");
      79           0 :     AbstractGlossaryDlg* pDlg = pFact->CreateGlossaryDlg(pViewFrame, this, pWrtShell);
      80             :     OSL_ENSURE(pDlg, "Dialogdiet fail!");
      81           0 :     OUString sName;
      82           0 :     OUString sShortName;
      83             : 
      84           0 :     if( RET_EDIT == pDlg->Execute() )
      85             :     {
      86           0 :         sName = pDlg->GetCurrGrpName();
      87           0 :         sShortName = pDlg->GetCurrShortName();
      88             :     }
      89             : 
      90           0 :     delete pDlg;
      91           0 :     DELETEZ(pCurGrp);
      92           0 :     if(HasGlossaryList())
      93             :     {
      94           0 :         GetGlossaryList()->ClearGroups();
      95             :     }
      96             : 
      97           0 :     if( !sName.isEmpty() || !sShortName.isEmpty() )
      98           0 :         rStatGlossaries.EditGroupDoc( sName, sShortName );
      99           0 : }
     100             : 
     101             : // set the default group; if called from the dialog
     102             : // the group is created temporarily for faster access
     103           0 : void SwGlossaryHdl::SetCurGroup(const OUString &rGrp, sal_Bool bApi, sal_Bool bAlwaysCreateNew )
     104             : {
     105           0 :     OUString sGroup(rGrp);
     106           0 :     if (sGroup.indexOf(GLOS_DELIM)<0 && !FindGroupName(sGroup))
     107             :     {
     108           0 :         sGroup += OUString(GLOS_DELIM) + "0";
     109             :     }
     110           0 :     if(pCurGrp)
     111             :     {
     112           0 :         bool bPathEqual = false;
     113           0 :         if(!bAlwaysCreateNew)
     114             :         {
     115           0 :             INetURLObject aTemp( pCurGrp->GetFileName() );
     116           0 :             const OUString sCurBase = aTemp.getBase();
     117           0 :             aTemp.removeSegment();
     118           0 :             const OUString sCurEntryPath = aTemp.GetMainURL(INetURLObject::NO_DECODE);
     119           0 :             const std::vector<OUString> & rPathArr = rStatGlossaries.GetPathArray();
     120           0 :             sal_uInt16 nCurrentPath = USHRT_MAX;
     121           0 :             for (size_t nPath = 0; nPath < rPathArr.size(); ++nPath)
     122             :             {
     123           0 :                 if (sCurEntryPath == rPathArr[nPath])
     124             :                 {
     125           0 :                     nCurrentPath = static_cast<sal_uInt16>(nPath);
     126           0 :                     break;
     127             :                 }
     128             :             }
     129           0 :             const OUString sPath = sGroup.getToken(1, GLOS_DELIM);
     130           0 :             sal_uInt16 nComparePath = (sal_uInt16)sPath.toInt32();
     131           0 :             if(nCurrentPath == nComparePath &&
     132           0 :                 sGroup.getToken(0, GLOS_DELIM) == sCurBase)
     133           0 :                 bPathEqual = true;
     134             :         }
     135             : 
     136             :         // When path changed, the name is not reliable
     137           0 :         if(!bAlwaysCreateNew && bPathEqual)
     138           0 :             return;
     139             :     }
     140           0 :     aCurGrp = sGroup;
     141           0 :     if(!bApi)
     142             :     {
     143           0 :         if(pCurGrp)
     144             :         {
     145           0 :             rStatGlossaries.PutGroupDoc(pCurGrp);
     146           0 :             pCurGrp = 0;
     147             :         }
     148           0 :         pCurGrp = rStatGlossaries.GetGroupDoc(aCurGrp, sal_True);
     149           0 :     }
     150             : }
     151             : 
     152           0 : sal_uInt16 SwGlossaryHdl::GetGroupCnt() const
     153             : {
     154           0 :     return rStatGlossaries.GetGroupCnt();
     155             : }
     156             : 
     157           0 : OUString SwGlossaryHdl::GetGroupName( sal_uInt16 nId, OUString* pTitle )
     158             : {
     159           0 :     OUString sRet = rStatGlossaries.GetGroupName(nId);
     160           0 :     if(pTitle)
     161             :     {
     162           0 :         SwTextBlocks* pGroup = rStatGlossaries.GetGroupDoc(sRet, sal_False);
     163           0 :         if(pGroup && !pGroup->GetError())
     164             :         {
     165           0 :             *pTitle = pGroup->GetName();
     166           0 :             if (pTitle->isEmpty())
     167             :             {
     168           0 :                 *pTitle = sRet.getToken(0, GLOS_DELIM);
     169           0 :                 pGroup->SetName(*pTitle);
     170             :             }
     171           0 :             rStatGlossaries.PutGroupDoc( pGroup );
     172             :         }
     173             :         else
     174           0 :             sRet = OUString();
     175             :     }
     176           0 :     return sRet;
     177             : }
     178             : 
     179           0 : sal_Bool SwGlossaryHdl::NewGroup(OUString &rGrpName, const OUString& rTitle)
     180             : {
     181           0 :     if (rGrpName.indexOf(GLOS_DELIM)<0)
     182           0 :         FindGroupName(rGrpName);
     183           0 :     return rStatGlossaries.NewGroupDoc(rGrpName, rTitle);
     184             : }
     185             : 
     186           0 : sal_Bool SwGlossaryHdl::RenameGroup(const OUString& rOld, OUString& rNew, const OUString& rNewTitle)
     187             : {
     188           0 :     sal_Bool bRet = sal_False;
     189           0 :     OUString sOldGroup(rOld);
     190           0 :     if (rOld.indexOf(GLOS_DELIM)<0)
     191           0 :         FindGroupName(sOldGroup);
     192           0 :     if(rOld == rNew)
     193             :     {
     194           0 :         SwTextBlocks* pGroup = rStatGlossaries.GetGroupDoc(sOldGroup, sal_False);
     195           0 :         if(pGroup)
     196             :         {
     197           0 :             pGroup->SetName(rNewTitle);
     198           0 :             rStatGlossaries.PutGroupDoc( pGroup );
     199           0 :             bRet = sal_True;
     200             :         }
     201             :     }
     202             :     else
     203             :     {
     204           0 :         OUString sNewGroup(rNew);
     205           0 :         if (sNewGroup.indexOf(GLOS_DELIM)<0)
     206             :         {
     207           0 :             sNewGroup += OUString(GLOS_DELIM) + "0";
     208             :         }
     209           0 :         bRet = rStatGlossaries.RenameGroupDoc(sOldGroup, sNewGroup, rNewTitle);
     210           0 :         rNew = sNewGroup;
     211             :     }
     212           0 :     return bRet;
     213             : }
     214             : 
     215           0 : sal_Bool SwGlossaryHdl::CopyOrMove( const OUString& rSourceGroupName, OUString& rSourceShortName,
     216             :                         const OUString& rDestGroupName, const OUString& rLongName, sal_Bool bMove )
     217             : {
     218           0 :     SwTextBlocks* pSourceGroup = rStatGlossaries.GetGroupDoc(rSourceGroupName, sal_False);
     219             : 
     220           0 :     SwTextBlocks* pDestGroup = rStatGlossaries.GetGroupDoc(rDestGroupName, sal_False);
     221           0 :     if(pDestGroup->IsReadOnly() || (bMove && pSourceGroup->IsReadOnly()) )
     222           0 :         return sal_False;
     223             : 
     224             :     //The index must be determined here because rSourceShortName maybe changed in CopyBlock
     225           0 :     sal_uInt16 nDeleteIdx = pSourceGroup->GetIndex( rSourceShortName );
     226             :     OSL_ENSURE(USHRT_MAX != nDeleteIdx, "entry not found");
     227           0 :     sal_uLong nRet = pSourceGroup->CopyBlock( *pDestGroup, rSourceShortName, rLongName );
     228           0 :     if(!nRet && bMove)
     229             :     {
     230             :         // the index must be existing
     231           0 :         nRet = pSourceGroup->Delete( nDeleteIdx ) ? 0 : 1;
     232             :     }
     233           0 :     rStatGlossaries.PutGroupDoc( pSourceGroup );
     234           0 :     rStatGlossaries.PutGroupDoc( pDestGroup );
     235           0 :     return !nRet;
     236             : }
     237             : 
     238             : // delete a autotext-file-group
     239           0 : sal_Bool SwGlossaryHdl::DelGroup(const OUString &rGrpName)
     240             : {
     241           0 :     OUString sGroup(rGrpName);
     242           0 :     if (sGroup.indexOf(GLOS_DELIM)<0)
     243           0 :         FindGroupName(sGroup);
     244           0 :     if( rStatGlossaries.DelGroupDoc(sGroup) )
     245             :     {
     246           0 :         if(pCurGrp)
     247             :         {
     248           0 :             if (pCurGrp->GetName() == sGroup)
     249           0 :                 DELETEZ(pCurGrp);
     250             :         }
     251           0 :         return sal_True;
     252             :     }
     253           0 :     return sal_False;
     254             : }
     255             : 
     256             : // ask for number of autotexts
     257           0 : sal_uInt16 SwGlossaryHdl::GetGlossaryCnt()
     258             : {
     259           0 :     return pCurGrp ? pCurGrp->GetCount() : 0;
     260             : }
     261             : 
     262           0 : OUString SwGlossaryHdl::GetGlossaryName( sal_uInt16 nId )
     263             : {
     264             :     OSL_ENSURE(nId < GetGlossaryCnt(), "Textbausteinarray ueberindiziert.");
     265           0 :     return pCurGrp->GetLongName( nId );
     266             : }
     267             : 
     268           0 : OUString SwGlossaryHdl::GetGlossaryShortName(sal_uInt16 nId)
     269             : {
     270             :     OSL_ENSURE(nId < GetGlossaryCnt(), "Textbausteinarray ueberindiziert.");
     271           0 :     return pCurGrp->GetShortName( nId );
     272             : }
     273             : 
     274             : // ask for short name
     275           0 : OUString SwGlossaryHdl::GetGlossaryShortName(const OUString &rName)
     276             : {
     277           0 :     OUString sReturn;
     278             :     SwTextBlocks *pTmp =
     279           0 :         pCurGrp ? pCurGrp: rStatGlossaries.GetGroupDoc( aCurGrp, sal_False );
     280           0 :     if(pTmp)
     281             :     {
     282           0 :         sal_uInt16 nIdx = pTmp->GetLongIndex( rName );
     283           0 :         if( nIdx != (sal_uInt16) -1 )
     284           0 :             sReturn = pTmp->GetShortName( nIdx );
     285           0 :         if( !pCurGrp )
     286           0 :             rStatGlossaries.PutGroupDoc( pTmp );
     287             :     }
     288           0 :     return sReturn;
     289             : }
     290             : 
     291             : // short name for autotext already used?
     292           0 : sal_Bool SwGlossaryHdl::HasShortName(const OUString& rShortName) const
     293             : {
     294             :     SwTextBlocks *pBlock = pCurGrp ? pCurGrp
     295           0 :                                    : rStatGlossaries.GetGroupDoc( aCurGrp );
     296           0 :     sal_Bool bRet = pBlock->GetIndex( rShortName ) != (sal_uInt16) -1;
     297           0 :     if( !pCurGrp )
     298           0 :         rStatGlossaries.PutGroupDoc( pBlock );
     299           0 :     return bRet;
     300             : }
     301             : 
     302           0 : sal_Bool    SwGlossaryHdl::ConvertToNew(SwTextBlocks& /*rOld*/)
     303             : {
     304           0 :     return sal_True;
     305             : }
     306             : 
     307             : // Create autotext
     308           0 : sal_Bool SwGlossaryHdl::NewGlossary(const OUString& rName, const OUString& rShortName,
     309             :                                 sal_Bool bCreateGroup, sal_Bool bNoAttr)
     310             : {
     311             :     SwTextBlocks *pTmp =
     312           0 :         pCurGrp ? pCurGrp: rStatGlossaries.GetGroupDoc( aCurGrp, bCreateGroup );
     313             :     //pTmp == 0 if the AutoText path setting is wrong
     314           0 :     if(!pTmp)
     315           0 :         return sal_False;
     316           0 :     if(!ConvertToNew(*pTmp))
     317           0 :         return sal_False;
     318             : 
     319           0 :     OUString sOnlyTxt;
     320           0 :     OUString* pOnlyTxt = 0;
     321           0 :     if( bNoAttr )
     322             :     {
     323           0 :         if( !pWrtShell->GetSelectedText( sOnlyTxt, GETSELTXT_PARABRK_TO_ONLYCR ))
     324           0 :             return sal_False;
     325           0 :         pOnlyTxt = &sOnlyTxt;
     326             :     }
     327             : 
     328           0 :     const SvxAutoCorrCfg& rCfg = SvxAutoCorrCfg::Get();
     329             : 
     330             :     const sal_uInt16 nSuccess = pWrtShell->MakeGlossary( *pTmp, rName, rShortName,
     331           0 :                             rCfg.IsSaveRelFile(), pOnlyTxt );
     332           0 :     if(nSuccess == (sal_uInt16) -1 )
     333             :     {
     334           0 :         InfoBox(pWrtShell->GetView().GetWindow(), SW_RES(MSG_ERR_INSERT_GLOS)).Execute();
     335             :     }
     336           0 :     if( !pCurGrp )
     337           0 :         rStatGlossaries.PutGroupDoc( pTmp );
     338           0 :     return sal_Bool( nSuccess != (sal_uInt16) -1 );
     339             : }
     340             : 
     341             : // Delete a autotext
     342           0 : sal_Bool SwGlossaryHdl::DelGlossary(const OUString &rShortName)
     343             : {
     344             :     SwTextBlocks *pGlossary = pCurGrp ? pCurGrp
     345           0 :                                     : rStatGlossaries.GetGroupDoc(aCurGrp);
     346             :     //pTmp == 0 if the AutoText path setting is wrong
     347           0 :     if(!pGlossary || !ConvertToNew(*pGlossary))
     348           0 :         return sal_False;
     349             : 
     350           0 :     sal_uInt16 nIdx = pGlossary->GetIndex( rShortName );
     351           0 :     if( nIdx != (sal_uInt16) -1 )
     352           0 :         pGlossary->Delete( nIdx );
     353           0 :     if( !pCurGrp )
     354           0 :         rStatGlossaries.PutGroupDoc( pGlossary );
     355           0 :     return sal_True;
     356             : }
     357             : 
     358             : // expand short name
     359           0 : sal_Bool SwGlossaryHdl::ExpandGlossary()
     360             : {
     361             :     OSL_ENSURE(pWrtShell->CanInsert(), "illegal");
     362             :     SwTextBlocks *pGlossary;
     363           0 :     SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
     364             :     OSL_ENSURE(pFact, "Dialogdiet fail!");
     365           0 :     ::GlossaryGetCurrGroup fnGetCurrGroup = pFact->GetGlossaryCurrGroupFunc();
     366             :     OSL_ENSURE(fnGetCurrGroup, "Dialogdiet fail!");
     367           0 :     OUString sGroupName( (*fnGetCurrGroup)() );
     368           0 :     if (sGroupName.indexOf(GLOS_DELIM)<0)
     369           0 :         FindGroupName(sGroupName);
     370           0 :     pGlossary = rStatGlossaries.GetGroupDoc(sGroupName);
     371             : 
     372           0 :     OUString aShortName;
     373             : 
     374             :         // use this at text selection
     375           0 :     if(pWrtShell->SwCrsrShell::HasSelection() && !pWrtShell->IsBlockMode())
     376             :     {
     377           0 :         aShortName = pWrtShell->GetSelTxt();
     378             :     }
     379             :     else
     380             :     {
     381           0 :         if(pWrtShell->IsAddMode())
     382           0 :             pWrtShell->LeaveAddMode();
     383           0 :         else if(pWrtShell->IsBlockMode())
     384           0 :             pWrtShell->LeaveBlockMode();
     385           0 :         else if(pWrtShell->IsExtMode())
     386           0 :             pWrtShell->LeaveExtMode();
     387             :         // select word
     388           0 :         pWrtShell->SelNearestWrd();
     389             :             // ask for word
     390           0 :         if(pWrtShell->IsSelection())
     391           0 :             aShortName = pWrtShell->GetSelTxt();
     392             :     }
     393           0 :     return pGlossary ? Expand( aShortName, &rStatGlossaries, pGlossary ) : sal_False;
     394             : }
     395             : 
     396           0 : sal_Bool SwGlossaryHdl::Expand( const OUString& rShortName,
     397             :                             SwGlossaries *pGlossaries,
     398             :                             SwTextBlocks *pGlossary  )
     399             : {
     400           0 :     TextBlockInfoArr aFoundArr;
     401           0 :     OUString aShortName( rShortName );
     402           0 :     sal_Bool bCancel = sal_False;
     403             :     // search for text block
     404             :     // - don't prefer current group depending on configuration setting
     405           0 :     const SvxAutoCorrCfg& rCfg = SvxAutoCorrCfg::Get();
     406           0 :     sal_uInt16 nFound = !rCfg.IsSearchInAllCategories() ? pGlossary->GetIndex( aShortName ) : -1;
     407             :     // if not found then search in all groups
     408           0 :     if( nFound == (sal_uInt16) -1 )
     409             :     {
     410           0 :         const ::utl::TransliterationWrapper& rSCmp = GetAppCmpStrIgnore();
     411           0 :         SwGlossaryList* pGlossaryList = ::GetGlossaryList();
     412           0 :         sal_uInt16 nGroupCount = pGlossaryList->GetGroupCount();
     413           0 :         for(sal_uInt16 i = 1; i <= nGroupCount; i++)
     414             :         {
     415           0 :             OUString sTitle = pGlossaryList->GetGroupTitle(i - 1);
     416             :             // get group name with path-extension
     417           0 :             OUString sGroupName = pGlossaryList->GetGroupName(i - 1, false);
     418           0 :             if(sGroupName == pGlossary->GetName())
     419           0 :                 continue;
     420           0 :             sal_uInt16 nBlockCount = pGlossaryList->GetBlockCount(i -1);
     421           0 :             if(nBlockCount)
     422             :             {
     423           0 :                 for(sal_uInt16 j = 0; j < nBlockCount; j++)
     424             :                 {
     425           0 :                     OUString sLongName(pGlossaryList->GetBlockLongName(i - 1, j));
     426           0 :                     OUString sShortName(pGlossaryList->GetBlockShortName(i - 1, j));
     427           0 :                     if( rSCmp.isEqual( rShortName, sShortName ))
     428             :                     {
     429           0 :                         TextBlockInfo_Impl* pData = new TextBlockInfo_Impl;
     430           0 :                         pData->sTitle = sTitle;
     431           0 :                         pData->sLongName = sLongName;
     432           0 :                         pData->sGroupName = sGroupName;
     433           0 :                         aFoundArr.push_back(pData);
     434             :                     }
     435           0 :                 }
     436             :             }
     437           0 :         }
     438           0 :         if( !aFoundArr.empty() )  // one was found
     439             :         {
     440           0 :             pGlossaries->PutGroupDoc(pGlossary);
     441           0 :             if(1 == aFoundArr.size())
     442             :             {
     443           0 :                 TextBlockInfo_Impl* pData = &aFoundArr.front();
     444           0 :                 pGlossary = (SwTextBlocks *)pGlossaries->GetGroupDoc(pData->sGroupName);
     445           0 :                 nFound = pGlossary->GetIndex( aShortName );
     446             :             }
     447             :             else
     448             :             {
     449           0 :                 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
     450             :                 OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
     451             : 
     452           0 :                 AbstractSwSelGlossaryDlg* pDlg = pFact->CreateSwSelGlossaryDlg(0, aShortName);
     453             :                 OSL_ENSURE(pDlg, "Dialogdiet fail!");
     454           0 :                 for(sal_uInt16 i = 0; i < aFoundArr.size(); ++i)
     455             :                 {
     456           0 :                     TextBlockInfo_Impl* pData = &aFoundArr[i];
     457           0 :                     pDlg->InsertGlos(pData->sTitle, pData->sLongName);
     458             :                 }
     459           0 :                 pDlg->SelectEntryPos(0);
     460           0 :                 const sal_Int32 nRet = RET_OK == pDlg->Execute()?
     461           0 :                                         pDlg->GetSelectedIdx():
     462           0 :                                         LISTBOX_ENTRY_NOTFOUND;
     463           0 :                 delete pDlg;
     464           0 :                 if(LISTBOX_ENTRY_NOTFOUND != nRet)
     465             :                 {
     466           0 :                     TextBlockInfo_Impl* pData = &aFoundArr[nRet];
     467           0 :                     pGlossary = (SwTextBlocks *)pGlossaries->GetGroupDoc(pData->sGroupName);
     468           0 :                     nFound = pGlossary->GetIndex( aShortName );
     469             :                 }
     470             :                 else
     471             :                 {
     472           0 :                     nFound = (sal_uInt16) -1;
     473           0 :                     bCancel = sal_True;
     474             :                 }
     475             :             }
     476             :         }
     477             :     }
     478             : 
     479             :     // not found
     480           0 :     if( nFound == (sal_uInt16) -1 )
     481             :     {
     482           0 :         if( !bCancel )
     483             :         {
     484           0 :             pGlossaries->PutGroupDoc(pGlossary);
     485             : 
     486           0 :             const sal_Int32 nMaxLen = 50;
     487           0 :             if(pWrtShell->IsSelection() && aShortName.getLength() > nMaxLen)
     488             :             {
     489           0 :                 aShortName = aShortName.copy(0, nMaxLen) + " ...";
     490             :             }
     491           0 :             OUString aTmp( SW_RES(STR_NOGLOS));
     492           0 :             aTmp = aTmp.replaceFirst("%1", aShortName);
     493           0 :             InfoBox( pWrtShell->GetView().GetWindow(), aTmp ).Execute();
     494             :         }
     495             : 
     496           0 :         return sal_False;
     497             :     }
     498             :     else
     499             :     {
     500           0 :         SvxMacro aStartMacro(aEmptyOUStr, aEmptyOUStr, STARBASIC);
     501           0 :         SvxMacro aEndMacro(aEmptyOUStr, aEmptyOUStr, STARBASIC);
     502           0 :         GetMacros( aShortName, aStartMacro, aEndMacro, pGlossary );
     503             : 
     504             :         // StartAction must not be before HasSelection and DelRight,
     505             :         // otherwise the possible Shell change gets delayed and
     506             :         // API-programs would hang.
     507             :         // Moreover the event macro must also not be called in an action
     508           0 :         pWrtShell->StartUndo(UNDO_INSGLOSSARY);
     509           0 :         if( aStartMacro.HasMacro() )
     510           0 :             pWrtShell->ExecMacro( aStartMacro );
     511           0 :         if(pWrtShell->HasSelection())
     512           0 :             pWrtShell->DelLeft();
     513           0 :         pWrtShell->StartAllAction();
     514             : 
     515             :         // cache all InputFields
     516           0 :         SwInputFieldList aFldLst( pWrtShell, sal_True );
     517             : 
     518           0 :         pWrtShell->InsertGlossary(*pGlossary, aShortName);
     519           0 :         pWrtShell->EndAllAction();
     520           0 :         if( aEndMacro.HasMacro() )
     521             :         {
     522           0 :             pWrtShell->ExecMacro( aEndMacro );
     523             :         }
     524           0 :         pWrtShell->EndUndo(UNDO_INSGLOSSARY);
     525             : 
     526             :         // demand input for all new InputFields
     527           0 :         if( aFldLst.BuildSortLst() )
     528           0 :             pWrtShell->UpdateInputFlds( &aFldLst );
     529             :     }
     530           0 :     pGlossaries->PutGroupDoc(pGlossary);
     531           0 :     return sal_True;
     532             : }
     533             : 
     534             : // add autotext
     535           0 : bool SwGlossaryHdl::InsertGlossary(const OUString &rName)
     536             : {
     537             :     OSL_ENSURE(pWrtShell->CanInsert(), "illegal");
     538             : 
     539             :     SwTextBlocks *pGlos =
     540           0 :         pCurGrp? pCurGrp: rStatGlossaries.GetGroupDoc(aCurGrp);
     541             : 
     542           0 :     if (!pGlos)
     543           0 :         return false;
     544             : 
     545           0 :     SvxMacro aStartMacro(aEmptyOUStr, aEmptyOUStr, STARBASIC);
     546           0 :     SvxMacro aEndMacro(aEmptyOUStr, aEmptyOUStr, STARBASIC);
     547           0 :     GetMacros( rName, aStartMacro, aEndMacro, pGlos );
     548             : 
     549             :     // StartAction must not be before HasSelection and DelRight,
     550             :     // otherwise the possible Shell change gets delayed and
     551             :     // API-programs would hang.
     552             :     // Moreover the event macro must also not be called in an action
     553           0 :     if( aStartMacro.HasMacro() )
     554           0 :         pWrtShell->ExecMacro( aStartMacro );
     555           0 :     if( pWrtShell->HasSelection() )
     556           0 :         pWrtShell->DelRight();
     557           0 :     pWrtShell->StartAllAction();
     558             : 
     559             :     // cache all InputFields
     560           0 :     SwInputFieldList aFldLst( pWrtShell, sal_True );
     561             : 
     562           0 :     pWrtShell->InsertGlossary(*pGlos, rName);
     563           0 :     pWrtShell->EndAllAction();
     564           0 :     if( aEndMacro.HasMacro() )
     565             :     {
     566           0 :         pWrtShell->ExecMacro( aEndMacro );
     567             :     }
     568             : 
     569             :     // demand input for all new InputFields
     570           0 :     if( aFldLst.BuildSortLst() )
     571           0 :         pWrtShell->UpdateInputFlds( &aFldLst );
     572             : 
     573           0 :     if(!pCurGrp)
     574           0 :         rStatGlossaries.PutGroupDoc(pGlos);
     575           0 :     return true;
     576             : }
     577             : 
     578             : // set / ask for macro
     579           0 : void SwGlossaryHdl::SetMacros(const OUString& rShortName,
     580             :                               const SvxMacro* pStart,
     581             :                               const SvxMacro* pEnd,
     582             :                               SwTextBlocks *pGlossary )
     583             : {
     584             :     SwTextBlocks *pGlos = pGlossary ? pGlossary :
     585             :                                 pCurGrp ? pCurGrp
     586           0 :                                   : rStatGlossaries.GetGroupDoc( aCurGrp );
     587           0 :     SvxMacroTableDtor aMacroTbl;
     588           0 :     if( pStart )
     589           0 :         aMacroTbl.Insert( SW_EVENT_START_INS_GLOSSARY, *pStart);
     590           0 :     if( pEnd )
     591           0 :         aMacroTbl.Insert( SW_EVENT_END_INS_GLOSSARY, *pEnd);
     592           0 :     sal_uInt16 nIdx = pGlos->GetIndex( rShortName );
     593           0 :     if( !pGlos->SetMacroTable( nIdx, aMacroTbl ) && pGlos->GetError() )
     594           0 :         ErrorHandler::HandleError( pGlos->GetError() );
     595             : 
     596           0 :     if(!pCurGrp && !pGlossary)
     597           0 :         rStatGlossaries.PutGroupDoc(pGlos);
     598           0 : }
     599             : 
     600           2 : void SwGlossaryHdl::GetMacros( const OUString &rShortName,
     601             :                                 SvxMacro& rStart,
     602             :                                 SvxMacro& rEnd,
     603             :                                 SwTextBlocks *pGlossary  )
     604             : {
     605             :     SwTextBlocks *pGlos = pGlossary ? pGlossary
     606             :                                     : pCurGrp ? pCurGrp
     607           2 :                                         : rStatGlossaries.GetGroupDoc(aCurGrp);
     608           2 :     sal_uInt16 nIndex = pGlos->GetIndex( rShortName );
     609           2 :     if( nIndex != USHRT_MAX )
     610             :     {
     611           1 :         SvxMacroTableDtor aMacroTbl;
     612           1 :         if( pGlos->GetMacroTable( nIndex, aMacroTbl ) )
     613             :         {
     614           0 :             SvxMacro *pMacro = aMacroTbl.Get( SW_EVENT_START_INS_GLOSSARY );
     615           0 :             if( pMacro )
     616           0 :                 rStart = *pMacro;
     617             : 
     618           0 :             pMacro = aMacroTbl.Get( SW_EVENT_END_INS_GLOSSARY );
     619           0 :             if( pMacro )
     620           0 :                 rEnd = *pMacro;
     621           1 :         }
     622             :     }
     623             : 
     624           2 :     if( !pCurGrp && !pGlossary )
     625           0 :         rStatGlossaries.PutGroupDoc( pGlos );
     626           2 : }
     627             : 
     628             : // ctor, dtor
     629           1 : SwGlossaryHdl::SwGlossaryHdl(SfxViewFrame* pVwFrm, SwWrtShell *pSh)
     630           1 :     : rStatGlossaries( *::GetGlossaries() ),
     631             :     aCurGrp( rStatGlossaries.GetDefName() ),
     632             :     pViewFrame( pVwFrm ),
     633             :     pWrtShell( pSh ),
     634           1 :     pCurGrp( 0 )
     635             : {
     636           1 : }
     637             : 
     638           2 : SwGlossaryHdl::~SwGlossaryHdl()
     639             : {
     640           1 :     if( pCurGrp )
     641           0 :         rStatGlossaries.PutGroupDoc( pCurGrp );
     642           1 : }
     643             : 
     644             : // rename an autotext
     645           0 : sal_Bool SwGlossaryHdl::Rename(const OUString& rOldShort, const OUString& rNewShortName,
     646             :                                const OUString& rNewName )
     647             : {
     648           0 :     sal_Bool bRet = sal_False;
     649             :     SwTextBlocks *pGlossary = pCurGrp ? pCurGrp
     650           0 :                                     : rStatGlossaries.GetGroupDoc(aCurGrp);
     651           0 :     if(pGlossary)
     652             :     {
     653           0 :         if(!ConvertToNew(*pGlossary))
     654           0 :             return sal_False;
     655             : 
     656           0 :         sal_uInt16 nIdx = pGlossary->GetIndex( rOldShort );
     657           0 :         sal_uInt16 nOldLongIdx = pGlossary->GetLongIndex( rNewName );
     658           0 :         sal_uInt16 nOldIdx = pGlossary->GetIndex( rNewShortName );
     659             : 
     660           0 :         if( nIdx != USHRT_MAX &&
     661           0 :                 (nOldLongIdx == USHRT_MAX || nOldLongIdx == nIdx )&&
     662           0 :                     (nOldIdx == USHRT_MAX || nOldIdx == nIdx ))
     663             :         {
     664           0 :             pGlossary->Rename( nIdx, &rNewShortName, &rNewName );
     665           0 :             bRet = pGlossary->GetError() == 0;
     666             :         }
     667           0 :         if( !pCurGrp )
     668           0 :             rStatGlossaries.PutGroupDoc(pGlossary);
     669             :     }
     670           0 :     return bRet;
     671             : }
     672             : 
     673           0 : sal_Bool SwGlossaryHdl::IsReadOnly( const OUString* pGrpNm ) const
     674             : {
     675           0 :     SwTextBlocks *pGlossary = 0;
     676             : 
     677           0 :     if (pGrpNm)
     678           0 :         pGlossary = rStatGlossaries.GetGroupDoc( *pGrpNm );
     679           0 :     else if (pCurGrp)
     680           0 :         pGlossary = pCurGrp;
     681             :     else
     682           0 :         pGlossary = rStatGlossaries.GetGroupDoc(aCurGrp);
     683             : 
     684           0 :     sal_Bool bRet = pGlossary ? pGlossary->IsReadOnly() : sal_True;
     685           0 :     if( pGrpNm || !pCurGrp )
     686           0 :         delete pGlossary;
     687           0 :     return bRet;
     688             : }
     689             : 
     690           0 : sal_Bool SwGlossaryHdl::IsOld() const
     691             : {
     692             :     SwTextBlocks *pGlossary = pCurGrp ? pCurGrp
     693           0 :                                       : rStatGlossaries.GetGroupDoc(aCurGrp);
     694           0 :     sal_Bool bRet = pGlossary ? pGlossary->IsOld() : sal_False;
     695           0 :     if( !pCurGrp )
     696           0 :         delete pGlossary;
     697           0 :     return bRet;
     698             : }
     699             : 
     700             : // find group without path index
     701           0 : sal_Bool SwGlossaryHdl::FindGroupName(OUString& rGroup)
     702             : {
     703           0 :     return rStatGlossaries.FindGroupName(rGroup);
     704             : }
     705             : 
     706           0 : sal_Bool SwGlossaryHdl::CopyToClipboard(SwWrtShell& rSh, const OUString& rShortName)
     707             : {
     708             :     SwTextBlocks *pGlossary = pCurGrp ? pCurGrp
     709           0 :                                     : rStatGlossaries.GetGroupDoc(aCurGrp);
     710             : 
     711           0 :     SwTransferable* pTransfer = new SwTransferable( rSh );
     712             : /*??*/uno::Reference<
     713           0 :         datatransfer::XTransferable > xRef( pTransfer );
     714             : 
     715           0 :     int nRet = pTransfer->CopyGlossary( *pGlossary, rShortName );
     716           0 :     if( !pCurGrp )
     717           0 :         rStatGlossaries.PutGroupDoc( pGlossary );
     718           0 :     return 0 != nRet;
     719             : }
     720             : 
     721           0 : sal_Bool SwGlossaryHdl::ImportGlossaries( const OUString& rName )
     722             : {
     723           0 :     sal_Bool bRet = sal_False;
     724           0 :     if( !rName.isEmpty() )
     725             :     {
     726           0 :         const SfxFilter* pFilter = 0;
     727           0 :         SfxMedium* pMed = new SfxMedium( rName, STREAM_READ, 0, 0 );
     728           0 :         SfxFilterMatcher aMatcher( OUString("swriter") );
     729           0 :         pMed->UseInteractionHandler( true );
     730           0 :         if( !aMatcher.GuessFilter( *pMed, &pFilter, sal_False ) )
     731             :         {
     732           0 :             SwTextBlocks *pGlossary = NULL;
     733           0 :             pMed->SetFilter( pFilter );
     734           0 :             Reader* pR = SwReaderWriter::GetReader( pFilter->GetUserData() );
     735           0 :             if( pR && 0 != ( pGlossary = pCurGrp ? pCurGrp
     736           0 :                                     : rStatGlossaries.GetGroupDoc(aCurGrp)) )
     737             :             {
     738           0 :                 SwReader aReader( *pMed, rName );
     739           0 :                 if( aReader.HasGlossaries( *pR ) )
     740             :                 {
     741           0 :                     const SvxAutoCorrCfg& rCfg = SvxAutoCorrCfg::Get();
     742             :                     bRet = aReader.ReadGlossaries( *pR, *pGlossary,
     743           0 :                                 rCfg.IsSaveRelFile() );
     744           0 :                 }
     745             :             }
     746             :         }
     747           0 :         DELETEZ(pMed);
     748             :     }
     749           0 :     return bRet;
     750         111 : }
     751             : 
     752             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10