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>
32
34
35#include "SalAquaFilePicker.hxx"
36
37#include <stdio.h>
38
39#pragma mark DEFINES
40#define kSetHideExtensionStateKey @"NSNavLastUserSetHideExtensionButtonState"
41
42using namespace ::com::sun::star;
43using namespace ::com::sun::star::lang;
44using namespace ::com::sun::star::uno;
45
47: m_pDialog(nullptr)
48, m_pControlHelper(new ControlHelper())
49{
50}
51
53{
54 SolarMutexGuard aGuard;
55
56 NSAutoreleasePool *pool = [NSAutoreleasePool new];
57
58 if (nullptr != m_pControlHelper)
59 delete m_pControlHelper;
60
61 if (nullptr != m_pDialog)
62 [m_pDialog release];
63
64 [pool release];
65}
66
68{
69 SolarMutexGuard aGuard;
70
71 if (m_pDialog != nil) {
72 return;
73 }
74
75 switch (m_nDialogType)
76 {
78 m_pDialog = [NSOpenPanel openPanel];
79 [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseDirectories:NO];
80 [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseFiles:YES];
81 break;
82
84 m_pDialog = [NSSavePanel savePanel];
85 [m_pDialog setCanSelectHiddenExtension:NO]; //changed for issue #102102
86 /* I would have loved to use
87 * [(NSSavePanel*)m_pDialog setExtensionHidden:YES];
88 * here but unfortunately this
89 * 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)
90 * 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
91 *
92 * So the only reliable way seems to be using the NSUserDefaults object because that is where that value is stored and
93 * to just overwrite it if it has the wrong value.
94 */
95 {
96 NSUserDefaults *pDefaults = [NSUserDefaults standardUserDefaults];
97 NSNumber *pExtn = [pDefaults objectForKey:kSetHideExtensionStateKey];
98 if(pExtn == nil || [pExtn boolValue] == NO) {
99 [pDefaults setBool:YES forKey:kSetHideExtensionStateKey];
100 }
101 }
102 break;
103
105 m_pDialog = [NSOpenPanel openPanel];
106 [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseDirectories:YES];
107 [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseFiles:NO];
108 break;
109
110 default:
111 break;
112 }
113
114 if (m_pDialog != nil) {
115 [static_cast<NSOpenPanel*>(m_pDialog) setCanCreateDirectories:YES];
116 //Retain the dialog instance or it will go away immediately
117 [m_pDialog retain];
118 }
119}
120
122{
123 SolarMutexGuard aGuard;
124
125 NSAutoreleasePool *pool = [NSAutoreleasePool new];
126
127 if (m_pDialog == nullptr) {
128 //this is the case e.g. for the folder picker at this stage
130 }
131
132 NSView *userPane = m_pControlHelper->getUserPane();
133 if (userPane != nullptr) {
134 [m_pDialog setAccessoryView:userPane];
135 }
136
137 int retVal = 0;
138
139 NSURL *startDirectory;
140 if (m_sDisplayDirectory.getLength() > 0) {
141 NSString *temp = [NSString stringWithOUString:m_sDisplayDirectory];
142 startDirectory = [NSURL URLWithString:temp];
143
144 SAL_INFO("fpicker.aqua", "start dir: " << [startDirectory path]);
145 }
146 else {
147 startDirectory = [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES];
148 }
149
150 switch(m_nDialogType) {
153 [m_pDialog setDirectoryURL:startDirectory];
154 retVal = [static_cast<NSOpenPanel*>(m_pDialog) runModal];
155 break;
157 [m_pDialog setDirectoryURL:startDirectory];
158 [m_pDialog setNameFieldStringValue:[NSString stringWithOUString:static_cast<SalAquaFilePicker*>(this)->getSaveFileName()]];
159 retVal = [m_pDialog runModal];
160 break;
161 default:
162 break;
163 }
164
165 if (retVal == NSModalResponseOK) {
166 NSURL* pDir = [m_pDialog directoryURL];
167 if (pDir) {
168 implsetDisplayDirectory([pDir OUString]);
169 }
170 }
171
172 [pool release];
173
174 return retVal;
175}
176
178{
179 SolarMutexGuard aGuard;
180
181 int status = run();
182
183 return status;
184}
185
186void SalAquaPicker::implsetDisplayDirectory( const OUString& aDirectory )
187{
188 SolarMutexGuard aGuard;
189
190 if (aDirectory != m_sDisplayDirectory) {
191 m_sDisplayDirectory = aDirectory;
192 }
193}
194
196{
197 return m_sDisplayDirectory;
198}
199
200void SalAquaPicker::implsetTitle( const OUString& aTitle )
201{
202 SolarMutexGuard aGuard;
203
204 if (m_pDialog != nil) {
205 [m_pDialog setTitle:[NSString stringWithOUString:aTitle]];
206 }
207}
208
209/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define kSetHideExtensionStateKey
NSView * getUserPane()
OUString const & implgetDisplayDirectory()
@ NAVIGATIONSERVICES_DIRECTORY
void implInitialize()
int runandwaitforresult()
void implsetTitle(const OUString &aTitle)
virtual ~SalAquaPicker()
OUString m_sDisplayDirectory
ControlHelper * m_pControlHelper
NavigationServices_DialogType m_nDialogType
NSSavePanel * m_pDialog
void implsetDisplayDirectory(const OUString &rDirectory)
#define SAL_INFO(area, stream)