1*b557fc96SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b557fc96SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b557fc96SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b557fc96SAndrew Rist  * distributed with this work for additional information
6*b557fc96SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b557fc96SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b557fc96SAndrew Rist  * "License"); you may not use this file except in compliance
9*b557fc96SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b557fc96SAndrew Rist  *
11*b557fc96SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b557fc96SAndrew Rist  *
13*b557fc96SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b557fc96SAndrew Rist  * software distributed under the License is distributed on an
15*b557fc96SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b557fc96SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b557fc96SAndrew Rist  * specific language governing permissions and limitations
18*b557fc96SAndrew Rist  * under the License.
19*b557fc96SAndrew Rist  *
20*b557fc96SAndrew Rist  *************************************************************/
21*b557fc96SAndrew Rist 
22*b557fc96SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir //------------------------------------------------------------------------
25cdf0e10cSrcweir // includes
26cdf0e10cSrcweir //------------------------------------------------------------------------
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
29cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
31cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
32cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
33cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
34cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.h>
35cdf0e10cSrcweir #include <osl/diagnose.h>
36cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
37cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
38cdf0e10cSrcweir #include <FPServiceInfo.hxx>
39cdf0e10cSrcweir #include <vos/mutex.hxx>
40cdf0e10cSrcweir #include <vcl/svapp.hxx>
41cdf0e10cSrcweir #include "SalAquaFolderPicker.hxx"
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <tools/urlobj.hxx>
44cdf0e10cSrcweir #include <iostream>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include "resourceprovider.hxx"
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #ifndef _SV_RC_H
49cdf0e10cSrcweir #include <tools/rc.hxx>
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #include <osl/file.hxx>
53cdf0e10cSrcweir #include "CFStringUtilities.hxx"
54cdf0e10cSrcweir #include "NSString_OOoAdditions.hxx"
55cdf0e10cSrcweir #include "NSURL_OOoAdditions.hxx"
56cdf0e10cSrcweir 
57cdf0e10cSrcweir #pragma mark DEFINES
58cdf0e10cSrcweir #define CLASS_NAME "SalAquaFolderPicker"
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //------------------------------------------------------------------------
61cdf0e10cSrcweir // namespace directives
62cdf0e10cSrcweir //------------------------------------------------------------------------
63cdf0e10cSrcweir 
64cdf0e10cSrcweir using namespace ::rtl;
65cdf0e10cSrcweir using namespace ::com::sun::star;
66cdf0e10cSrcweir using namespace ::com::sun::star::ui::dialogs;
67cdf0e10cSrcweir using namespace ::com::sun::star::lang;
68cdf0e10cSrcweir using namespace ::com::sun::star::uno;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir //------------------------------------------------------------------------
71cdf0e10cSrcweir // helper functions
72cdf0e10cSrcweir //------------------------------------------------------------------------
73cdf0e10cSrcweir 
74cdf0e10cSrcweir namespace
75cdf0e10cSrcweir {
76cdf0e10cSrcweir     // controling event notifications
FolderPicker_getSupportedServiceNames()77cdf0e10cSrcweir     uno::Sequence<rtl::OUString> SAL_CALL FolderPicker_getSupportedServiceNames()
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         uno::Sequence<rtl::OUString> aRet(2);
80cdf0e10cSrcweir         aRet[0] = rtl::OUString::createFromAscii( "com.sun.star.ui.dialogs.SystemFolderPicker" );
81cdf0e10cSrcweir         aRet[1] = rtl::OUString::createFromAscii( "com.sun.star.ui.dialogs.AquaFolderPicker" );
82cdf0e10cSrcweir         return aRet;
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir //-----------------------------------------------------------------------------------------
87cdf0e10cSrcweir // constructor
88cdf0e10cSrcweir //-----------------------------------------------------------------------------------------
SalAquaFolderPicker(const uno::Reference<lang::XMultiServiceFactory> & xServiceMgr)89cdf0e10cSrcweir SalAquaFolderPicker::SalAquaFolderPicker( const uno::Reference<lang::XMultiServiceFactory>& xServiceMgr ) :
90cdf0e10cSrcweir     m_xServiceMgr( xServiceMgr )
91cdf0e10cSrcweir {
92cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     m_nDialogType = NAVIGATIONSERVICES_DIRECTORY;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir //-----------------------------------------------------------------------------------------
100cdf0e10cSrcweir // XExecutableDialog functions
101cdf0e10cSrcweir //-----------------------------------------------------------------------------------------
102cdf0e10cSrcweir 
setTitle(const rtl::OUString & aTitle)103cdf0e10cSrcweir void SAL_CALL SalAquaFolderPicker::setTitle( const rtl::OUString& aTitle ) throw( uno::RuntimeException )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__, "title", aTitle);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     implsetTitle(aTitle);
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
execute()114cdf0e10cSrcweir sal_Int16 SAL_CALL SalAquaFolderPicker::execute() throw( uno::RuntimeException )
115cdf0e10cSrcweir {
116cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     sal_Int16 retVal = 0;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     int nResult = runandwaitforresult();
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     switch( nResult )
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir     case NSOKButton:
127cdf0e10cSrcweir         OSL_TRACE("Dialog returned with OK");
128cdf0e10cSrcweir         retVal = ExecutableDialogResults::OK;
129cdf0e10cSrcweir         break;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     case NSCancelButton:
132cdf0e10cSrcweir         OSL_TRACE("Dialog was cancelled!");
133cdf0e10cSrcweir         retVal = ExecutableDialogResults::CANCEL;
134cdf0e10cSrcweir         break;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     default:
137cdf0e10cSrcweir         throw uno::RuntimeException(rtl::OUString::createFromAscii("The dialog returned with an unknown result!"), static_cast< XFolderPicker* >( this ));
138cdf0e10cSrcweir         break;
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
142cdf0e10cSrcweir     return retVal;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir //------------------------------------------------------------------------------------
146cdf0e10cSrcweir // XFolderPicker functions
147cdf0e10cSrcweir //------------------------------------------------------------------------------------
148cdf0e10cSrcweir 
setDisplayDirectory(const rtl::OUString & aDirectory)149cdf0e10cSrcweir void SAL_CALL SalAquaFolderPicker::setDisplayDirectory( const rtl::OUString& aDirectory )
150cdf0e10cSrcweir     throw( lang::IllegalArgumentException, uno::RuntimeException )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__, "directory", aDirectory);
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     implsetDisplayDirectory(aDirectory);
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
getDisplayDirectory()161cdf0e10cSrcweir rtl::OUString SAL_CALL SalAquaFolderPicker::getDisplayDirectory() throw( uno::RuntimeException )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     OUString aDirectory = implgetDisplayDirectory();
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__, aDirectory);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     return aDirectory;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
getDirectory()174cdf0e10cSrcweir rtl::OUString SAL_CALL SalAquaFolderPicker::getDirectory() throw( uno::RuntimeException )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     NSArray *files = nil;
181cdf0e10cSrcweir     if (m_nDialogType == NAVIGATIONSERVICES_DIRECTORY) {
182cdf0e10cSrcweir         files = [(NSOpenPanel*)m_pDialog URLs];
183cdf0e10cSrcweir     }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     long nFiles = [files count];
186cdf0e10cSrcweir     OSL_TRACE("# of items: %d", nFiles);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     if (nFiles < 1) {
189cdf0e10cSrcweir         throw uno::RuntimeException(rtl::OUString::createFromAscii("no directory selected"), static_cast< XFolderPicker* >( this ));
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     rtl::OUString aDirectory;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     NSURL *url = [files objectAtIndex:0];
195cdf0e10cSrcweir     OSL_TRACE("handling %s", [[url description] UTF8String]);
196cdf0e10cSrcweir 
197cdf0e10cSrcweir     aDirectory = [url OUStringForInfo:FULLPATH];
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     implsetDisplayDirectory(aDirectory);
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     OSL_TRACE("dir url: %s", OUStringToOString(aDirectory, RTL_TEXTENCODING_UTF8).getStr());
202cdf0e10cSrcweir 
203cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
204cdf0e10cSrcweir     return aDirectory;
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
setDescription(const rtl::OUString & rDescription)207cdf0e10cSrcweir void SAL_CALL SalAquaFolderPicker::setDescription( const rtl::OUString& rDescription )
208cdf0e10cSrcweir     throw( uno::RuntimeException )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__, "description", rDescription);
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     [m_pDialog setMessage:[NSString stringWithOUString:rDescription]];
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir // -------------------------------------------------
218cdf0e10cSrcweir // XServiceInfo
219cdf0e10cSrcweir // -------------------------------------------------
220cdf0e10cSrcweir 
getImplementationName()221cdf0e10cSrcweir rtl::OUString SAL_CALL SalAquaFolderPicker::getImplementationName()
222cdf0e10cSrcweir     throw( uno::RuntimeException )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     rtl::OUString retVal = rtl::OUString::createFromAscii( FOLDER_PICKER_IMPL_NAME );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__, retVal);
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     return retVal;
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
supportsService(const rtl::OUString & sServiceName)233cdf0e10cSrcweir sal_Bool SAL_CALL SalAquaFolderPicker::supportsService( const rtl::OUString& sServiceName )
234cdf0e10cSrcweir     throw( uno::RuntimeException )
235cdf0e10cSrcweir {
236cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__, "serviceName", sServiceName);
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     sal_Bool retVal = sal_False;
239cdf0e10cSrcweir     uno::Sequence <rtl::OUString> supportedServicesNames = FolderPicker_getSupportedServiceNames();
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     for( sal_Int32 n = supportedServicesNames.getLength(); n--; ) {
242cdf0e10cSrcweir         if( supportedServicesNames[n].compareTo( sServiceName ) == 0) {
243cdf0e10cSrcweir             retVal = sal_True;
244cdf0e10cSrcweir             break;
245cdf0e10cSrcweir         }
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__, retVal);
249cdf0e10cSrcweir     return retVal;
250cdf0e10cSrcweir }
251cdf0e10cSrcweir 
getSupportedServiceNames()252cdf0e10cSrcweir uno::Sequence<rtl::OUString> SAL_CALL SalAquaFolderPicker::getSupportedServiceNames()
253cdf0e10cSrcweir     throw( uno::RuntimeException )
254cdf0e10cSrcweir {
255cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
256cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
257cdf0e10cSrcweir 
258cdf0e10cSrcweir     return FolderPicker_getSupportedServiceNames();
259cdf0e10cSrcweir }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir //------------------------------------------------------------------------------------
262cdf0e10cSrcweir // XCancellable
263cdf0e10cSrcweir //------------------------------------------------------------------------------------
264cdf0e10cSrcweir 
cancel()265cdf0e10cSrcweir void SAL_CALL SalAquaFolderPicker::cancel() throw( uno::RuntimeException )
266cdf0e10cSrcweir {
267cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     [m_pDialog cancel:nil];
272cdf0e10cSrcweir 
273cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
274cdf0e10cSrcweir }
275cdf0e10cSrcweir 
276cdf0e10cSrcweir // -------------------------------------------------
277cdf0e10cSrcweir // XEventListener
278cdf0e10cSrcweir // -------------------------------------------------
279cdf0e10cSrcweir 
disposing(const lang::EventObject &)280cdf0e10cSrcweir void SAL_CALL SalAquaFolderPicker::disposing( const lang::EventObject& )
281cdf0e10cSrcweir     throw( uno::RuntimeException )
282cdf0e10cSrcweir {
283cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
284cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
285cdf0e10cSrcweir }
286