cid#1516423 Uninitialized scalar field
[LibreOffice.git] / fpicker / source / win32 / VistaFilePicker.cxx
blobdbd50307391cdab61c25ef205957892f2176120d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include "VistaFilePicker.hxx"
24 #include "WinImplHelper.hxx"
25 #include "shared.hxx"
27 #include <com/sun/star/awt/XWindow.hpp>
28 #include <com/sun/star/lang/DisposedException.hpp>
29 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
30 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
31 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
32 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
34 #include <cppuhelper/interfacecontainer.h>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <osl/file.hxx>
38 #include <officecfg/Office/Common.hxx>
40 #include <shlobj.h>
42 namespace fpicker{
43 namespace win32{
44 namespace vista{
46 VistaFilePicker::VistaFilePicker(bool bFolderPicker)
47 : TVistaFilePickerBase (m_aMutex )
48 , m_bInitialized (false )
49 , m_bFolderPicker (bFolderPicker )
53 VistaFilePicker::~VistaFilePicker()
57 void SAL_CALL VistaFilePicker::addFilePickerListener(const css::uno::Reference< css::ui::dialogs::XFilePickerListener >& xListener)
59 Request rRequest;
60 rRequest.setRequest (VistaFilePickerImpl::E_ADD_PICKER_LISTENER);
61 rRequest.setArgument(PROP_PICKER_LISTENER, xListener);
63 m_rDialog.doRequest(rRequest);
66 void SAL_CALL VistaFilePicker::removeFilePickerListener(const css::uno::Reference< css::ui::dialogs::XFilePickerListener >& xListener )
68 Request rRequest;
69 rRequest.setRequest (VistaFilePickerImpl::E_REMOVE_PICKER_LISTENER);
70 rRequest.setArgument(PROP_PICKER_LISTENER, xListener);
72 m_rDialog.doRequest(rRequest);
75 void VistaFilePicker::disposing(const css::lang::EventObject& /*aEvent*/)
79 void SAL_CALL VistaFilePicker::setMultiSelectionMode(sal_Bool bMode)
81 ensureInit();
83 Request rRequest;
84 rRequest.setRequest (VistaFilePickerImpl::E_SET_MULTISELECTION_MODE);
85 rRequest.setArgument(PROP_MULTISELECTION_MODE, bMode);
87 m_rDialog.doRequest(rRequest);
90 void SAL_CALL VistaFilePicker::setTitle(const OUString& sTitle)
92 ensureInit();
94 Request rRequest;
95 rRequest.setRequest (VistaFilePickerImpl::E_SET_TITLE);
96 rRequest.setArgument(PROP_TITLE, sTitle);
98 m_rDialog.doRequest(rRequest);
101 void SAL_CALL VistaFilePicker::appendFilter(const OUString& sTitle ,
102 const OUString& sFilter)
104 Request rRequest;
105 rRequest.setRequest (VistaFilePickerImpl::E_APPEND_FILTER);
106 rRequest.setArgument(PROP_FILTER_TITLE, sTitle );
107 rRequest.setArgument(PROP_FILTER_VALUE, sFilter);
109 m_rDialog.doRequest(rRequest);
112 void SAL_CALL VistaFilePicker::setCurrentFilter(const OUString& sTitle)
114 Request rRequest;
115 rRequest.setRequest (VistaFilePickerImpl::E_SET_CURRENT_FILTER);
116 rRequest.setArgument(PROP_FILTER_TITLE, sTitle);
118 m_rDialog.doRequest(rRequest);
121 OUString SAL_CALL VistaFilePicker::getCurrentFilter()
123 Request rRequest;
124 rRequest.setRequest (VistaFilePickerImpl::E_GET_CURRENT_FILTER);
126 m_rDialog.doRequest(rRequest);
128 const OUString sTitle = rRequest.getArgumentOrDefault(PROP_FILTER_TITLE, OUString());
129 return sTitle;
132 void SAL_CALL VistaFilePicker::appendFilterGroup(const OUString& /*sGroupTitle*/,
133 const css::uno::Sequence< css::beans::StringPair >& rFilters )
135 Request rRequest;
136 rRequest.setRequest (VistaFilePickerImpl::E_APPEND_FILTERGROUP);
137 rRequest.setArgument(PROP_FILTER_GROUP, rFilters);
139 m_rDialog.doRequest(rRequest);
142 void SAL_CALL VistaFilePicker::setDefaultName(const OUString& sName )
144 ensureInit();
146 Request rRequest;
147 rRequest.setRequest (VistaFilePickerImpl::E_SET_DEFAULT_NAME);
148 rRequest.setArgument(PROP_FILENAME, sName);
150 m_rDialog.doRequest(rRequest);
153 void SAL_CALL VistaFilePicker::setDisplayDirectory(const OUString& sDirectory)
155 ensureInit();
157 Request rRequest;
158 rRequest.setRequest (VistaFilePickerImpl::E_SET_DIRECTORY);
159 rRequest.setArgument(PROP_DIRECTORY, sDirectory);
161 m_rDialog.doRequest(rRequest);
164 OUString SAL_CALL VistaFilePicker::getDisplayDirectory()
166 ensureInit();
168 Request rRequest;
169 rRequest.setRequest (VistaFilePickerImpl::E_GET_DIRECTORY);
170 m_rDialog.doRequest(rRequest);
171 const OUString sDirectory = rRequest.getArgumentOrDefault(PROP_DIRECTORY, OUString());
173 return sDirectory;
176 // @deprecated can't be supported any longer ... see IDL description for further details
177 css::uno::Sequence< OUString > SAL_CALL VistaFilePicker::getFiles()
179 css::uno::Sequence< OUString > lFiles = getSelectedFiles();
180 // multiselection doesn't really work
181 // so just retrieve the first url
182 if (lFiles.getLength() > 1)
183 lFiles.realloc(1);
184 m_lLastFiles = lFiles;
185 return lFiles;
188 css::uno::Sequence< OUString > SAL_CALL VistaFilePicker::getSelectedFiles()
190 Request rRequest;
191 rRequest.setRequest (VistaFilePickerImpl::E_GET_SELECTED_FILES);
193 m_rDialog.doRequest(rRequest);
195 const css::uno::Sequence< OUString > lFiles = rRequest.getArgumentOrDefault(PROP_SELECTED_FILES, css::uno::Sequence< OUString >());
196 m_lLastFiles = lFiles;
197 return lFiles;
200 void VistaFilePicker::ensureInit()
202 if ( !m_bInitialized )
204 if (m_bFolderPicker)
206 Request rRequest;
207 rRequest.setRequest (VistaFilePickerImpl::E_CREATE_FOLDER_PICKER);
208 m_rDialog.doRequest(rRequest);
209 m_bInitialized = true;
211 else
213 initialize( { css::uno::Any(css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE) });
218 ::sal_Int16 SAL_CALL VistaFilePicker::execute()
220 ensureInit();
222 Request rRequest;
223 rRequest.setRequest (VistaFilePickerImpl::E_SHOW_DIALOG_MODAL);
225 // show a modal window
226 m_rDialog.doRequest(rRequest);
228 const bool bOK = rRequest.getArgumentOrDefault(PROP_DIALOG_SHOW_RESULT, false );
229 m_lLastFiles = rRequest.getArgumentOrDefault(PROP_SELECTED_FILES , css::uno::Sequence< OUString >());
231 ::sal_Int16 nResult = css::ui::dialogs::ExecutableDialogResults::CANCEL;
232 if (bOK)
233 nResult = css::ui::dialogs::ExecutableDialogResults::OK;
234 return nResult;
237 // XFilePicker
239 void SAL_CALL VistaFilePicker::setValue( ::sal_Int16 nControlId ,
240 ::sal_Int16 nControlAction,
241 const css::uno::Any& aValue )
243 Request rRequest;
244 rRequest.setRequest (VistaFilePickerImpl::E_SET_CONTROL_VALUE);
245 rRequest.setArgument(PROP_CONTROL_ID , nControlId );
246 rRequest.setArgument(PROP_CONTROL_ACTION, nControlAction);
247 rRequest.setArgument(PROP_CONTROL_VALUE , aValue );
249 m_rDialog.doRequest(rRequest);
252 css::uno::Any SAL_CALL VistaFilePicker::getValue(::sal_Int16 nControlId ,
253 ::sal_Int16 nControlAction)
255 Request rRequest;
256 rRequest.setRequest (VistaFilePickerImpl::E_GET_CONTROL_VALUE);
257 rRequest.setArgument(PROP_CONTROL_ID , nControlId );
258 rRequest.setArgument(PROP_CONTROL_ACTION, nControlAction);
260 m_rDialog.doRequest(rRequest);
261 return rRequest.getValue(PROP_CONTROL_VALUE);
264 void SAL_CALL VistaFilePicker::enableControl(::sal_Int16 nControlId,
265 sal_Bool bEnable )
267 Request rRequest;
268 rRequest.setRequest (VistaFilePickerImpl::E_ENABLE_CONTROL);
269 rRequest.setArgument(PROP_CONTROL_ID , nControlId);
270 rRequest.setArgument(PROP_CONTROL_ENABLE, bEnable );
272 m_rDialog.doRequest(rRequest);
275 void SAL_CALL VistaFilePicker::setLabel( ::sal_Int16 nControlId,
276 const OUString& sLabel )
278 Request rRequest;
279 rRequest.setRequest (VistaFilePickerImpl::E_SET_CONTROL_LABEL);
280 rRequest.setArgument(PROP_CONTROL_ID , nControlId);
281 rRequest.setArgument(PROP_CONTROL_LABEL, sLabel );
283 m_rDialog.doRequest(rRequest);
286 OUString SAL_CALL VistaFilePicker::getLabel(::sal_Int16 nControlId)
288 Request rRequest;
289 rRequest.setRequest (VistaFilePickerImpl::E_GET_CONTROL_LABEL);
290 rRequest.setArgument(PROP_CONTROL_ID, nControlId);
292 m_rDialog.doRequest(rRequest);
293 const OUString sLabel = rRequest.getArgumentOrDefault(PROP_CONTROL_LABEL, OUString());
294 return sLabel;
297 css::uno::Sequence< ::sal_Int16 > SAL_CALL VistaFilePicker::getSupportedImageFormats()
299 return css::uno::Sequence< sal_Int16 >();
302 sal_Int32 SAL_CALL VistaFilePicker::getTargetColorDepth()
304 return 0;
307 sal_Int32 SAL_CALL VistaFilePicker::getAvailableWidth()
309 return 0;
312 sal_Int32 SAL_CALL VistaFilePicker::getAvailableHeight()
314 return 0;
317 void SAL_CALL VistaFilePicker::setImage( sal_Int16 /*nImageFormat*/,
318 const css::uno::Any& /*aImage */)
322 sal_Bool SAL_CALL VistaFilePicker::setShowState(sal_Bool /*bShowState*/)
324 return false;
327 sal_Bool SAL_CALL VistaFilePicker::getShowState()
329 return false;
332 void SAL_CALL VistaFilePicker::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
334 if (lArguments.getLength() < 1)
335 throw css::lang::IllegalArgumentException(
336 "XInitialization::initialize() called without arguments.",
337 static_cast< css::ui::dialogs::XFilePicker2* >( this ),
340 sal_Int32 nTemplate = -1;
341 lArguments[0] >>= nTemplate;
343 bool bFileOpenDialog = true;
344 ::sal_Int32 nFeatures = 0;
346 switch(nTemplate)
348 case css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE :
350 bFileOpenDialog = true;
352 break;
354 case css::ui::dialogs::TemplateDescription::FILESAVE_SIMPLE :
356 bFileOpenDialog = false;
358 break;
360 case css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD :
362 bFileOpenDialog = false;
363 nFeatures |= FEATURE_AUTOEXTENSION;
364 nFeatures |= FEATURE_PASSWORD;
365 nFeatures |= FEATURE_GPGPASSWORD;
367 break;
369 case css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS :
371 bFileOpenDialog = false;
372 nFeatures |= FEATURE_AUTOEXTENSION;
373 nFeatures |= FEATURE_PASSWORD;
374 nFeatures |= FEATURE_FILTEROPTIONS;
375 nFeatures |= FEATURE_GPGPASSWORD;
377 break;
379 case css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION_SELECTION :
381 bFileOpenDialog = false;
382 nFeatures |= FEATURE_AUTOEXTENSION;
383 nFeatures |= FEATURE_SELECTION;
385 break;
387 case css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION_TEMPLATE :
389 bFileOpenDialog = false;
390 nFeatures |= FEATURE_AUTOEXTENSION;
391 nFeatures |= FEATURE_TEMPLATE;
393 break;
395 case css::ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE :
397 bFileOpenDialog = true;
398 nFeatures |= FEATURE_LINK;
399 nFeatures |= FEATURE_PREVIEW;
400 nFeatures |= FEATURE_IMAGETEMPLATE;
402 break;
404 case css::ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW_IMAGE_ANCHOR :
406 bFileOpenDialog = true;
407 nFeatures |= FEATURE_LINK;
408 nFeatures |= FEATURE_PREVIEW;
409 nFeatures |= FEATURE_IMAGEANCHOR;
411 break;
413 case css::ui::dialogs::TemplateDescription::FILEOPEN_PLAY :
415 bFileOpenDialog = true;
416 nFeatures |= FEATURE_PLAY;
418 break;
420 case css::ui::dialogs::TemplateDescription::FILEOPEN_LINK_PLAY :
422 bFileOpenDialog = true;
423 nFeatures |= FEATURE_LINK;
424 nFeatures |= FEATURE_PLAY;
426 break;
428 case css::ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION :
430 bFileOpenDialog = true;
431 nFeatures |= FEATURE_READONLY;
432 nFeatures |= FEATURE_VERSION;
434 break;
436 case css::ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW :
438 bFileOpenDialog = true;
439 nFeatures |= FEATURE_LINK;
440 nFeatures |= FEATURE_PREVIEW;
442 break;
444 case css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION :
446 bFileOpenDialog = false;
447 nFeatures |= FEATURE_AUTOEXTENSION;
449 break;
451 case css::ui::dialogs::TemplateDescription::FILEOPEN_PREVIEW :
453 bFileOpenDialog = true;
454 nFeatures |= FEATURE_PREVIEW;
456 break;
458 css::uno::Reference<css::awt::XWindow> xParentWindow;
459 if(lArguments.getLength() > 1)
461 lArguments[1] >>= xParentWindow;
463 Request rRequest;
464 if (bFileOpenDialog)
466 if (!m_bFolderPicker)
467 rRequest.setRequest(VistaFilePickerImpl::E_CREATE_OPEN_DIALOG);
468 else
469 rRequest.setRequest(VistaFilePickerImpl::E_CREATE_FOLDER_PICKER);
471 else
472 rRequest.setRequest (VistaFilePickerImpl::E_CREATE_SAVE_DIALOG);
473 rRequest.setArgument(PROP_FEATURES, nFeatures);
474 rRequest.setArgument(PROP_TEMPLATE_DESCR, nTemplate);
475 if(xParentWindow.is())
476 rRequest.setArgument(PROP_PARENT_WINDOW, xParentWindow);
477 m_rDialog.doRequest(rRequest);
479 m_bInitialized = true;
482 void SAL_CALL VistaFilePicker::cancel()
486 OUString SAL_CALL VistaFilePicker::getDirectory()
488 ensureInit();
489 css::uno::Sequence< OUString > aFileSeq = getSelectedFiles();
490 assert(aFileSeq.getLength() <= 1);
491 return aFileSeq.getLength() ? aFileSeq[0] : OUString();
494 void SAL_CALL VistaFilePicker::setDescription( const OUString& aDescription )
496 setTitle(aDescription);
499 // XServiceInfo
501 OUString SAL_CALL VistaFilePicker::getImplementationName()
503 if (m_bFolderPicker)
504 return "com.sun.star.ui.dialogs.Win32FolderPicker";
505 else
506 return "com.sun.star.ui.dialogs.Win32FilePicker";
509 sal_Bool SAL_CALL VistaFilePicker::supportsService(const OUString& sServiceName)
511 return cppu::supportsService(this, sServiceName);
514 css::uno::Sequence< OUString > SAL_CALL VistaFilePicker::getSupportedServiceNames()
516 return {
517 "com.sun.star.ui.dialogs.FilePicker",
518 "com.sun.star.ui.dialogs.SystemFilePicker",
519 "com.sun.star.ui.dialogs.SystemFolderPicker" };
522 } // namespace vista
523 } // namespace win32
524 } // namespace fpicker
526 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
527 fpicker_win32_FilePicker_get_implementation(
528 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
530 return cppu::acquire(new ::fpicker::win32::vista::VistaFilePicker(false));
533 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
534 fpicker_win32_FolderPicker_get_implementation(
535 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
537 return cppu::acquire(new ::fpicker::win32::vista::VistaFilePicker(true));
541 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */