xref: /trunk/main/fpicker/source/generic/fpicker.cxx (revision b557fc96)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_fpicker.hxx"
26 #include "sal/types.h"
27 #include "rtl/ustring.hxx"
28 
29 #ifndef _CPPUHELPER_IMPLEMENTATIONENTRY_HXX_
30 #include "cppuhelper/implementationentry.hxx"
31 #endif
32 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
33 #include "svtools/miscopt.hxx"
34 #include "svl/pickerhistoryaccess.hxx"
35 
36 #ifndef _SV_APP_HXX
37 #include "vcl/svapp.hxx"
38 #endif
39 
40 namespace css = com::sun::star;
41 
42 using css::uno::Reference;
43 using css::uno::Sequence;
44 using rtl::OUString;
45 
46 /*
47  * FilePicker implementation.
48  */
FilePicker_getSystemPickerServiceName()49 static OUString FilePicker_getSystemPickerServiceName()
50 {
51 	OUString aDesktopEnvironment (Application::GetDesktopEnvironment());
52 	if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("gnome"))
53 		return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.GtkFilePicker"));
54 	else if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("kde"))
55 		return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.KDEFilePicker"));
56 	else if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("kde4"))
57 		return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.KDE4FilePicker"));
58     else if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("macosx"))
59         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.AquaFilePicker"));
60     else
61         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.SystemFilePicker"));
62 }
63 
FilePicker_createInstance(Reference<css::uno::XComponentContext> const & rxContext)64 static Reference< css::uno::XInterface > FilePicker_createInstance (
65 	Reference< css::uno::XComponentContext > const & rxContext)
66 {
67 	Reference< css::uno::XInterface > xResult;
68 	if (rxContext.is())
69 	{
70 		Reference< css::lang::XMultiComponentFactory > xFactory (rxContext->getServiceManager());
71 		if (xFactory.is())
72 		{
73 			if (SvtMiscOptions().UseSystemFileDialog())
74 			{
75 				try
76 				{
77 					xResult = xFactory->createInstanceWithContext (
78 						FilePicker_getSystemPickerServiceName(),
79 						rxContext);
80 				}
81 				catch (css::uno::Exception const &)
82 				{
83 					// Handled below (see @ fallback).
84 				}
85 			}
86 			if (!xResult.is())
87 			{
88 				// Always fall back to OfficeFilePicker.
89 				xResult = xFactory->createInstanceWithContext (
90 					OUString (RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.OfficeFilePicker")),
91 					rxContext);
92 			}
93 			if (xResult.is())
94 			{
95 				// Add to FilePicker history.
96 				svt::addFilePicker (xResult);
97 			}
98 		}
99 	}
100 	return xResult;
101 }
102 
FilePicker_getImplementationName()103 static OUString FilePicker_getImplementationName()
104 {
105 	return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.comp.fpicker.FilePicker"));
106 }
107 
FilePicker_getSupportedServiceNames()108 static Sequence< OUString > FilePicker_getSupportedServiceNames()
109 {
110 	Sequence< OUString > aServiceNames(1);
111 	aServiceNames.getArray()[0] =
112 		OUString (RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FilePicker"));
113 	return aServiceNames;
114 }
115 
116 /*
117  * FolderPicker implementation.
118  */
FolderPicker_getSystemPickerServiceName()119 static OUString FolderPicker_getSystemPickerServiceName()
120 {
121 	OUString aDesktopEnvironment (Application::GetDesktopEnvironment());
122 	if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("gnome"))
123 		return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.GtkFolderPicker"));
124 	else if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("kde"))
125 		return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.KDEFolderPicker"));
126     else if (aDesktopEnvironment.equalsIgnoreAsciiCaseAscii ("macosx"))
127         return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.AquaFolderPicker"));
128 	else
129 		return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.ui.dialogs.SystemFolderPicker"));
130 }
131 
FolderPicker_createInstance(Reference<css::uno::XComponentContext> const & rxContext)132 static Reference< css::uno::XInterface > FolderPicker_createInstance (
133 	Reference< css::uno::XComponentContext > const & rxContext)
134 {
135 	Reference< css::uno::XInterface > xResult;
136 	if (rxContext.is())
137 	{
138 		Reference< css::lang::XMultiComponentFactory > xFactory (rxContext->getServiceManager());
139 		if (xFactory.is())
140 		{
141 			if (SvtMiscOptions().UseSystemFileDialog())
142 			{
143 				try
144 				{
145 					xResult = xFactory->createInstanceWithContext (
146 						FolderPicker_getSystemPickerServiceName(),
147 						rxContext);
148 				}
149 				catch (css::uno::Exception const &)
150 				{
151 					// Handled below (see @ fallback).
152 				}
153 			}
154 			if (!xResult.is())
155 			{
156 				// Always fall back to OfficeFolderPicker.
157 				xResult = xFactory->createInstanceWithContext (
158 					OUString (RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.OfficeFolderPicker")),
159 					rxContext);
160 			}
161 			if (xResult.is())
162 			{
163 				// Add to FolderPicker history.
164 				svt::addFolderPicker (xResult);
165 			}
166 		}
167 	}
168 	return xResult;
169 }
170 
FolderPicker_getImplementationName()171 static OUString FolderPicker_getImplementationName()
172 {
173 	return OUString (RTL_CONSTASCII_USTRINGPARAM ("com.sun.star.comp.fpicker.FolderPicker"));
174 }
175 
FolderPicker_getSupportedServiceNames()176 static Sequence< OUString > FolderPicker_getSupportedServiceNames()
177 {
178 	Sequence< OUString > aServiceNames(1);
179 	aServiceNames.getArray()[0] =
180 		OUString (RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FolderPicker"));
181 	return aServiceNames;
182 }
183 
184 /*
185  * Implementation entries.
186  */
187 static cppu::ImplementationEntry g_entries[] =
188 {
189 	{
190 		FilePicker_createInstance,
191 		FilePicker_getImplementationName,
192 		FilePicker_getSupportedServiceNames,
193 		cppu::createSingleComponentFactory, 0, 0
194 	},
195 	{
196 		FolderPicker_createInstance,
197 		FolderPicker_getImplementationName,
198 		FolderPicker_getSupportedServiceNames,
199 		cppu::createSingleComponentFactory, 0, 0
200 	},
201 	{ 0, 0, 0, 0, 0, 0 }
202 };
203 
204 /*
205  * Public (exported) interface.
206  */
207 extern "C"
208 {
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)209 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment (
210 	const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */)
211 {
212 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
213 }
214 
component_getFactory(const sal_Char * pImplementationName,void * pServiceManager,void * pRegistryKey)215 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory (
216 	const sal_Char * pImplementationName, void * pServiceManager, void * pRegistryKey)
217 {
218 	return cppu::component_getFactoryHelper (
219 		pImplementationName, pServiceManager, pRegistryKey, g_entries);
220 }
221 
222 } // extern "C"
223