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