1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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"
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>
40 #define PATH_CASE_SENSITIVE 0x01
41 #define PATH_READONLY 0x02
43 #define RENAME_TOKEN_DELIM (sal_Unicode)1
45 SwGlossaryGroupDlg::SwGlossaryGroupDlg(Window
* pParent
,
46 std::vector
<String
> const& rPathArr
, SwGlossaryHdl
*pHdl
)
47 : SvxStandardDialog(pParent
, "EditCategoriesDialog",
48 "modules/swriter/ui/editcategories.ui")
51 get(m_pPathLB
, "pathlb");
53 get(m_pDelPB
, "delete");
54 get(m_pRenamePB
, "rename");
55 get(m_pNameED
, "name");
56 get(m_pGroupTLB
, "group");
58 const int nAppFontUnits
= 130;
59 long nWidth
= LogicToPixel(Size(nAppFontUnits
, 0), MAP_APPFONT
).Width();
60 m_pPathLB
->set_width_request(nWidth
);
61 //just has to be something small, real size will be available space
62 m_pGroupTLB
->set_width_request(nWidth
);
65 { 2, // Number of Tabs
69 m_pGroupTLB
->SetTabs( &nTabs
[0], MAP_APPFONT
);
70 m_pGroupTLB
->SetSelectHdl(LINK(this, SwGlossaryGroupDlg
, SelectHdl
));
71 m_pGroupTLB
->GetModel()->SetSortMode(SortAscending
);
72 m_pNewPB
->SetClickHdl(LINK(this, SwGlossaryGroupDlg
, NewHdl
));
73 m_pDelPB
->SetClickHdl(LINK(this, SwGlossaryGroupDlg
, DeleteHdl
));
74 m_pNameED
->SetModifyHdl(LINK(this, SwGlossaryGroupDlg
, ModifyHdl
));
75 m_pPathLB
->SetSelectHdl(LINK(this, SwGlossaryGroupDlg
, ModifyHdl
));
76 m_pRenamePB
->SetClickHdl(LINK(this, SwGlossaryGroupDlg
, RenameHdl
));
78 for (size_t i
= 0; i
< rPathArr
.size(); ++i
)
80 String
sPath(rPathArr
[i
]);
81 INetURLObject
aTempURL(sPath
);
82 sPath
= aTempURL
.GetMainURL(INetURLObject::DECODE_WITH_CHARSET
);
83 m_pPathLB
->InsertEntry(sPath
);
84 sal_uLong nCaseReadonly
= 0;
85 utl::TempFile
aTempFile(&sPath
);
86 aTempFile
.EnableKillingFile();
87 if(!aTempFile
.IsValid())
88 nCaseReadonly
|= PATH_READONLY
;
89 else if( SWUnoHelper::UCB_IsCaseSensitiveFileName( aTempFile
.GetURL()))
90 nCaseReadonly
|= PATH_CASE_SENSITIVE
;
91 m_pPathLB
->SetEntryData(i
, (void*)nCaseReadonly
);
93 m_pPathLB
->SelectEntryPos(0);
94 m_pPathLB
->Enable(sal_True
);
96 const sal_uInt16 nCount
= pHdl
->GetGroupCnt();
97 for( sal_uInt16 i
= 0; i
< nCount
; ++i
)
100 String sGroup
= pHdl
->GetGroupName(i
, &sTitle
);
103 GlosBibUserData
* pData
= new GlosBibUserData
;
104 pData
->sGroupName
= sGroup
;
105 pData
->sGroupTitle
= sTitle
;
106 String
sTemp(sTitle
);
108 pData
->sPath
= m_pPathLB
->GetEntry((sal_uInt16
)sGroup
.GetToken(1, GLOS_DELIM
).ToInt32());
109 sTemp
+= pData
->sPath
;
110 SvTreeListEntry
* pEntry
= m_pGroupTLB
->InsertEntry(sTemp
);
111 pEntry
->SetUserData(pData
);
114 m_pGroupTLB
->GetModel()->Resort();
117 SwGlossaryGroupDlg::~SwGlossaryGroupDlg()
121 void SwGlossaryGroupDlg::Apply()
123 if(m_pNewPB
->IsEnabled())
126 String aActGroup
= SwGlossaryDlg::GetCurrGroup();
128 for (OUVector_t::const_iterator
it(m_RemovedArr
.begin());
129 it
!= m_RemovedArr
.end(); ++it
)
131 const String sDelGroup
=
132 ::comphelper::string::getToken(*it
, 0, '\t');
133 if( sDelGroup
== aActGroup
)
135 //when the current group is deleted, the current group has to be relocated
136 if(m_pGroupTLB
->GetEntryCount())
138 SvTreeListEntry
* pFirst
= m_pGroupTLB
->First();
139 GlosBibUserData
* pUserData
= (GlosBibUserData
*)pFirst
->GetUserData();
140 pGlosHdl
->SetCurGroup(pUserData
->sGroupName
);
143 String
sMsg(SW_RES(STR_QUERY_DELETE_GROUP1
));
144 String
sTitle( ::comphelper::string::getToken(*it
, 1, '\t') );
148 sDelGroup
.GetToken(1, GLOS_DELIM
);
149 sMsg
+= SW_RESSTR(STR_QUERY_DELETE_GROUP2
);
150 QueryBox
aQuery(this->GetParent(), WB_YES_NO
|WB_DEF_NO
, sMsg
);
151 if(RET_YES
== aQuery
.Execute())
152 pGlosHdl
->DelGroup( sDelGroup
);
155 //don't rename before there was one
156 for (OUVector_t::const_iterator
it(m_RenamedArr
.begin());
157 it
!= m_RenamedArr
.end(); ++it
)
160 ::comphelper::string::getToken(*it
, 0, RENAME_TOKEN_DELIM
));
162 ::comphelper::string::getToken(*it
, 1, RENAME_TOKEN_DELIM
));
163 OUString
const sTitle(
164 ::comphelper::string::getToken(*it
, 2, RENAME_TOKEN_DELIM
));
165 pGlosHdl
->RenameGroup(sOld
, sNew
, sTitle
);
166 if (it
== m_RenamedArr
.begin())
168 sCreatedGroup
= sNew
;
171 for (OUVector_t::const_iterator
it(m_InsertedArr
.begin());
172 it
!= m_InsertedArr
.end(); ++it
)
174 String sNewGroup
= *it
;
175 String sNewTitle
= sNewGroup
.GetToken(0, GLOS_DELIM
);
176 if( sNewGroup
!= aActGroup
)
178 pGlosHdl
->NewGroup(sNewGroup
, sNewTitle
);
179 if(!sCreatedGroup
.Len())
180 sCreatedGroup
= sNewGroup
;
185 IMPL_LINK( SwGlossaryGroupDlg
, SelectHdl
, SvTabListBox
*, EMPTYARG
)
187 m_pNewPB
->Enable(sal_False
);
188 SvTreeListEntry
* pFirstEntry
= m_pGroupTLB
->FirstSelected();
191 GlosBibUserData
* pUserData
= (GlosBibUserData
*)pFirstEntry
->GetUserData();
192 String
sEntry(pUserData
->sGroupName
);
193 String
sName(m_pNameED
->GetText());
194 bool bExists
= false;
195 sal_uLong nPos
= m_pGroupTLB
->GetEntryPos(sName
, 0);
196 if( 0xffffffff > nPos
)
198 SvTreeListEntry
* pEntry
= m_pGroupTLB
->GetEntry(nPos
);
199 GlosBibUserData
* pFoundData
= (GlosBibUserData
*)pEntry
->GetUserData();
200 String sGroup
= pFoundData
->sGroupName
;
201 bExists
= sGroup
== sEntry
;
204 m_pRenamePB
->Enable(!bExists
&& sName
.Len());
205 m_pDelPB
->Enable(IsDeleteAllowed(sEntry
));
210 IMPL_LINK_NOARG(SwGlossaryGroupDlg
, NewHdl
)
212 String
sGroup(m_pNameED
->GetText());
213 sGroup
+= GLOS_DELIM
;
214 sGroup
+= OUString::number(m_pPathLB
->GetSelectEntryPos());
215 OSL_ENSURE(!pGlosHdl
->FindGroupName(sGroup
), "group already available!");
216 m_InsertedArr
.push_back(sGroup
);
217 String
sTemp(m_pNameED
->GetText());
219 sTemp
+= m_pPathLB
->GetSelectEntry();
220 SvTreeListEntry
* pEntry
= m_pGroupTLB
->InsertEntry(sTemp
);
221 GlosBibUserData
* pData
= new GlosBibUserData
;
222 pData
->sPath
= m_pPathLB
->GetSelectEntry();
223 pData
->sGroupName
= sGroup
;
224 pData
->sGroupTitle
= m_pNameED
->GetText();
225 pEntry
->SetUserData(pData
);
226 m_pGroupTLB
->Select(pEntry
);
227 m_pGroupTLB
->MakeVisible(pEntry
);
228 m_pGroupTLB
->GetModel()->Resort();
233 IMPL_LINK( SwGlossaryGroupDlg
, DeleteHdl
, Button
*, pButton
)
235 SvTreeListEntry
* pEntry
= m_pGroupTLB
->FirstSelected();
238 pButton
->Enable(sal_False
);
241 GlosBibUserData
* pUserData
= (GlosBibUserData
*)pEntry
->GetUserData();
242 OUString
const sEntry(pUserData
->sGroupName
);
243 // if the name to be deleted is among the new ones - get rid of it
245 for (OUVector_t::iterator
it(m_InsertedArr
.begin());
246 it
!= m_InsertedArr
.end(); ++it
)
250 m_InsertedArr
.erase(it
);
256 // it should probably be renamed?
259 for (OUVector_t::iterator
it(m_RenamedArr
.begin());
260 it
!= m_RenamedArr
.end(); ++it
)
262 if (::comphelper::string::getToken(*it
, 0, RENAME_TOKEN_DELIM
)
265 m_RenamedArr
.erase(it
);
273 String
sGroupEntry(pUserData
->sGroupName
);
275 sGroupEntry
+= pUserData
->sGroupTitle
;
276 m_RemovedArr
.push_back(sGroupEntry
);
279 m_pGroupTLB
->GetModel()->Remove(pEntry
);
280 if(!m_pGroupTLB
->First())
281 pButton
->Enable(sal_False
);
282 //the content must be deleted - otherwise the new handler would be called in Apply()
283 m_pNameED
->SetText(aEmptyStr
);
287 IMPL_LINK_NOARG(SwGlossaryGroupDlg
, RenameHdl
)
289 SvTreeListEntry
* pEntry
= m_pGroupTLB
->FirstSelected();
290 GlosBibUserData
* pUserData
= (GlosBibUserData
*)pEntry
->GetUserData();
291 String
sEntry(pUserData
->sGroupName
);
293 String
sNewName(m_pNameED
->GetText());
294 String
sNewTitle(sNewName
);
296 sNewName
+= GLOS_DELIM
;
297 sNewName
+= OUString::number(m_pPathLB
->GetSelectEntryPos());
298 OSL_ENSURE(!pGlosHdl
->FindGroupName(sNewName
), "group already available!");
300 // if the name to be renamed is among the new ones - replace
302 for (OUVector_t::iterator
it(m_InsertedArr
.begin());
303 it
!= m_InsertedArr
.end(); ++it
)
305 if (String(*it
) == sEntry
)
307 m_InsertedArr
.erase(it
);
308 m_InsertedArr
.push_back(sNewName
);
315 sEntry
+= RENAME_TOKEN_DELIM
;
317 sEntry
+= RENAME_TOKEN_DELIM
;
319 m_RenamedArr
.push_back(sEntry
);
321 delete (GlosBibUserData
*)pEntry
->GetUserData();
322 m_pGroupTLB
->GetModel()->Remove(pEntry
);
323 String
sTemp(m_pNameED
->GetText());
325 sTemp
+= m_pPathLB
->GetSelectEntry();
326 pEntry
= m_pGroupTLB
->InsertEntry(sTemp
);
327 GlosBibUserData
* pData
= new GlosBibUserData
;
328 pData
->sPath
= m_pPathLB
->GetSelectEntry();
329 pData
->sGroupName
= sNewName
;
330 pData
->sGroupTitle
= sNewTitle
;
331 pEntry
->SetUserData(pData
);
332 m_pGroupTLB
->Select(pEntry
);
333 m_pGroupTLB
->MakeVisible(pEntry
);
334 m_pGroupTLB
->GetModel()->Resort();
338 IMPL_LINK_NOARG(SwGlossaryGroupDlg
, ModifyHdl
)
340 String
sEntry(m_pNameED
->GetText());
341 sal_Bool bEnableNew
= sal_True
;
342 sal_Bool bEnableDel
= sal_False
;
343 sal_uLong nCaseReadonly
=
344 (sal_uLong
)m_pPathLB
->GetEntryData(m_pPathLB
->GetSelectEntryPos());
345 bool bDirReadonly
= 0 != (nCaseReadonly
&PATH_READONLY
);
347 if(!sEntry
.Len() || bDirReadonly
)
348 bEnableNew
= sal_False
;
349 else if(sEntry
.Len())
351 sal_uLong nPos
= m_pGroupTLB
->GetEntryPos(sEntry
, 0);
352 //if it's not case sensitive you have to search for yourself
353 if( 0xffffffff == nPos
)
355 const ::utl::TransliterationWrapper
& rSCmp
= GetAppCmpStrIgnore();
356 for(sal_uInt16 i
= 0; i
< m_pGroupTLB
->GetEntryCount(); i
++)
358 String sTemp
= m_pGroupTLB
->GetEntryText( i
, 0 );
359 nCaseReadonly
= (sal_uLong
)m_pPathLB
->GetEntryData(
360 m_pPathLB
->GetEntryPos(m_pGroupTLB
->GetEntryText(i
,1)));
361 bool bCase
= 0 != (nCaseReadonly
& PATH_CASE_SENSITIVE
);
363 if( !bCase
&& rSCmp
.isEqual( sTemp
, sEntry
))
370 if( 0xffffffff > nPos
)
372 bEnableNew
= sal_False
;
373 m_pGroupTLB
->Select(m_pGroupTLB
->GetEntry( nPos
));
374 m_pGroupTLB
->MakeVisible(m_pGroupTLB
->GetEntry( nPos
));
377 SvTreeListEntry
* pEntry
= m_pGroupTLB
->FirstSelected();
380 GlosBibUserData
* pUserData
= (GlosBibUserData
*)pEntry
->GetUserData();
381 bEnableDel
= IsDeleteAllowed(pUserData
->sGroupName
);
384 m_pDelPB
->Enable(bEnableDel
);
385 m_pNewPB
->Enable(bEnableNew
);
386 m_pRenamePB
->Enable(bEnableNew
&& pEntry
);
390 sal_Bool
SwGlossaryGroupDlg::IsDeleteAllowed(const String
&rGroup
)
392 sal_Bool bDel
= (!pGlosHdl
->IsReadOnly(&rGroup
));
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
398 for (OUVector_t::const_iterator
it(m_InsertedArr
.begin());
399 it
!= m_InsertedArr
.end(); ++it
)
401 if (String(*it
) == rGroup
)
411 void FEdit::KeyInput( const KeyEvent
& rKEvent
)
413 KeyCode aCode
= rKEvent
.GetKeyCode();
414 if( KEYGROUP_CURSOR
== aCode
.GetGroup() ||
415 ( KEYGROUP_MISC
== aCode
.GetGroup() &&
416 KEY_DELETE
>= aCode
.GetCode() ) ||
417 SVT_SEARCHPATH_DELIMITER
!= rKEvent
.GetCharCode() )
418 Edit::KeyInput( rKEvent
);
421 extern "C" SAL_DLLPUBLIC_EXPORT Window
* SAL_CALL
makeFEdit(Window
*pParent
, VclBuilder::stringmap
&)
423 return new FEdit(pParent
);
426 void SwGlossaryGroupTLB::RequestHelp( const HelpEvent
& rHEvt
)
428 Point
aPos( ScreenToOutputPixel( rHEvt
.GetMousePosPixel() ));
429 SvTreeListEntry
* pEntry
= GetEntry( aPos
);
433 SvLBoxItem
* pItem
= GetItem( pEntry
, aPos
.X(), &pTab
);
436 aPos
= GetEntryPosition( pEntry
);
437 Size
aSize(pItem
->GetSize( this, pEntry
));
438 aPos
.X() = GetTabPos( pEntry
, pTab
);
440 if((aPos
.X() + aSize
.Width()) > GetSizePixel().Width())
441 aSize
.Width() = GetSizePixel().Width() - aPos
.X();
442 aPos
= OutputToScreenPixel(aPos
);
443 Rectangle
aItemRect( aPos
, aSize
);
445 GlosBibUserData
* pData
= (GlosBibUserData
*)pEntry
->GetUserData();
447 sMsg
+= INET_PATH_TOKEN
;
448 sMsg
+= pData
->sGroupName
.GetToken(0, GLOS_DELIM
);
449 sMsg
+= SwGlossaries::GetExtension();
451 Help::ShowQuickHelp( this, aItemRect
, sMsg
,
452 QUICKHELP_LEFT
|QUICKHELP_VCENTER
);
457 extern "C" SAL_DLLPUBLIC_EXPORT Window
* SAL_CALL
makeSwGlossaryGroupTLB(Window
*pParent
, VclBuilder::stringmap
&)
459 return new SwGlossaryGroupTLB(pParent
);
462 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */