LCOV - code coverage report
Current view: top level - sw/source/core/uibase/utlui - gloslst.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 218 0.0 %
Date: 2014-04-14 Functions: 0 27 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <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(Window* pParent);
      62           0 :     ListBox&    GetListBox() {return *m_pListLB;}
      63             : };
      64             : 
      65           0 : SwGlossDecideDlg::SwGlossDecideDlg(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 : sal_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 :     sal_uInt16 nCount = aGroupArr.size();
     114           0 :     sal_uInt16 nFound = 0;
     115           0 :     for(sal_uInt16 i = 0; i < nCount; i++ )
     116             :     {
     117           0 :         AutoTextGroup* pGroup = aGroupArr[i];
     118           0 :         if(!rGroupName.isEmpty() && rGroupName != pGroup->sName)
     119           0 :             continue;
     120             : 
     121           0 :         for(sal_uInt16 j = 0; j < pGroup->nCount; j++)
     122             :         {
     123           0 :             OUString sLong = pGroup->sLongNames.getToken(j, STRING_DELIM);
     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 :             ++nFound;
     133           0 :         }
     134             :     }
     135             : 
     136           0 :     sal_Bool bRet = sal_False;
     137           0 :     nCount = aTripleStrings.size();
     138           0 :     if(1 == nCount)
     139             :     {
     140           0 :         const TripleString& pTriple(aTripleStrings.front());
     141           0 :         rShortName = pTriple.sShort;
     142           0 :         rGroupName = pTriple.sGroup;
     143           0 :         bRet = sal_True;
     144             :     }
     145           0 :     else if(1 < nCount)
     146             :     {
     147           0 :         SwGlossDecideDlg aDlg(0);
     148           0 :         OUString sTitle = aDlg.GetText() + " " + aTripleStrings.front().sBlock;
     149           0 :         aDlg.SetText(sTitle);
     150             : 
     151           0 :         ListBox& rLB = aDlg.GetListBox();
     152           0 :         for(std::vector<TripleString>::const_iterator i = aTripleStrings.begin(); i != aTripleStrings.end(); ++i)
     153           0 :             rLB.InsertEntry(i->sGroup.getToken(0, GLOS_DELIM));
     154             : 
     155           0 :         rLB.SelectEntryPos(0);
     156           0 :         if(RET_OK == aDlg.Execute() &&
     157           0 :             LISTBOX_ENTRY_NOTFOUND != rLB.GetSelectEntryPos())
     158             :         {
     159           0 :             const TripleString& pTriple(aTripleStrings[rLB.GetSelectEntryPos()]);
     160           0 :             rShortName = pTriple.sShort;
     161           0 :             rGroupName = pTriple.sGroup;
     162           0 :             bRet = sal_True;
     163             :         }
     164             :         else
     165           0 :             bRet = sal_False;
     166             :     }
     167           0 :     return bRet;
     168             : }
     169             : 
     170           0 : sal_uInt16  SwGlossaryList::GetGroupCount()
     171             : {
     172           0 :     if(!bFilled)
     173           0 :         Update();
     174           0 :     return aGroupArr.size();
     175             : }
     176             : 
     177           0 : OUString SwGlossaryList::GetGroupName(sal_uInt16 nPos, bool bNoPath)
     178             : {
     179             :     OSL_ENSURE(aGroupArr.size() > nPos, "group not available");
     180           0 :     if(nPos < aGroupArr.size())
     181             :     {
     182           0 :         AutoTextGroup* pGroup = aGroupArr[nPos];
     183           0 :         OUString sRet = pGroup->sName;
     184           0 :         if(bNoPath)
     185           0 :             sRet = sRet.getToken(0, GLOS_DELIM);
     186           0 :         return sRet;
     187             :     }
     188           0 :     return OUString();
     189             : }
     190             : 
     191           0 : OUString SwGlossaryList::GetGroupTitle(sal_uInt16 nPos)
     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 :         return pGroup->sTitle;
     198             :     }
     199           0 :     return OUString();
     200             : }
     201             : 
     202           0 : sal_uInt16 SwGlossaryList::GetBlockCount(sal_uInt16 nGroup)
     203             : {
     204             :     OSL_ENSURE(aGroupArr.size() > nGroup, "group not available");
     205           0 :     if(nGroup < aGroupArr.size())
     206             :     {
     207           0 :         AutoTextGroup* pGroup = aGroupArr[nGroup];
     208           0 :         return pGroup->nCount;
     209             :     }
     210           0 :     return 0;
     211             : }
     212             : 
     213           0 : OUString SwGlossaryList::GetBlockLongName(sal_uInt16 nGroup, sal_uInt16 nBlock)
     214             : {
     215             :     OSL_ENSURE(aGroupArr.size() > nGroup, "group not available");
     216           0 :     if(nGroup < aGroupArr.size())
     217             :     {
     218           0 :         AutoTextGroup* pGroup = aGroupArr[nGroup];
     219           0 :         return pGroup->sLongNames.getToken(nBlock, STRING_DELIM);
     220             :     }
     221           0 :     return OUString();
     222             : }
     223             : 
     224           0 : OUString SwGlossaryList::GetBlockShortName(sal_uInt16 nGroup, sal_uInt16 nBlock)
     225             : {
     226             :     OSL_ENSURE(aGroupArr.size() > nGroup, "group not available");
     227           0 :     if(nGroup < aGroupArr.size())
     228             :     {
     229           0 :         AutoTextGroup* pGroup = aGroupArr[nGroup];
     230           0 :         return pGroup->sShortNames.getToken(nBlock, STRING_DELIM);
     231             :     }
     232           0 :     return OUString();
     233             : }
     234             : 
     235           0 : void SwGlossaryList::Update()
     236             : {
     237           0 :     if(!IsActive())
     238           0 :         Start();
     239             : 
     240           0 :     SvtPathOptions aPathOpt;
     241           0 :     OUString sTemp( aPathOpt.GetAutoTextPath() );
     242           0 :     if(sTemp != sPath)
     243             :     {
     244           0 :         sPath = sTemp;
     245           0 :         bFilled = false;
     246           0 :         ClearGroups();
     247             :     }
     248           0 :     SwGlossaries* pGlossaries = ::GetGlossaries();
     249           0 :     const std::vector<OUString> & rPathArr = pGlossaries->GetPathArray();
     250           0 :     const OUString sExt( SwGlossaries::GetExtension() );
     251           0 :     if(!bFilled)
     252             :     {
     253           0 :         sal_uInt16 nGroupCount = pGlossaries->GetGroupCnt();
     254           0 :         for(sal_uInt16 i = 0; i < nGroupCount; i++)
     255             :         {
     256           0 :             OUString sGrpName = pGlossaries->GetGroupName(i);
     257           0 :             sal_uInt16 nPath = (sal_uInt16)sGrpName.getToken(1, GLOS_DELIM).toInt32();
     258           0 :             if( static_cast<size_t>(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[ static_cast<sal_uInt16>(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 :             sal_uInt16 nArrCount = aGroupArr.size();
     315           0 :             for( sal_uInt16 i = nArrCount; i; --i)
     316             :             {
     317             :                 // maybe remove deleted groups
     318           0 :                 AutoTextGroup* pGroup = aGroupArr[i - 1];
     319             :                 sal_uInt16 nGroupPath = (sal_uInt16)pGroup->sName.getToken( 1,
     320           0 :                                                         GLOS_DELIM).toInt32();
     321             :                 // Only the groups will be checked which are registered
     322             :                 // for the current subpath.
     323           0 :                 if( nGroupPath == static_cast<sal_uInt16>(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 - 1);
     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(sal_uInt16 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 :     pGlossaries->PutGroupDoc(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 :     sal_uInt16 nCount = aGroupArr.size();
     386           0 :     sal_Int32 nBeginLen = rBegin.getLength();
     387           0 :     const ::utl::TransliterationWrapper& rSCmp = GetAppCmpStrIgnore();
     388             : 
     389           0 :     for(sal_uInt16 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 :     sal_uInt16 nCount = aGroupArr.size();
     411           0 :     for( sal_uInt16 i = 0; i < nCount; ++i )
     412           0 :         delete aGroupArr[ i ];
     413             : 
     414           0 :     aGroupArr.clear();
     415           0 :     bFilled = false;
     416           0 : }
     417             : 
     418             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10