Line data Source code
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 <tools/stream.hxx>
22 : #include <vcl/msgbox.hxx>
23 : #include <vcl/help.hxx>
24 : #include <vcl/builderfactory.hxx>
25 : #include <unotools/transliterationwrapper.hxx>
26 : #include <unotools/tempfile.hxx>
27 : #include <unotools/pathoptions.hxx>
28 : #include <svtools/treelistentry.hxx>
29 :
30 : #include <swtypes.hxx>
31 : #include <glosbib.hxx>
32 : #include <gloshdl.hxx>
33 : #include <actctrl.hxx>
34 : #include <glossary.hxx>
35 : #include <glosdoc.hxx>
36 : #include <swunohelper.hxx>
37 :
38 : #include <misc.hrc>
39 :
40 : #define PATH_CASE_SENSITIVE 0x01
41 : #define PATH_READONLY 0x02
42 :
43 : #define RENAME_TOKEN_DELIM (sal_Unicode)1
44 :
45 0 : SwGlossaryGroupDlg::SwGlossaryGroupDlg(vcl::Window * pParent,
46 : std::vector<OUString> const& rPathArr, SwGlossaryHdl *pHdl)
47 : : SvxStandardDialog(pParent, "EditCategoriesDialog",
48 : "modules/swriter/ui/editcategories.ui")
49 0 : , pGlosHdl(pHdl)
50 : {
51 0 : get(m_pPathLB, "pathlb");
52 0 : get(m_pNewPB, "new");
53 0 : get(m_pDelPB, "delete");
54 0 : get(m_pRenamePB, "rename");
55 0 : get(m_pNameED, "name");
56 0 : get(m_pGroupTLB, "group");
57 :
58 0 : const int nAppFontUnits = 130;
59 0 : long nWidth = LogicToPixel(Size(nAppFontUnits, 0), MAP_APPFONT).Width();
60 0 : m_pPathLB->set_width_request(nWidth);
61 : //just has to be something small, real size will be available space
62 0 : m_pGroupTLB->set_width_request(nWidth);
63 0 : m_pGroupTLB->set_height_request(GetTextHeight() * 10);
64 :
65 : long nTabs[] =
66 : { 2, // Number of Tabs
67 : 0, nAppFontUnits
68 0 : };
69 :
70 0 : m_pGroupTLB->SetTabs( &nTabs[0], MAP_APPFONT );
71 0 : m_pGroupTLB->SetSelectHdl(LINK(this, SwGlossaryGroupDlg, SelectHdl));
72 0 : m_pGroupTLB->GetModel()->SetSortMode(SortAscending);
73 0 : m_pNewPB->SetClickHdl(LINK(this, SwGlossaryGroupDlg, NewHdl));
74 0 : m_pDelPB->SetClickHdl(LINK(this, SwGlossaryGroupDlg, DeleteHdl));
75 0 : m_pNameED->SetModifyHdl(LINK(this, SwGlossaryGroupDlg, ModifyHdl));
76 0 : m_pPathLB->SetSelectHdl(LINK(this, SwGlossaryGroupDlg, ModifyHdl));
77 0 : m_pRenamePB->SetClickHdl(LINK(this, SwGlossaryGroupDlg, RenameHdl));
78 :
79 0 : for (size_t i = 0; i < rPathArr.size(); ++i)
80 : {
81 0 : INetURLObject aTempURL(rPathArr[i]);
82 0 : const OUString sPath = aTempURL.GetMainURL(INetURLObject::DECODE_WITH_CHARSET );
83 0 : m_pPathLB->InsertEntry(sPath);
84 0 : sal_uLong nCaseReadonly = 0;
85 0 : utl::TempFile aTempFile(&sPath);
86 0 : aTempFile.EnableKillingFile();
87 0 : if(!aTempFile.IsValid())
88 0 : nCaseReadonly |= PATH_READONLY;
89 0 : else if( SWUnoHelper::UCB_IsCaseSensitiveFileName( aTempFile.GetURL()))
90 0 : nCaseReadonly |= PATH_CASE_SENSITIVE;
91 0 : m_pPathLB->SetEntryData(i, reinterpret_cast<void*>(nCaseReadonly));
92 0 : }
93 0 : m_pPathLB->SelectEntryPos(0);
94 0 : m_pPathLB->Enable(true);
95 :
96 0 : const size_t nCount = pHdl->GetGroupCnt();
97 0 : for( size_t i = 0; i < nCount; ++i)
98 : {
99 0 : OUString sTitle;
100 0 : OUString sGroup = pHdl->GetGroupName(i, &sTitle);
101 0 : if(sGroup.isEmpty())
102 0 : continue;
103 0 : GlosBibUserData* pData = new GlosBibUserData;
104 0 : pData->sGroupName = sGroup;
105 0 : pData->sGroupTitle = sTitle;
106 0 : pData->sPath = m_pPathLB->GetEntry(sGroup.getToken(1, GLOS_DELIM).toInt32());
107 0 : SvTreeListEntry* pEntry = m_pGroupTLB->InsertEntry(sTitle + "\t" + pData->sPath);
108 0 : pEntry->SetUserData(pData);
109 :
110 0 : }
111 0 : m_pGroupTLB->GetModel()->Resort();
112 0 : }
113 :
114 0 : SwGlossaryGroupDlg::~SwGlossaryGroupDlg()
115 : {
116 0 : disposeOnce();
117 0 : }
118 :
119 0 : void SwGlossaryGroupDlg::dispose()
120 : {
121 0 : m_pNameED.clear();
122 0 : m_pPathLB.clear();
123 0 : m_pGroupTLB.clear();
124 0 : m_pNewPB.clear();
125 0 : m_pDelPB.clear();
126 0 : m_pRenamePB.clear();
127 0 : SvxStandardDialog::dispose();
128 0 : }
129 :
130 0 : void SwGlossaryGroupDlg::Apply()
131 : {
132 0 : if(m_pNewPB->IsEnabled())
133 0 : NewHdl(m_pNewPB);
134 :
135 0 : OUString aActGroup = SwGlossaryDlg::GetCurrGroup();
136 :
137 0 : for (OUVector_t::const_iterator it(m_RemovedArr.begin());
138 0 : it != m_RemovedArr.end(); ++it)
139 : {
140 0 : const OUString sDelGroup = it->getToken(0, '\t');
141 0 : if( sDelGroup == aActGroup )
142 : {
143 : //when the current group is deleted, the current group has to be relocated
144 0 : if(m_pGroupTLB->GetEntryCount())
145 : {
146 0 : SvTreeListEntry* pFirst = m_pGroupTLB->First();
147 0 : GlosBibUserData* pUserData = static_cast<GlosBibUserData*>(pFirst->GetUserData());
148 0 : pGlosHdl->SetCurGroup(pUserData->sGroupName);
149 : }
150 : }
151 0 : OUString sTitle( it->getToken(1, '\t') );
152 : const OUString sMsg(SW_RESSTR(STR_QUERY_DELETE_GROUP1)
153 0 : + sTitle
154 0 : + SW_RESSTR(STR_QUERY_DELETE_GROUP2));
155 0 : ScopedVclPtrInstance< QueryBox > aQuery(this->GetParent(), WB_YES_NO|WB_DEF_NO, sMsg );
156 0 : if(RET_YES == aQuery->Execute())
157 0 : pGlosHdl->DelGroup( sDelGroup );
158 0 : }
159 :
160 : //don't rename before there was one
161 0 : for (OUVector_t::const_iterator it(m_RenamedArr.begin());
162 0 : it != m_RenamedArr.end(); ++it)
163 : {
164 0 : OUString const sOld(it->getToken(0, RENAME_TOKEN_DELIM));
165 0 : OUString sNew(it->getToken(1, RENAME_TOKEN_DELIM));
166 0 : OUString const sTitle(it->getToken(2, RENAME_TOKEN_DELIM));
167 0 : pGlosHdl->RenameGroup(sOld, sNew, sTitle);
168 0 : if (it == m_RenamedArr.begin())
169 : {
170 0 : sCreatedGroup = sNew;
171 : }
172 0 : }
173 0 : for (OUVector_t::const_iterator it(m_InsertedArr.begin());
174 0 : it != m_InsertedArr.end(); ++it)
175 : {
176 0 : OUString sNewGroup = *it;
177 0 : OUString sNewTitle = sNewGroup.getToken(0, GLOS_DELIM);
178 0 : if( sNewGroup != aActGroup )
179 : {
180 0 : pGlosHdl->NewGroup(sNewGroup, sNewTitle);
181 0 : if(sCreatedGroup.isEmpty())
182 0 : sCreatedGroup = sNewGroup;
183 : }
184 0 : }
185 0 : }
186 :
187 0 : IMPL_LINK( SwGlossaryGroupDlg, SelectHdl, SvTabListBox*, )
188 : {
189 0 : m_pNewPB->Enable(false);
190 0 : SvTreeListEntry* pFirstEntry = m_pGroupTLB->FirstSelected();
191 0 : if(pFirstEntry)
192 : {
193 0 : GlosBibUserData* pUserData = static_cast<GlosBibUserData*>(pFirstEntry->GetUserData());
194 0 : OUString sEntry(pUserData->sGroupName);
195 0 : OUString sName(m_pNameED->GetText());
196 0 : bool bExists = false;
197 0 : sal_uLong nPos = m_pGroupTLB->GetEntryPos(sName, 0);
198 0 : if( 0xffffffff > nPos)
199 : {
200 0 : SvTreeListEntry* pEntry = m_pGroupTLB->GetEntry(nPos);
201 0 : GlosBibUserData* pFoundData = static_cast<GlosBibUserData*>(pEntry->GetUserData());
202 0 : bExists = pFoundData->sGroupName == sEntry;
203 : }
204 :
205 0 : m_pRenamePB->Enable(!bExists && !sName.isEmpty());
206 0 : m_pDelPB->Enable(IsDeleteAllowed(sEntry));
207 : }
208 0 : return 0;
209 : }
210 :
211 0 : IMPL_LINK_NOARG(SwGlossaryGroupDlg, NewHdl)
212 : {
213 0 : OUString sGroup = m_pNameED->GetText()
214 0 : + OUStringLiteral1<GLOS_DELIM>()
215 0 : + OUString::number(m_pPathLB->GetSelectEntryPos());
216 : OSL_ENSURE(!pGlosHdl->FindGroupName(sGroup), "group already available!");
217 0 : m_InsertedArr.push_back(sGroup);
218 0 : const OUString sTemp(m_pNameED->GetText() + "\t" + m_pPathLB->GetSelectEntry());
219 0 : SvTreeListEntry* pEntry = m_pGroupTLB->InsertEntry(sTemp);
220 0 : GlosBibUserData* pData = new GlosBibUserData;
221 0 : pData->sPath = m_pPathLB->GetSelectEntry();
222 0 : pData->sGroupName = sGroup;
223 0 : pData->sGroupTitle = m_pNameED->GetText();
224 0 : pEntry->SetUserData(pData);
225 0 : m_pGroupTLB->Select(pEntry);
226 0 : m_pGroupTLB->MakeVisible(pEntry);
227 0 : m_pGroupTLB->GetModel()->Resort();
228 :
229 0 : return 0;
230 : }
231 :
232 0 : IMPL_LINK( SwGlossaryGroupDlg, DeleteHdl, Button*, pButton )
233 : {
234 0 : SvTreeListEntry* pEntry = m_pGroupTLB->FirstSelected();
235 0 : if(!pEntry)
236 : {
237 0 : pButton->Enable(false);
238 0 : return 0;
239 : }
240 0 : GlosBibUserData* pUserData = static_cast<GlosBibUserData*>(pEntry->GetUserData());
241 0 : OUString const sEntry(pUserData->sGroupName);
242 : // if the name to be deleted is among the new ones - get rid of it
243 0 : bool bDelete = true;
244 0 : for (OUVector_t::iterator it(m_InsertedArr.begin());
245 0 : it != m_InsertedArr.end(); ++it)
246 : {
247 0 : if (*it == sEntry)
248 : {
249 0 : m_InsertedArr.erase(it);
250 0 : bDelete = false;
251 0 : break;
252 : }
253 :
254 : }
255 : // it should probably be renamed?
256 0 : if(bDelete)
257 : {
258 0 : for (OUVector_t::iterator it(m_RenamedArr.begin());
259 0 : it != m_RenamedArr.end(); ++it)
260 : {
261 0 : if (it->getToken(0, RENAME_TOKEN_DELIM) == sEntry)
262 : {
263 0 : m_RenamedArr.erase(it);
264 0 : bDelete = false;
265 0 : break;
266 : }
267 : }
268 : }
269 0 : if(bDelete)
270 : {
271 0 : m_RemovedArr.push_back(pUserData->sGroupName + "\t" + pUserData->sGroupTitle);
272 : }
273 0 : delete pUserData;
274 0 : m_pGroupTLB->GetModel()->Remove(pEntry);
275 0 : if(!m_pGroupTLB->First())
276 0 : pButton->Enable(false);
277 : //the content must be deleted - otherwise the new handler would be called in Apply()
278 0 : m_pNameED->SetText(aEmptyOUStr);
279 0 : return 0;
280 : }
281 :
282 0 : IMPL_LINK_NOARG(SwGlossaryGroupDlg, RenameHdl)
283 : {
284 0 : SvTreeListEntry* pEntry = m_pGroupTLB->FirstSelected();
285 0 : GlosBibUserData* pUserData = static_cast<GlosBibUserData*>(pEntry->GetUserData());
286 0 : OUString sEntry(pUserData->sGroupName);
287 :
288 0 : const OUString sNewTitle(m_pNameED->GetText());
289 : OUString sNewName = sNewTitle
290 0 : + OUStringLiteral1<GLOS_DELIM>()
291 0 : + OUString::number(m_pPathLB->GetSelectEntryPos());
292 : OSL_ENSURE(!pGlosHdl->FindGroupName(sNewName), "group already available!");
293 :
294 : // if the name to be renamed is among the new ones - replace
295 0 : bool bDone = false;
296 0 : for (OUVector_t::iterator it(m_InsertedArr.begin());
297 0 : it != m_InsertedArr.end(); ++it)
298 : {
299 0 : if (*it == sEntry)
300 : {
301 0 : m_InsertedArr.erase(it);
302 0 : m_InsertedArr.push_back(sNewName);
303 0 : bDone = true;
304 0 : break;
305 : }
306 : }
307 0 : if(!bDone)
308 : {
309 0 : sEntry += OUStringLiteral1<RENAME_TOKEN_DELIM>() + sNewName
310 0 : + OUStringLiteral1<RENAME_TOKEN_DELIM>() + sNewTitle;
311 0 : m_RenamedArr.push_back(sEntry);
312 : }
313 0 : delete static_cast<GlosBibUserData*>(pEntry->GetUserData());
314 0 : m_pGroupTLB->GetModel()->Remove(pEntry);
315 0 : pEntry = m_pGroupTLB->InsertEntry(m_pNameED->GetText() + "\t"
316 0 : + m_pPathLB->GetSelectEntry());
317 0 : GlosBibUserData* pData = new GlosBibUserData;
318 0 : pData->sPath = m_pPathLB->GetSelectEntry();
319 0 : pData->sGroupName = sNewName;
320 0 : pData->sGroupTitle = sNewTitle;
321 0 : pEntry->SetUserData(pData);
322 0 : m_pGroupTLB->Select(pEntry);
323 0 : m_pGroupTLB->MakeVisible(pEntry);
324 0 : m_pGroupTLB->GetModel()->Resort();
325 0 : return 0;
326 : }
327 :
328 0 : IMPL_LINK_NOARG(SwGlossaryGroupDlg, ModifyHdl)
329 : {
330 0 : OUString sEntry(m_pNameED->GetText());
331 0 : bool bEnableNew = true;
332 0 : bool bEnableDel = false;
333 : sal_uLong nCaseReadonly =
334 0 : reinterpret_cast<sal_uLong>(m_pPathLB->GetSelectEntryData());
335 0 : bool bDirReadonly = 0 != (nCaseReadonly&PATH_READONLY);
336 :
337 0 : if(sEntry.isEmpty() || bDirReadonly)
338 0 : bEnableNew = false;
339 0 : else if(!sEntry.isEmpty())
340 : {
341 0 : sal_uLong nPos = m_pGroupTLB->GetEntryPos(sEntry, 0);
342 : //if it's not case sensitive you have to search for yourself
343 0 : if( 0xffffffff == nPos)
344 : {
345 0 : const ::utl::TransliterationWrapper& rSCmp = GetAppCmpStrIgnore();
346 0 : for(sal_uLong i = 0; i < m_pGroupTLB->GetEntryCount(); i++)
347 : {
348 0 : OUString sTemp = m_pGroupTLB->GetEntryText( i, 0 );
349 : nCaseReadonly = reinterpret_cast<sal_uLong>(m_pPathLB->GetEntryData(
350 0 : m_pPathLB->GetEntryPos(m_pGroupTLB->GetEntryText(i,1))));
351 0 : bool bCase = 0 != (nCaseReadonly & PATH_CASE_SENSITIVE);
352 :
353 0 : if( !bCase && rSCmp.isEqual( sTemp, sEntry ))
354 : {
355 0 : nPos = i;
356 0 : break;
357 : }
358 0 : }
359 : }
360 0 : if( 0xffffffff > nPos)
361 : {
362 0 : bEnableNew = false;
363 0 : m_pGroupTLB->Select(m_pGroupTLB->GetEntry( nPos ));
364 0 : m_pGroupTLB->MakeVisible(m_pGroupTLB->GetEntry( nPos ));
365 : }
366 : }
367 0 : SvTreeListEntry* pEntry = m_pGroupTLB->FirstSelected();
368 0 : if(pEntry)
369 : {
370 0 : GlosBibUserData* pUserData = static_cast<GlosBibUserData*>(pEntry->GetUserData());
371 0 : bEnableDel = IsDeleteAllowed(pUserData->sGroupName);
372 : }
373 :
374 0 : m_pDelPB->Enable(bEnableDel);
375 0 : m_pNewPB->Enable(bEnableNew);
376 0 : m_pRenamePB->Enable(bEnableNew && pEntry);
377 0 : return 0;
378 : }
379 :
380 0 : bool SwGlossaryGroupDlg::IsDeleteAllowed(const OUString &rGroup)
381 : {
382 0 : bool bDel = (!pGlosHdl->IsReadOnly(&rGroup));
383 :
384 : // OM: if the name is among the new region name, it is deletable
385 : // as well! Because for non existing region names ReadOnly issues
386 : // true.
387 :
388 0 : for (OUVector_t::const_iterator it(m_InsertedArr.begin());
389 0 : it != m_InsertedArr.end(); ++it)
390 : {
391 0 : if (*it == rGroup)
392 : {
393 0 : bDel = true;
394 0 : break;
395 : }
396 : }
397 :
398 0 : return bDel;
399 : }
400 :
401 0 : void FEdit::KeyInput( const KeyEvent& rKEvent )
402 : {
403 0 : vcl::KeyCode aCode = rKEvent.GetKeyCode();
404 0 : if( KEYGROUP_CURSOR == aCode.GetGroup() ||
405 0 : ( KEYGROUP_MISC == aCode.GetGroup() &&
406 0 : KEY_DELETE >= aCode.GetCode() ) ||
407 0 : SVT_SEARCHPATH_DELIMITER != rKEvent.GetCharCode() )
408 0 : Edit::KeyInput( rKEvent );
409 0 : }
410 :
411 0 : VCL_BUILDER_FACTORY(FEdit)
412 :
413 0 : void SwGlossaryGroupTLB::RequestHelp( const HelpEvent& rHEvt )
414 : {
415 0 : Point aPos( ScreenToOutputPixel( rHEvt.GetMousePosPixel() ));
416 0 : SvTreeListEntry* pEntry = GetEntry( aPos );
417 0 : if(pEntry)
418 : {
419 : SvLBoxTab* pTab;
420 0 : SvLBoxItem* pItem = GetItem( pEntry, aPos.X(), &pTab );
421 0 : if(pItem)
422 : {
423 0 : aPos = GetEntryPosition( pEntry );
424 0 : Size aSize(pItem->GetSize( this, pEntry ));
425 0 : aPos.X() = GetTabPos( pEntry, pTab );
426 :
427 0 : if((aPos.X() + aSize.Width()) > GetSizePixel().Width())
428 0 : aSize.Width() = GetSizePixel().Width() - aPos.X();
429 0 : aPos = OutputToScreenPixel(aPos);
430 0 : Rectangle aItemRect( aPos, aSize );
431 0 : GlosBibUserData* pData = static_cast<GlosBibUserData*>(pEntry->GetUserData());
432 0 : const OUString sMsg = pData->sPath + "/"
433 0 : + pData->sGroupName.getToken(0, GLOS_DELIM)
434 0 : + SwGlossaries::GetExtension();
435 :
436 : Help::ShowQuickHelp( this, aItemRect, sMsg,
437 0 : QuickHelpFlags::Left|QuickHelpFlags::VCenter );
438 : }
439 : }
440 0 : }
441 :
442 0 : VCL_BUILDER_FACTORY(SwGlossaryGroupTLB)
443 :
444 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|