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

Generated by: LCOV version 1.10