Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / fpicker / source / aqua / SalAquaPicker.mm
blob0b235bb92dffd4065bea37573220dcf714f062ec
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>
21 #include <sal/log.hxx>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <cppuhelper/interfacecontainer.h>
26 #include <osl/diagnose.h>
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"
39 #include <stdio.h>
41 #pragma mark DEFINES
42 #define kSetHideExtensionStateKey @"NSNavLastUserSetHideExtensionButtonState"
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::uno;
48 SalAquaPicker::SalAquaPicker()
49 : m_pDialog(nullptr)
50 , m_pControlHelper(new ControlHelper())
54 SalAquaPicker::~SalAquaPicker()
56     SolarMutexGuard aGuard;
58     NSAutoreleasePool *pool = [NSAutoreleasePool new];
60     if (nullptr != m_pControlHelper)
61         delete m_pControlHelper;
63     if (nullptr != m_pDialog)
64         [m_pDialog release];
66     [pool release];
69 void SalAquaPicker::implInitialize()
71     SolarMutexGuard aGuard;
73     if (m_pDialog != nil) {
74         return;
75     }
77     switch (m_nDialogType)
78     {
79         case NAVIGATIONSERVICES_OPEN:
80             m_pDialog = [NSOpenPanel openPanel];
81             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseDirectories:NO];
82             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseFiles:YES];
83             break;
85         case NAVIGATIONSERVICES_SAVE:
86             m_pDialog = [NSSavePanel savePanel];
87             [m_pDialog setCanSelectHiddenExtension:NO]; //changed for issue #102102
88             /* I would have loved to use
89              * [(NSSavePanel*)m_pDialog setExtensionHidden:YES];
90              * here but unfortunately this
91              * 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)
92              * b) macOS 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
93              *
94              * So the only reliable way seems to be using the NSUserDefaults object because that is where that value is stored and
95              * to just overwrite it if it has the wrong value.
96              */
97             {
98                 NSUserDefaults *pDefaults = [NSUserDefaults standardUserDefaults];
99                 NSNumber *pExtn = [pDefaults objectForKey:kSetHideExtensionStateKey];
100                 if(pExtn == nil || [pExtn boolValue] == NO) {
101                     [pDefaults setBool:YES forKey:kSetHideExtensionStateKey];
102                 }
103             }
104             break;
106         case NAVIGATIONSERVICES_DIRECTORY:
107             m_pDialog = [NSOpenPanel openPanel];
108             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseDirectories:YES];
109             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseFiles:NO];
110             break;
112         default:
113             break;
114     }
116     if (m_pDialog != nil) {
117         [static_cast<NSOpenPanel*>(m_pDialog) setCanCreateDirectories:YES];
118         //Retain the dialog instance or it will go away immediately
119         [m_pDialog retain];
120     }
123 int SalAquaPicker::run()
125     SolarMutexGuard aGuard;
127     NSAutoreleasePool *pool = [NSAutoreleasePool new];
129     if (m_pDialog == nullptr) {
130         //this is the case e.g. for the folder picker at this stage
131         implInitialize();
132     }
134     NSView *userPane = m_pControlHelper->getUserPane();
135     if (userPane != nullptr) {
136         [m_pDialog setAccessoryView:userPane];
137     }
139     int retVal = 0;
141     NSURL *startDirectory;
142     if (m_sDisplayDirectory.getLength() > 0) {
143         NSString *temp = [NSString stringWithOUString:m_sDisplayDirectory];
144         startDirectory = [NSURL URLWithString:temp];
146         SAL_INFO("fpicker.aqua", "start dir: " << [startDirectory path]);
147     }
148     else {
149         startDirectory = [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES];
150     }
152     switch(m_nDialogType) {
153         case NAVIGATIONSERVICES_DIRECTORY:
154         case NAVIGATIONSERVICES_OPEN:
155             [m_pDialog setDirectoryURL:startDirectory];
156             retVal = [static_cast<NSOpenPanel*>(m_pDialog) runModal];
157             break;
158         case NAVIGATIONSERVICES_SAVE:
159             [m_pDialog setDirectoryURL:startDirectory];
160             [m_pDialog setNameFieldStringValue:[NSString stringWithOUString:static_cast<SalAquaFilePicker*>(this)->getSaveFileName()]];
161             retVal = [m_pDialog runModal];
162             break;
163         // [m_pDialog beginSheetForDirectory:startDirectory file:[m_pDialog saveFilename] modalForWindow:[NSApp keyWindow] modalDelegate:((SalAquaFilePicker*)this)->getDelegate() didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:nil];
164         default:
165             break;
166     }
168     SAL_WNODEPRECATED_DECLARATIONS_PUSH
169         //TODO: 10.13 NSFileHandlingPanelOKButton
170     if (retVal == NSFileHandlingPanelOKButton) {
171     SAL_WNODEPRECATED_DECLARATIONS_POP
172         NSURL* pDir = [m_pDialog directoryURL];
173         if (pDir) {
174             implsetDisplayDirectory([pDir OUStringForInfo:FULLPATH]);
175         }
176     }
178     [pool release];
180     return retVal;
183 int SalAquaPicker::runandwaitforresult()
185     SolarMutexGuard aGuard;
187     int status = run();
189     return status;
192 void SalAquaPicker::implsetDisplayDirectory( const OUString& aDirectory )
194     SolarMutexGuard aGuard;
196     if (aDirectory != m_sDisplayDirectory) {
197         m_sDisplayDirectory = aDirectory;
198     }
201 OUString const & SalAquaPicker::implgetDisplayDirectory()
203     return m_sDisplayDirectory;
206 void SalAquaPicker::implsetTitle( const OUString& aTitle )
208     SolarMutexGuard aGuard;
210     if (m_pDialog != nil) {
211         [m_pDialog setTitle:[NSString stringWithOUString:aTitle]];
212     }
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */