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 "sal/config.h"
22 #include <com/sun/star/lang/DisposedException.hpp>
23 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 #include <cppuhelper/interfacecontainer.h>
25 #include <osl/diagnose.h>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <FPServiceInfo.hxx>
28 #include <osl/mutex.hxx>
29 #include <vcl/svapp.hxx>
30 #include "SalAquaPicker.hxx"
31 #include <osl/file.hxx>
32 #include "CFStringUtilities.hxx"
33 #include "NSString_OOoAdditions.hxx"
35 #include "NSURL_OOoAdditions.hxx"
37 #include "SalAquaFilePicker.hxx"
42 #define CLASS_NAME "SalAquaPicker"
43 #define kSetHideExtensionStateKey @"NSNavLastUserSetHideExtensionButtonState"
45 //------------------------------------------------------------------------
46 // namespace directives
47 //------------------------------------------------------------------------
49 using namespace ::rtl;
50 using namespace ::com::sun::star;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::uno;
55 SalAquaPicker::SalAquaPicker()
57 , m_pControlHelper(new ControlHelper())
59 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
60 DBG_PRINT_EXIT(CLASS_NAME, __func__);
63 SalAquaPicker::~SalAquaPicker()
65 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
67 SolarMutexGuard aGuard;
69 NSAutoreleasePool *pool = [NSAutoreleasePool new];
71 if (NULL != m_pControlHelper)
72 delete m_pControlHelper;
74 if (NULL != m_pDialog)
79 DBG_PRINT_EXIT(CLASS_NAME, __func__);
82 void SAL_CALL SalAquaPicker::implInitialize()
84 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
86 SolarMutexGuard aGuard;
88 if (m_pDialog != nil) {
92 switch (m_nDialogType)
94 case NAVIGATIONSERVICES_OPEN:
95 OSL_TRACE("NAVIGATIONSERVICES_OPEN");
96 m_pDialog = [NSOpenPanel openPanel];
97 [(NSOpenPanel*)m_pDialog setCanChooseDirectories:NO];
98 [(NSOpenPanel*)m_pDialog setCanChooseFiles:YES];
101 case NAVIGATIONSERVICES_SAVE:
102 OSL_TRACE("NAVIGATIONSERVICES_SAVE");
103 m_pDialog = [NSSavePanel savePanel];
104 [(NSSavePanel*)m_pDialog setCanSelectHiddenExtension:NO]; //changed for issue #102102
105 /* I would have loved to use
106 * [(NSSavePanel*)m_pDialog setExtensionHidden:YES];
107 * here but unfortunately this
108 * 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)
109 * 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
111 * So the only reliable way seems to be using the NSUserDefaults object because that is where that value is stored and
112 * to just overwrite it if it has the wrong value.
115 NSUserDefaults *pDefaults = [NSUserDefaults standardUserDefaults];
116 NSNumber *pExtn = [pDefaults objectForKey:kSetHideExtensionStateKey];
117 if(pExtn == nil || [pExtn boolValue] == NO) {
118 OSL_TRACE("Hiding extension");
119 [pDefaults setBool:YES forKey:kSetHideExtensionStateKey];
124 case NAVIGATIONSERVICES_DIRECTORY:
125 OSL_TRACE("NAVIGATIONSERVICES_DIRECTORY");
126 m_pDialog = [NSOpenPanel openPanel];
127 [(NSOpenPanel*)m_pDialog setCanChooseDirectories:YES];
128 [(NSOpenPanel*)m_pDialog setCanChooseFiles:NO];
132 OSL_TRACE("m_nDialogType is UNKNOWN: %d", m_nDialogType);
136 if (m_pDialog == nil) {
137 OSL_TRACE("An error occurred while creating the dialog!");
140 [(NSOpenPanel*)m_pDialog setCanCreateDirectories:YES];
141 //Retain the dialog instance or it will go away immediately
145 DBG_PRINT_EXIT(CLASS_NAME, __func__);
148 int SalAquaPicker::run()
150 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
152 SolarMutexGuard aGuard;
154 NSAutoreleasePool *pool = [NSAutoreleasePool new];
156 if (m_pDialog == NULL) {
157 //this is the case e.g. for the folder picker at this stage
161 NSView *userPane = m_pControlHelper->getUserPane();
162 if (userPane != NULL) {
163 [m_pDialog setAccessoryView:userPane];
168 NSString *startDirectory;
169 if (m_sDisplayDirectory.getLength() > 0) {
170 NSString *temp = [NSString stringWithOUString:m_sDisplayDirectory];
171 NSURL *url = [NSURL URLWithString:temp];
172 startDirectory = [url path];
174 OSL_TRACE("start dir: %s", [startDirectory UTF8String]);
175 // NSLog(@"%@", startDirectory);
178 startDirectory = NSHomeDirectory();
181 #if HAVE_GCC_PRAGMA_DIAGNOSTIC_MODIFY && HAVE_GCC_PRAGMA_DIAGNOSTIC_SCOPE
182 #pragma GCC diagnostic push
183 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
185 switch(m_nDialogType) {
186 case NAVIGATIONSERVICES_DIRECTORY:
187 case NAVIGATIONSERVICES_OPEN:
188 retVal = [(NSOpenPanel*)m_pDialog runModalForDirectory:startDirectory file:nil types:nil];
190 case NAVIGATIONSERVICES_SAVE:
191 retVal = [m_pDialog runModalForDirectory:startDirectory file:[NSString stringWithOUString:((SalAquaFilePicker*)this)->getSaveFileName()]/*[m_pDialog saveFilename]*/];
193 // [m_pDialog beginSheetForDirectory:startDirectory file:[m_pDialog saveFilename] modalForWindow:[NSApp keyWindow] modalDelegate:((SalAquaFilePicker*)this)->getDelegate() didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:nil];
198 if (retVal == NSFileHandlingPanelOKButton) {
199 NSString* pDir = [m_pDialog directory];
201 implsetDisplayDirectory([[NSURL fileURLWithPath:pDir] OUStringForInfo:FULLPATH]);
204 #if HAVE_GCC_PRAGMA_DIAGNOSTIC_MODIFY && HAVE_GCC_PRAGMA_DIAGNOSTIC_SCOPE
205 #pragma GCC diagnostic pop
207 DBG_PRINT_EXIT(CLASS_NAME, __func__, retVal);
214 int SalAquaPicker::runandwaitforresult()
216 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
218 SolarMutexGuard aGuard;
220 int status = this->run();
222 DBG_PRINT_EXIT(CLASS_NAME, __func__, status);
226 void SAL_CALL SalAquaPicker::implsetDisplayDirectory( const rtl::OUString& aDirectory )
227 throw( lang::IllegalArgumentException, uno::RuntimeException )
229 DBG_PRINT_ENTRY(CLASS_NAME, __func__, "directory", aDirectory);
231 SolarMutexGuard aGuard;
233 if (aDirectory != m_sDisplayDirectory) {
234 m_sDisplayDirectory = aDirectory;
237 DBG_PRINT_EXIT(CLASS_NAME, __func__);
240 rtl::OUString SAL_CALL SalAquaPicker::implgetDisplayDirectory() throw( uno::RuntimeException )
242 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
243 DBG_PRINT_EXIT(CLASS_NAME, __func__, m_sDisplayDirectory);
245 return m_sDisplayDirectory;
248 void SAL_CALL SalAquaPicker::implsetTitle( const rtl::OUString& aTitle ) throw( uno::RuntimeException )
250 DBG_PRINT_ENTRY(CLASS_NAME, __func__, "title", aTitle);
252 SolarMutexGuard aGuard;
254 if (m_pDialog != nil) {
255 [m_pDialog setTitle:[NSString stringWithOUString:aTitle]];
258 DBG_PRINT_EXIT(CLASS_NAME, __func__);
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */