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 <comphelper/string.hxx>
21 : #include <tools/urlobj.hxx>
22 : #include <tools/stream.hxx>
23 : #include <vcl/msgbox.hxx>
24 : #include <vcl/help.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(Window * pParent,
46 : std::vector<String> 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 :
64 : long nTabs[] =
65 : { 2, // Number of Tabs
66 : 0, nAppFontUnits
67 0 : };
68 :
69 0 : m_pGroupTLB->SetTabs( &nTabs[0], MAP_APPFONT );
70 0 : m_pGroupTLB->SetSelectHdl(LINK(this, SwGlossaryGroupDlg, SelectHdl));
71 0 : m_pGroupTLB->GetModel()->SetSortMode(SortAscending);
72 0 : m_pNewPB->SetClickHdl(LINK(this, SwGlossaryGroupDlg, NewHdl));
73 0 : m_pDelPB->SetClickHdl(LINK(this, SwGlossaryGroupDlg, DeleteHdl));
74 0 : m_pNameED->SetModifyHdl(LINK(this, SwGlossaryGroupDlg, ModifyHdl));
75 0 : m_pPathLB->SetSelectHdl(LINK(this, SwGlossaryGroupDlg, ModifyHdl));
76 0 : m_pRenamePB->SetClickHdl(LINK(this, SwGlossaryGroupDlg, RenameHdl));
77 :
78 0 : for (size_t i = 0; i < rPathArr.size(); ++i)
79 : {
80 0 : OUString sPath(rPathArr[i]);
81 0 : INetURLObject aTempURL(sPath);
82 0 : 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, (void*)nCaseReadonly);
92 0 : }
93 0 : m_pPathLB->SelectEntryPos(0);
94 0 : m_pPathLB->Enable(sal_True);
95 :
96 0 : const sal_uInt16 nCount = pHdl->GetGroupCnt();
97 0 : for( sal_uInt16 i = 0; i < nCount; ++i)
98 : {
99 0 : String sTitle;
100 0 : String sGroup = pHdl->GetGroupName(i, &sTitle);
101 0 : if(!sGroup.Len())
102 0 : continue;
103 0 : GlosBibUserData* pData = new GlosBibUserData;
104 0 : pData->sGroupName = sGroup;
105 0 : pData->sGroupTitle = sTitle;
106 0 : String sTemp(sTitle);
107 0 : sTemp += '\t';
108 0 : pData->sPath = m_pPathLB->GetEntry((sal_uInt16)sGroup.GetToken(1, GLOS_DELIM).ToInt32());
109 0 : sTemp += pData->sPath;
110 0 : SvTreeListEntry* pEntry = m_pGroupTLB->InsertEntry(sTemp);
111 0 : pEntry->SetUserData(pData);
112 :
113 0 : }
114 0 : m_pGroupTLB->GetModel()->Resort();
115 0 : }
116 :
117 0 : SwGlossaryGroupDlg::~SwGlossaryGroupDlg()
118 : {
119 0 : }
120 :
121 0 : void SwGlossaryGroupDlg::Apply()
122 : {
123 0 : if(m_pNewPB->IsEnabled())
124 0 : NewHdl(m_pNewPB);
125 :
126 0 : String aActGroup = SwGlossaryDlg::GetCurrGroup();
127 :
128 0 : for (OUVector_t::const_iterator it(m_RemovedArr.begin());
129 0 : it != m_RemovedArr.end(); ++it)
130 : {
131 : const String sDelGroup =
132 0 : ::comphelper::string::getToken(*it, 0, '\t');
133 0 : if( sDelGroup == aActGroup )
134 : {
135 : //when the current group is deleted, the current group has to be relocated
136 0 : if(m_pGroupTLB->GetEntryCount())
137 : {
138 0 : SvTreeListEntry* pFirst = m_pGroupTLB->First();
139 0 : GlosBibUserData* pUserData = (GlosBibUserData*)pFirst->GetUserData();
140 0 : pGlosHdl->SetCurGroup(pUserData->sGroupName);
141 : }
142 : }
143 0 : String sMsg(SW_RES(STR_QUERY_DELETE_GROUP1));
144 0 : String sTitle( ::comphelper::string::getToken(*it, 1, '\t') );
145 0 : if(sTitle.Len())
146 0 : sMsg += sTitle;
147 : else
148 0 : sDelGroup.GetToken(1, GLOS_DELIM);
149 0 : sMsg += SW_RESSTR(STR_QUERY_DELETE_GROUP2);
150 0 : QueryBox aQuery(this->GetParent(), WB_YES_NO|WB_DEF_NO, sMsg );
151 0 : if(RET_YES == aQuery.Execute())
152 0 : pGlosHdl->DelGroup( sDelGroup );
153 0 : }
154 :
155 : //don't rename before there was one
156 0 : for (OUVector_t::const_iterator it(m_RenamedArr.begin());
157 0 : it != m_RenamedArr.end(); ++it)
158 : {
159 : OUString const sOld(
160 0 : ::comphelper::string::getToken(*it, 0, RENAME_TOKEN_DELIM));
161 : String sNew(
162 0 : ::comphelper::string::getToken(*it, 1, RENAME_TOKEN_DELIM));
163 : OUString const sTitle(
164 0 : ::comphelper::string::getToken(*it, 2, RENAME_TOKEN_DELIM));
165 0 : pGlosHdl->RenameGroup(sOld, sNew, sTitle);
166 0 : if (it == m_RenamedArr.begin())
167 : {
168 0 : sCreatedGroup = sNew;
169 : }
170 0 : }
171 0 : for (OUVector_t::const_iterator it(m_InsertedArr.begin());
172 0 : it != m_InsertedArr.end(); ++it)
173 : {
174 0 : String sNewGroup = *it;
175 0 : String sNewTitle = sNewGroup.GetToken(0, GLOS_DELIM);
176 0 : if( sNewGroup != aActGroup )
177 : {
178 0 : pGlosHdl->NewGroup(sNewGroup, sNewTitle);
179 0 : if(!sCreatedGroup.Len())
180 0 : sCreatedGroup = sNewGroup;
181 : }
182 0 : }
183 0 : }
184 :
185 0 : IMPL_LINK( SwGlossaryGroupDlg, SelectHdl, SvTabListBox*, EMPTYARG )
186 : {
187 0 : m_pNewPB->Enable(sal_False);
188 0 : SvTreeListEntry* pFirstEntry = m_pGroupTLB->FirstSelected();
189 0 : if(pFirstEntry)
190 : {
191 0 : GlosBibUserData* pUserData = (GlosBibUserData*)pFirstEntry->GetUserData();
192 0 : String sEntry(pUserData->sGroupName);
193 0 : String sName(m_pNameED->GetText());
194 0 : bool bExists = false;
195 0 : sal_uLong nPos = m_pGroupTLB->GetEntryPos(sName, 0);
196 0 : if( 0xffffffff > nPos)
197 : {
198 0 : SvTreeListEntry* pEntry = m_pGroupTLB->GetEntry(nPos);
199 0 : GlosBibUserData* pFoundData = (GlosBibUserData*)pEntry->GetUserData();
200 0 : String sGroup = pFoundData->sGroupName;
201 0 : bExists = sGroup == sEntry;
202 : }
203 :
204 0 : m_pRenamePB->Enable(!bExists && sName.Len());
205 0 : m_pDelPB->Enable(IsDeleteAllowed(sEntry));
206 : }
207 0 : return 0;
208 : }
209 :
210 0 : IMPL_LINK_NOARG(SwGlossaryGroupDlg, NewHdl)
211 : {
212 0 : String sGroup(m_pNameED->GetText());
213 0 : sGroup += GLOS_DELIM;
214 0 : sGroup += OUString::number(m_pPathLB->GetSelectEntryPos());
215 : OSL_ENSURE(!pGlosHdl->FindGroupName(sGroup), "group already available!");
216 0 : m_InsertedArr.push_back(sGroup);
217 0 : String sTemp(m_pNameED->GetText());
218 0 : sTemp += '\t';
219 0 : sTemp += m_pPathLB->GetSelectEntry();
220 0 : SvTreeListEntry* pEntry = m_pGroupTLB->InsertEntry(sTemp);
221 0 : GlosBibUserData* pData = new GlosBibUserData;
222 0 : pData->sPath = m_pPathLB->GetSelectEntry();
223 0 : pData->sGroupName = sGroup;
224 0 : pData->sGroupTitle = m_pNameED->GetText();
225 0 : pEntry->SetUserData(pData);
226 0 : m_pGroupTLB->Select(pEntry);
227 0 : m_pGroupTLB->MakeVisible(pEntry);
228 0 : m_pGroupTLB->GetModel()->Resort();
229 :
230 0 : return 0;
231 : }
232 :
233 0 : IMPL_LINK( SwGlossaryGroupDlg, DeleteHdl, Button*, pButton )
234 : {
235 0 : SvTreeListEntry* pEntry = m_pGroupTLB->FirstSelected();
236 0 : if(!pEntry)
237 : {
238 0 : pButton->Enable(sal_False);
239 0 : return 0;
240 : }
241 0 : GlosBibUserData* pUserData = (GlosBibUserData*)pEntry->GetUserData();
242 0 : OUString const sEntry(pUserData->sGroupName);
243 : // if the name to be deleted is among the new ones - get rid of it
244 0 : bool bDelete = true;
245 0 : for (OUVector_t::iterator it(m_InsertedArr.begin());
246 0 : it != m_InsertedArr.end(); ++it)
247 : {
248 0 : if (*it == sEntry)
249 : {
250 0 : m_InsertedArr.erase(it);
251 0 : bDelete = false;
252 0 : break;
253 : }
254 :
255 : }
256 : // it should probably be renamed?
257 0 : if(bDelete)
258 : {
259 0 : for (OUVector_t::iterator it(m_RenamedArr.begin());
260 0 : it != m_RenamedArr.end(); ++it)
261 : {
262 0 : if (::comphelper::string::getToken(*it, 0, RENAME_TOKEN_DELIM)
263 0 : == sEntry)
264 : {
265 0 : m_RenamedArr.erase(it);
266 0 : bDelete = false;
267 0 : break;
268 : }
269 : }
270 : }
271 0 : if(bDelete)
272 : {
273 0 : String sGroupEntry(pUserData->sGroupName);
274 0 : sGroupEntry += '\t';
275 0 : sGroupEntry += pUserData->sGroupTitle;
276 0 : m_RemovedArr.push_back(sGroupEntry);
277 : }
278 0 : delete pUserData;
279 0 : m_pGroupTLB->GetModel()->Remove(pEntry);
280 0 : if(!m_pGroupTLB->First())
281 0 : pButton->Enable(sal_False);
282 : //the content must be deleted - otherwise the new handler would be called in Apply()
283 0 : m_pNameED->SetText(aEmptyStr);
284 0 : return 0;
285 : }
286 :
287 0 : IMPL_LINK_NOARG(SwGlossaryGroupDlg, RenameHdl)
288 : {
289 0 : SvTreeListEntry* pEntry = m_pGroupTLB->FirstSelected();
290 0 : GlosBibUserData* pUserData = (GlosBibUserData*)pEntry->GetUserData();
291 0 : String sEntry(pUserData->sGroupName);
292 :
293 0 : String sNewName(m_pNameED->GetText());
294 0 : String sNewTitle(sNewName);
295 :
296 0 : sNewName += GLOS_DELIM;
297 0 : sNewName += OUString::number(m_pPathLB->GetSelectEntryPos());
298 : OSL_ENSURE(!pGlosHdl->FindGroupName(sNewName), "group already available!");
299 :
300 : // if the name to be renamed is among the new ones - replace
301 0 : bool bDone = false;
302 0 : for (OUVector_t::iterator it(m_InsertedArr.begin());
303 0 : it != m_InsertedArr.end(); ++it)
304 : {
305 0 : if (String(*it) == sEntry)
306 : {
307 0 : m_InsertedArr.erase(it);
308 0 : m_InsertedArr.push_back(sNewName);
309 0 : bDone = true;
310 0 : break;
311 : }
312 : }
313 0 : if(!bDone)
314 : {
315 0 : sEntry += RENAME_TOKEN_DELIM;
316 0 : sEntry += sNewName;
317 0 : sEntry += RENAME_TOKEN_DELIM;
318 0 : sEntry += sNewTitle;
319 0 : m_RenamedArr.push_back(sEntry);
320 : }
321 0 : delete (GlosBibUserData*)pEntry->GetUserData();
322 0 : m_pGroupTLB->GetModel()->Remove(pEntry);
323 0 : String sTemp(m_pNameED->GetText());
324 0 : sTemp += '\t';
325 0 : sTemp += m_pPathLB->GetSelectEntry();
326 0 : pEntry = m_pGroupTLB->InsertEntry(sTemp);
327 0 : GlosBibUserData* pData = new GlosBibUserData;
328 0 : pData->sPath = m_pPathLB->GetSelectEntry();
329 0 : pData->sGroupName = sNewName;
330 0 : pData->sGroupTitle = sNewTitle;
331 0 : pEntry->SetUserData(pData);
332 0 : m_pGroupTLB->Select(pEntry);
333 0 : m_pGroupTLB->MakeVisible(pEntry);
334 0 : m_pGroupTLB->GetModel()->Resort();
335 0 : return 0;
336 : }
337 :
338 0 : IMPL_LINK_NOARG(SwGlossaryGroupDlg, ModifyHdl)
339 : {
340 0 : String sEntry(m_pNameED->GetText());
341 0 : sal_Bool bEnableNew = sal_True;
342 0 : sal_Bool bEnableDel = sal_False;
343 : sal_uLong nCaseReadonly =
344 0 : (sal_uLong)m_pPathLB->GetEntryData(m_pPathLB->GetSelectEntryPos());
345 0 : bool bDirReadonly = 0 != (nCaseReadonly&PATH_READONLY);
346 :
347 0 : if(!sEntry.Len() || bDirReadonly)
348 0 : bEnableNew = sal_False;
349 0 : else if(sEntry.Len())
350 : {
351 0 : sal_uLong nPos = m_pGroupTLB->GetEntryPos(sEntry, 0);
352 : //if it's not case sensitive you have to search for yourself
353 0 : if( 0xffffffff == nPos)
354 : {
355 0 : const ::utl::TransliterationWrapper& rSCmp = GetAppCmpStrIgnore();
356 0 : for(sal_uInt16 i = 0; i < m_pGroupTLB->GetEntryCount(); i++)
357 : {
358 0 : String sTemp = m_pGroupTLB->GetEntryText( i, 0 );
359 : nCaseReadonly = (sal_uLong)m_pPathLB->GetEntryData(
360 0 : m_pPathLB->GetEntryPos(m_pGroupTLB->GetEntryText(i,1)));
361 0 : bool bCase = 0 != (nCaseReadonly & PATH_CASE_SENSITIVE);
362 :
363 0 : if( !bCase && rSCmp.isEqual( sTemp, sEntry ))
364 : {
365 0 : nPos = i;
366 0 : break;
367 : }
368 0 : }
369 : }
370 0 : if( 0xffffffff > nPos)
371 : {
372 0 : bEnableNew = sal_False;
373 0 : m_pGroupTLB->Select(m_pGroupTLB->GetEntry( nPos ));
374 0 : m_pGroupTLB->MakeVisible(m_pGroupTLB->GetEntry( nPos ));
375 : }
376 : }
377 0 : SvTreeListEntry* pEntry = m_pGroupTLB->FirstSelected();
378 0 : if(pEntry)
379 : {
380 0 : GlosBibUserData* pUserData = (GlosBibUserData*)pEntry->GetUserData();
381 0 : bEnableDel = IsDeleteAllowed(pUserData->sGroupName);
382 : }
383 :
384 0 : m_pDelPB->Enable(bEnableDel);
385 0 : m_pNewPB->Enable(bEnableNew);
386 0 : m_pRenamePB->Enable(bEnableNew && pEntry);
387 0 : return 0;
388 : }
389 :
390 0 : sal_Bool SwGlossaryGroupDlg::IsDeleteAllowed(const String &rGroup)
391 : {
392 0 : sal_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 : // sal_True.
397 :
398 0 : for (OUVector_t::const_iterator it(m_InsertedArr.begin());
399 0 : it != m_InsertedArr.end(); ++it)
400 : {
401 0 : if (String(*it) == rGroup)
402 : {
403 0 : bDel = sal_True;
404 0 : break;
405 : }
406 : }
407 :
408 0 : return bDel;
409 : }
410 :
411 0 : void FEdit::KeyInput( const KeyEvent& rKEvent )
412 : {
413 0 : KeyCode aCode = rKEvent.GetKeyCode();
414 0 : if( KEYGROUP_CURSOR == aCode.GetGroup() ||
415 0 : ( KEYGROUP_MISC == aCode.GetGroup() &&
416 0 : KEY_DELETE >= aCode.GetCode() ) ||
417 0 : SVT_SEARCHPATH_DELIMITER != rKEvent.GetCharCode() )
418 0 : Edit::KeyInput( rKEvent );
419 0 : }
420 :
421 0 : extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeFEdit(Window *pParent, VclBuilder::stringmap &)
422 : {
423 0 : return new FEdit(pParent);
424 : }
425 :
426 0 : void SwGlossaryGroupTLB::RequestHelp( const HelpEvent& rHEvt )
427 : {
428 0 : Point aPos( ScreenToOutputPixel( rHEvt.GetMousePosPixel() ));
429 0 : SvTreeListEntry* pEntry = GetEntry( aPos );
430 0 : if(pEntry)
431 : {
432 : SvLBoxTab* pTab;
433 0 : SvLBoxItem* pItem = GetItem( pEntry, aPos.X(), &pTab );
434 0 : if(pItem)
435 : {
436 0 : aPos = GetEntryPosition( pEntry );
437 0 : Size aSize(pItem->GetSize( this, pEntry ));
438 0 : aPos.X() = GetTabPos( pEntry, pTab );
439 :
440 0 : if((aPos.X() + aSize.Width()) > GetSizePixel().Width())
441 0 : aSize.Width() = GetSizePixel().Width() - aPos.X();
442 0 : aPos = OutputToScreenPixel(aPos);
443 0 : Rectangle aItemRect( aPos, aSize );
444 0 : String sMsg;
445 0 : GlosBibUserData* pData = (GlosBibUserData*)pEntry->GetUserData();
446 0 : sMsg = pData->sPath;
447 0 : sMsg += INET_PATH_TOKEN;
448 0 : sMsg += pData->sGroupName.GetToken(0, GLOS_DELIM);
449 0 : sMsg += SwGlossaries::GetExtension();
450 :
451 : Help::ShowQuickHelp( this, aItemRect, sMsg,
452 0 : QUICKHELP_LEFT|QUICKHELP_VCENTER );
453 : }
454 : }
455 0 : }
456 :
457 0 : extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeSwGlossaryGroupTLB(Window *pParent, VclBuilder::stringmap &)
458 : {
459 0 : return new SwGlossaryGroupTLB(pParent);
460 0 : }
461 :
462 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|