LibreOffice Module fpicker (master)  1
SalAquaPicker.mm
Go to the documentation of this file.
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  */
19 
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22 
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <osl/diagnose.h>
27 #include <osl/mutex.hxx>
28 #include <vcl/svapp.hxx>
29 #include "SalAquaPicker.hxx"
30 #include <osl/file.hxx>
31 #include "CFStringUtilities.hxx"
33 
34 #include "NSURL_OOoAdditions.hxx"
35 
36 #include "SalAquaFilePicker.hxx"
37 
38 #include <stdio.h>
39 
40 #pragma mark DEFINES
41 #define kSetHideExtensionStateKey @"NSNavLastUserSetHideExtensionButtonState"
42 
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::uno;
46 
48 : m_pDialog(nullptr)
49 , m_pControlHelper(new ControlHelper())
50 {
51 }
52 
54 {
55  SolarMutexGuard aGuard;
56 
57  NSAutoreleasePool *pool = [NSAutoreleasePool new];
58 
59  if (nullptr != m_pControlHelper)
60  delete m_pControlHelper;
61 
62  if (nullptr != m_pDialog)
63  [m_pDialog release];
64 
65  [pool release];
66 }
67 
69 {
70  SolarMutexGuard aGuard;
71 
72  if (m_pDialog != nil) {
73  return;
74  }
75 
76  switch (m_nDialogType)
77  {
79  m_pDialog = [NSOpenPanel openPanel];
80  [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseDirectories:NO];
81  [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseFiles:YES];
82  break;
83 
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) 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
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;
104 
106  m_pDialog = [NSOpenPanel openPanel];
107  [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseDirectories:YES];
108  [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseFiles:NO];
109  break;
110 
111  default:
112  break;
113  }
114 
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  }
120 }
121 
123 {
124  SolarMutexGuard aGuard;
125 
126  NSAutoreleasePool *pool = [NSAutoreleasePool new];
127 
128  if (m_pDialog == nullptr) {
129  //this is the case e.g. for the folder picker at this stage
130  implInitialize();
131  }
132 
133  NSView *userPane = m_pControlHelper->getUserPane();
134  if (userPane != nullptr) {
135  [m_pDialog setAccessoryView:userPane];
136  }
137 
138  int retVal = 0;
139 
140  NSURL *startDirectory;
141  if (m_sDisplayDirectory.getLength() > 0) {
142  NSString *temp = [NSString stringWithOUString:m_sDisplayDirectory];
143  startDirectory = [NSURL URLWithString:temp];
144 
145  SAL_INFO("fpicker.aqua", "start dir: " << [startDirectory path]);
146  }
147  else {
148  startDirectory = [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES];
149  }
150 
151  switch(m_nDialogType) {
154  [m_pDialog setDirectoryURL:startDirectory];
155  retVal = [static_cast<NSOpenPanel*>(m_pDialog) runModal];
156  break;
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  }
166 
168  //TODO: 10.13 NSFileHandlingPanelOKButton
169  if (retVal == NSFileHandlingPanelOKButton) {
171  NSURL* pDir = [m_pDialog directoryURL];
172  if (pDir) {
173  implsetDisplayDirectory([pDir OUStringForInfo:FULLPATH]);
174  }
175  }
176 
177  [pool release];
178 
179  return retVal;
180 }
181 
183 {
184  SolarMutexGuard aGuard;
185 
186  int status = run();
187 
188  return status;
189 }
190 
191 void SalAquaPicker::implsetDisplayDirectory( const OUString& aDirectory )
192 {
193  SolarMutexGuard aGuard;
194 
195  if (aDirectory != m_sDisplayDirectory) {
196  m_sDisplayDirectory = aDirectory;
197  }
198 }
199 
201 {
202  return m_sDisplayDirectory;
203 }
204 
205 void SalAquaPicker::implsetTitle( const OUString& aTitle )
206 {
207  SolarMutexGuard aGuard;
208 
209  if (m_pDialog != nil) {
210  [m_pDialog setTitle:[NSString stringWithOUString:aTitle]];
211  }
212 }
213 
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
exports com.sun.star.frame. status
OUString const & implgetDisplayDirectory()
int runandwaitforresult()
void implsetDisplayDirectory(const OUString &rDirectory)
#define SAL_WNODEPRECATED_DECLARATIONS_PUSH
void implInitialize()
#define SAL_WNODEPRECATED_DECLARATIONS_POP
ControlHelper * m_pControlHelper
void implsetTitle(const OUString &aTitle)
virtual ~SalAquaPicker()
OUString m_sDisplayDirectory
NSSavePanel * m_pDialog
#define SAL_INFO(area, stream)
NSView * getUserPane()
NavigationServices_DialogType m_nDialogType