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