summaryrefslogtreecommitdiff
path: root/fpicker/source/aqua/SalAquaFilePicker.mm
blob: deff8783bbe941f082a5cd0a9ac80a2ea17de3f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/* -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <config_features.h>

#include <sal/config.h>
#include <sal/log.hxx>

#include <com/sun/star/lang/DisposedException.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
#include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
#include <cppuhelper/interfacecontainer.h>
#include <cppuhelper/supportsservice.hxx>
#include <osl/diagnose.h>
#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
#include <com/sun/star/ui/dialogs/ControlActions.hpp>
#include <com/sun/star/uno/Any.hxx>
#include <osl/mutex.hxx>
#include <vcl/svapp.hxx>

#include "resourceprovider.hxx"

#include <osl/file.hxx>
#include "CFStringUtilities.hxx"
#include "NSString_OOoAdditions.hxx"
#include "NSURL_OOoAdditions.hxx"

#include <iostream>

#include "SalAquaFilePicker.hxx"

#include <objc/objc-runtime.h>

#pragma mark DEFINES

using namespace ::com::sun::star;
using namespace ::com::sun::star::ui::dialogs;
using namespace ::com::sun::star::ui::dialogs::TemplateDescription;
using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds;
using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::uno;

namespace
{
    uno::Sequence<OUString> FilePicker_getSupportedServiceNames()
    {
        return { "com.sun.star.ui.dialogs.FilePicker",
                 "com.sun.star.ui.dialogs.SystemFilePicker",
                 "com.sun.star.ui.dialogs.AquaFilePicker" };
    }
}

#pragma mark Constructor

SalAquaFilePicker::SalAquaFilePicker()
  : SalAquaFilePicker_Base( m_rbHelperMtx )
   , m_pFilterHelper( nullptr )
{
    m_pDelegate = [[AquaFilePickerDelegate alloc] initWithFilePicker:this];
    m_pControlHelper->setFilePickerDelegate(m_pDelegate);
}

SalAquaFilePicker::~SalAquaFilePicker()
{
    if (nullptr != m_pFilterHelper)
        delete m_pFilterHelper;

    [m_pDelegate release];
}


#pragma mark XFilePickerNotifier

void SAL_CALL SalAquaFilePicker::addFilePickerListener( const uno::Reference<XFilePickerListener>& xListener )
{
    SolarMutexGuard aGuard;
    m_xListener = xListener;
}

void SAL_CALL SalAquaFilePicker::removeFilePickerListener( const uno::Reference<XFilePickerListener>& )
{
    SolarMutexGuard aGuard;
    m_xListener.clear();
}

#pragma mark XAsynchronousExecutableDialog

void SAL_CALL SalAquaFilePicker::setTitle( const OUString& aTitle )
{
    SolarMutexGuard aGuard;
    implsetTitle(aTitle);
}

sal_Int16 SAL_CALL SalAquaFilePicker::execute()
{
    SolarMutexGuard aGuard;

    sal_Int16 retVal = 0;

    implInitialize();

    // if m_pDialog is nil after initialization, something must have gone wrong before
    // or there was no initialization (see issue https://bz.apache.org/ooo/show_bug.cgi?id=100214)
    if (m_pDialog == nil) {
        m_nDialogType = NAVIGATIONSERVICES_OPEN;
    }

    if (m_pFilterHelper) {
        m_pFilterHelper->SetFilters();
    }

    if (m_nDialogType == NAVIGATIONSERVICES_SAVE) {
        if (m_sSaveFileName.getLength() == 0) {
            //if no filename is set, NavigationServices will set the name to "untitled". We don't want this!
            //So let's try to get the window title to get the real untitled name
            NSWindow *frontWindow = [NSApp keyWindow];
            if (nullptr != frontWindow) {
                NSString *windowTitle = [frontWindow title];
                if (windowTitle != nil) {
                    OUString ouName = [windowTitle OUString];
                    //a window title will typically be something like "Untitled1 - OpenOffice.org Writer"
                    //but we only want the "Untitled1" part of it
                    sal_Int32 indexOfDash = ouName.indexOf(" - ");
                    if (indexOfDash > -1) {
                        m_sSaveFileName = ouName.copy(0,indexOfDash);
                        if (m_sSaveFileName.getLength() > 0) {
                            setDefaultName(m_sSaveFileName);
                        }
                    }
                }
            }
        }
    }

    //Set the delegate to be notified of certain events

    // I don't know why, but with gcc 4.2.1, this line results in the warning:
    // class 'AquaFilePickerDelegate' does not implement the 'NSOpenSavePanelDelegate' protocol
    // So instead of:
    // [m_pDialog setDelegate:m_pDelegate];
    // do:
    reinterpret_cast<id (*)(id, SEL, ...)>(objc_msgSend)(
        m_pDialog, @selector(setDelegate:), m_pDelegate);

    int nStatus = runandwaitforresult();

    [m_pDialog setDelegate:nil];

    switch( nStatus )
    {
        case NSModalResponseOK:
            retVal = ExecutableDialogResults::OK;
            break;

        case NSModalResponseCancel:
            retVal = ExecutableDialogResults::CANCEL;
            break;

        default:
            throw uno::RuntimeException(
                      "The dialog returned with an unknown result!",
                      static_cast<XFilePicker*>( static_cast<XFilePicker3*>( this ) ));
            break;
    }

    return retVal;
}


#pragma mark XFilePicker

void SAL_CALL SalAquaFilePicker::setMultiSelectionMode( sal_Bool /* bMode */ )
{
    SolarMutexGuard aGuard;

    if (m_nDialogType == NAVIGATIONSERVICES_OPEN) {
        [static_cast<NSOpenPanel*>(m_pDialog) setAllowsMultipleSelection:YES];
    }
}

void SAL_CALL SalAquaFilePicker::setDefaultName( const OUString& aName )
{
    SolarMutexGuard aGuard;

    m_sSaveFileName = aName;
}

void SAL_CALL SalAquaFilePicker::setDisplayDirectory( const OUString& rDirectory )
{
    SolarMutexGuard aGuard;

    implsetDisplayDirectory(rDirectory);
}

OUString SAL_CALL SalAquaFilePicker::getDisplayDirectory()
{
    OUString retVal = implgetDisplayDirectory();

    return retVal;
}

uno::Sequence<OUString> SAL_CALL SalAquaFilePicker::getFiles()
{
    uno::Sequence< OUString > aSelectedFiles = getSelectedFiles();
    // multiselection doesn't really work with getFiles
    // so just retrieve the first url
    if (aSelectedFiles.getLength() > 1)
        aSelectedFiles.realloc(1);

    return aSelectedFiles;
}

uno::Sequence<OUString> SAL_CALL SalAquaFilePicker::getSelectedFiles()
{
    SolarMutexGuard aGuard;

#if HAVE_FEATURE_MACOSX_SANDBOX
    static NSUserDefaults *userDefaults;
    static bool triedUserDefaults = false;

    if (!triedUserDefaults)
    {
        userDefaults = [NSUserDefaults standardUserDefaults];
        triedUserDefaults = true;
    }
#endif

    NSArray *files = nil;
    if (m_nDialogType == NAVIGATIONSERVICES_OPEN) {
        files = [static_cast<NSOpenPanel*>(m_pDialog) URLs];
    }
    else if (m_nDialogType == NAVIGATIONSERVICES_SAVE) {
        files = [NSArray arrayWithObjects:[m_pDialog URL], nil];
    }

    NSUInteger nFiles = [files count];
    SAL_INFO("fpicker.aqua", "# of items: " << nFiles);

    uno::Sequence< OUString > aSelectedFiles(nFiles);

    for(NSUInteger nIndex = 0; nIndex < nFiles; nIndex += 1)
    {
        NSURL *url = [files objectAtIndex:nIndex];

#if HAVE_FEATURE_MACOSX_SANDBOX
        if (userDefaults != NULL &&
            [url respondsToSelector:@selector(bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error:)])
        {
            // In the case of "Save As" when the user has input a new
            // file name, this call will return nil, as bookmarks can
            // (naturally) only be created for existing file system
            // objects. In that case, code at a much lower level, in
            // sal, takes care of creating a bookmark when a new file
            // has been created outside the sandbox.
            NSData *data = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                         includingResourceValuesForKeys:nil
                                          relativeToURL:nil
                                                  error:nil];
            if (data != NULL)
            {
                [userDefaults setObject:data
                                 forKey:[@"bookmarkFor:" stringByAppendingString:[url absoluteString]]];
            }
        }
#endif

        OUString sFileOrDirURL = [url OUStringForInfo:FULLPATH];

        aSelectedFiles[nIndex] = sFileOrDirURL;
    }

    return aSelectedFiles;
}

#pragma mark XFilterManager

void SAL_CALL SalAquaFilePicker::appendFilter( const OUString& aTitle, const OUString& aFilter )
{
    SolarMutexGuard aGuard;

    ensureFilterHelper();
    m_pFilterHelper->appendFilter( aTitle, aFilter );
    m_pControlHelper->setFilterControlNeeded(true);
}

