1b557fc96SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b557fc96SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b557fc96SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b557fc96SAndrew Rist  * distributed with this work for additional information
6b557fc96SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b557fc96SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b557fc96SAndrew Rist  * "License"); you may not use this file except in compliance
9b557fc96SAndrew Rist  * with the License.  You may obtain a copy of the License at
10b557fc96SAndrew Rist  *
11b557fc96SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12b557fc96SAndrew Rist  *
13b557fc96SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b557fc96SAndrew Rist  * software distributed under the License is distributed on an
15b557fc96SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b557fc96SAndrew Rist  * KIND, either express or implied.  See the License for the
17b557fc96SAndrew Rist  * specific language governing permissions and limitations
18b557fc96SAndrew Rist  * under the License.
19b557fc96SAndrew Rist  *
20b557fc96SAndrew Rist  *************************************************************/
21b557fc96SAndrew Rist 
22b557fc96SAndrew 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 <cppuhelper/interfacecontainer.h>
31cdf0e10cSrcweir #include <osl/diagnose.h>
32cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
33cdf0e10cSrcweir #include <FPServiceInfo.hxx>
34cdf0e10cSrcweir #include <vos/mutex.hxx>
35cdf0e10cSrcweir #include <vcl/svapp.hxx>
36cdf0e10cSrcweir #ifndef _SALAQUAPICKER_HXX_
37cdf0e10cSrcweir #include "SalAquaPicker.hxx"
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #include <tools/urlobj.hxx>
40cdf0e10cSrcweir #include <osl/file.hxx>
41cdf0e10cSrcweir #include "CFStringUtilities.hxx"
42cdf0e10cSrcweir #include "NSString_OOoAdditions.hxx"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #ifndef _NSURL_OOOADDITIONS_HXX_
45cdf0e10cSrcweir #include "NSURL_OOoAdditions.hxx"
46cdf0e10cSrcweir #endif
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include "SalAquaFilePicker.hxx"
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #include <stdio.h>
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #pragma mark DEFINES
53cdf0e10cSrcweir #define CLASS_NAME "SalAquaPicker"
54cdf0e10cSrcweir #define kSetHideExtensionStateKey @"NSNavLastUserSetHideExtensionButtonState"
55cdf0e10cSrcweir 
56cdf0e10cSrcweir //------------------------------------------------------------------------
57cdf0e10cSrcweir // namespace directives
58cdf0e10cSrcweir //------------------------------------------------------------------------
59cdf0e10cSrcweir 
60cdf0e10cSrcweir using namespace ::rtl;
61cdf0e10cSrcweir using namespace ::com::sun::star;
62cdf0e10cSrcweir using namespace ::com::sun::star::lang;
63cdf0e10cSrcweir using namespace ::com::sun::star::uno;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir // constructor
SalAquaPicker()66cdf0e10cSrcweir SalAquaPicker::SalAquaPicker()
67cdf0e10cSrcweir : m_pDialog(NULL)
68cdf0e10cSrcweir , m_pControlHelper(new ControlHelper())
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
71cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
~SalAquaPicker()74cdf0e10cSrcweir SalAquaPicker::~SalAquaPicker()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     NSAutoreleasePool *pool = [NSAutoreleasePool new];
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     if (NULL != m_pControlHelper)
83cdf0e10cSrcweir         delete m_pControlHelper;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     if (NULL != m_pDialog)
86cdf0e10cSrcweir         [m_pDialog release];
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     [pool release];
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
implInitialize()93cdf0e10cSrcweir void SAL_CALL SalAquaPicker::implInitialize()
94cdf0e10cSrcweir {
95cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     if (m_pDialog != nil) {
100cdf0e10cSrcweir         return;
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     switch (m_nDialogType)
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         case NAVIGATIONSERVICES_OPEN:
106cdf0e10cSrcweir             OSL_TRACE("NAVIGATIONSERVICES_OPEN");
107cdf0e10cSrcweir             m_pDialog = [NSOpenPanel openPanel];
108cdf0e10cSrcweir             [(NSOpenPanel*)m_pDialog setCanChooseDirectories:NO];
109cdf0e10cSrcweir             [(NSOpenPanel*)m_pDialog setCanChooseFiles:YES];
110cdf0e10cSrcweir             break;
111cdf0e10cSrcweir 
112*d192718aSHerbert Dürr         case NAVIGATIONSERVICES_SAVE: {
113cdf0e10cSrcweir             OSL_TRACE("NAVIGATIONSERVICES_SAVE");
114cdf0e10cSrcweir             m_pDialog = [NSSavePanel savePanel];
115cdf0e10cSrcweir             [(NSSavePanel*)m_pDialog setCanSelectHiddenExtension:NO]; //changed for issue #102102
116cdf0e10cSrcweir             /* I would have loved to use
117cdf0e10cSrcweir              * [(NSSavePanel*)m_pDialog setExtensionHidden:YES];
118cdf0e10cSrcweir              * here but unfortunately this
119cdf0e10cSrcweir              * a) only works when the dialog is already displayed because it seems to act on the corresponding checkbox (that we don't show but that doesn't matter)
120cdf0e10cSrcweir              * b) Mac OS X saves this setting on an application-based level which means that the last state is always being restored again when the app runs for the next time
121cdf0e10cSrcweir              *
122cdf0e10cSrcweir              * So the only reliable way seems to be using the NSUserDefaults object because that is where that value is stored and
123cdf0e10cSrcweir              * to just overwrite it if it has the wrong value.
124cdf0e10cSrcweir              */
125cdf0e10cSrcweir             NSUserDefaults *pDefaults = [NSUserDefaults standardUserDefaults];
126cdf0e10cSrcweir             NSNumber *pExtn = [pDefaults objectForKey:kSetHideExtensionStateKey];
127cdf0e10cSrcweir             if(pExtn == nil || [pExtn boolValue] == NO) {
128cdf0e10cSrcweir                 OSL_TRACE("Hiding extension");
129cdf0e10cSrcweir                 [pDefaults setBool:YES forKey:kSetHideExtensionStateKey];
130cdf0e10cSrcweir             }
131*d192718aSHerbert Dürr             } break;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         case NAVIGATIONSERVICES_DIRECTORY:
134cdf0e10cSrcweir             OSL_TRACE("NAVIGATIONSERVICES_DIRECTORY");
135cdf0e10cSrcweir             m_pDialog = [NSOpenPanel openPanel];
136cdf0e10cSrcweir             [(NSOpenPanel*)m_pDialog setCanChooseDirectories:YES];
137cdf0e10cSrcweir             [(NSOpenPanel*)m_pDialog setCanChooseFiles:NO];
138cdf0e10cSrcweir             break;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         default:
141cdf0e10cSrcweir             OSL_TRACE("m_nDialogType is UNKNOWN: %d", m_nDialogType);
142cdf0e10cSrcweir             break;
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     if (m_pDialog == nil) {
146cdf0e10cSrcweir         OSL_TRACE("An error occurred while creating the dialog!");
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir     else {
149cdf0e10cSrcweir         [(NSOpenPanel*)m_pDialog setCanCreateDirectories:YES];
150cdf0e10cSrcweir         //Retain the dialog instance or it will go away immediately
151cdf0e10cSrcweir         [m_pDialog retain];
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
run()157cdf0e10cSrcweir int SalAquaPicker::run()
158cdf0e10cSrcweir {
159cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     NSAutoreleasePool *pool = [NSAutoreleasePool new];
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     if (m_pDialog == NULL) {
166cdf0e10cSrcweir         //this is the case e.g. for the folder picker at this stage
167cdf0e10cSrcweir         implInitialize();
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     NSView *userPane = m_pControlHelper->getUserPane();
171cdf0e10cSrcweir     if (userPane != NULL) {
172cdf0e10cSrcweir         [m_pDialog setAccessoryView:userPane];
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     int retVal = 0;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     NSString *startDirectory;
178cdf0e10cSrcweir     if (m_sDisplayDirectory.getLength() > 0) {
179cdf0e10cSrcweir         NSString *temp = [NSString stringWithOUString:m_sDisplayDirectory];
180cdf0e10cSrcweir         NSURL *url = [NSURL URLWithString:temp];
181cdf0e10cSrcweir         startDirectory = [url path];
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         OSL_TRACE("start dir: %s", [startDirectory UTF8String]);
184cdf0e10cSrcweir         // NSLog(@"%@", startDirectory);
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir     else {
187cdf0e10cSrcweir         startDirectory = NSHomeDirectory();
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     switch(m_nDialogType) {
191cdf0e10cSrcweir         case NAVIGATIONSERVICES_DIRECTORY:
192cdf0e10cSrcweir         case NAVIGATIONSERVICES_OPEN:
193cdf0e10cSrcweir             retVal = [(NSOpenPanel*)m_pDialog runModalForDirectory:startDirectory file:nil types:nil];
194cdf0e10cSrcweir             break;
195cdf0e10cSrcweir         case NAVIGATIONSERVICES_SAVE:
196cdf0e10cSrcweir             retVal = [m_pDialog runModalForDirectory:startDirectory file:[NSString stringWithOUString:((SalAquaFilePicker*)this)->getSaveFileName()]/*[m_pDialog saveFilename]*/];
197cdf0e10cSrcweir             break;
198cdf0e10cSrcweir         // [m_pDialog beginSheetForDirectory:startDirectory file:[m_pDialog saveFilename] modalForWindow:[NSApp keyWindow] modalDelegate:((SalAquaFilePicker*)this)->getDelegate() didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:nil];
199cdf0e10cSrcweir         default:
200cdf0e10cSrcweir             break;
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir     if (retVal == NSFileHandlingPanelOKButton) {
204cdf0e10cSrcweir         NSString* pDir = [m_pDialog directory];
205cdf0e10cSrcweir         if (pDir) {
206cdf0e10cSrcweir             implsetDisplayDirectory([[NSURL fileURLWithPath:pDir] OUStringForInfo:FULLPATH]);
207cdf0e10cSrcweir         }
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__, retVal);
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     [pool release];
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     return retVal;
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
runandwaitforresult()217cdf0e10cSrcweir int SalAquaPicker::runandwaitforresult()
218cdf0e10cSrcweir {
219cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
220cdf0e10cSrcweir 
221cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     int status = this->run();
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__, status);
226cdf0e10cSrcweir     return status;
227cdf0e10cSrcweir }
228cdf0e10cSrcweir 
implsetDisplayDirectory(const rtl::OUString & aDirectory)229cdf0e10cSrcweir void SAL_CALL SalAquaPicker::implsetDisplayDirectory( const rtl::OUString& aDirectory )
230cdf0e10cSrcweir     throw( lang::IllegalArgumentException, uno::RuntimeException )
231cdf0e10cSrcweir {
232cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__, "directory", aDirectory);
233cdf0e10cSrcweir 
234cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     if (aDirectory != m_sDisplayDirectory) {
237cdf0e10cSrcweir         m_sDisplayDirectory = aDirectory;
238cdf0e10cSrcweir     }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
241cdf0e10cSrcweir }
242cdf0e10cSrcweir 
implgetDisplayDirectory()243cdf0e10cSrcweir rtl::OUString SAL_CALL SalAquaPicker::implgetDisplayDirectory() throw( uno::RuntimeException )
244cdf0e10cSrcweir {
245cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__);
246cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__, m_sDisplayDirectory);
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     return m_sDisplayDirectory;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir 
implsetTitle(const rtl::OUString & aTitle)251cdf0e10cSrcweir void SAL_CALL SalAquaPicker::implsetTitle( const rtl::OUString& aTitle ) throw( uno::RuntimeException )
252cdf0e10cSrcweir {
253cdf0e10cSrcweir     DBG_PRINT_ENTRY(CLASS_NAME, __func__, "title", aTitle);
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     if (m_pDialog != nil) {
258cdf0e10cSrcweir         [m_pDialog setTitle:[NSString stringWithOUString:aTitle]];
259cdf0e10cSrcweir     }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir     DBG_PRINT_EXIT(CLASS_NAME, __func__);
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
264