xref: /core/svtools/source/uno/fpicker.cxx (revision 16a90925)
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 <rtl/ustring.hxx>
21 
22 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
23 
24 #include <svl/pickerhistoryaccess.hxx>
25 #include <officecfg/Office/Common.hxx>
26 
27 #include <vcl/svapp.hxx>
28 
29 #include "fpicker.hxx"
30 
31 using css::uno::Reference;
32 using css::uno::Sequence;
33 
34 /*
35  * FilePicker implementation.
36  */
FilePicker_getSystemPickerServiceName()37 static OUString FilePicker_getSystemPickerServiceName()
38 {
39 #ifdef UNX
40     OUString aDesktopEnvironment (Application::GetDesktopEnvironment());
41     if (aDesktopEnvironment.equalsIgnoreAsciiCase("macosx"))
42         return u"com.sun.star.ui.dialogs.AquaFilePicker"_ustr;
43     else
44         return u"com.sun.star.ui.dialogs.SystemFilePicker"_ustr;
45 #endif
46 #ifdef _WIN32
47     return "com.sun.star.ui.dialogs.Win32FilePicker";
48 #endif
49 }
50 
51 // Ensure that we use not the system file dialogs as headless mode relies on
52 // Application::EnableHeadlessMode() which only works for VCL dialogs
UseSystemFileDialog()53 static bool UseSystemFileDialog()
54 {
55     return !Application::IsHeadlessModeEnabled() && officecfg::Office::Common::Misc::UseSystemFileDialog::get();
56 }
57 
FilePicker_CreateInstance(Reference<css::uno::XComponentContext> const & context)58 Reference< css::uno::XInterface > FilePicker_CreateInstance (
59     Reference< css::uno::XComponentContext > const & context)
60 {
61     Reference< css::uno::XInterface > xResult;
62 
63     if (!context.is())
64         return xResult;
65 
66     Reference< css::lang::XMultiComponentFactory > xFactory (context->getServiceManager());
67     if (xFactory.is() && UseSystemFileDialog())
68     {
69         xResult.set( Application::createFilePicker( context ) );
70 
71         if (!xResult.is())
72         {
73             try
74             {
75                 xResult = xFactory->createInstanceWithContext (
76                         FilePicker_getSystemPickerServiceName(),
77                         context);
78             }
79             catch (css::uno::Exception const &)
80             {
81                 // Handled below (see @ fallback).
82             }
83         }
84     }
85 
86 
87     if (!xResult.is() && xFactory.is())
88     {
89         // Always fall back to OfficeFilePicker.
90         xResult = xFactory->createInstanceWithContext (
91                 u"com.sun.star.ui.dialogs.OfficeFilePicker"_ustr,
92                 context);
93     }
94     if (xResult.is())
95     {
96         // Add to FilePicker history.
97         svt::addFilePicker (xResult);
98     }
99     return xResult;
100 }
101 
FilePicker_getImplementationName()102 OUString FilePicker_getImplementationName()
103 {
104     return u"com.sun.star.comp.svt.FilePicker"_ustr;
105 }
106 
FilePicker_getSupportedServiceNames()107 Sequence< OUString > FilePicker_getSupportedServiceNames()
108 {
109     Sequence< OUString > aServiceNames { u"com.sun.star.ui.dialogs.FilePicker"_ustr };
110     return aServiceNames;
111 }
112 
113 /*
114  * FolderPicker implementation.
115  */
FolderPicker_getSystemPickerServiceName()116 static OUString FolderPicker_getSystemPickerServiceName()
117 {
118 #ifdef UNX
119     OUString aDesktopEnvironment (Application::GetDesktopEnvironment());
120     if (aDesktopEnvironment.equalsIgnoreAsciiCase("macosx"))
121         return u"com.sun.star.ui.dialogs.AquaFolderPicker"_ustr;
122 #endif
123     return u"com.sun.star.ui.dialogs.SystemFolderPicker"_ustr;
124 }
125 
FolderPicker_CreateInstance(Reference<css::uno::XComponentContext> const & context)126 Reference< css::uno::XInterface > FolderPicker_CreateInstance (
127     Reference< css::uno::XComponentContext > const & context)
128 {
129     Reference< css::uno::XInterface > xResult;
130 
131     if (!context.is())
132         return xResult;
133 
134     Reference< css::lang::XMultiComponentFactory > xFactory (context->getServiceManager());
135     if (xFactory.is() && UseSystemFileDialog())
136     {
137         xResult.set( Application::createFolderPicker( context ) );
138         if (!xResult.is())
139         {
140             try
141             {
142                 xResult = xFactory->createInstanceWithContext (
143                                 FolderPicker_getSystemPickerServiceName(),
144                                 context);
145             }
146             catch (css::uno::Exception const &)
147             {
148                 // Handled below (see @ fallback).
149             }
150         }
151     }
152     if (!xResult.is() && xFactory.is() )
153     {
154         // Always fall back to OfficeFolderPicker.
155         xResult = xFactory->createInstanceWithContext (
156                  u"com.sun.star.ui.dialogs.OfficeFolderPicker"_ustr,
157                 context);
158     }
159     if (xResult.is())
160     {
161         // Add to FolderPicker history.
162         svt::addFolderPicker (xResult);
163     }
164     return xResult;
165 }
166 
FolderPicker_getImplementationName()167 OUString FolderPicker_getImplementationName()
168 {
169     return u"com.sun.star.comp.svt.FolderPicker"_ustr;
170 }
171 
FolderPicker_getSupportedServiceNames()172 Sequence< OUString > FolderPicker_getSupportedServiceNames()
173 {
174     Sequence< OUString > aServiceNames { u"com.sun.star.ui.dialogs.FolderPicker"_ustr };
175     return aServiceNames;
176 }
177 
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
179