void SAL_CALL SalAquaFilePicker::setCurrentFilter( const OUString& aTitle )
{
    SolarMutexGuard aGuard;

    ensureFilterHelper();
    m_pFilterHelper->setCurrentFilter(aTitle);
    updateFilterUI();

    updateSaveFileNameExtension();
}

OUString SAL_CALL SalAquaFilePicker::getCurrentFilter()
{
    SolarMutexGuard aGuard;

    ensureFilterHelper();

    return m_pFilterHelper->getCurrentFilter();
}

#pragma mark XFilterGroupManager

void SAL_CALL SalAquaFilePicker::appendFilterGroup( const OUString& sGroupTitle, const uno::Sequence<beans::StringPair>& aFilters )
{
    SolarMutexGuard aGuard;

    ensureFilterHelper();
    m_pFilterHelper->appendFilterGroup(sGroupTitle, aFilters);
    m_pControlHelper->setFilterControlNeeded(true);
}

#pragma mark XFilePickerControlAccess

void SAL_CALL SalAquaFilePicker::setValue( sal_Int16 nControlId, sal_Int16 nControlAction, const uno::Any& rValue )
{
    SolarMutexGuard aGuard;

    m_pControlHelper->setValue(nControlId, nControlAction, rValue);

    if (nControlId == ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION && m_nDialogType == NAVIGATIONSERVICES_SAVE) {
        updateSaveFileNameExtension();
    }
}

uno::Any SAL_CALL SalAquaFilePicker::getValue( sal_Int16 nControlId, sal_Int16 nControlAction )
{
    uno::Any aValue = m_pControlHelper->getValue(nControlId, nControlAction);

    return aValue;
}

void SAL_CALL SalAquaFilePicker::enableControl( sal_Int16 nControlId, sal_Bool bEnable )
{
    m_pControlHelper->enableControl(nControlId, bEnable);
}

void SAL_CALL SalAquaFilePicker::setLabel( sal_Int16 nControlId, const OUString& aLabel )
{
    SolarMutexGuard aGuard;

    NSString* sLabel = [NSString stringWithOUString:aLabel];
    m_pControlHelper->setLabel( nControlId, sLabel ) ;
}

OUString SAL_CALL SalAquaFilePicker::getLabel( sal_Int16 nControlId )
{
    return m_pControlHelper->getLabel(nControlId);
}

#pragma mark XInitialization

void SAL_CALL SalAquaFilePicker::initialize( const uno::Sequence<uno::Any>& aArguments )
{
    SolarMutexGuard aGuard;

    // parameter checking
    uno::Any aAny;
    if( 0 == aArguments.getLength() )
        throw lang::IllegalArgumentException("no arguments",
                                             static_cast<XFilePicker*>( static_cast<XFilePicker3*>(this) ), 1 );

    aAny = aArguments[0];

    if( ( aAny.getValueType() != ::cppu::UnoType<sal_Int16>::get() ) &&
        (aAny.getValueType() != ::cppu::UnoType<sal_Int8>::get() ) )
        throw lang::IllegalArgumentException("invalid argument type",
                                             static_cast<XFilePicker*>( static_cast<XFilePicker3*>(this) ), 1 );

    sal_Int16 templateId = -1;
    aAny >>= templateId;

    switch( templateId )
    {
        case FILEOPEN_SIMPLE:
            m_nDialogType = NAVIGATIONSERVICES_OPEN;
            break;
        case FILESAVE_SIMPLE:
            m_nDialogType = NAVIGATIONSERVICES_SAVE;
            break;
        case FILESAVE_AUTOEXTENSION_PASSWORD:
            m_nDialogType = NAVIGATIONSERVICES_SAVE;
            break;
        case FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS:
            m_nDialogType = NAVIGATIONSERVICES_SAVE;
            break;
        case FILESAVE_AUTOEXTENSION_SELECTION:
            m_nDialogType = NAVIGATIONSERVICES_SAVE;
            break;
        case FILESAVE_AUTOEXTENSION_TEMPLATE:
            m_nDialogType = NAVIGATIONSERVICES_SAVE;
            break;
        case FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE:
            m_nDialogType = NAVIGATIONSERVICES_OPEN;
            break;
        case FILEOPEN_LINK_PREVIEW_IMAGE_ANCHOR:
            m_nDialogType = NAVIGATIONSERVICES_OPEN;
            break;
        case FILEOPEN_PLAY:
            m_nDialogType = NAVIGATIONSERVICES_OPEN;
            break;
        case FILEOPEN_LINK_PLAY:
            m_nDialogType = NAVIGATIONSERVICES_OPEN;
            break;
        case FILEOPEN_READONLY_VERSION:
            m_nDialogType = NAVIGATIONSERVICES_OPEN;
            break;
        case FILEOPEN_LINK_PREVIEW:
            m_nDialogType = NAVIGATIONSERVICES_OPEN;
            break;
        case FILESAVE_AUTOEXTENSION:
            m_nDialogType = NAVIGATIONSERVICES_SAVE;
            break;
        case FILEOPEN_PREVIEW:
            m_nDialogType = NAVIGATIONSERVICES_OPEN;
            break;
        default:
            throw lang::IllegalArgumentException("Unknown template",
                                                 static_cast<XFilePicker*>( static_cast<XFilePicker3*>(this) ),
                                                 1 );
    }

    m_pControlHelper->initialize(templateId);

    implInitialize();
}

#pragma mark XCancellable

void SAL_CALL SalAquaFilePicker::cancel()
{
    SolarMutexGuard aGuard;

    if (m_pDialog != nil) {
        [m_pDialog cancel:nil];
    }
}

#pragma mark XEventListener

void SalAquaFilePicker::disposing( const lang::EventObject& aEvent )
{
    SolarMutexGuard aGuard;

    uno::Reference<XFilePickerListener> xFilePickerListener( aEvent.Source, css::uno::UNO_QUERY );

    if( xFilePickerListener.is() )
        removeFilePickerListener( xFilePickerListener );
}

#pragma mark XServiceInfo

OUString SAL_CALL SalAquaFilePicker::getImplementationName()
{
    return "com.sun.star.ui.dialogs.SalAquaFilePicker";
}

sal_Bool SAL_CALL SalAquaFilePicker::supportsService( const OUString& sServiceName )
{
    return cppu::supportsService(this, sServiceName);
}

uno::Sequence<OUString> SAL_CALL SalAquaFilePicker::getSupportedServiceNames()
{
    return FilePicker_getSupportedServiceNames();
}

#pragma mark Misc/Private

void SalAquaFilePicker::fileSelectionChanged( FilePickerEvent aEvent )
{
    if (m_xListener.is())
        m_xListener->fileSelectionChanged( aEvent );
}

void SalAquaFilePicker::directoryChanged( FilePickerEvent aEvent )
{
    if (m_xListener.is())
        m_xListener->directoryChanged( aEvent );
}

void SalAquaFilePicker::controlStateChanged( FilePickerEvent aEvent )
{
    if (m_xListener.is())
        m_xListener->controlStateChanged( aEvent );
}

void SalAquaFilePicker::dialogSizeChanged()
{
    if (m_xListener.is())
        m_xListener->dialogSizeChanged();
}


// Misc

void SalAquaFilePicker::ensureFilterHelper()
{
    SolarMutexGuard aGuard;

    if (nullptr == m_pFilterHelper) {
        m_pFilterHelper = new FilterHelper;
        m_pControlHelper->setFilterHelper(m_pFilterHelper);
        [m_pDelegate setFilterHelper:m_pFilterHelper];
    }
}

void SalAquaFilePicker::updateFilterUI()
{
    m_pControlHelper->updateFilterUI();
}

void SalAquaFilePicker::updateSaveFileNameExtension()
{
    if (m_nDialogType != NAVIGATIONSERVICES_SAVE) {
        return;
    }

    // we need to set this here again because initial setting does
    //[m_pDialog setExtensionHidden:YES];

    SolarMutexGuard aGuard;

    if (!m_pControlHelper->isAutoExtensionEnabled()) {
        [m_pDialog setAllowedFileTypes:nil];
        [m_pDialog setAllowsOtherFileTypes:YES];
    } else {
        ensureFilterHelper();

        OUStringList aStringList = m_pFilterHelper->getCurrentFilterSuffixList();
        if( aStringList.empty()) // #i9328#
            return;

        OUString suffix = (*(aStringList.begin())).copy(1);
        NSString *requiredFileType = [NSString stringWithOUString:suffix];

        [m_pDialog setAllowedFileTypes:[NSArray arrayWithObjects:requiredFileType, nil]];

        [m_pDialog setAllowsOtherFileTypes:NO];
    }
}

void SalAquaFilePicker::filterControlChanged()
{
    if (m_pDialog == nil) {
        return;
    }

    SolarMutexGuard aGuard;

    updateSaveFileNameExtension();

    [m_pDialog validateVisibleColumns];

    FilePickerEvent evt;
    evt.ElementId = LISTBOX_FILTER;
    controlStateChanged( evt );
}

extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
fpicker_SalAquaFilePicker_get_implementation(
    css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
{
    return cppu::acquire(new SalAquaFilePicker());
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */