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