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