LibreOffice Module sw (master) 1
glosbib.cxx
Go to the documentation of this file.
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/svapp.hxx>
22#include <vcl/weld.hxx>
24#include <unotools/tempfile.hxx>
26#include <osl/diagnose.h>
27#include <o3tl/string_view.hxx>
28
29#include <swtypes.hxx>
30#include <glosbib.hxx>
31#include <gloshdl.hxx>
32#include <glossary.hxx>
33#include <glosdoc.hxx>
34#include <swunohelper.hxx>
35
36#include <strings.hrc>
37
38#define PATH_CASE_SENSITIVE 0x01
39#define PATH_READONLY 0x02
40
41#define RENAME_TOKEN_DELIM u'\x0001'
42
44 std::vector<OUString> const& rPathArr, SwGlossaryHdl *pHdl)
45 : SfxDialogController(pParent, "modules/swriter/ui/editcategories.ui",
46 "EditCategoriesDialog")
47 , m_pParent(pParent)
48 , m_pGlosHdl(pHdl)
49 , m_xNameED(m_xBuilder->weld_entry("name"))
50 , m_xPathLB(m_xBuilder->weld_combo_box("pathlb"))
51 , m_xGroupTLB(m_xBuilder->weld_tree_view("group"))
52 , m_xNewPB(m_xBuilder->weld_button("new"))
53 , m_xDelPB(m_xBuilder->weld_button("delete"))
54 , m_xRenamePB(m_xBuilder->weld_button("rename"))
55{
56 int nWidth = m_xGroupTLB->get_approximate_digit_width() * 34;
57 m_xPathLB->set_size_request(nWidth, -1);
58 //just has to be something small, real size will be available space
59 m_xGroupTLB->set_size_request(nWidth, m_xGroupTLB->get_height_rows(10));
60
61 m_xGroupTLB->set_column_fixed_widths( { nWidth } );
62 m_xGroupTLB->connect_changed(LINK(this, SwGlossaryGroupDlg, SelectHdl));
63
64 m_xNewPB->connect_clicked(LINK(this, SwGlossaryGroupDlg, NewHdl));
65 m_xDelPB->connect_clicked(LINK(this, SwGlossaryGroupDlg, DeleteHdl));
66 m_xNameED->connect_changed(LINK(this, SwGlossaryGroupDlg, ModifyHdl));
67 m_xNameED->connect_insert_text(LINK(this, SwGlossaryGroupDlg, EditInsertTextHdl));
68 m_xPathLB->connect_changed(LINK(this, SwGlossaryGroupDlg, ModifyListBoxHdl));
69 m_xRenamePB->connect_clicked(LINK(this, SwGlossaryGroupDlg, RenameHdl));
70
71 m_xNameED->connect_size_allocate(LINK(this, SwGlossaryGroupDlg, EntrySizeAllocHdl));
72 m_xPathLB->connect_size_allocate(LINK(this, SwGlossaryGroupDlg, EntrySizeAllocHdl));
73
74 for (size_t i = 0; i < rPathArr.size(); ++i)
75 {
76 INetURLObject aTempURL(rPathArr[i]);
77 const OUString sPath = aTempURL.GetMainURL(INetURLObject::DecodeMechanism::WithCharset );
78 sal_uInt32 nCaseReadonly = 0;
79 utl::TempFileNamed aTempFile(&sPath);
80 aTempFile.EnableKillingFile();
81 if(!aTempFile.IsValid())
82 nCaseReadonly |= PATH_READONLY;
84 nCaseReadonly |= PATH_CASE_SENSITIVE;
85 m_xPathLB->append(OUString::number(nCaseReadonly), sPath);
86 }
87 m_xPathLB->set_active(0);
88 m_xPathLB->set_sensitive(true);
89
90 const size_t nCount = pHdl->GetGroupCnt();
91 /* tdf#111870 "My AutoText" comes from mytexts.bau but should be translated
92 here as well, see also SwGlossaryDlg::Init */
93 static constexpr OUStringLiteral sMyAutoTextEnglish(u"My AutoText");
94 for( size_t i = 0; i < nCount; ++i)
95 {
96 OUString sTitle;
97 OUString sGroup = pHdl->GetGroupName(i, &sTitle);
98 if(sGroup.isEmpty())
99 continue;
101 pData->sGroupName = sGroup;
102 if ( sTitle == sMyAutoTextEnglish )
103 pData->sGroupTitle = SwResId(STR_MY_AUTOTEXT);
104 else
105 pData->sGroupTitle = sTitle;
106 pData->sPath = m_xPathLB->get_text(o3tl::toInt32(o3tl::getToken(sGroup, 1, GLOS_DELIM)));
107 const OUString sId(weld::toId(pData));
108 m_xGroupTLB->append(sId, pData->sGroupTitle);
109 int nEntry = m_xGroupTLB->find_id(sId);
110 m_xGroupTLB->set_text(nEntry, pData->sPath, 1);
111
112 }
113 m_xGroupTLB->make_sorted();
114}
115
117{
118 int nCount = m_xGroupTLB->n_children();
119 for (int i = 0; i < nCount; ++i)
120 {
121 GlosBibUserData* pUserData = weld::fromId<GlosBibUserData*>(m_xGroupTLB->get_id(i));
122 delete pUserData;
123 }
124}
125
127{
128 short nRet = SfxDialogController::run();
129 if (nRet == RET_OK)
130 Apply();
131 return nRet;
132}
133
135{
136 if (m_xNewPB->get_sensitive())
137 NewHdl(*m_xNewPB);
138
139 const OUString aActGroup = SwGlossaryDlg::GetCurrGroup();
140
141 for (const auto& removedStr : m_RemovedArr)
142 {
143 sal_Int32 nIdx{ 0 };
144 const OUString sDelGroup = removedStr.getToken(0, '\t', nIdx);
145 if( sDelGroup == aActGroup )
146 {
147 //when the current group is deleted, the current group has to be relocated
148 if (m_xGroupTLB->n_children())
149 {
150 GlosBibUserData* pUserData = weld::fromId<GlosBibUserData*>(m_xGroupTLB->get_id(0));
151 m_pGlosHdl->SetCurGroup(pUserData->sGroupName);
152 }
153 }
154 const OUString sMsg(SwResId(STR_QUERY_DELETE_GROUP1)
155 + o3tl::getToken(removedStr, 0, '\t', nIdx)
156 + SwResId(STR_QUERY_DELETE_GROUP2));
157
158 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(m_pParent,
159 VclMessageType::Question, VclButtonsType::YesNo, sMsg));
160 xQueryBox->set_default_response(RET_NO);
161 if (RET_YES == xQueryBox->run())
162 m_pGlosHdl->DelGroup( sDelGroup );
163 }
164
165 //don't rename before there was one
166 for (auto it(m_RenamedArr.cbegin()); it != m_RenamedArr.cend(); ++it)
167 {
168 sal_Int32 nIdx{ 0 };
169 OUString const sOld(it->getToken(0, RENAME_TOKEN_DELIM, nIdx));
170 OUString sNew(it->getToken(0, RENAME_TOKEN_DELIM, nIdx));
171 OUString const sTitle(it->getToken(0, RENAME_TOKEN_DELIM, nIdx));
172 m_pGlosHdl->RenameGroup(sOld, sNew, sTitle);
173 if (it == m_RenamedArr.begin())
174 {
175 m_sCreatedGroup = sNew;
176 }
177 }
178 for (auto& sNewGroup : m_InsertedArr)
179 {
180 OUString sNewTitle = sNewGroup.getToken(0, GLOS_DELIM);
181 if( sNewGroup != aActGroup )
182 {
183 m_pGlosHdl->NewGroup(sNewGroup, sNewTitle);
184 if(m_sCreatedGroup.isEmpty())
185 m_sCreatedGroup = sNewGroup;
186 }
187 }
188}
189
191{
192 m_xNewPB->set_sensitive(false);
193 int nFirstEntry = m_xGroupTLB->get_selected_index();
194 if (nFirstEntry == -1)
195 return;
196
197 GlosBibUserData* pUserData = weld::fromId<GlosBibUserData*>(m_xGroupTLB->get_id(nFirstEntry));
198 const OUString sEntry(pUserData->sGroupName);
199 const OUString sName(m_xNameED->get_text());
200 bool bExists = false;
201 int nPos = m_xGroupTLB->find_text(sName);
202 if (nPos != -1)
203 {
204 GlosBibUserData* pFoundData = weld::fromId<GlosBibUserData*>(m_xGroupTLB->get_id(nPos));
205 bExists = pFoundData->sGroupName == sEntry;
206 }
207
208 m_xRenamePB->set_sensitive(!bExists && !sName.isEmpty());
209 m_xDelPB->set_sensitive(IsDeleteAllowed(sEntry));
210}
211
213{
214 OUString sGroup = m_xNameED->get_text()
215 + OUStringChar(GLOS_DELIM)
216 + OUString::number(m_xPathLB->get_active());
217 OSL_ENSURE(!m_pGlosHdl->FindGroupName(sGroup), "group already available!");
218 m_InsertedArr.push_back(sGroup);
220 pData->sPath = m_xPathLB->get_active_text();
221 pData->sGroupName = sGroup;
222 pData->sGroupTitle = m_xNameED->get_text();
223 OUString sId(weld::toId(pData));
224 m_xGroupTLB->append(sId, m_xNameED->get_text());
225 int nEntry = m_xGroupTLB->find_id(sId);
226 m_xGroupTLB->set_text(nEntry, pData->sPath, 1);
227 m_xGroupTLB->select(nEntry);
228 SelectHdl(*m_xGroupTLB);
229 m_xGroupTLB->scroll_to_row(nEntry);
230}
231
232IMPL_LINK_NOARG(SwGlossaryGroupDlg, EntrySizeAllocHdl, const Size&, void)
233{
234 std::vector<int> aWidths;
235 int x, y, width, height;
236 if (m_xPathLB->get_extents_relative_to(*m_xGroupTLB, x, y, width, height))
237 {
238 aWidths.push_back(x);
239 m_xGroupTLB->set_column_fixed_widths(aWidths);
240 }
241}
242
243IMPL_LINK( SwGlossaryGroupDlg, DeleteHdl, weld::Button&, rButton, void )
244{
245 int nEntry = m_xGroupTLB->get_selected_index();
246 if (nEntry == -1)
247 {
248 rButton.set_sensitive(false);
249 return;
250 }
251 GlosBibUserData* pUserData = weld::fromId<GlosBibUserData*>(m_xGroupTLB->get_id(nEntry));
252 OUString const sEntry(pUserData->sGroupName);
253 // if the name to be deleted is among the new ones - get rid of it
254 bool bDelete = true;
255 auto it = std::find(m_InsertedArr.begin(), m_InsertedArr.end(), sEntry);
256 if (it != m_InsertedArr.end())
257 {
258 m_InsertedArr.erase(it);
259 bDelete = false;
260 }
261 // it should probably be renamed?
262 if(bDelete)
263 {
264 it = std::find_if(m_RenamedArr.begin(), m_RenamedArr.end(),
265 [&sEntry](OUString& s) { return o3tl::getToken(s, 0, RENAME_TOKEN_DELIM) == sEntry; });
266 if (it != m_RenamedArr.end())
267 {
268 m_RenamedArr.erase(it);
269 bDelete = false;
270 }
271 }
272 if(bDelete)
273 {
274 m_RemovedArr.emplace_back(pUserData->sGroupName + "\t" + pUserData->sGroupTitle);
275 }
276 delete pUserData;
277 m_xGroupTLB->remove(nEntry);
278 if (!m_xGroupTLB->n_children())
279 rButton.set_sensitive(false);
280 //the content must be deleted - otherwise the new handler would be called in Apply()
281 m_xNameED->set_text(OUString());
282 ModifyHdl(*m_xNameED);
283}
284
286{
287 int nEntry = m_xGroupTLB->get_selected_index();
288 GlosBibUserData* pUserData = weld::fromId<GlosBibUserData*>(m_xGroupTLB->get_id(nEntry));
289 OUString sEntry(pUserData->sGroupName);
290
291 const OUString sNewTitle(m_xNameED->get_text());
292 OUString sNewName = sNewTitle
293 + OUStringChar(GLOS_DELIM)
294 + OUString::number(m_xPathLB->get_active());
295 OSL_ENSURE(!m_pGlosHdl->FindGroupName(sNewName), "group already available!");
296
297 // if the name to be renamed is among the new ones - replace
298 bool bDone = false;
299 auto it = std::find(m_InsertedArr.begin(), m_InsertedArr.end(), sEntry);
300 if (it != m_InsertedArr.end())
301 {
302 m_InsertedArr.erase(it);
303 m_InsertedArr.push_back(sNewName);
304 bDone = true;
305 }
306 if(!bDone)
307 {
308 sEntry += OUStringChar(RENAME_TOKEN_DELIM) + sNewName
309 + OUStringChar(RENAME_TOKEN_DELIM) + sNewTitle;
310 m_RenamedArr.push_back(sEntry);
311 }
312 delete pUserData;
313 m_xGroupTLB->remove(nEntry);
314
316 pData->sPath = m_xPathLB->get_active_text();
317 pData->sGroupName = sNewName;
318 pData->sGroupTitle = sNewTitle;
319
320 OUString sId(weld::toId(pData));
321 m_xGroupTLB->append(sId, m_xNameED->get_text());
322 nEntry = m_xGroupTLB->find_id(sId);
323 m_xGroupTLB->set_text(nEntry, m_xPathLB->get_active_text(), 1);
324 m_xGroupTLB->select(nEntry);
325 SelectHdl(*m_xGroupTLB);
326 m_xGroupTLB->scroll_to_row(nEntry);
327}
328
330{
331 ModifyHdl(*m_xNameED);
332}
333
335{
336 const OUString sEntry(m_xNameED->get_text());
337 bool bEnableNew = true;
338 bool bEnableDel = false;
339 sal_uInt32 nCaseReadonly = m_xPathLB->get_active_id().toUInt32();
340 bool bDirReadonly = 0 != (nCaseReadonly&PATH_READONLY);
341
342 if (sEntry.isEmpty() || bDirReadonly)
343 bEnableNew = false;
344 else if(!sEntry.isEmpty())
345 {
346 int nPos = m_xGroupTLB->find_text(sEntry);
347 //if it's not case sensitive you have to search for yourself
348 if (nPos == -1)
349 {
350 const ::utl::TransliterationWrapper& rSCmp = GetAppCmpStrIgnore();
351 for (int i = 0, nEntryCount = m_xGroupTLB->n_children(); i < nEntryCount; ++i)
352 {
353 const OUString sTemp = m_xGroupTLB->get_text(i, 0);
354 nCaseReadonly = m_xPathLB->get_id(m_xPathLB->find_text(m_xGroupTLB->get_text(i,1))).toUInt32();
355 bool bCase = 0 != (nCaseReadonly & PATH_CASE_SENSITIVE);
356
357 if( !bCase && rSCmp.isEqual( sTemp, sEntry ))
358 {
359 nPos = i;
360 break;
361 }
362 }
363 }
364 if (nPos != -1)
365 {
366 bEnableNew = false;
367 m_xGroupTLB->select(nPos);
368 m_xGroupTLB->scroll_to_row(nPos);
369 SelectHdl(*m_xGroupTLB);
370 }
371 }
372 int nEntry = m_xGroupTLB->get_selected_index();
373 if (nEntry != -1)
374 {
375 GlosBibUserData* pUserData = weld::fromId<GlosBibUserData*>(m_xGroupTLB->get_id(nEntry));
376 bEnableDel = IsDeleteAllowed(pUserData->sGroupName);
377 }
378
379 m_xDelPB->set_sensitive(bEnableDel);
380 m_xNewPB->set_sensitive(bEnableNew);
381 m_xRenamePB->set_sensitive(bEnableNew && nEntry != -1);
382}
383
384bool SwGlossaryGroupDlg::IsDeleteAllowed(const OUString &rGroup)
385{
386 bool bDel = !m_pGlosHdl->IsReadOnly(&rGroup);
387
388 // OM: if the name is among the new region name, it is deletable
389 // as well! Because for non existing region names ReadOnly issues
390 // true.
391
392 auto it = std::find(m_InsertedArr.cbegin(), m_InsertedArr.cend(), rGroup);
393 if (it != m_InsertedArr.cend())
394 bDel = true;
395
396 return bDel;
397}
398
399IMPL_STATIC_LINK(SwGlossaryGroupDlg, EditInsertTextHdl, OUString&, rText, bool)
400{
401 rText = rText.replaceAll(OUStringChar(SVT_SEARCHPATH_DELIMITER), "");
402 return true;
403}
404
405/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
static OUString GetCurrGroup()
Definition: glossary.cxx:161
virtual short run() override
Definition: glosbib.cxx:126
SwGlossaryGroupDlg(weld::Window *pParent, std::vector< OUString > const &rPathArr, SwGlossaryHdl *pGlosHdl)
Definition: glosbib.cxx:43
std::unique_ptr< weld::Button > m_xDelPB
Definition: glosbib.hxx:54
std::unique_ptr< weld::Button > m_xRenamePB
Definition: glosbib.hxx:55
OUString m_sCreatedGroup
Definition: glosbib.hxx:47
std::vector< OUString > m_InsertedArr
Definition: glosbib.hxx:41
SwGlossaryHdl * m_pGlosHdl
Definition: glosbib.hxx:45
std::vector< OUString > m_RemovedArr
Definition: glosbib.hxx:40
weld::Window * m_pParent
Definition: glosbib.hxx:44
std::unique_ptr< weld::TreeView > m_xGroupTLB
Definition: glosbib.hxx:51
std::unique_ptr< weld::Entry > m_xNameED
Definition: glosbib.hxx:49
std::unique_ptr< weld::Button > m_xNewPB
Definition: glosbib.hxx:53
virtual ~SwGlossaryGroupDlg() override
Definition: glosbib.cxx:116
std::unique_ptr< weld::ComboBox > m_xPathLB
Definition: glosbib.hxx:50
std::vector< OUString > m_RenamedArr
Definition: glosbib.hxx:42
bool IsDeleteAllowed(const OUString &rGroup)
Definition: glosbib.cxx:384
bool DelGroup(const OUString &)
Definition: gloshdl.cxx:222
bool IsReadOnly(const OUString *=nullptr) const
Definition: gloshdl.cxx:648
void SetCurGroup(const OUString &aGrp, bool bApi=false, bool bAlwaysCreateNew=false)
Definition: gloshdl.cxx:95
OUString GetGroupName(size_t, OUString *pTitle)
Definition: gloshdl.cxx:144
size_t GetGroupCnt() const
Definition: gloshdl.cxx:139
void RenameGroup(const OUString &rOld, OUString &rNew, const OUString &rNewTitle)
Definition: gloshdl.cxx:174
void NewGroup(OUString &rGroupName, const OUString &rTitle)
Definition: gloshdl.cxx:167
void EnableKillingFile(bool bEnable=true)
bool IsValid() const
OUString const & GetURL() const
int nCount
float u
float y
float x
OUString sName
IMPL_LINK_NOARG(SwGlossaryGroupDlg, SelectHdl, weld::TreeView &, void)
Definition: glosbib.cxx:190
#define RENAME_TOKEN_DELIM
Definition: glosbib.cxx:41
IMPL_LINK(SwGlossaryGroupDlg, DeleteHdl, weld::Button &, rButton, void)
Definition: glosbib.cxx:243
#define PATH_CASE_SENSITIVE
Definition: glosbib.cxx:38
IMPL_STATIC_LINK(SwGlossaryGroupDlg, EditInsertTextHdl, OUString &, rText, bool)
Definition: glosbib.cxx:399
#define PATH_READONLY
Definition: glosbib.cxx:39
#define GLOS_DELIM
Definition: glosdoc.hxx:45
const ::utl::TransliterationWrapper & GetAppCmpStrIgnore()
Definition: init.cxx:802
sal_uInt16 nPos
std::unique_ptr< sal_Int32[]> pData
bool UCB_IsCaseSensitiveFileName(std::u16string_view rURL)
int i
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OUString toId(const void *pValue)
#define SVT_SEARCHPATH_DELIMITER
OUString sGroupName
Definition: glosbib.hxx:34
OUString sGroupTitle
Definition: glosbib.hxx:35
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:168
OUString sId
RET_OK
RET_NO
RET_YES