update dev300-m58
[ooovba.git] / sw / source / ui / utlui / gloslst.cxx
blob3b1f9dd01217ad356461cd8db1988897e9c83026
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gloslst.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
36 #define _SVSTDARR_STRINGSDTOR
37 #define _SVSTDARR_STRINGSISORTDTOR
38 #define _SVSTDARR_STRINGS
39 #include <svtools/svstdarr.hxx>
40 #include <tools/urlobj.hxx>
41 #include <vcl/dialog.hxx>
42 #include <vcl/msgbox.hxx>
43 #ifndef _SV_BUTTON_HXX //autogen
44 #include <vcl/button.hxx>
45 #endif
46 #ifndef _FIXED_HXX //autogen
47 #include <vcl/fixed.hxx>
48 #endif
49 #include <vcl/lstbox.hxx>
50 #ifndef SVTOOLS_FSTATHELPER_HXX
51 #include <svtools/fstathelper.hxx>
52 #endif
53 #include <svtools/pathoptions.hxx>
54 #include <unotools/transliterationwrapper.hxx>
55 #include <swtypes.hxx>
56 #include <swmodule.hxx>
57 #include <shellio.hxx>
58 #include <initui.hxx>
59 #include <glosdoc.hxx>
60 #include <gloslst.hxx>
61 #include <swunohelper.hxx>
63 #ifndef _UTLUI_HRC
64 #include <utlui.hrc>
65 #endif
66 #ifndef _GLOSLST_HRC
67 #include <gloslst.hrc>
68 #endif
71 #define STRING_DELIM (char)0x0A
72 #define GLOS_TIMEOUT 30000 // alle 30 s updaten
73 #define FIND_MAX_GLOS 20
76 struct TripleString
78 String sGroup;
79 String sBlock;
80 String sShort;
83 typedef TripleString* TripleStringPtr;
84 SV_DECL_PTRARR_DEL( TripleStrings, TripleStringPtr, 0, 4 )
85 SV_IMPL_PTRARR( TripleStrings, TripleStringPtr )
87 class SwGlossDecideDlg : public ModalDialog
89 OKButton aOk;
90 CancelButton aCancel;
91 HelpButton aHelp;
92 ListBox aListLB;
93 FixedLine aFL;
95 DECL_LINK(DoubleClickHdl, ListBox*);
96 DECL_LINK(SelectHdl, ListBox*);
98 public:
99 SwGlossDecideDlg(Window* pParent);
100 ListBox& GetListBox() {return aListLB;}
103 /*-----------------21.01.97 13.25-------------------
105 --------------------------------------------------*/
107 SwGlossDecideDlg::SwGlossDecideDlg(Window* pParent) :
108 ModalDialog(pParent, SW_RES(DLG_GLOSSARY_DECIDE_DLG)),
109 aOk(this, SW_RES(PB_OK)),
110 aCancel(this, SW_RES(PB_CANCEL)),
111 aHelp(this, SW_RES(PB_HELP)),
112 aListLB(this, SW_RES(LB_LIST)),
113 aFL(this, SW_RES(FL_GLOSS))
115 FreeResource();
116 aListLB.SetDoubleClickHdl(LINK(this, SwGlossDecideDlg, DoubleClickHdl));
117 aListLB.SetSelectHdl(LINK(this, SwGlossDecideDlg, SelectHdl));
120 /*-----------------21.01.97 13.25-------------------
122 --------------------------------------------------*/
124 IMPL_LINK(SwGlossDecideDlg, DoubleClickHdl, ListBox*, EMPTYARG)
126 EndDialog(RET_OK);
127 return 0;
129 /*-----------------21.01.97 13.29-------------------
131 --------------------------------------------------*/
133 IMPL_LINK(SwGlossDecideDlg, SelectHdl, ListBox*, EMPTYARG)
135 aOk.Enable(LISTBOX_ENTRY_NOTFOUND != aListLB.GetSelectEntryPos());
136 return 0;
139 /********************************************************************
141 ********************************************************************/
144 SwGlossaryList::SwGlossaryList() :
145 bFilled(FALSE)
147 SvtPathOptions aPathOpt;
148 sPath = aPathOpt.GetAutoTextPath();
149 SetTimeout(GLOS_TIMEOUT);
152 /********************************************************************
154 ********************************************************************/
157 SwGlossaryList::~SwGlossaryList()
159 ClearGroups();
162 /********************************************************************
163 * Wenn der GroupName bereits bekannt ist, dann wird nur
164 * rShortName gefuellt, sonst wird rGroupName ebenfals gesetzt und
165 * bei Bedarf nach der richtigen Gruppe gefragt
166 ********************************************************************/
169 BOOL SwGlossaryList::GetShortName(const String& rLongName,
170 String& rShortName, String& rGroupName )
172 if(!bFilled)
173 Update();
175 TripleStrings aTripleStrings;
177 USHORT nCount = aGroupArr.Count();
178 USHORT nFound = 0;
179 for(USHORT i = 0; i < nCount; i++ )
181 AutoTextGroup* pGroup = aGroupArr.GetObject(i);
182 if(!rGroupName.Len() || rGroupName == pGroup->sName)
183 for(USHORT j = 0; j < pGroup->nCount; j++)
185 String sLong = pGroup->sLongNames.GetToken(j, STRING_DELIM);
186 if((rLongName == sLong))
188 TripleString* pTriple = new TripleString;
189 pTriple->sGroup = pGroup->sName;
190 pTriple->sBlock = sLong;
191 pTriple->sShort = pGroup->sShortNames.GetToken(j, STRING_DELIM);
192 aTripleStrings.Insert(pTriple, nFound++);
197 BOOL bRet = FALSE;
198 nCount = aTripleStrings.Count();
199 if(1 == nCount )
201 TripleString* pTriple = aTripleStrings[0];
202 rShortName = pTriple->sShort;
203 rGroupName = pTriple->sGroup;
204 bRet = TRUE;
206 else if(1 < nCount)
208 SwGlossDecideDlg aDlg(0);
209 String sTitle = aDlg.GetText();
210 sTitle += ' ';
211 sTitle += aTripleStrings[0]->sBlock;
212 aDlg.SetText(sTitle);
214 ListBox& rLB = aDlg.GetListBox();
215 for(USHORT i = 0; i < nCount; i++ )
216 rLB.InsertEntry(aTripleStrings[i]->sGroup.GetToken(0, GLOS_DELIM));
218 rLB.SelectEntryPos(0);
219 if(RET_OK == aDlg.Execute() &&
220 LISTBOX_ENTRY_NOTFOUND != rLB.GetSelectEntryPos())
222 TripleString* pTriple = aTripleStrings[rLB.GetSelectEntryPos()];
223 rShortName = pTriple->sShort;
224 rGroupName = pTriple->sGroup;
225 bRet = TRUE;
227 else
228 bRet = FALSE;
230 return bRet;
233 /********************************************************************
235 ********************************************************************/
238 USHORT SwGlossaryList::GetGroupCount()
240 if(!bFilled)
241 Update();
242 return aGroupArr.Count();
245 /********************************************************************
247 ********************************************************************/
250 String SwGlossaryList::GetGroupName(USHORT nPos, BOOL bNoPath, String* pTitle)
252 DBG_ASSERT(aGroupArr.Count() > nPos, "Gruppe nicht vorhanden");
253 String sRet(aEmptyStr);
254 if(nPos < aGroupArr.Count())
256 AutoTextGroup* pGroup = aGroupArr.GetObject(nPos);
257 sRet = pGroup->sName;
258 if(bNoPath)
259 sRet = sRet.GetToken(0, GLOS_DELIM);
260 if(pTitle)
261 *pTitle = pGroup->sTitle;
263 return sRet;
267 /********************************************************************
269 ********************************************************************/
272 USHORT SwGlossaryList::GetBlockCount(USHORT nGroup)
274 DBG_ASSERT(aGroupArr.Count() > nGroup, "Gruppe nicht vorhanden");
275 if(nGroup < aGroupArr.Count())
277 AutoTextGroup* pGroup = aGroupArr.GetObject(nGroup);
278 return pGroup->nCount;
280 return 0;
283 /********************************************************************
285 ********************************************************************/
288 String SwGlossaryList::GetBlockName(USHORT nGroup, USHORT nBlock, String& rShortName)
290 DBG_ASSERT(aGroupArr.Count() > nGroup, "Gruppe nicht vorhanden");
291 if(nGroup < aGroupArr.Count())
293 AutoTextGroup* pGroup = aGroupArr.GetObject(nGroup);
294 rShortName = pGroup->sShortNames.GetToken(nBlock, STRING_DELIM);
295 return pGroup->sLongNames.GetToken(nBlock, STRING_DELIM);
297 return aEmptyStr;
300 /********************************************************************
302 ********************************************************************/
305 void SwGlossaryList::Update()
307 if(!IsActive())
308 Start();
310 SvtPathOptions aPathOpt;
311 String sTemp( aPathOpt.GetAutoTextPath() );
312 if(sTemp != sPath)
314 sPath = sTemp;
315 bFilled = FALSE;
316 ClearGroups();
318 SwGlossaries* pGlossaries = ::GetGlossaries();
319 const SvStrings* pPathArr = pGlossaries->GetPathArray();
320 String sExt( SwGlossaries::GetExtension() );
321 if(!bFilled)
323 USHORT nGroupCount = pGlossaries->GetGroupCnt();
324 for(USHORT i = 0; i < nGroupCount; i++)
326 String sGrpName = pGlossaries->GetGroupName(i);
327 USHORT nPath = (USHORT)sGrpName.GetToken(1, GLOS_DELIM).ToInt32();
328 if(nPath < pPathArr->Count())
330 AutoTextGroup* pGroup = new AutoTextGroup;
331 pGroup->sName = sGrpName;
333 FillGroup(pGroup, pGlossaries);
334 String sName = *(*pPathArr)[nPath];
335 sName += INET_PATH_TOKEN;
336 sName += pGroup->sName.GetToken(0, GLOS_DELIM);
337 sName += sExt;
339 FStatHelper::GetModifiedDateTimeOfFile( sName,
340 &pGroup->aDateModified,
341 &pGroup->aDateModified );
343 aGroupArr.Insert( pGroup, i );
346 bFilled = TRUE;
348 else
350 for(USHORT nPath = 0; nPath < pPathArr->Count(); nPath++)
352 SvStringsDtor aFoundGroupNames;
353 SvStrings aFiles( 16, 16 );
354 SvPtrarr aDateTimeArr( 16, 16 );
356 SWUnoHelper::UCB_GetFileListOfFolder( *(*pPathArr)[nPath], aFiles,
357 &sExt, &aDateTimeArr );
358 for( USHORT nFiles = 0, nFEnd = aFiles.Count();
359 nFiles < nFEnd; ++nFiles )
361 String* pTitle = aFiles[ nFiles ];
362 ::DateTime* pDT = (::DateTime*) aDateTimeArr[ nFiles ];
364 String sName( pTitle->Copy( 0, pTitle->Len() - sExt.Len() ));
366 aFoundGroupNames.Insert( new String(sName),
367 aFoundGroupNames.Count());
368 sName += GLOS_DELIM;
369 sName += String::CreateFromInt32( nPath );
370 AutoTextGroup* pFound = FindGroup( sName );
371 if( !pFound )
373 pFound = new AutoTextGroup;
374 pFound->sName = sName;
375 FillGroup( pFound, pGlossaries );
376 pFound->aDateModified = *pDT;
378 aGroupArr.Insert(pFound, aGroupArr.Count());
380 else if( pFound->aDateModified < *pDT )
382 FillGroup(pFound, pGlossaries);
383 pFound->aDateModified = *pDT;
386 // don't need any more these pointers
387 delete pTitle;
388 delete pDT;
391 USHORT nArrCount = aGroupArr.Count();
392 for( USHORT i = nArrCount; i; --i)
394 // evtl. geloeschte Gruppen entfernen
395 AutoTextGroup* pGroup = aGroupArr.GetObject(i - 1);
396 USHORT nGroupPath = (USHORT)pGroup->sName.GetToken( 1,
397 GLOS_DELIM).ToInt32();
398 // nur die Gruppen werden geprueft, die fuer den
399 // aktuellen Teilpfad registriert sind
400 if(nGroupPath == nPath)
402 BOOL bFound = FALSE;
403 String sCompareGroup = pGroup->sName.GetToken(0, GLOS_DELIM);
404 for( USHORT j = 0; j < aFoundGroupNames.Count() && !bFound; ++j)
406 bFound = sCompareGroup == *aFoundGroupNames[j];
408 if(!bFound)
410 aGroupArr.Remove(i - 1);
411 delete pGroup;
419 /********************************************************************
421 ********************************************************************/
424 void SwGlossaryList::Timeout()
426 // nur, wenn eine SwView den Fokus hat, wird automatisch upgedated
427 if(::GetActiveView())
428 Update();
431 /********************************************************************
433 ********************************************************************/
436 AutoTextGroup* SwGlossaryList::FindGroup(const String& rGroupName)
438 for(USHORT i = 0; i < aGroupArr.Count(); i++)
440 AutoTextGroup* pRet = aGroupArr.GetObject(i);
441 if(pRet->sName == rGroupName)
442 return pRet;
444 return 0;
447 /********************************************************************
449 ********************************************************************/
452 void SwGlossaryList::FillGroup(AutoTextGroup* pGroup, SwGlossaries* pGlossaries)
454 SwTextBlocks* pBlock = pGlossaries->GetGroupDoc(pGroup->sName);
455 pGroup->nCount = pBlock ? pBlock->GetCount() : 0;
456 pGroup->sLongNames = pGroup->sShortNames = aEmptyStr;
457 if(pBlock)
458 pGroup->sTitle = pBlock->GetName();
460 for(USHORT j = 0; j < pGroup->nCount; j++)
462 pGroup->sLongNames += pBlock->GetLongName(j);
463 pGroup->sLongNames += STRING_DELIM;
464 pGroup->sShortNames += pBlock->GetShortName(j);
465 pGroup->sShortNames += STRING_DELIM;
467 pGlossaries->PutGroupDoc(pBlock);
470 /********************************************************************
471 Alle (nicht mehr als FIND_MAX_GLOS) gefunden Bausteine mit
472 passendem Anfang zurueckgeben
473 ********************************************************************/
475 BOOL SwGlossaryList::HasLongName(const String& rBegin, SvStringsISortDtor* pLongNames )
477 if(!bFilled)
478 Update();
479 USHORT nFound = 0;
480 USHORT nCount = aGroupArr.Count();
481 USHORT nBeginLen = rBegin.Len();
482 const ::utl::TransliterationWrapper& rSCmp = GetAppCmpStrIgnore();
484 for(USHORT i = 0; i < nCount; i++ )
486 AutoTextGroup* pGroup = aGroupArr.GetObject(i);
487 for(USHORT j = 0; j < pGroup->nCount; j++)
489 String sBlock = pGroup->sLongNames.GetToken(j, STRING_DELIM);
490 if( rSCmp.isEqual( sBlock.Copy(0, nBeginLen), rBegin ) &&
491 nBeginLen + 1 < sBlock.Len())
493 String* pBlock = new String(sBlock);
494 pLongNames->Insert(pBlock);
495 nFound++;
496 if(FIND_MAX_GLOS == nFound)
497 break;
501 return nFound > 0;
504 /********************************************************************
506 ********************************************************************/
507 void SwGlossaryList::ClearGroups()
509 USHORT nCount = aGroupArr.Count();
510 for( USHORT i = 0; i < nCount; ++i )
511 delete aGroupArr.GetObject( i );
513 aGroupArr.Remove( 0, nCount );
514 bFilled = FALSE;