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 <tools/urlobj.hxx>
21 #include <vcl/msgbox.hxx>
22 #include <sfx2/filedlghelper.hxx>
24 #include "multipat.hxx"
25 #include <dialmgr.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <comphelper/string.hxx>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
32 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
34 #include <unotools/localfilehelper.hxx>
35 #include <unotools/pathoptions.hxx>
36 #include "svtools/treelistentry.hxx"
38 using namespace ::com::sun::star::lang
;
39 using namespace ::com::sun::star::ui::dialogs
;
40 using namespace ::com::sun::star::uno
;
42 IMPL_LINK_NOARG(SvxMultiPathDialog
, SelectHdl_Impl
)
44 sal_uLong nCount
= m_pRadioLB
->GetEntryCount();
45 bool bIsSelected
= m_pRadioLB
->FirstSelected() != NULL
;
46 bool bEnable
= nCount
> 1;
47 m_pDelBtn
->Enable(bEnable
&& bIsSelected
);
51 IMPL_LINK_NOARG(SvxPathSelectDialog
, SelectHdl_Impl
)
53 sal_uLong nCount
= m_pPathLB
->GetEntryCount();
54 bool bIsSelected
= m_pPathLB
->GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND
;
55 bool bEnable
= nCount
> 1;
56 m_pDelBtn
->Enable(bEnable
&& bIsSelected
);
60 IMPL_LINK( SvxMultiPathDialog
, CheckHdl_Impl
, svx::SvxRadioButtonListBox
*, pBox
)
62 SvTreeListEntry
* pEntry
=
63 pBox
? pBox
->GetEntry( pBox
->GetCurMousePoint() ) : m_pRadioLB
->FirstSelected();
65 m_pRadioLB
->HandleEntryChecked( pEntry
);
69 IMPL_LINK_NOARG(SvxMultiPathDialog
, AddHdl_Impl
)
71 Reference
< XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
72 Reference
< XFolderPicker2
> xFolderPicker
= FolderPicker::create(xContext
);
74 if ( xFolderPicker
->execute() == ExecutableDialogResults::OK
)
76 INetURLObject
aPath( xFolderPicker
->getDirectory() );
77 aPath
.removeFinalSlash();
78 OUString aURL
= aPath
.GetMainURL( INetURLObject::NO_DECODE
);
80 ::utl::LocalFileHelper::ConvertURLToSystemPath( aURL
, sInsPath
);
82 sal_uLong nPos
= m_pRadioLB
->GetEntryPos( sInsPath
, 1 );
83 if ( 0xffffffff == nPos
) //See svtools/source/contnr/svtabbx.cxx SvTabListBox::GetEntryPos
85 OUString
sNewEntry( '\t' );
86 sNewEntry
+= sInsPath
;
87 SvTreeListEntry
* pEntry
= m_pRadioLB
->InsertEntry( sNewEntry
);
88 OUString
* pData
= new OUString( aURL
);
89 pEntry
->SetUserData( pData
);
93 OUString
sMsg( CUI_RES( RID_MULTIPATH_DBL_ERR
) );
94 sMsg
= sMsg
.replaceFirst( "%1", sInsPath
);
95 ScopedVclPtr
<InfoBox
>::Create( this, sMsg
)->Execute();
98 SelectHdl_Impl( NULL
);
103 IMPL_LINK_NOARG(SvxPathSelectDialog
, AddHdl_Impl
)
105 Reference
< XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
106 Reference
< XFolderPicker2
> xFolderPicker
= FolderPicker::create(xContext
);
108 if ( xFolderPicker
->execute() == ExecutableDialogResults::OK
)
110 INetURLObject
aPath( xFolderPicker
->getDirectory() );
111 aPath
.removeFinalSlash();
112 OUString aURL
= aPath
.GetMainURL( INetURLObject::NO_DECODE
);
114 ::utl::LocalFileHelper::ConvertURLToSystemPath( aURL
, sInsPath
);
116 if ( LISTBOX_ENTRY_NOTFOUND
!= m_pPathLB
->GetEntryPos( sInsPath
) )
118 OUString
sMsg( CUI_RES( RID_MULTIPATH_DBL_ERR
) );
119 sMsg
= sMsg
.replaceFirst( "%1", sInsPath
);
120 ScopedVclPtr
<InfoBox
>::Create( this, sMsg
)->Execute();
124 sal_uInt16 nPos
= m_pPathLB
->InsertEntry( sInsPath
, LISTBOX_APPEND
);
125 m_pPathLB
->SetEntryData( nPos
, new OUString( aURL
) );
128 SelectHdl_Impl( NULL
);
133 IMPL_LINK_NOARG(SvxMultiPathDialog
, DelHdl_Impl
)
135 SvTreeListEntry
* pEntry
= m_pRadioLB
->FirstSelected();
136 delete static_cast<OUString
*>(pEntry
->GetUserData());
137 bool bChecked
= m_pRadioLB
->GetCheckButtonState( pEntry
) == SV_BUTTON_CHECKED
;
138 sal_uLong nPos
= m_pRadioLB
->GetEntryPos( pEntry
);
139 m_pRadioLB
->RemoveEntry( pEntry
);
140 sal_uLong nCnt
= m_pRadioLB
->GetEntryCount();
146 pEntry
= m_pRadioLB
->GetEntry( nPos
);
149 m_pRadioLB
->SetCheckButtonState( pEntry
, SV_BUTTON_CHECKED
);
150 m_pRadioLB
->HandleEntryChecked( pEntry
);
153 m_pRadioLB
->Select( pEntry
);
156 SelectHdl_Impl( NULL
);
160 IMPL_LINK_NOARG(SvxPathSelectDialog
, DelHdl_Impl
)
162 sal_uInt16 nPos
= m_pPathLB
->GetSelectEntryPos();
163 m_pPathLB
->RemoveEntry( nPos
);
164 sal_uInt16 nCnt
= m_pPathLB
->GetEntryCount();
172 m_pPathLB
->SelectEntryPos( nPos
);
175 SelectHdl_Impl( NULL
);
179 SvxMultiPathDialog::SvxMultiPathDialog(vcl::Window
* pParent
)
180 : ModalDialog(pParent
, "MultiPathDialog", "cui/ui/multipathdialog.ui")
182 get(m_pAddBtn
, "add");
183 get(m_pDelBtn
, "delete");
185 SvSimpleTableContainer
* pRadioLBContainer
= get
<SvSimpleTableContainer
>("paths");
186 Size
aSize(LogicToPixel(Size(195, 77), MAP_APPFONT
));
187 pRadioLBContainer
->set_width_request(aSize
.Width());
188 pRadioLBContainer
->set_height_request(aSize
.Height());
189 m_pRadioLB
= VclPtr
<svx::SvxRadioButtonListBox
>::Create(*pRadioLBContainer
, 0);
191 static long aStaticTabs
[]= { 2, 0, 12 };
192 m_pRadioLB
->SvSimpleTable::SetTabs( aStaticTabs
);
193 OUString
sHeader(get
<FixedText
>("pathlist")->GetText());
194 m_pRadioLB
->SetQuickHelpText( sHeader
);
195 sHeader
= "\t" + sHeader
;
196 m_pRadioLB
->InsertHeaderEntry( sHeader
, HEADERBAR_APPEND
, HeaderBarItemBits::LEFT
);
198 m_pRadioLB
->SetSelectHdl( LINK( this, SvxMultiPathDialog
, SelectHdl_Impl
) );
199 m_pRadioLB
->SetCheckButtonHdl( LINK( this, SvxMultiPathDialog
, CheckHdl_Impl
) );
200 m_pAddBtn
->SetClickHdl( LINK( this, SvxMultiPathDialog
, AddHdl_Impl
) );
201 m_pDelBtn
->SetClickHdl( LINK( this, SvxMultiPathDialog
, DelHdl_Impl
) );
203 SelectHdl_Impl( NULL
);
205 m_pRadioLB
->ShowTable();
208 SvxPathSelectDialog::SvxPathSelectDialog(vcl::Window
* pParent
)
209 : ModalDialog(pParent
, "SelectPathDialog", "cui/ui/selectpathdialog.ui")
211 get(m_pAddBtn
, "add");
212 get(m_pDelBtn
, "delete");
213 get(m_pPathLB
, "paths");
214 Size
aSize(LogicToPixel(Size(189, 80), MAP_APPFONT
));
215 m_pPathLB
->set_width_request(aSize
.Width());
216 m_pPathLB
->set_height_request(aSize
.Height());
218 m_pPathLB
->SetSelectHdl( LINK( this, SvxPathSelectDialog
, SelectHdl_Impl
) );
219 m_pAddBtn
->SetClickHdl( LINK( this, SvxPathSelectDialog
, AddHdl_Impl
) );
220 m_pDelBtn
->SetClickHdl( LINK( this, SvxPathSelectDialog
, DelHdl_Impl
) );
222 SelectHdl_Impl( NULL
);
225 SvxMultiPathDialog::~SvxMultiPathDialog()
230 void SvxMultiPathDialog::dispose()
234 sal_uInt16 nPos
= (sal_uInt16
)m_pRadioLB
->GetEntryCount();
237 SvTreeListEntry
* pEntry
= m_pRadioLB
->GetEntry( nPos
);
238 delete static_cast<OUString
*>(pEntry
->GetUserData());
242 m_pRadioLB
.disposeAndClear();
245 ModalDialog::dispose();
248 SvxPathSelectDialog::~SvxPathSelectDialog()
253 void SvxPathSelectDialog::dispose()
257 sal_uInt16 nPos
= m_pPathLB
->GetEntryCount();
259 delete static_cast<OUString
*>(m_pPathLB
->GetEntryData(nPos
));
264 ModalDialog::dispose();
267 OUString
SvxMultiPathDialog::GetPath() const
270 sal_Unicode cDelim
= SVT_SEARCHPATH_DELIMITER
;
273 for ( sal_uInt16 i
= 0; i
< m_pRadioLB
->GetEntryCount(); ++i
)
275 SvTreeListEntry
* pEntry
= m_pRadioLB
->GetEntry(i
);
276 if ( m_pRadioLB
->GetCheckButtonState( pEntry
) == SV_BUTTON_CHECKED
)
277 sWritable
= *static_cast<OUString
*>(pEntry
->GetUserData());
280 if ( !sNewPath
.isEmpty() )
281 sNewPath
+= OUString(cDelim
);
282 sNewPath
+= *static_cast<OUString
*>(pEntry
->GetUserData());
285 if ( !sNewPath
.isEmpty() )
286 sNewPath
+= OUString(cDelim
);
287 sNewPath
+= sWritable
;
292 OUString
SvxPathSelectDialog::GetPath() const
295 sal_Unicode cDelim
= SVT_SEARCHPATH_DELIMITER
;
297 for ( sal_uInt16 i
= 0; i
< m_pPathLB
->GetEntryCount(); ++i
)
299 if ( !sNewPath
.isEmpty() )
300 sNewPath
+= OUString(cDelim
);
301 sNewPath
+= *static_cast<OUString
*>(m_pPathLB
->GetEntryData(i
));
307 void SvxMultiPathDialog::SetPath( const OUString
& rPath
)
309 sal_Unicode cDelim
= SVT_SEARCHPATH_DELIMITER
;
310 sal_uInt16 nCount
= comphelper::string::getTokenCount(rPath
, cDelim
);
312 for ( sal_uInt16 i
= 0; i
< nCount
; ++i
)
314 OUString sPath
= rPath
.getToken( i
, cDelim
);
315 OUString sSystemPath
;
317 ::utl::LocalFileHelper::ConvertURLToSystemPath( sPath
, sSystemPath
);
319 OUString
sEntry( '\t' );
320 sEntry
+= (bIsSystemPath
? sSystemPath
: OUString(sPath
));
321 SvTreeListEntry
* pEntry
= m_pRadioLB
->InsertEntry( sEntry
);
322 OUString
* pURL
= new OUString( sPath
);
323 pEntry
->SetUserData( pURL
);
328 SvTreeListEntry
* pEntry
= m_pRadioLB
->GetEntry( nCount
- 1 );
331 m_pRadioLB
->SetCheckButtonState( pEntry
, SV_BUTTON_CHECKED
);
332 m_pRadioLB
->HandleEntryChecked( pEntry
);
336 SelectHdl_Impl( NULL
);
339 void SvxPathSelectDialog::SetPath(const OUString
& rPath
)
341 sal_Unicode cDelim
= SVT_SEARCHPATH_DELIMITER
;
342 sal_uInt16 nPos
, nCount
= comphelper::string::getTokenCount(rPath
, cDelim
);
344 for ( sal_uInt16 i
= 0; i
< nCount
; ++i
)
346 OUString sPath
= rPath
.getToken( i
, cDelim
);
347 OUString sSystemPath
;
349 ::utl::LocalFileHelper::ConvertURLToSystemPath( sPath
, sSystemPath
);
352 nPos
= m_pPathLB
->InsertEntry( sSystemPath
, LISTBOX_APPEND
);
354 nPos
= m_pPathLB
->InsertEntry( sPath
, LISTBOX_APPEND
);
355 m_pPathLB
->SetEntryData( nPos
, new OUString( sPath
) );
358 SelectHdl_Impl( NULL
);
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */