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>
25 #include <unotools/pathoptions.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  , 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::TempFile aTempFile(&sPath);
80  aTempFile.EnableKillingFile();
81  if(!aTempFile.IsValid())
82  nCaseReadonly |= PATH_READONLY;
83  else if( SWUnoHelper::UCB_IsCaseSensitiveFileName( aTempFile.GetURL()))
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 const 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  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  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  pGlosHdl->RenameGroup(sOld, sNew, sTitle);
173  if (it == m_RenamedArr.begin())
174  {
175  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  pGlosHdl->NewGroup(sNewGroup, sNewTitle);
184  if(sCreatedGroup.isEmpty())
185  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  fprintf(stderr, "comparing %s and %s\n",
206  OUStringToOString(pFoundData->sGroupName, RTL_TEXTENCODING_UTF8).getStr(),
207  OUStringToOString(sEntry, RTL_TEXTENCODING_UTF8).getStr());
208  bExists = pFoundData->sGroupName == sEntry;
209  }
210 
211  m_xRenamePB->set_sensitive(!bExists && !sName.isEmpty());
212  fprintf(stderr, "one rename %d\n", !bExists && !sName.isEmpty());
213  m_xDelPB->set_sensitive(IsDeleteAllowed(sEntry));
214 }
215 
217 {
218  OUString sGroup = m_xNameED->get_text()
219  + OUStringChar(GLOS_DELIM)
220  + OUString::number(m_xPathLB->get_active());
221  OSL_ENSURE(!pGlosHdl->FindGroupName(sGroup), "group already available!");
222  m_InsertedArr.push_back(sGroup);
224  pData->sPath = m_xPathLB->get_active_text();
225  pData->sGroupName = sGroup;
226  pData->sGroupTitle = m_xNameED->get_text();
227  OUString sId(weld::toId(pData));
228  m_xGroupTLB->append(sId, m_xNameED->get_text());
229  int nEntry = m_xGroupTLB->find_id(sId);
230  m_xGroupTLB->set_text(nEntry, pData->sPath, 1);
231  m_xGroupTLB->select(nEntry);
232  SelectHdl(*m_xGroupTLB);
233  m_xGroupTLB->scroll_to_row(nEntry);
234 }
235 
236 IMPL_LINK_NOARG(SwGlossaryGroupDlg, EntrySizeAllocHdl, const Size&, void)
237 {
238  std::vector<int> aWidths;
239  int x, y, width, height;
240  if (m_xPathLB->get_extents_relative_to(*m_xGroupTLB, x, y, width, height))
241  {
242  aWidths.push_back(x);
243  m_xGroupTLB->set_column_fixed_widths(aWidths);
244  }
245 }
246 
247 IMPL_LINK( SwGlossaryGroupDlg, DeleteHdl, weld::Button&, rButton, void )
248 {
249  int nEntry = m_xGroupTLB->get_selected_index();
250  if (nEntry == -1)
251  {
252  rButton.set_sensitive(false);
253  return;
254  }
255  GlosBibUserData* pUserData = weld::fromId<GlosBibUserData*>(m_xGroupTLB->get_id(nEntry));
256  OUString const sEntry(pUserData->sGroupName);
257  // if the name to be deleted is among the new ones - get rid of it
258  bool bDelete = true;
259  auto it = std::find(m_InsertedArr.begin(), m_InsertedArr.end(), sEntry);
260  if (it != m_InsertedArr.end())
261  {
262  m_InsertedArr.erase(it);
263  bDelete = false;
264  }
265  // it should probably be renamed?
266  if(bDelete)
267  {
268  it = std::find_if(m_RenamedArr.begin(), m_RenamedArr.end(),
269  [&sEntry](OUString& s) { return o3tl::getToken(s, 0, RENAME_TOKEN_DELIM) == sEntry; });
270  if (it != m_RenamedArr.end())
271  {
272  m_RenamedArr.erase(it);
273  bDelete = false;
274  }
275  }
276  if(bDelete)
277  {
278  m_RemovedArr.emplace_back(pUserData->sGroupName + "\t" + pUserData->sGroupTitle);
279  }
280  delete pUserData;
281  m_xGroupTLB->remove(nEntry);
282  if (!m_xGroupTLB->n_children())
283  rButton.set_sensitive(false);
284  //the content must be deleted - otherwise the new handler would be called in Apply()
285  m_xNameED->set_text(OUString());
286  ModifyHdl(*m_xNameED);
287 }
288 
290 {
291  int nEntry = m_xGroupTLB->get_selected_index();
292  GlosBibUserData* pUserData = weld::fromId<GlosBibUserData*>(m_xGroupTLB->get_id(nEntry));
293  OUString sEntry(pUserData->sGroupName);
294 
295  const OUString sNewTitle(m_xNameED->get_text());
296  OUString sNewName = sNewTitle
297  + OUStringChar(GLOS_DELIM)
298  + OUString::number(m_xPathLB->get_active());
299  OSL_ENSURE(!pGlosHdl->FindGroupName(sNewName), "group already available!");
300 
301  // if the name to be renamed is among the new ones - replace
302  bool bDone = false;
303  auto it = std::find(m_InsertedArr.begin(), m_InsertedArr.end(), sEntry);
304  if (it != m_InsertedArr.end())
305  {
306  m_InsertedArr.erase(it);
307  m_InsertedArr.push_back(sNewName);
308  bDone = true;
309  }
310  if(!bDone)
311  {
312  sEntry += OUStringChar(RENAME_TOKEN_DELIM) + sNewName
313  + OUStringChar(RENAME_TOKEN_DELIM) + sNewTitle;
314  m_RenamedArr.push_back(sEntry);
315  }
316  delete pUserData;
317  m_xGroupTLB->remove(nEntry);
318 
320  pData->sPath = m_xPathLB->get_active_text();
321  pData->sGroupName = sNewName;
322  pData->sGroupTitle = sNewTitle;
323 
324  OUString sId(weld::toId(pData));
325  m_xGroupTLB->append(sId, m_xNameED->get_text());
326  nEntry = m_xGroupTLB->find_id(sId);
327  m_xGroupTLB->set_text(nEntry, m_xPathLB->get_active_text(), 1);
328  m_xGroupTLB->select(nEntry);
329  SelectHdl(*m_xGroupTLB);
330  m_xGroupTLB->scroll_to_row(nEntry);
331 }
332 
334 {
335  ModifyHdl(*m_xNameED);
336 }
337 
339 {
340  const OUString sEntry(m_xNameED->get_text());
341  bool bEnableNew = true;
342  bool bEnableDel = false;
343  sal_uInt32 nCaseReadonly = m_xPathLB->get_active_id().toUInt32();
344  bool bDirReadonly = 0 != (nCaseReadonly&PATH_READONLY);
345 
346  if (sEntry.isEmpty() || bDirReadonly)
347  bEnableNew = false;
348  else if(!sEntry.isEmpty())
349  {
350  int nPos = m_xGroupTLB->find_text(sEntry);
351  //if it's not case sensitive you have to search for yourself
352  if (nPos == -1)
353  {
354  const ::utl::TransliterationWrapper& rSCmp = GetAppCmpStrIgnore();
355  for (int i = 0, nEntryCount = m_xGroupTLB->n_children(); i < nEntryCount; ++i)
356  {
357  const OUString sTemp = m_xGroupTLB->get_text(i, 0);
358  nCaseReadonly = m_xPathLB->get_id(m_xPathLB->find_text(m_xGroupTLB->get_text(i,1))).toUInt32();
359  bool bCase = 0 != (nCaseReadonly & PATH_CASE_SENSITIVE);
360 
361  if( !bCase && rSCmp.isEqual( sTemp, sEntry ))
362  {
363  nPos = i;
364  break;
365  }
366  }
367  }
368  if (nPos != -1)
369  {
370  bEnableNew = false;
371  m_xGroupTLB->select(nPos);
372  m_xGroupTLB->scroll_to_row(nPos);
373  SelectHdl(*m_xGroupTLB);
374  }
375  }
376  int nEntry = m_xGroupTLB->get_selected_index();
377  if (nEntry != -1)
378  {
379  GlosBibUserData* pUserData = weld::fromId<GlosBibUserData*>(m_xGroupTLB->get_id(nEntry));
380  bEnableDel = IsDeleteAllowed(pUserData->sGroupName);
381  }
382 
383  m_xDelPB->set_sensitive(bEnableDel);
384  m_xNewPB->set_sensitive(bEnableNew);
385  m_xRenamePB->set_sensitive(bEnableNew && nEntry != -1);
386  fprintf(stderr, "two rename %d\n", bEnableNew && nEntry != -1);
387 }
388 
389 bool SwGlossaryGroupDlg::IsDeleteAllowed(const OUString &rGroup)
390 {
391  bool bDel = !pGlosHdl->IsReadOnly(&rGroup);
392 
393  // OM: if the name is among the new region name, it is deletable
394  // as well! Because for non existing region names ReadOnly issues
395  // true.
396 
397  auto it = std::find(m_InsertedArr.cbegin(), m_InsertedArr.cend(), rGroup);
398  if (it != m_InsertedArr.cend())
399  bDel = true;
400 
401  return bDel;
402 }
403 
404 IMPL_STATIC_LINK(SwGlossaryGroupDlg, EditInsertTextHdl, OUString&, rText, bool)
405 {
406  rText = rText.replaceAll(OUStringChar(SVT_SEARCHPATH_DELIMITER), "");
407  return true;
408 }
409 
410 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define PATH_READONLY
Definition: glosbib.cxx:39
const ::utl::TransliterationWrapper & GetAppCmpStrIgnore()
Definition: init.cxx:786
std::unique_ptr< weld::Entry > m_xNameED
Definition: glosbib.hxx:49
IMPL_LINK_NOARG(SwGlossaryGroupDlg, SelectHdl, weld::TreeView &, void)
Definition: glosbib.cxx:190
std::unique_ptr< sal_Int32[]> pData
std::vector< OUString > m_RemovedArr
Definition: glosbib.hxx:40
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
std::unique_ptr< weld::TreeView > m_xGroupTLB
Definition: glosbib.hxx:51
float x
virtual short run() override
Definition: glosbib.cxx:126
size_t GetGroupCnt() const
Definition: gloshdl.cxx:138
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:164
RET_NO
SwGlossaryHdl * pGlosHdl
Definition: glosbib.hxx:45
std::unique_ptr< weld::Button > m_xRenamePB
Definition: glosbib.hxx:55
RET_YES
OUString sPath
Definition: glosbib.hxx:33
bool IsValid() const
int nCount
SwGlossaryGroupDlg(weld::Window *pParent, std::vector< OUString > const &rPathArr, SwGlossaryHdl *pGlosHdl)
Definition: glosbib.cxx:43
void SetCurGroup(const OUString &aGrp, bool bApi=false, bool bAlwaysCreateNew=false)
Definition: gloshdl.cxx:94
bool IsDeleteAllowed(const OUString &rGroup)
Definition: glosbib.cxx:389
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
const char * sName
static OUString GetCurrGroup()
Definition: glossary.cxx:161
std::vector< OUString > m_RenamedArr
Definition: glosbib.hxx:42
float y
IMPL_LINK(SwGlossaryGroupDlg, DeleteHdl, weld::Button &, rButton, void)
Definition: glosbib.cxx:247
OUString sGroupTitle
Definition: glosbib.hxx:35
bool DelGroup(const OUString &)
Definition: gloshdl.cxx:199
#define RENAME_TOKEN_DELIM
Definition: glosbib.cxx:41
int i
OUString const & GetURL() const
float u
bool IsReadOnly(const OUString *=nullptr) const
Definition: gloshdl.cxx:625
OUString GetGroupName(size_t, OUString *pTitle)
Definition: gloshdl.cxx:143
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
void NewGroup(OUString &rGroupName, const OUString &rTitle)
Definition: gloshdl.cxx:166
virtual ~SwGlossaryGroupDlg() override
Definition: glosbib.cxx:116
#define SVT_SEARCHPATH_DELIMITER
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString toId(const void *pValue)
OUString sGroupName
Definition: glosbib.hxx:34
OUString sCreatedGroup
Definition: glosbib.hxx:47
std::vector< OUString > m_InsertedArr
Definition: glosbib.hxx:41
std::unique_ptr< weld::Button > m_xNewPB
Definition: glosbib.hxx:53
RET_OK
std::unique_ptr< weld::Button > m_xDelPB
Definition: glosbib.hxx:54
#define GLOS_DELIM
Definition: glosdoc.hxx:41
#define PATH_CASE_SENSITIVE
Definition: glosbib.cxx:38
void RenameGroup(const OUString &rOld, OUString &rNew, const OUString &rNewTitle)
Definition: gloshdl.cxx:173
IMPL_STATIC_LINK(SwGlossaryGroupDlg, EditInsertTextHdl, OUString &, rText, bool)
Definition: glosbib.cxx:404
weld::Window * m_pParent
Definition: glosbib.hxx:44
bool UCB_IsCaseSensitiveFileName(std::u16string_view rURL)
void EnableKillingFile(bool bEnable=true)
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
std::unique_ptr< weld::ComboBox > m_xPathLB
Definition: glosbib.hxx:50
sal_uInt16 nPos
OUString sId