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