bump product version to 6.1.0.2
[LibreOffice.git] / fpicker / source / aqua / SalAquaPicker.mm
bloba54cc49cb3ab7d95f55983398623cd7014ea200d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
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 "FPServiceInfo.hxx"
27 #include <osl/mutex.hxx>
28 #include <vcl/svapp.hxx>
29 #include "SalAquaPicker.hxx"
30 #include <osl/file.hxx>
31 #include "CFStringUtilities.hxx"
32 #include "NSString_OOoAdditions.hxx"
34 #include "NSURL_OOoAdditions.hxx"
36 #include "SalAquaFilePicker.hxx"
38 #include <stdio.h>
40 #pragma mark DEFINES
41 #define kSetHideExtensionStateKey @"NSNavLastUserSetHideExtensionButtonState"
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::uno;
47 SalAquaPicker::SalAquaPicker()
48 : m_pDialog(nullptr)
49 , m_pControlHelper(new ControlHelper())
53 SalAquaPicker::~SalAquaPicker()
55     SolarMutexGuard aGuard;
57     NSAutoreleasePool *pool = [NSAutoreleasePool new];
59     if (nullptr != m_pControlHelper)
60         delete m_pControlHelper;
62     if (nullptr != m_pDialog)
63         [m_pDialog release];
65     [pool release];
68 void SalAquaPicker::implInitialize()
70     SolarMutexGuard aGuard;
72     if (m_pDialog != nil) {
73         return;
74     }
76     switch (m_nDialogType)
77     {
78         case NAVIGATIONSERVICES_OPEN:
79             m_pDialog = [NSOpenPanel openPanel];
80             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseDirectories:NO];
81             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseFiles:YES];
82             break;
84         case NAVIGATIONSERVICES_SAVE:
85             m_pDialog = [NSSavePanel savePanel];
86             [m_pDialog setCanSelectHiddenExtension:NO]; //changed for issue #102102
87             /* I would have loved to use
88              * [(NSSavePanel*)m_pDialog setExtensionHidden:YES];
89              * here but unfortunately this
90              * 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)
91              * 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
92              *
93              * So the only reliable way seems to be using the NSUserDefaults object because that is where that value is stored and
94              * to just overwrite it if it has the wrong value.
95              */
96             {
97                 NSUserDefaults *pDefaults = [NSUserDefaults standardUserDefaults];
98                 NSNumber *pExtn = [pDefaults objectForKey:kSetHideExtensionStateKey];
99                 if(pExtn == nil || [pExtn boolValue] == NO) {
100                     [pDefaults setBool:YES forKey:kSetHideExtensionStateKey];
101                 }
102             }
103             break;
105         case NAVIGATIONSERVICES_DIRECTORY:
106             m_pDialog = [NSOpenPanel openPanel];
107             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseDirectories:YES];
108             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseFiles:NO];
109             break;
111         default:
112             break;
113     }
115     if (m_pDialog != nil) {
116         [static_cast<NSOpenPanel*>(m_pDialog) setCanCreateDirectories:YES];
117         //Retain the dialog instance or it will go away immediately
118         [m_pDialog retain];
119     }
122 int SalAquaPicker::run()
124     SolarMutexGuard aGuard;
126     NSAutoreleasePool *pool = [NSAutoreleasePool new];
128     if (m_pDialog == nullptr) {
129         //this is the case e.g. for the folder picker at this stage
130         implInitialize();
131     }
133     NSView *userPane = m_pControlHelper->getUserPane();
134     if (userPane != nullptr) {
135         [m_pDialog setAccessoryView:userPane];
136     }
138     int retVal = 0;
140     NSURL *startDirectory;
141     if (m_sDisplayDirectory.getLength() > 0) {
142         NSString *temp = [NSString stringWithOUString:m_sDisplayDirectory];
143         startDirectory = [NSURL URLWithString:temp];
145         SAL_INFO("fpicker.aqua", "start dir: " << [startDirectory path]);
146     }
147     else {
148         startDirectory = [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES];
149     }
151     switch(m_nDialogType) {
152         case NAVIGATIONSERVICES_DIRECTORY:
153         case NAVIGATIONSERVICES_OPEN:
154             [m_pDialog setDirectoryURL:startDirectory];
155             retVal = [static_cast<NSOpenPanel*>(m_pDialog) runModal];
156             break;
157         case NAVIGATIONSERVICES_SAVE:
158             [m_pDialog setDirectoryURL:startDirectory];
159             [m_pDialog setNameFieldStringValue:[NSString stringWithOUString:static_cast<SalAquaFilePicker*>(this)->getSaveFileName()]];
160             retVal = [m_pDialog runModal];
161             break;
162         // [m_pDialog beginSheetForDirectory:startDirectory file:[m_pDialog saveFilename] modalForWindow:[NSApp keyWindow] modalDelegate:((SalAquaFilePicker*)this)->getDelegate() didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:nil];
163         default:
164             break;
165     }
167     SAL_WNODEPRECATED_DECLARATIONS_PUSH
168         //TODO: 10.13 NSFileHandlingPanelOKButton
169     if (retVal == NSFileHandlingPanelOKButton) {
170     SAL_WNODEPRECATED_DECLARATIONS_POP
171         NSURL* pDir = [m_pDialog directoryURL];
172         if (pDir) {
173             implsetDisplayDirectory([pDir OUStringForInfo:FULLPATH]);
174         }
175     }
177     [pool release];
179     return retVal;
182 int SalAquaPicker::runandwaitforresult()
184     SolarMutexGuard aGuard;
186     int status = run();
188     return status;
191 void SalAquaPicker::implsetDisplayDirectory( const rtl::OUString& aDirectory )
193     SolarMutexGuard aGuard;
195     if (aDirectory != m_sDisplayDirectory) {
196         m_sDisplayDirectory = aDirectory;
197     }
200 rtl::OUString const & SalAquaPicker::implgetDisplayDirectory()
202     return m_sDisplayDirectory;
205 void SalAquaPicker::implsetTitle( const rtl::OUString& aTitle )
207     SolarMutexGuard aGuard;
209     if (m_pDialog != nil) {
210         [m_pDialog setTitle:[NSString stringWithOUString:aTitle]];
211     }
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */