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"
46 // namespace directives
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::uno;
54 SalAquaPicker::SalAquaPicker()
56 , m_pControlHelper(new ControlHelper())
58 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
59 DBG_PRINT_EXIT(CLASS_NAME, __func__);
62 SalAquaPicker::~SalAquaPicker()
64 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
66 SolarMutexGuard aGuard;
68 NSAutoreleasePool *pool = [NSAutoreleasePool new];
70 if (NULL != m_pControlHelper)
71 delete m_pControlHelper;
73 if (NULL != m_pDialog)
78 DBG_PRINT_EXIT(CLASS_NAME, __func__);
81 void SAL_CALL SalAquaPicker::implInitialize()
83 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
85 SolarMutexGuard aGuard;
87 if (m_pDialog != nil) {
91 switch (m_nDialogType)
93 case NAVIGATIONSERVICES_OPEN:
94 OSL_TRACE("NAVIGATIONSERVICES_OPEN");
95 m_pDialog = [NSOpenPanel openPanel];
96 [(NSOpenPanel*)m_pDialog setCanChooseDirectories:NO];
97 [(NSOpenPanel*)m_pDialog setCanChooseFiles:YES];
100 case NAVIGATIONSERVICES_SAVE:
101 OSL_TRACE("NAVIGATIONSERVICES_SAVE");
102 m_pDialog = [NSSavePanel savePanel];
103 [(NSSavePanel*)m_pDialog setCanSelectHiddenExtension:NO]; //changed for issue #102102
104 /* I would have loved to use
105 * [(NSSavePanel*)m_pDialog setExtensionHidden:YES];
106 * here but unfortunately this
107 * 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)
108 * 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
110 * So the only reliable way seems to be using the NSUserDefaults object because that is where that value is stored and
111 * to just overwrite it if it has the wrong value.
114 NSUserDefaults *pDefaults = [NSUserDefaults standardUserDefaults];
115 NSNumber *pExtn = [pDefaults objectForKey:kSetHideExtensionStateKey];
116 if(pExtn == nil || [pExtn boolValue] == NO) {
117 OSL_TRACE("Hiding extension");
118 [pDefaults setBool:YES forKey:kSetHideExtensionStateKey];
123 case NAVIGATIONSERVICES_DIRECTORY:
124 OSL_TRACE("NAVIGATIONSERVICES_DIRECTORY");
125 m_pDialog = [NSOpenPanel openPanel];
126 [(NSOpenPanel*)m_pDialog setCanChooseDirectories:YES];
127 [(NSOpenPanel*)m_pDialog setCanChooseFiles:NO];
131 OSL_TRACE("m_nDialogType is UNKNOWN: %d", m_nDialogType);
135 if (m_pDialog == nil) {
136 OSL_TRACE("An error occurred while creating the dialog!");
139 [(NSOpenPanel*)m_pDialog setCanCreateDirectories:YES];
140 //Retain the dialog instance or it will go away immediately
144 DBG_PRINT_EXIT(CLASS_NAME, __func__);
147 int SalAquaPicker::run()
149 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
151 SolarMutexGuard aGuard;
153 NSAutoreleasePool *pool = [NSAutoreleasePool new];
155 if (m_pDialog == NULL) {
156 //this is the case e.g. for the folder picker at this stage
160 NSView *userPane = m_pControlHelper->getUserPane();
161 if (userPane != NULL) {
162 [m_pDialog setAccessoryView:userPane];
167 NSURL *startDirectory;
168 if (m_sDisplayDirectory.getLength() > 0) {
169 NSString *temp = [NSString stringWithOUString:m_sDisplayDirectory];
170 startDirectory = [NSURL URLWithString:temp];
172 SAL_INFO("fpicker.aqua", "start dir: " << [startDirectory path]);
175 startDirectory = [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES];
178 switch(m_nDialogType) {
179 case NAVIGATIONSERVICES_DIRECTORY:
180 case NAVIGATIONSERVICES_OPEN:
181 [m_pDialog setDirectoryURL:startDirectory];
182 retVal = [(NSOpenPanel*)m_pDialog runModal];
184 case NAVIGATIONSERVICES_SAVE:
185 [m_pDialog setDirectoryURL:startDirectory];
186 [m_pDialog setNameFieldStringValue:[NSString stringWithOUString:static_cast<SalAquaFilePicker*>(this)->getSaveFileName()]];
187 retVal = [m_pDialog runModal];
189 // [m_pDialog beginSheetForDirectory:startDirectory file:[m_pDialog saveFilename] modalForWindow:[NSApp keyWindow] modalDelegate:((SalAquaFilePicker*)this)->getDelegate() didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:nil];
194 if (retVal == NSFileHandlingPanelOKButton) {
195 NSURL* pDir = [m_pDialog directoryURL];
197 implsetDisplayDirectory([pDir OUStringForInfo:FULLPATH]);
200 DBG_PRINT_EXIT(CLASS_NAME, __func__, retVal);
207 int SalAquaPicker::runandwaitforresult()
209 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
211 SolarMutexGuard aGuard;
213 int status = this->run();
215 DBG_PRINT_EXIT(CLASS_NAME, __func__, status);
219 void SAL_CALL SalAquaPicker::implsetDisplayDirectory( const rtl::OUString& aDirectory )
220 throw( lang::IllegalArgumentException, uno::RuntimeException )
222 DBG_PRINT_ENTRY(CLASS_NAME, __func__, "directory", aDirectory);
224 SolarMutexGuard aGuard;
226 if (aDirectory != m_sDisplayDirectory) {
227 m_sDisplayDirectory = aDirectory;
230 DBG_PRINT_EXIT(CLASS_NAME, __func__);
233 rtl::OUString SAL_CALL SalAquaPicker::implgetDisplayDirectory() throw( uno::RuntimeException )
235 DBG_PRINT_ENTRY(CLASS_NAME, __func__);
236 DBG_PRINT_EXIT(CLASS_NAME, __func__, m_sDisplayDirectory);
238 return m_sDisplayDirectory;
241 void SAL_CALL SalAquaPicker::implsetTitle( const rtl::OUString& aTitle ) throw( uno::RuntimeException )
243 DBG_PRINT_ENTRY(CLASS_NAME, __func__, "title", aTitle);
245 SolarMutexGuard aGuard;
247 if (m_pDialog != nil) {
248 [m_pDialog setTitle:[NSString stringWithOUString:aTitle]];
251 DBG_PRINT_EXIT(CLASS_NAME, __func__);
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */