sc: factor out some more code
[LibreOffice.git] / sw / source / uibase / dochdl / gloshdl.cxx
blob5f746df07528d37a502a276c3c41cf08f05ecf2f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <utility>
21 #include <vcl/errinf.hxx>
22 #include <vcl/weld.hxx>
23 #include <svl/macitem.hxx>
24 #include <sfx2/fcontnr.hxx>
25 #include <sfx2/docfile.hxx>
26 #include <sfx2/docfilt.hxx>
27 #include <unotools/transliterationwrapper.hxx>
28 #include <o3tl/string_view.hxx>
29 #include <docsh.hxx>
30 #include <wrtsh.hxx>
31 #include <view.hxx>
32 #include <gloshdl.hxx>
33 #include <glosdoc.hxx>
34 #include <shellio.hxx>
35 #include <swundo.hxx>
36 #include <expfld.hxx>
37 #include <initui.hxx>
38 #include <gloslst.hxx>
39 #include <swdtflvr.hxx>
41 #include <strings.hrc>
42 #include <vcl/svapp.hxx>
43 #include <osl/diagnose.h>
45 #include <editeng/acorrcfg.hxx>
46 #include <sfx2/event.hxx>
47 #include <swabstdlg.hxx>
49 #include <memory>
51 using namespace ::com::sun::star;
53 const short RET_EDIT = 100;
55 namespace {
57 struct TextBlockInfo_Impl
59 OUString sTitle;
60 OUString sLongName;
61 OUString sGroupName;
62 TextBlockInfo_Impl(OUString aTitle, OUString aLongName, OUString aGroupName)
63 : sTitle(std::move(aTitle)), sLongName(std::move(aLongName)), sGroupName(std::move(aGroupName)) {}
68 // Dialog for edit templates
69 void SwGlossaryHdl::GlossaryDlg()
71 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
72 VclPtr<AbstractGlossaryDlg> pDlg(pFact->CreateGlossaryDlg(m_rViewFrame, this, m_pWrtShell));
74 pDlg->StartExecuteAsync(
75 [this, pDlg] (sal_Int32 nResult)->void
77 OUString sName;
78 OUString sShortName;
79 if (nResult == RET_OK)
80 pDlg->Apply();
81 if (nResult == RET_EDIT)
83 sName = pDlg->GetCurrGrpName();
84 sShortName = pDlg->GetCurrShortName();
86 pDlg->disposeOnce();
87 m_pCurGrp.reset();
88 if(HasGlossaryList())
90 GetGlossaryList()->ClearGroups();
93 if( !sName.isEmpty() || !sShortName.isEmpty() )
94 m_rStatGlossaries.EditGroupDoc( sName, sShortName );
96 SwGlossaryList* pList = ::GetGlossaryList();
97 if(pList->IsActive())
98 pList->Update();
103 // set the default group; if called from the dialog
104 // the group is created temporarily for faster access
105 void SwGlossaryHdl::SetCurGroup(const OUString &rGrp, bool bApi, bool bAlwaysCreateNew )
107 OUString sGroup(rGrp);
108 if (sGroup.indexOf(GLOS_DELIM)<0 && !FindGroupName(sGroup))
110 sGroup += OUStringChar(GLOS_DELIM) + "0";
112 if(m_pCurGrp)
114 bool bPathEqual = false;
115 if(!bAlwaysCreateNew)
117 INetURLObject aTemp( m_pCurGrp->GetFileName() );
118 const OUString sCurBase = aTemp.getBase();
119 aTemp.removeSegment();
120 const OUString sCurEntryPath = aTemp.GetMainURL(INetURLObject::DecodeMechanism::NONE);
121 const std::vector<OUString> & rPathArr = m_rStatGlossaries.GetPathArray();
122 sal_uInt16 nCurrentPath = USHRT_MAX;
123 for (size_t nPath = 0; nPath < rPathArr.size(); ++nPath)
125 if (sCurEntryPath == rPathArr[nPath])
127 nCurrentPath = o3tl::narrowing<sal_uInt16>(nPath);
128 break;
131 const std::u16string_view sPath = o3tl::getToken(sGroup, 1, GLOS_DELIM);
132 sal_uInt16 nComparePath = o3tl::narrowing<sal_uInt16>(o3tl::toInt32(sPath));
133 if(nCurrentPath == nComparePath &&
134 o3tl::getToken(sGroup, 0, GLOS_DELIM) == sCurBase)
135 bPathEqual = true;
138 // When path changed, the name is not reliable
139 if(!bAlwaysCreateNew && bPathEqual)
140 return;
142 m_aCurGrp = sGroup;
143 if(!bApi)
145 m_pCurGrp = m_rStatGlossaries.GetGroupDoc(m_aCurGrp, true);
149 size_t SwGlossaryHdl::GetGroupCnt() const
151 return m_rStatGlossaries.GetGroupCnt();
154 OUString SwGlossaryHdl::GetGroupName( size_t nId, OUString* pTitle )
156 OUString sRet = m_rStatGlossaries.GetGroupName(nId);
157 if(pTitle)
159 std::unique_ptr<SwTextBlocks> pGroup = m_rStatGlossaries.GetGroupDoc(sRet);
160 if (pGroup && !pGroup->GetError())
162 *pTitle = pGroup->GetName();
163 if (pTitle->isEmpty())
165 *pTitle = sRet.getToken(0, GLOS_DELIM);
166 pGroup->SetName(*pTitle);
169 else
171 sRet.clear();
174 return sRet;
177 void SwGlossaryHdl::NewGroup(OUString &rGrpName, const OUString& rTitle)
179 if (rGrpName.indexOf(GLOS_DELIM)<0)
180 FindGroupName(rGrpName);
181 m_rStatGlossaries.NewGroupDoc(rGrpName, rTitle);
184 void SwGlossaryHdl::RenameGroup(const OUString& rOld, OUString& rNew, const OUString& rNewTitle)
186 OUString sOldGroup(rOld);
187 if (rOld.indexOf(GLOS_DELIM)<0)
188 FindGroupName(sOldGroup);
189 if(rOld == rNew)
191 std::unique_ptr<SwTextBlocks> pGroup = m_rStatGlossaries.GetGroupDoc(sOldGroup);
192 if(pGroup)
194 pGroup->SetName(rNewTitle);
197 else
199 OUString sNewGroup(rNew);
200 if (sNewGroup.indexOf(GLOS_DELIM)<0)
202 sNewGroup += OUStringChar(GLOS_DELIM) + "0";
204 m_rStatGlossaries.RenameGroupDoc(sOldGroup, sNewGroup, rNewTitle);
205 rNew = sNewGroup;
209 bool SwGlossaryHdl::CopyOrMove(const OUString& rSourceGroupName, OUString& rSourceShortName,
210 const OUString& rDestGroupName, const OUString& rLongName, bool bMove)
212 std::unique_ptr<SwTextBlocks> pSourceGroup = m_rStatGlossaries.GetGroupDoc(rSourceGroupName);
213 std::unique_ptr<SwTextBlocks> pDestGroup = m_rStatGlossaries.GetGroupDoc(rDestGroupName);
214 if (pDestGroup->IsReadOnly() || (bMove && pSourceGroup->IsReadOnly()) )
216 return false;
219 //The index must be determined here because rSourceShortName maybe changed in CopyBlock
220 sal_uInt16 nDeleteIdx = pSourceGroup->GetIndex( rSourceShortName );
221 OSL_ENSURE(USHRT_MAX != nDeleteIdx, "entry not found");
222 ErrCode nRet = pSourceGroup->CopyBlock( *pDestGroup, rSourceShortName, rLongName );
223 if(!nRet && bMove)
225 // the index must be existing
226 nRet = pSourceGroup->Delete( nDeleteIdx ) ? ERRCODE_NONE : ErrCode(1);
228 return !nRet;
231 // delete an autotext-file-group
232 bool SwGlossaryHdl::DelGroup(const OUString &rGrpName)
234 OUString sGroup(rGrpName);
235 if (sGroup.indexOf(GLOS_DELIM)<0)
236 FindGroupName(sGroup);
237 if( m_rStatGlossaries.DelGroupDoc(sGroup) )
239 if(m_pCurGrp)
241 if (m_pCurGrp->GetName() == sGroup)
242 m_pCurGrp.reset();
244 return true;
246 return false;
249 // ask for number of autotexts
250 sal_uInt16 SwGlossaryHdl::GetGlossaryCnt() const
252 return m_pCurGrp ? m_pCurGrp->GetCount() : 0;
255 const OUString & SwGlossaryHdl::GetGlossaryName( sal_uInt16 nId )
257 OSL_ENSURE(nId < GetGlossaryCnt(), "Text building block array over-indexed.");
258 return m_pCurGrp->GetLongName( nId );
261 const OUString & SwGlossaryHdl::GetGlossaryShortName(sal_uInt16 nId)
263 OSL_ENSURE(nId < GetGlossaryCnt(), "Text building block array over-indexed.");
264 return m_pCurGrp->GetShortName( nId );
267 // ask for short name
268 OUString SwGlossaryHdl::GetGlossaryShortName(std::u16string_view aName)
270 OUString sReturn;
271 SwTextBlocks *pTmp =
272 m_pCurGrp ? m_pCurGrp.get() : m_rStatGlossaries.GetGroupDoc( m_aCurGrp ).release();
273 if(pTmp)
275 sal_uInt16 nIdx = pTmp->GetLongIndex( aName );
276 if( nIdx != sal_uInt16(-1) )
277 sReturn = pTmp->GetShortName( nIdx );
278 if( !m_pCurGrp )
279 delete pTmp;
281 return sReturn;
284 // short name for autotext already used?
285 bool SwGlossaryHdl::HasShortName(const OUString& rShortName) const
287 SwTextBlocks *pBlock = m_pCurGrp ? m_pCurGrp.get()
288 : m_rStatGlossaries.GetGroupDoc( m_aCurGrp ).release();
289 bool bRet = pBlock->GetIndex( rShortName ) != sal_uInt16(-1);
290 if( !m_pCurGrp )
291 delete pBlock;
292 return bRet;
295 // Create autotext
296 bool SwGlossaryHdl::NewGlossary(const OUString& rName, const OUString& rShortName,
297 bool bCreateGroup, bool bNoAttr)
299 SwTextBlocks *pTmp =
300 m_pCurGrp ? m_pCurGrp.get() : m_rStatGlossaries.GetGroupDoc( m_aCurGrp, bCreateGroup ).release();
301 //pTmp == 0 if the AutoText path setting is wrong
302 if(!pTmp)
304 if (!m_pCurGrp)
305 delete pTmp;
306 return false;
309 OUString sOnlyText;
310 OUString* pOnlyText = nullptr;
311 if( bNoAttr )
313 m_pWrtShell->GetSelectedText( sOnlyText, ParaBreakType::ToOnlyCR );
314 pOnlyText = &sOnlyText;
317 const SvxAutoCorrCfg& rCfg = SvxAutoCorrCfg::Get();
319 const sal_uInt16 nSuccess = m_pWrtShell->MakeGlossary( *pTmp, rName, rShortName,
320 rCfg.IsSaveRelFile(), pOnlyText );
321 if(nSuccess == sal_uInt16(-1) )
323 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_pWrtShell->GetView().GetFrameWeld(),
324 VclMessageType::Info, VclButtonsType::Ok, SwResId(STR_ERR_INSERT_GLOS)));
325 xBox->run();
327 if( !m_pCurGrp )
328 delete pTmp;
329 return nSuccess != sal_uInt16(-1);
332 // Delete an autotext
333 bool SwGlossaryHdl::DelGlossary(const OUString &rShortName)
335 SwTextBlocks *pGlossary = m_pCurGrp ? m_pCurGrp.get()
336 : m_rStatGlossaries.GetGroupDoc(m_aCurGrp).release();
337 //pTmp == 0 if the AutoText path setting is wrong
338 if(!pGlossary)
340 if( !m_pCurGrp )
341 delete pGlossary;
342 return false;
345 sal_uInt16 nIdx = pGlossary->GetIndex( rShortName );
346 if( nIdx != sal_uInt16(-1) )
347 pGlossary->Delete( nIdx );
348 if( !m_pCurGrp )
349 delete pGlossary;
350 return true;
353 // expand short name
354 bool SwGlossaryHdl::ExpandGlossary(weld::Window* pParent)
356 OSL_ENSURE(m_pWrtShell->CanInsert(), "illegal");
357 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
358 ::GlossaryGetCurrGroup fnGetCurrGroup = pFact->GetGlossaryCurrGroupFunc();
359 OUString sGroupName( (*fnGetCurrGroup)() );
360 if (sGroupName.indexOf(GLOS_DELIM)<0)
361 FindGroupName(sGroupName);
362 std::unique_ptr<SwTextBlocks> pGlossary = m_rStatGlossaries.GetGroupDoc(sGroupName);
364 OUString aShortName;
366 // use this at text selection
367 if(m_pWrtShell->SwCursorShell::HasSelection() && !m_pWrtShell->IsBlockMode())
369 aShortName = m_pWrtShell->GetSelText();
371 else
373 if(m_pWrtShell->IsAddMode())
374 m_pWrtShell->LeaveAddMode();
375 else if(m_pWrtShell->IsBlockMode())
376 m_pWrtShell->LeaveBlockMode();
377 else if(m_pWrtShell->IsExtMode())
378 m_pWrtShell->LeaveExtMode();
379 // select word (tdf#126589: part to the left of cursor)
380 if (m_pWrtShell->IsInWord() || m_pWrtShell->IsEndWrd())
381 m_pWrtShell->PrvWrd(true);
382 // ask for word
383 if(m_pWrtShell->IsSelection())
384 aShortName = m_pWrtShell->GetSelText();
386 return Expand(pParent, aShortName, &m_rStatGlossaries, std::move(pGlossary));
389 bool SwGlossaryHdl::Expand(weld::Window* pParent, const OUString& rShortName,
390 SwGlossaries *pGlossaries,
391 std::unique_ptr<SwTextBlocks> pGlossary)
393 std::vector<TextBlockInfo_Impl> aFoundArr;
394 OUString aShortName( rShortName );
395 bool bCancel = false;
396 // search for text block
397 // - don't prefer current group depending on configuration setting
398 const SvxAutoCorrCfg& rCfg = SvxAutoCorrCfg::Get();
399 sal_uInt16 nFound = (!rCfg.IsSearchInAllCategories() && pGlossary) ?
400 pGlossary->GetIndex( aShortName ) : USHRT_MAX;
401 // if not found then search in all groups
402 if (nFound == USHRT_MAX)
404 const ::utl::TransliterationWrapper& rSCmp = GetAppCmpStrIgnore();
405 SwGlossaryList* pGlossaryList = ::GetGlossaryList();
406 const size_t nGroupCount = pGlossaryList->GetGroupCount();
407 for(size_t i = 0; i < nGroupCount; ++i)
409 // get group name with path-extension
410 const OUString sGroupName = pGlossaryList->GetGroupName(i);
411 if (pGlossary && sGroupName == pGlossary->GetName())
412 continue;
413 const sal_uInt16 nBlockCount = pGlossaryList->GetBlockCount(i);
414 if(nBlockCount)
416 const OUString sTitle = pGlossaryList->GetGroupTitle(i);
417 for(sal_uInt16 j = 0; j < nBlockCount; j++)
419 const OUString sLongName(pGlossaryList->GetBlockLongName(i, j));
420 const OUString sShortName(pGlossaryList->GetBlockShortName(i, j));
421 if( rSCmp.isEqual( rShortName, sShortName ))
423 aFoundArr.emplace_back(sTitle, sLongName, sGroupName);
428 if( !aFoundArr.empty() ) // one was found
430 pGlossary.reset();
431 if (1 == aFoundArr.size())
433 TextBlockInfo_Impl& rData = aFoundArr.front();
434 pGlossary = pGlossaries->GetGroupDoc(rData.sGroupName);
435 nFound = pGlossary->GetIndex( aShortName );
437 else
439 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
440 ScopedVclPtr<AbstractSwSelGlossaryDlg> pDlg(pFact->CreateSwSelGlossaryDlg(pParent, aShortName));
441 for(const TextBlockInfo_Impl & i : aFoundArr)
443 if (i.sTitle == "My AutoText")
444 pDlg->InsertGlos(SwResId(STR_MY_AUTOTEXT), i.sLongName);
445 else
446 pDlg->InsertGlos(i.sTitle, i.sLongName);
448 pDlg->SelectEntryPos(0);
449 const sal_Int32 nRet = RET_OK == pDlg->Execute() ?
450 pDlg->GetSelectedIdx() :
452 pDlg.disposeAndClear();
453 if (nRet != -1)
455 TextBlockInfo_Impl& rData = aFoundArr[nRet];
456 pGlossary = pGlossaries->GetGroupDoc(rData.sGroupName);
457 nFound = pGlossary->GetIndex( aShortName );
459 else
461 nFound = USHRT_MAX;
462 bCancel = true;
468 // not found
469 if (nFound == USHRT_MAX)
471 if( !bCancel )
473 pGlossary.reset();
475 const sal_Int32 nMaxLen = 50;
476 if(m_pWrtShell->IsSelection() && aShortName.getLength() > nMaxLen)
478 aShortName = OUString::Concat(aShortName.subView(0, nMaxLen)) + " ...";
480 OUString aTmp( SwResId(STR_NOGLOS));
481 aTmp = aTmp.replaceFirst("%1", aShortName);
482 std::shared_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(m_pWrtShell->GetView().GetFrameWeld(),
483 VclMessageType::Info, VclButtonsType::Ok,
484 aTmp));
485 xInfoBox->runAsync(xInfoBox, [] (sal_uInt32){ });
488 return false;
490 else
492 SvxMacro aStartMacro(OUString(), OUString(), STARBASIC);
493 SvxMacro aEndMacro(OUString(), OUString(), STARBASIC);
494 GetMacros( aShortName, aStartMacro, aEndMacro, pGlossary.get() );
496 // StartAction must not be before HasSelection and DelRight,
497 // otherwise the possible Shell change gets delayed and
498 // API-programs would hang.
499 // Moreover the event macro must also not be called in an action
500 m_pWrtShell->StartUndo(SwUndoId::INSGLOSSARY);
501 if( aStartMacro.HasMacro() )
502 m_pWrtShell->ExecMacro( aStartMacro );
503 if(m_pWrtShell->HasSelection())
504 m_pWrtShell->DelLeft();
505 m_pWrtShell->StartAllAction();
507 // cache all InputFields
508 SwInputFieldList aFieldLst( m_pWrtShell, true );
510 m_pWrtShell->InsertGlossary(*pGlossary, aShortName);
511 m_pWrtShell->EndAllAction();
512 if( aEndMacro.HasMacro() )
514 m_pWrtShell->ExecMacro( aEndMacro );
516 m_pWrtShell->EndUndo(SwUndoId::INSGLOSSARY);
518 // demand input for all new InputFields
519 if( aFieldLst.BuildSortLst() )
520 m_pWrtShell->UpdateInputFields( &aFieldLst );
522 return true;
525 // add autotext
526 bool SwGlossaryHdl::InsertGlossary(const OUString &rName)
528 OSL_ENSURE(m_pWrtShell->CanInsert(), "illegal");
530 SwTextBlocks *pGlos =
531 m_pCurGrp ? m_pCurGrp.get() : m_rStatGlossaries.GetGroupDoc(m_aCurGrp).release();
533 if (!pGlos)
535 if (!m_pCurGrp)
536 delete pGlos;
537 return false;
540 SvxMacro aStartMacro(OUString(), OUString(), STARBASIC);
541 SvxMacro aEndMacro(OUString(), OUString(), STARBASIC);
542 GetMacros( rName, aStartMacro, aEndMacro, pGlos );
544 // StartAction must not be before HasSelection and DelRight,
545 // otherwise the possible Shell change gets delayed and
546 // API-programs would hang.
547 // Moreover the event macro must also not be called in an action
548 if( aStartMacro.HasMacro() )
549 m_pWrtShell->ExecMacro( aStartMacro );
550 if( m_pWrtShell->HasSelection() )
551 m_pWrtShell->DelRight();
552 m_pWrtShell->StartAllAction();
554 // cache all InputFields
555 SwInputFieldList aFieldLst( m_pWrtShell, true );
557 m_pWrtShell->InsertGlossary(*pGlos, rName);
558 m_pWrtShell->EndAllAction();
559 if( aEndMacro.HasMacro() )
561 m_pWrtShell->ExecMacro( aEndMacro );
564 // demand input for all new InputFields
565 if( aFieldLst.BuildSortLst() )
566 m_pWrtShell->UpdateInputFields( &aFieldLst );
568 if(!m_pCurGrp)
569 delete pGlos;
570 return true;
573 // set / ask for macro
574 void SwGlossaryHdl::SetMacros(const OUString& rShortName,
575 const SvxMacro* pStart,
576 const SvxMacro* pEnd,
577 SwTextBlocks *pGlossary )
579 SwTextBlocks *pGlos = pGlossary ? pGlossary :
580 m_pCurGrp ? m_pCurGrp.get()
581 : m_rStatGlossaries.GetGroupDoc( m_aCurGrp ).release();
582 SvxMacroTableDtor aMacroTable;
583 if( pStart )
584 aMacroTable.Insert( SvMacroItemId::SwStartInsGlossary, *pStart);
585 if( pEnd )
586 aMacroTable.Insert( SvMacroItemId::SwEndInsGlossary, *pEnd);
587 sal_uInt16 nIdx = pGlos->GetIndex( rShortName );
588 if( !pGlos->SetMacroTable( nIdx, aMacroTable ) && pGlos->GetError() )
589 ErrorHandler::HandleError( pGlos->GetError() );
591 if(!m_pCurGrp && !pGlossary)
592 delete pGlos;
595 void SwGlossaryHdl::GetMacros( const OUString &rShortName,
596 SvxMacro& rStart,
597 SvxMacro& rEnd,
598 SwTextBlocks *pGlossary )
600 SwTextBlocks *pGlos = pGlossary ? pGlossary
601 : m_pCurGrp ? m_pCurGrp.get()
602 : m_rStatGlossaries.GetGroupDoc(m_aCurGrp).release();
603 sal_uInt16 nIndex = pGlos->GetIndex( rShortName );
604 if( nIndex != USHRT_MAX )
606 SvxMacroTableDtor aMacroTable;
607 if( pGlos->GetMacroTable( nIndex, aMacroTable ) )
609 SvxMacro *pMacro = aMacroTable.Get( SvMacroItemId::SwStartInsGlossary );
610 if( pMacro )
611 rStart = *pMacro;
613 pMacro = aMacroTable.Get( SvMacroItemId::SwEndInsGlossary );
614 if( pMacro )
615 rEnd = *pMacro;
619 if( !m_pCurGrp && !pGlossary )
620 delete pGlos;
623 // ctor, dtor
624 SwGlossaryHdl::SwGlossaryHdl(SfxViewFrame& rVwFrame, SwWrtShell *pSh)
625 : m_rStatGlossaries( *::GetGlossaries() ),
626 m_aCurGrp( SwGlossaries::GetDefName() ),
627 m_rViewFrame(rVwFrame),
628 m_pWrtShell( pSh )
632 SwGlossaryHdl::~SwGlossaryHdl()
636 // rename an autotext
637 bool SwGlossaryHdl::Rename(const OUString& rOldShort, const OUString& rNewShortName,
638 const OUString& rNewName )
640 bool bRet = false;
641 SwTextBlocks *pGlossary = m_pCurGrp ? m_pCurGrp.get()
642 : m_rStatGlossaries.GetGroupDoc(m_aCurGrp).release();
643 if(pGlossary)
645 sal_uInt16 nIdx = pGlossary->GetIndex( rOldShort );
646 sal_uInt16 nOldLongIdx = pGlossary->GetLongIndex( rNewName );
647 sal_uInt16 nOldIdx = pGlossary->GetIndex( rNewShortName );
649 if( nIdx != USHRT_MAX &&
650 (nOldLongIdx == USHRT_MAX || nOldLongIdx == nIdx )&&
651 (nOldIdx == USHRT_MAX || nOldIdx == nIdx ))
653 pGlossary->Rename( nIdx, &rNewShortName, &rNewName );
654 bRet = pGlossary->GetError() == ERRCODE_NONE;
656 if( !m_pCurGrp )
657 delete pGlossary;
659 return bRet;
662 bool SwGlossaryHdl::IsReadOnly( const OUString* pGrpNm ) const
664 SwTextBlocks *pGlossary = nullptr;
666 if (pGrpNm)
667 pGlossary = m_rStatGlossaries.GetGroupDoc( *pGrpNm ).release();
668 else if (m_pCurGrp)
669 pGlossary = m_pCurGrp.get();
670 else
671 pGlossary = m_rStatGlossaries.GetGroupDoc(m_aCurGrp).release();
673 const bool bRet = !pGlossary || pGlossary->IsReadOnly();
674 if( pGrpNm || !m_pCurGrp )
675 delete pGlossary;
676 return bRet;
679 bool SwGlossaryHdl::IsOld() const
681 if( !m_pCurGrp )
682 m_rStatGlossaries.GetGroupDoc(m_aCurGrp).reset();
683 return false;
686 // find group without path index
687 bool SwGlossaryHdl::FindGroupName(OUString& rGroup)
689 return m_rStatGlossaries.FindGroupName(rGroup);
692 bool SwGlossaryHdl::CopyToClipboard(SwWrtShell& rSh, const OUString& rShortName)
694 SwTextBlocks *pGlossary = m_pCurGrp ? m_pCurGrp.get()
695 : m_rStatGlossaries.GetGroupDoc(m_aCurGrp).release();
697 rtl::Reference<SwTransferable> pTransfer = new SwTransferable( rSh );
699 bool bRet = pTransfer->CopyGlossary( *pGlossary, rShortName );
700 if( !m_pCurGrp )
701 delete pGlossary;
702 return bRet;
705 bool SwGlossaryHdl::ImportGlossaries( const OUString& rName )
707 bool bRet = false;
708 if( !rName.isEmpty() )
710 std::shared_ptr<const SfxFilter> pFilter;
711 SfxMedium aMed( rName, StreamMode::READ, nullptr, nullptr );
712 SfxFilterMatcher aMatcher( u"swriter"_ustr );
713 aMed.UseInteractionHandler( true );
714 if (aMatcher.GuessFilter(aMed, pFilter, SfxFilterFlags::NONE) == ERRCODE_NONE)
716 assert(pFilter && "success means pFilter was set");
717 SwTextBlocks *pGlossary = nullptr;
718 aMed.SetFilter( pFilter );
719 Reader* pR = SwReaderWriter::GetReader( pFilter->GetUserData() );
720 if( pR && nullptr != ( pGlossary = m_pCurGrp ? m_pCurGrp.get()
721 : m_rStatGlossaries.GetGroupDoc(m_aCurGrp).release()) )
723 SwReader aReader( aMed, rName );
724 if( aReader.HasGlossaries( *pR ) )
726 const SvxAutoCorrCfg& rCfg = SvxAutoCorrCfg::Get();
727 bRet = aReader.ReadGlossaries( *pR, *pGlossary,
728 rCfg.IsSaveRelFile() );
731 if (!m_pCurGrp)
732 delete pGlossary;
736 return bRet;
739 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */