xref: /trunk/main/fpicker/source/generic/fpicker.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_fpicker.hxx"
30 #include "sal/types.h"
31 #include "rtl/ustring.hxx"
32 
33 #ifndef _CPPUHELPER_IMPLEMENTATIONENTRY_HXX_
34 #include "cppuhelper/implementationentry.hxx"
35 #endif
36 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
37 #include "svtools/miscopt.hxx"
38 #include "svl/pickerhistoryaccess.hxx"
39 
40 #ifndef _SV_APP_HXX
41 #include "vcl/svapp.hxx"
42 #endif
43 
44 namespace css = com::sun::star;
45 
46 using css::uno::Reference;
47 using css::uno::Sequence;
48 using rtl::OUString;
49 
50 /*
51  * FilePicker implementation.
52  */
53 static OUString FilePicker_getSystemPickerServiceName()
54 {
55     OUString aDesktopEnvironment (Application::GetDesktopEnvironment());
56     if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("gnome"))
57         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.GtkFilePicker"));
58     else if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("kde"))
59         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.KDEFilePicker"));
60     else if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("kde4"))
61         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.KDE4FilePicker"));
62     else if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("macosx"))
63         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.AquaFilePicker"));
64     else
65         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.SystemFilePicker"));
66 }
67 
68 static Reference< css::uno::XInterface > FilePicker_createInstance (
69     Reference< css::uno::XComponentContext > const & rxContext)
70 {
71     Reference< css::uno::XInterface > xResult;
72     if (rxContext.is())
73     {
74         Reference< css::lang::XMultiComponentFactory > xFactory (rxContext->getServiceManager());
75         if (xFactory.is())
76         {
77             if (SvtMiscOptions().UseSystemFileDialog())
78             {
79                 try
80                 {
81                     xResult = xFactory->createInstanceWithContext (
82                         FilePicker_getSystemPickerServiceName(),
83                         rxContext);
84                 }
85                 catch (css::uno::Exception const &)
86                 {
87                     // Handled below (see @ fallback).
88                 }
89             }
90             if (!xResult.is())
91             {
92                 // Always fall back to OfficeFilePicker.
93                 xResult = xFactory->createInstanceWithContext (
94                     OUString (RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.OfficeFilePicker")),
95                     rxContext);
96             }
97             if (xResult.is())
98             {
99                 // Add to FilePicker history.
100                 svt::addFilePicker (xResult);
101             }
102         }
103     }
104     return xResult;
105 }
106 
107 static OUString FilePicker_getImplementationName()
108 {
109     return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.comp.fpicker.FilePicker"));
110 }
111 
112 static Sequence< OUString > FilePicker_getSupportedServiceNames()
113 {
114     Sequence< OUString > aServiceNames(1);
115     aServiceNames.getArray()[0] =
116         OUString (RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FilePicker"));
117     return aServiceNames;
118 }
119 
120 /*
121  * FolderPicker implementation.
122  */
123 static OUString FolderPicker_getSystemPickerServiceName()
124 {
125     OUString aDesktopEnvironment (Application::GetDesktopEnvironment());
126     if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("gnome"))
127         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.GtkFolderPicker"));
128     else if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("kde"))
129         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.KDEFolderPicker"));
130     else if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("macosx"))
131         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.AquaFolderPicker"));
132     else
133         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.SystemFolderPicker"));
134 }
135 
136 static Reference< css::uno::XInterface > FolderPicker_createInstance (
137     Reference< css::uno::XComponentContext > const & rxContext)
138 {
139     Reference< css::uno::XInterface > xResult;
140     if (rxContext.is())
141     {
142         Reference< css::lang::XMultiComponentFactory > xFactory (rxContext->getServiceManager());
143         if (xFactory.is())
144         {
145             if (SvtMiscOptions().UseSystemFileDialog())
146             {
147                 try
148                 {
149                     xResult = xFactory->createInstanceWithContext (
150                         FolderPicker_getSystemPickerServiceName(),
151                         rxContext);
152                 }
153                 catch (css::uno::Exception const &)
154                 {
155                     // Handled below (see @ fallback).
156                 }
157             }
158             if (!xResult.is())
159             {
160                 // Always fall back to OfficeFolderPicker.
161                 xResult = xFactory->createInstanceWithContext (
162                     OUString (RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.OfficeFolderPicker")),
163                     rxContext);
164             }
165             if (xResult.is())
166             {
167                 // Add to FolderPicker history.
168                 svt::addFolderPicker (xResult);
169             }
170         }
171     }
172     return xResult;
173 }
174 
175 static OUString FolderPicker_getImplementationName()
176 {
177     return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.comp.fpicker.FolderPicker"));
178 }
179 
180 static Sequence< OUString > FolderPicker_getSupportedServiceNames()
181 {
182     Sequence< OUString > aServiceNames(1);
183     aServiceNames.getArray()[0] =
184         OUString (RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FolderPicker"));
185     return aServiceNames;
186 }
187 
188 /*
189  * Implementation entries.
190  */
191 static cppu::ImplementationEntry g_entries[] =
192 {
193     {
194         FilePicker_createInstance,
195         FilePicker_getImplementationName,
196         FilePicker_getSupportedServiceNames,
197         cppu::createSingleComponentFactory, 0, 0
198     },
199     {
200         FolderPicker_createInstance,
201         FolderPicker_getImplementationName,
202         FolderPicker_getSupportedServiceNames,
203         cppu::createSingleComponentFactory, 0, 0
204     },
205     { 0, 0, 0, 0, 0, 0 }
206 };
207 
208 /*
209  * Public (exported) interface.
210  */
211 extern "C"
212 {
213 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment (
214     const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */)
215 {
216     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
217 }
218 
219 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory (
220     const sal_Char * pImplementationName, void * pServiceManager, void * pRegistryKey)
221 {
222     return cppu::component_getFactoryHelper (
223         pImplementationName, pServiceManager, pRegistryKey, g_entries);
224 }
225 
226 } // extern "C"
227