1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_fpicker.hxx"
30 
31 #include "OfficeFolderPicker.hxx"
32 
33 #include "iodlg.hxx"
34 
35 #include <list>
36 #include <tools/urlobj.hxx>
37 
38 #define _SVSTDARR_STRINGSDTOR
39 #include "svl/svstdarr.hxx"
40 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
41 #include <com/sun/star/container/XSet.hpp>
42 #include <com/sun/star/uno/Any.hxx>
43 #include <cppuhelper/factory.hxx>
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 #include <unotools/pathoptions.hxx>
46 
47 // using ----------------------------------------------------------------
48 
49 using namespace     ::com::sun::star::container;
50 using namespace     ::com::sun::star::lang;
51 using namespace     ::com::sun::star::uno;
52 using namespace     ::com::sun::star::beans;
53 
54 //------------------------------------------------------------------------------------
55 // class SvtFolderPicker
56 //------------------------------------------------------------------------------------
57 
58 //------------------------------------------------------------------------------------
59 SvtFolderPicker::SvtFolderPicker( const Reference < XMultiServiceFactory >& _rxFactory )
60 	:OCommonPicker( _rxFactory )
61 {
62 }
63 
64 //------------------------------------------------------------------------------------
65 SvtFolderPicker::~SvtFolderPicker()
66 {
67 }
68 
69 //------------------------------------------------------------------------------------
70 // disambiguate XInterface
71 //------------------------------------------------------------------------------------
72 IMPLEMENT_FORWARD_XINTERFACE2( SvtFolderPicker, OCommonPicker, SvtFolderPicker_Base )
73 
74 //------------------------------------------------------------------------------------
75 // disambiguate XTypeProvider
76 //------------------------------------------------------------------------------------
77 IMPLEMENT_FORWARD_XTYPEPROVIDER2( SvtFolderPicker, OCommonPicker, SvtFolderPicker_Base )
78 
79 //------------------------------------------------------------------------------------
80 // XExecutableDialog functions
81 //------------------------------------------------------------------------------------
82 
83 //------------------------------------------------------------------------------------
84 void SAL_CALL SvtFolderPicker::setTitle( const ::rtl::OUString& _rTitle ) throw (RuntimeException)
85 {
86 	OCommonPicker::setTitle( _rTitle );
87 }
88 
89 //------------------------------------------------------------------------------------
90 sal_Int16 SAL_CALL SvtFolderPicker::execute(  ) throw (RuntimeException)
91 {
92 	return OCommonPicker::execute();
93 }
94 
95 //------------------------------------------------------------------------------------
96 // XAsynchronousExecutableDialog functions
97 //------------------------------------------------------------------------------------
98 
99 //------------------------------------------------------------------------------------
100 void SAL_CALL SvtFolderPicker::setDialogTitle( const ::rtl::OUString& _rTitle) throw (RuntimeException)
101 {
102     setTitle( _rTitle );
103 }
104 
105 //------------------------------------------------------------------------------------
106 void SAL_CALL SvtFolderPicker::startExecuteModal( const Reference< ::com::sun::star::ui::dialogs::XDialogClosedListener >& xListener ) throw (RuntimeException)
107 {
108     m_xListener = xListener;
109     prepareDialog();
110     prepareExecute();
111     getDialog()->EnableAutocompletion( sal_True );
112     getDialog()->StartExecuteModal( LINK( this, SvtFolderPicker, DialogClosedHdl ) );
113 }
114 
115 //------------------------------------------------------------------------------------
116 SvtFileDialog* SvtFolderPicker::implCreateDialog( Window* _pParent )
117 {
118 	return new SvtFileDialog( _pParent, SFXWB_PATHDIALOG );
119 }
120 
121 //------------------------------------------------------------------------------------
122 sal_Int16 SvtFolderPicker::implExecutePicker( )
123 {
124     prepareExecute();
125 
126     // now we are ready to execute the dialog
127     getDialog()->EnableAutocompletion( sal_False );
128 	sal_Int16 nRet = getDialog()->Execute();
129 
130 	return nRet;
131 }
132 
133 //------------------------------------------------------------------------------------
134 void SvtFolderPicker::prepareExecute()
135 {
136     // set the default directory
137     if ( m_aDisplayDirectory.getLength() > 0 )
138         getDialog()->SetPath( m_aDisplayDirectory );
139     else
140     {
141         // Default-Standard-Dir setzen
142         INetURLObject aStdDirObj( SvtPathOptions().GetWorkPath() );
143         getDialog()->SetPath( aStdDirObj.GetMainURL( INetURLObject::NO_DECODE) );
144     }
145 }
146 
147 //-----------------------------------------------------------------------------
148 IMPL_LINK( SvtFolderPicker, DialogClosedHdl, Dialog*, pDlg )
149 {
150     if ( m_xListener.is() )
151     {
152         sal_Int16 nRet = static_cast< sal_Int16 >( pDlg->GetResult() );
153         ::com::sun::star::ui::dialogs::DialogClosedEvent aEvent( *this, nRet );
154         m_xListener->dialogClosed( aEvent );
155         m_xListener.clear();
156     }
157     return 0;
158   }
159 
160 //------------------------------------------------------------------------------------
161 // XFolderPicker functions
162 //------------------------------------------------------------------------------------
163 
164 void SAL_CALL SvtFolderPicker::setDisplayDirectory( const ::rtl::OUString& aDirectory )
165     throw( IllegalArgumentException, RuntimeException )
166 {
167 	m_aDisplayDirectory = aDirectory;
168 }
169 
170 //------------------------------------------------------------------------------------
171 ::rtl::OUString SAL_CALL SvtFolderPicker::getDisplayDirectory() throw( RuntimeException )
172 {
173 	::rtl::OUString aResult;
174 
175     if ( ! getDialog() )
176 		return m_aDisplayDirectory;
177 
178 	SvStringsDtor* pPathList = getDialog()->GetPathList();
179 
180 	if ( pPathList->Count() )
181 		aResult = ::rtl::OUString( *pPathList->GetObject( 0 ) );
182 
183 	delete pPathList;
184 
185     return aResult;
186 }
187 
188 //------------------------------------------------------------------------------------
189 ::rtl::OUString SAL_CALL SvtFolderPicker::getDirectory() throw( RuntimeException )
190 {
191 	::rtl::OUString aResult;
192 
193     if ( ! getDialog() )
194 		return m_aDisplayDirectory;
195 
196 	SvStringsDtor* pPathList = getDialog()->GetPathList();
197 
198 	if ( pPathList->Count() )
199 		aResult = ::rtl::OUString( *pPathList->GetObject( 0 ) );
200 
201 	delete pPathList;
202 
203     return aResult;
204 }
205 
206 //------------------------------------------------------------------------------------
207 void SAL_CALL SvtFolderPicker::setDescription( const ::rtl::OUString& aDescription )
208     throw( RuntimeException )
209 {
210 	m_aDescription = aDescription;
211 }
212 
213 //------------------------------------------------------------------------------------
214 // XServiceInfo
215 //------------------------------------------------------------------------------------
216 
217 /* XServiceInfo */
218 ::rtl::OUString SAL_CALL SvtFolderPicker::getImplementationName() throw( RuntimeException )
219 {
220 	return impl_getStaticImplementationName();
221 }
222 
223 /* XServiceInfo */
224 sal_Bool SAL_CALL SvtFolderPicker::supportsService( const ::rtl::OUString& sServiceName ) throw( RuntimeException )
225 {
226     Sequence< ::rtl::OUString > seqServiceNames = getSupportedServiceNames();
227     const ::rtl::OUString* pArray = seqServiceNames.getConstArray();
228     for ( sal_Int32 i = 0; i < seqServiceNames.getLength(); i++ )
229 	{
230         if ( sServiceName == pArray[i] )
231 		{
232             return sal_True ;
233 		}
234 	}
235     return sal_False ;
236 }
237 
238 /* XServiceInfo */
239 Sequence< ::rtl::OUString > SAL_CALL SvtFolderPicker::getSupportedServiceNames() throw( RuntimeException )
240 {
241 	return impl_getStaticSupportedServiceNames();
242 }
243 
244 /* Helper for XServiceInfo */
245 Sequence< ::rtl::OUString > SvtFolderPicker::impl_getStaticSupportedServiceNames()
246 {
247     Sequence< ::rtl::OUString > seqServiceNames(1);
248     seqServiceNames[0] = ::rtl::OUString::createFromAscii( "com.sun.star.ui.dialogs.OfficeFolderPicker" );
249     return seqServiceNames ;
250 }
251 
252 /* Helper for XServiceInfo */
253 ::rtl::OUString SvtFolderPicker::impl_getStaticImplementationName()
254 {
255     return ::rtl::OUString::createFromAscii( "com.sun.star.svtools.OfficeFolderPicker" );
256 }
257 
258 /* Helper for registry */
259 Reference< XInterface > SAL_CALL SvtFolderPicker::impl_createInstance( const Reference< XComponentContext >& rxContext )
260     throw( Exception )
261 {
262 	Reference< XMultiServiceFactory > xServiceManager (rxContext->getServiceManager(), UNO_QUERY_THROW);
263 	return Reference< XInterface >( *new SvtFolderPicker( xServiceManager ) );
264 }
265 
266