LCOV - code coverage report
Current view: top level - sw/source/uibase/utlui - gloslst.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 225 0.4 %
Date: 2015-06-13 12:38:46 Functions: 2 30 6.7 %
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 <tools/urlobj.hxx>
      21             : #include <vcl/dialog.hxx>
      22             : #include <vcl/msgbox.hxx>
      23             : #include <vcl/button.hxx>
      24             : #include <vcl/fixed.hxx>
      25             : #include <vcl/lstbox.hxx>
      26             : #include <svl/fstathelper.hxx>
      27             : #include <unotools/pathoptions.hxx>
      28             : #include <unotools/transliterationwrapper.hxx>
      29             : #include <swtypes.hxx>
      30             : #include <swmodule.hxx>
      31             : #include <shellio.hxx>
      32             : #include <initui.hxx>
      33             : #include <glosdoc.hxx>
      34             : #include <gloslst.hxx>
      35             : #include <swunohelper.hxx>
      36             : 
      37             : #include <vector>
      38             : 
      39             : #include <utlui.hrc>
      40             : 
      41             : #define STRING_DELIM (char)0x0A
      42             : #define GLOS_TIMEOUT 30000   // update every 30 seconds
      43             : #define FIND_MAX_GLOS 20
      44             : 
      45           0 : struct TripleString
      46             : {
      47             :     OUString sGroup;
      48             :     OUString sBlock;
      49             :     OUString sShort;
      50             : };
      51             : 
      52             : class SwGlossDecideDlg : public ModalDialog
      53             : {
      54             :     VclPtr<OKButton> m_pOk;
      55             :     VclPtr<ListBox>  m_pListLB;
      56             : 
      57             :     DECL_LINK(DoubleClickHdl, void*);
      58             :     DECL_LINK(SelectHdl, void*);
      59             : 
      60             : public:
      61             :     explicit SwGlossDecideDlg(vcl::Window* pParent);
      62             :     virtual ~SwGlossDecideDlg();
      63             :     virtual void dispose() SAL_OVERRIDE;
      64             : 
      65           0 :     ListBox&    GetListBox() {return *m_pListLB;}
      66             : };
      67             : 
      68           0 : SwGlossDecideDlg::SwGlossDecideDlg(vcl::Window* pParent)
      69             :     : ModalDialog(pParent, "SelectAutoTextDialog",
      70           0 :         "modules/swriter/ui/selectautotextdialog.ui")
      71             : {
      72           0 :     get(m_pOk, "ok");
      73           0 :     get(m_pListLB, "treeview");
      74           0 :     m_pListLB->set_height_request(m_pListLB->GetTextHeight() * 10);
      75           0 :     m_pListLB->SetDoubleClickHdl(LINK(this, SwGlossDecideDlg, DoubleClickHdl));
      76           0 :     m_pListLB->SetSelectHdl(LINK(this, SwGlossDecideDlg, SelectHdl));
      77           0 : }
      78             : 
      79           0 : SwGlossDecideDlg::~SwGlossDecideDlg()
      80             : {
      81           0 :     disposeOnce();
      82           0 : }
      83             : 
      84           0 : void SwGlossDecideDlg::dispose()
      85             : {
      86           0 :     m_pOk.clear();
      87           0 :     m_pListLB.clear();
      88           0 :     ModalDialog::dispose();
      89           0 : }
      90             : 
      91           0 : IMPL_LINK_NOARG(SwGlossDecideDlg, DoubleClickHdl)
      92             : {
      93           0 :     EndDialog(RET_OK);
      94           0 :     return 0;
      95             : }
      96             : 
      97           0 : IMPL_LINK_NOARG(SwGlossDecideDlg, SelectHdl)
      98             : {
      99           0 :     m_pOk->Enable(LISTBOX_ENTRY_NOTFOUND != m_pListLB->GetSelectEntryPos());
     100           0 :     return 0;
     101             : }
     102             : 
     103           0 : SwGlossaryList::SwGlossaryList() :
     104           0 :     bFilled(false)
     105             : {
     106           0 :     SvtPathOptions aPathOpt;
     107           0 :     sPath = aPathOpt.GetAutoTextPath();
     108           0 :     SetTimeout(GLOS_TIMEOUT);
     109           0 : }
     110             : 
     111           0 : SwGlossaryList::~SwGlossaryList()
     112             : {
     113           0 :     ClearGroups();
     114           0 : }
     115             : 
     116             : // If the GroupName is already known, then only rShortName
     117             : // will be filled. Otherwise also rGroupName will be set and
     118             : // on demand asked for the right group.
     119             : 
     120           0 : bool SwGlossaryList::GetShortName(const OUString& rLongName,
     121             :                                   OUString& rShortName, OUString& rGroupName )
     122             : {
     123           0 :     if(!bFilled)
     124           0 :         Update();
     125             : 
     126           0 :     std::vector<TripleString> aTripleStrings;
     127             : 
     128           0 :     size_t nCount = aGroupArr.size();
     129           0 :     for(size_t i = 0; i < nCount; i++ )
     130             :     {
     131           0 :         AutoTextGroup* pGroup = aGroupArr[i];
     132           0 :         if(!rGroupName.isEmpty() && rGroupName != pGroup->sName)
     133           0 :             continue;
     134             : 
     135           0 :         sal_Int32 nPosLong = 0;
     136           0 :         for(sal_uInt16 j = 0; j < pGroup->nCount; j++)
     137             :         {
     138           0 :             const OUString sLong = pGroup->sLongNames.getToken(0, STRING_DELIM, nPosLong);
     139           0 :             if(rLongName != sLong)
     140           0 :                 continue;
     141             : 
     142           0 :             TripleString pTriple;
     143           0 :             pTriple.sGroup = pGroup->sName;
     144           0 :             pTriple.sBlock = sLong;
     145           0 :             pTriple.sShort = pGroup->sShortNames.getToken(j, STRING_DELIM);
     146           0 :             aTripleStrings.push_back(pTriple);
     147           0 :         }
     148             :     }
     149             : 
     150           0 :     bool bRet = false;
     151           0 :     nCount = aTripleStrings.size();
     152           0 :     if(1 == nCount)
     153             :     {
     154           0 :         const TripleString& pTriple(aTripleStrings.front());
     155           0 :         rShortName = pTriple.sShort;
     156           0 :         rGroupName = pTriple.sGroup;
     157           0 :         bRet = true;
     158             :     }
     159           0 :     else if(1 < nCount)
     160             :     {
     161           0 :         ScopedVclPtrInstance< SwGlossDecideDlg > aDlg(nullptr);
     162           0 :         OUString sTitle = aDlg->GetText() + " " + aTripleStrings.front().sBlock;
     163           0 :         aDlg->SetText(sTitle);
     164             : 
     165           0 :         ListBox& rLB = aDlg->GetListBox();
     166           0 :         for(std::vector<TripleString>::const_iterator i = aTripleStrings.begin(); i != aTripleStrings.end(); ++i)
     167           0 :             rLB.InsertEntry(i->sGroup.getToken(0, GLOS_DELIM));
     168             : 
     169           0 :         rLB.SelectEntryPos(0);
     170           0 :         if(RET_OK == aDlg->Execute() &&
     171           0 :             LISTBOX_ENTRY_NOTFOUND != rLB.GetSelectEntryPos())
     172             :         {
     173           0 :             const TripleString& pTriple(aTripleStrings[rLB.GetSelectEntryPos()]);
     174           0 :             rShortName = pTriple.sShort;
     175           0 :             rGroupName = pTriple.sGroup;
     176           0 :             bRet = true;
     177             :         }
     178             :         else
     179           0 :             bRet = false;
     180             :     }
     181           0 :     return bRet;
     182             : }
     183             : 
     184           0 : size_t SwGlossaryList::GetGroupCount()
     185             : {
     186           0 :     if(!bFilled)
     187           0 :         Update();
     188           0 :     return aGroupArr.size();
     189             : }
     190             : 
     191           0 : OUString SwGlossaryList::GetGroupName(size_t nPos, bool bNoPath)
     192             : {
     193             :     OSL_ENSURE(aGroupArr.size() > nPos, "group not available");
     194           0 :     if(nPos < aGroupArr.size())
     195             :     {
     196           0 :         AutoTextGroup* pGroup = aGroupArr[nPos];
     197           0 :         OUString sRet = pGroup->sName;
     198           0 :         if(bNoPath)
     199           0 :             sRet = sRet.getToken(0, GLOS_DELIM);
     200           0 :         return sRet;
     201             :     }
     202           0 :     return OUString();
     203             : }
     204             : 
     205           0 : OUString SwGlossaryList::GetGroupTitle(size_t nPos)
     206             : {
     207             :     OSL_ENSURE(aGroupArr.size() > nPos, "group not available");
     208           0 :     if(nPos < aGroupArr.size())
     209             :     {
     210           0 :         AutoTextGroup* pGroup = aGroupArr[nPos];
     211           0 :         return pGroup->sTitle;
     212             :     }
     213           0 :     return OUString();
     214             : }
     215             : 
     216           0 : sal_uInt16 SwGlossaryList::GetBlockCount(size_t nGroup)
     217             : {
     218             :     OSL_ENSURE(aGroupArr.size() > nGroup, "group not available");
     219           0 :     if(nGroup < aGroupArr.size())
     220             :     {
     221           0 :         AutoTextGroup* pGroup = aGroupArr[nGroup];
     222           0 :         return pGroup->nCount;
     223             :     }
     224           0 :     return 0;
     225             : }
     226             : 
     227           0 : OUString SwGlossaryList::GetBlockLongName(size_t nGroup, sal_uInt16 nBlock)
     228             : {
     229             :     OSL_ENSURE(aGroupArr.size() > nGroup, "group not available");
     230           0 :     if(nGroup < aGroupArr.size())
     231             :     {
     232           0 :         AutoTextGroup* pGroup = aGroupArr[nGroup];
     233           0 :         return pGroup->sLongNames.getToken(nBlock, STRING_DELIM);
     234             :     }
     235           0 :     return OUString();
     236             : }
     237             : 
     238           0 : OUString SwGlossaryList::GetBlockShortName(size_t nGroup, sal_uInt16 nBlock)
     239             : {
     240             :     OSL_ENSURE(aGroupArr.size() > nGroup, "group not available");
     241           0 :     if(nGroup < aGroupArr.size())
     242             :     {
     243           0 :         AutoTextGroup* pGroup = aGroupArr[nGroup];
     244           0 :         return pGroup->sShortNames.getToken(nBlock, STRING_DELIM);
     245             :     }
     246           0 :     return OUString();
     247             : }
     248             : 
     249           0 : void SwGlossaryList::Update()
     250             : {
     251           0 :     if(!IsActive())
     252           0 :         Start();
     253             : 
     254           0 :     SvtPathOptions aPathOpt;
     255           0 :     OUString sTemp( aPathOpt.GetAutoTextPath() );
     256           0 :     if(sTemp != sPath)
     257             :     {
     258           0 :         sPath = sTemp;
     259           0 :         bFilled = false;
     260           0 :         ClearGroups();
     261             :     }
     262           0 :     SwGlossaries* pGlossaries = ::GetGlossaries();
     263           0 :     const std::vector<OUString> & rPathArr = pGlossaries->GetPathArray();
     264           0 :     const OUString sExt( SwGlossaries::GetExtension() );
     265           0 :     if(!bFilled)
     266             :     {
     267           0 :         const size_t nGroupCount = pGlossaries->GetGroupCnt();
     268           0 :         for(size_t i = 0; i < nGroupCount; ++i)
     269             :         {
     270           0 :             OUString sGrpName = pGlossaries->GetGroupName(i);
     271             :             const size_t nPath = static_cast<size_t>(
     272           0 :                 sGrpName.getToken(1, GLOS_DELIM).toInt32());
     273           0 :             if( nPath < rPathArr.size() )
     274             :             {
     275           0 :                 AutoTextGroup* pGroup = new AutoTextGroup;
     276           0 :                 pGroup->sName = sGrpName;
     277             : 
     278           0 :                 FillGroup(pGroup, pGlossaries);
     279           0 :                 OUString sName = rPathArr[nPath] + "/" +
     280           0 :                     pGroup->sName.getToken(0, GLOS_DELIM) + sExt;
     281             :                 FStatHelper::GetModifiedDateTimeOfFile( sName,
     282             :                                                 &pGroup->aDateModified,
     283           0 :                                                 &pGroup->aDateModified );
     284             : 
     285           0 :                 aGroupArr.insert( aGroupArr.begin(), pGroup );
     286             :             }
     287           0 :         }
     288           0 :         bFilled = true;
     289             :     }
     290             :     else
     291             :     {
     292           0 :         for( size_t nPath = 0; nPath < rPathArr.size(); nPath++ )
     293             :         {
     294           0 :             std::vector<OUString> aFoundGroupNames;
     295           0 :             std::vector<OUString> aFiles;
     296           0 :             std::vector<DateTime*> aDateTimeArr;
     297             : 
     298           0 :             SWUnoHelper::UCB_GetFileListOfFolder( rPathArr[nPath], aFiles,
     299           0 :                                                     &sExt, &aDateTimeArr );
     300           0 :             for( size_t nFiles = 0; nFiles < aFiles.size(); ++nFiles )
     301             :             {
     302           0 :                 const OUString aTitle = aFiles[ nFiles ];
     303           0 :                 ::DateTime* pDT = aDateTimeArr[ nFiles ];
     304             : 
     305           0 :                 OUString sName( aTitle.copy( 0, aTitle.getLength() - sExt.getLength() ));
     306             : 
     307           0 :                 aFoundGroupNames.push_back(sName);
     308           0 :                 sName += OUStringLiteral1<GLOS_DELIM>() + OUString::number( static_cast<sal_uInt16>(nPath) );
     309           0 :                 AutoTextGroup* pFound = FindGroup( sName );
     310           0 :                 if( !pFound )
     311             :                 {
     312           0 :                     pFound = new AutoTextGroup;
     313           0 :                     pFound->sName = sName;
     314           0 :                     FillGroup( pFound, pGlossaries );
     315           0 :                     pFound->aDateModified = *pDT;
     316             : 
     317           0 :                     aGroupArr.push_back(pFound);
     318             :                 }
     319           0 :                 else if( pFound->aDateModified < *pDT )
     320             :                 {
     321           0 :                     FillGroup(pFound, pGlossaries);
     322           0 :                     pFound->aDateModified = *pDT;
     323             :                 }
     324             : 
     325             :                 // don't need any more these pointers
     326           0 :                 delete pDT;
     327           0 :             }
     328             : 
     329           0 :             for( size_t i = aGroupArr.size(); i>0; )
     330             :             {
     331           0 :                 --i;
     332             :                 // maybe remove deleted groups
     333           0 :                 AutoTextGroup* pGroup = aGroupArr[i];
     334             :                 const size_t nGroupPath = static_cast<size_t>(
     335           0 :                     pGroup->sName.getToken( 1, GLOS_DELIM).toInt32());
     336             :                 // Only the groups will be checked which are registered
     337             :                 // for the current subpath.
     338           0 :                 if( nGroupPath == nPath )
     339             :                 {
     340           0 :                     bool bFound = false;
     341           0 :                     OUString sCompareGroup = pGroup->sName.getToken(0, GLOS_DELIM);
     342           0 :                     for(std::vector<OUString>::const_iterator j = aFoundGroupNames.begin(); j != aFoundGroupNames.end() && !bFound; ++j)
     343           0 :                         bFound = (sCompareGroup == *j);
     344             : 
     345           0 :                     if(!bFound)
     346             :                     {
     347           0 :                         aGroupArr.erase(aGroupArr.begin() + i);
     348           0 :                         delete pGroup;
     349           0 :                     }
     350             :                 }
     351             :             }
     352           0 :         }
     353           0 :     }
     354           0 : }
     355             : 
     356           0 : void SwGlossaryList::Invoke()
     357             : {
     358             :     // Only update automatically if a SwView has the focus.
     359           0 :     if(::GetActiveView())
     360           0 :         Update();
     361           0 : }
     362             : 
     363           0 : AutoTextGroup* SwGlossaryList::FindGroup(const OUString& rGroupName)
     364             : {
     365           0 :     for(size_t i = 0; i < aGroupArr.size(); ++i)
     366             :     {
     367           0 :         AutoTextGroup* pRet = aGroupArr[i];
     368           0 :         if(pRet->sName == rGroupName)
     369           0 :             return pRet;
     370             :     }
     371           0 :     return 0;
     372             : }
     373             : 
     374           0 : void SwGlossaryList::FillGroup(AutoTextGroup* pGroup, SwGlossaries* pGlossaries)
     375             : {
     376           0 :     SwTextBlocks*   pBlock = pGlossaries->GetGroupDoc(pGroup->sName);
     377           0 :     pGroup->nCount = pBlock ? pBlock->GetCount() : 0;
     378           0 :     (pGroup->sLongNames).clear();
     379           0 :     (pGroup->sShortNames).clear();
     380           0 :     if(pBlock)
     381           0 :         pGroup->sTitle = pBlock->GetName();
     382             : 
     383           0 :     for(sal_uInt16 j = 0; j < pGroup->nCount; j++)
     384             :     {
     385           0 :         pGroup->sLongNames  += pBlock->GetLongName(j);
     386           0 :         pGroup->sLongNames  += OUString(STRING_DELIM);
     387           0 :         pGroup->sShortNames += pBlock->GetShortName(j);
     388           0 :         pGroup->sShortNames += OUString(STRING_DELIM);
     389             :     }
     390           0 :     delete pBlock;
     391           0 : }
     392             : 
     393             : // Give back all (not exceeding FIND_MAX_GLOS) found modules
     394             : // with matching beginning.
     395             : 
     396           0 : bool SwGlossaryList::HasLongName(const OUString& rBegin, std::vector<OUString> *pLongNames)
     397             : {
     398           0 :     if(!bFilled)
     399           0 :         Update();
     400           0 :     sal_uInt16 nFound = 0;
     401           0 :     const size_t nCount = aGroupArr.size();
     402           0 :     sal_Int32 nBeginLen = rBegin.getLength();
     403           0 :     const ::utl::TransliterationWrapper& rSCmp = GetAppCmpStrIgnore();
     404             : 
     405           0 :     for(size_t i = 0; i < nCount; ++i)
     406             :     {
     407           0 :         AutoTextGroup* pGroup = aGroupArr[i];
     408           0 :         for(sal_uInt16 j = 0; j < pGroup->nCount; j++)
     409             :         {
     410           0 :             OUString sBlock = pGroup->sLongNames.getToken(j, STRING_DELIM);
     411           0 :             if( nBeginLen + 1 < sBlock.getLength() &&
     412           0 :                 rSCmp.isEqual( sBlock.copy(0, nBeginLen), rBegin ))
     413             :             {
     414           0 :                 pLongNames->push_back( sBlock );
     415           0 :                 nFound++;
     416           0 :                 if(FIND_MAX_GLOS == nFound)
     417           0 :                     break;
     418             :             }
     419           0 :         }
     420             :     }
     421           0 :     return nFound > 0;
     422             : }
     423             : 
     424           0 : void    SwGlossaryList::ClearGroups()
     425             : {
     426           0 :     const size_t nCount = aGroupArr.size();
     427           0 :     for( size_t i = 0; i < nCount; ++i )
     428           0 :         delete aGroupArr[ i ];
     429             : 
     430           0 :     aGroupArr.clear();
     431           0 :     bFilled = false;
     432         177 : }
     433             : 
     434             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11