Update git submodules
[LibreOffice.git] / vcl / qt5 / Qt5FilePicker.cxx
blob18e24580107eceefe79ebc87ad4ca46674206efa
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 <Qt5FilePicker.hxx>
21 #include <Qt5FilePicker.moc>
23 #include <Qt5Frame.hxx>
24 #include <Qt5Tools.hxx>
25 #include <Qt5Widget.hxx>
26 #include <Qt5Instance.hxx>
28 #include <com/sun/star/awt/SystemDependentXWindow.hpp>
29 #include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
30 #include <com/sun/star/awt/XWindow.hpp>
31 #include <com/sun/star/frame/Desktop.hpp>
32 #include <com/sun/star/frame/TerminationVetoException.hpp>
33 #include <com/sun/star/frame/XDesktop.hpp>
34 #include <com/sun/star/lang/DisposedException.hpp>
35 #include <com/sun/star/lang/IllegalArgumentException.hpp>
36 #include <com/sun/star/lang/SystemDependent.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
39 #include <com/sun/star/ui/dialogs/ControlActions.hpp>
40 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
41 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
42 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
43 #include <com/sun/star/uri/ExternalUriReferenceTranslator.hpp>
44 #include <cppuhelper/interfacecontainer.h>
45 #include <cppuhelper/supportsservice.hxx>
46 #include <rtl/process.h>
47 #include <sal/log.hxx>
49 #include <QtCore/QDebug>
50 #include <QtCore/QRegularExpression>
51 #include <QtCore/QThread>
52 #include <QtCore/QUrl>
53 #include <QtGui/QClipboard>
54 #include <QtGui/QWindow>
55 #include <QtWidgets/QApplication>
56 #include <QtWidgets/QCheckBox>
57 #include <QtWidgets/QComboBox>
58 #include <QtWidgets/QGridLayout>
59 #include <QtWidgets/QHBoxLayout>
60 #include <QtWidgets/QLabel>
61 #include <QtWidgets/QMessageBox>
62 #include <QtWidgets/QPushButton>
63 #include <QtWidgets/QWidget>
65 #include <unx/geninst.h>
66 #include <strings.hrc>
68 using namespace ::com::sun::star;
69 using namespace ::com::sun::star::ui::dialogs;
70 using namespace ::com::sun::star::ui::dialogs::TemplateDescription;
71 using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds;
72 using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds;
73 using namespace ::com::sun::star::lang;
74 using namespace ::com::sun::star::beans;
75 using namespace ::com::sun::star::uno;
77 namespace
79 uno::Sequence<OUString> FilePicker_getSupportedServiceNames()
81 return { "com.sun.star.ui.dialogs.FilePicker", "com.sun.star.ui.dialogs.SystemFilePicker",
82 "com.sun.star.ui.dialogs.Qt5FilePicker" };
86 Qt5FilePicker::Qt5FilePicker(css::uno::Reference<css::uno::XComponentContext> const& context,
87 QFileDialog::FileMode eMode, bool bUseNative)
88 : Qt5FilePicker_Base(m_aHelperMutex)
89 , m_context(context)
90 , m_bIsFolderPicker(eMode == QFileDialog::Directory)
91 , m_pParentWidget(nullptr)
92 , m_pFileDialog(new QFileDialog(nullptr, {}, QDir::homePath()))
93 , m_pExtraControls(new QWidget())
95 m_pFileDialog->setOption(QFileDialog::DontUseNativeDialog, !bUseNative);
97 m_pFileDialog->setFileMode(eMode);
98 m_pFileDialog->setWindowModality(Qt::ApplicationModal);
100 if (m_bIsFolderPicker)
102 m_pFileDialog->setOption(QFileDialog::ShowDirsOnly, true);
103 m_pFileDialog->setWindowTitle(toQString(VclResId(STR_FPICKER_FOLDER_DEFAULT_TITLE)));
106 m_pLayout = dynamic_cast<QGridLayout*>(m_pFileDialog->layout());
108 setMultiSelectionMode(false);
110 // XFilePickerListener notifications
111 connect(m_pFileDialog.get(), SIGNAL(filterSelected(const QString&)), this,
112 SLOT(filterSelected(const QString&)));
113 connect(m_pFileDialog.get(), SIGNAL(currentChanged(const QString&)), this,
114 SLOT(currentChanged(const QString&)));
116 // update automatic file extension when filter is changed
117 connect(m_pFileDialog.get(), SIGNAL(filterSelected(const QString&)), this,
118 SLOT(updateAutomaticFileExtension()));
121 Qt5FilePicker::~Qt5FilePicker()
123 SolarMutexGuard g;
124 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
125 assert(pSalInst);
126 pSalInst->RunInMainThread([this]() {
127 // must delete it in main thread, otherwise
128 // QSocketNotifier::setEnabled() will crash us
129 m_pFileDialog.reset();
133 void SAL_CALL
134 Qt5FilePicker::addFilePickerListener(const uno::Reference<XFilePickerListener>& xListener)
136 SolarMutexGuard aGuard;
137 m_xListener = xListener;
140 void SAL_CALL Qt5FilePicker::removeFilePickerListener(const uno::Reference<XFilePickerListener>&)
142 SolarMutexGuard aGuard;
143 m_xListener.clear();
146 void SAL_CALL Qt5FilePicker::setTitle(const OUString& title)
148 SolarMutexGuard g;
149 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
150 assert(pSalInst);
151 pSalInst->RunInMainThread(
152 [this, &title]() { m_pFileDialog->setWindowTitle(toQString(title)); });
155 sal_Int16 SAL_CALL Qt5FilePicker::execute()
157 SolarMutexGuard g;
158 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
159 assert(pSalInst);
160 if (!pSalInst->IsMainThread())
162 sal_uInt16 ret;
163 pSalInst->RunInMainThread([&ret, this]() { ret = execute(); });
164 return ret;
167 QWidget* pTransientParent = m_pParentWidget;
168 if (!pTransientParent)
170 vcl::Window* pWindow = ::Application::GetActiveTopWindow();
171 if (pWindow)
173 Qt5Frame* pFrame = dynamic_cast<Qt5Frame*>(pWindow->ImplGetFrame());
174 assert(pFrame);
175 if (pFrame)
176 pTransientParent = pFrame->asChild();
180 if (!m_aNamedFilterList.isEmpty())
181 m_pFileDialog->setNameFilters(m_aNamedFilterList);
182 if (!m_aCurrentFilter.isEmpty())
183 m_pFileDialog->selectNameFilter(m_aCurrentFilter);
185 updateAutomaticFileExtension();
187 uno::Reference<css::frame::XDesktop> xDesktop(css::frame::Desktop::create(m_context),
188 UNO_QUERY_THROW);
190 // will hide the window, so do before show
191 m_pFileDialog->setParent(pTransientParent, m_pFileDialog->windowFlags());
192 m_pFileDialog->show();
193 xDesktop->addTerminateListener(this);
194 int result = m_pFileDialog->exec();
195 xDesktop->removeTerminateListener(this);
196 m_pFileDialog->setParent(nullptr, m_pFileDialog->windowFlags());
198 if (QFileDialog::Rejected == result)
199 return ExecutableDialogResults::CANCEL;
200 return ExecutableDialogResults::OK;
203 void SAL_CALL Qt5FilePicker::setMultiSelectionMode(sal_Bool multiSelect)
205 SolarMutexGuard g;
206 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
207 assert(pSalInst);
208 pSalInst->RunInMainThread([this, multiSelect]() {
209 if (m_bIsFolderPicker || m_pFileDialog->acceptMode() == QFileDialog::AcceptSave)
210 return;
212 m_pFileDialog->setFileMode(multiSelect ? QFileDialog::ExistingFiles
213 : QFileDialog::ExistingFile);
217 void SAL_CALL Qt5FilePicker::setDefaultName(const OUString& name)
219 SolarMutexGuard g;
220 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
221 assert(pSalInst);
222 pSalInst->RunInMainThread([this, &name]() { m_pFileDialog->selectFile(toQString(name)); });
225 void SAL_CALL Qt5FilePicker::setDisplayDirectory(const OUString& dir)
227 SolarMutexGuard g;
228 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
229 assert(pSalInst);
230 pSalInst->RunInMainThread([this, &dir]() {
231 QString qDir(toQString(dir));
232 m_pFileDialog->setDirectoryUrl(QUrl(qDir));
236 OUString SAL_CALL Qt5FilePicker::getDisplayDirectory()
238 SolarMutexGuard g;
239 OUString ret;
240 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
241 assert(pSalInst);
242 pSalInst->RunInMainThread(
243 [&ret, this]() { ret = toOUString(m_pFileDialog->directoryUrl().toString()); });
244 return ret;
247 uno::Sequence<OUString> SAL_CALL Qt5FilePicker::getFiles()
249 uno::Sequence<OUString> seq = getSelectedFiles();
250 if (seq.getLength() > 1)
251 seq.realloc(1);
252 return seq;
255 uno::Sequence<OUString> SAL_CALL Qt5FilePicker::getSelectedFiles()
257 SolarMutexGuard g;
258 QList<QUrl> urls;
259 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
260 assert(pSalInst);
261 pSalInst->RunInMainThread([&urls, this]() { urls = m_pFileDialog->selectedUrls(); });
263 uno::Sequence<OUString> seq(urls.size());
265 auto const trans = css::uri::ExternalUriReferenceTranslator::create(m_context);
266 size_t i = 0;
267 for (const QUrl& aURL : urls)
269 // Unlike LO, QFileDialog (<https://doc.qt.io/qt-5/qfiledialog.html>) apparently always
270 // treats file-system pathnames as UTF-8--encoded, regardless of LANG/LC_CTYPE locale
271 // setting. And pathnames containing byte sequences that are not valid UTF-8 are apparently
272 // filtered out and not even displayed by QFileDialog, so aURL will always have a "payload"
273 // that matches the pathname's byte sequence. So the pathname's byte sequence (which
274 // happens to also be aURL's payload) in the LANG/LC_CTYPE encoding needs to be converted
275 // into LO's internal UTF-8 file URL encoding via
276 // XExternalUriReferenceTranslator::translateToInternal (which looks somewhat paradoxical as
277 // aURL.toEncoded() nominally already has a UTF-8 payload):
278 auto const extUrl = toOUString(aURL.toEncoded());
279 auto intUrl = trans->translateToInternal(extUrl);
280 if (intUrl.isEmpty())
282 // If translation failed, fall back to original URL:
283 SAL_WARN("vcl.qt5", "cannot convert <" << extUrl << "> from locale encoding to UTF-8");
284 intUrl = extUrl;
286 seq[i++] = intUrl;
289 return seq;
292 void SAL_CALL Qt5FilePicker::appendFilter(const OUString& title, const OUString& filter)
294 SolarMutexGuard g;
295 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
296 assert(pSalInst);
297 if (!pSalInst->IsMainThread())
299 pSalInst->RunInMainThread([this, &title, &filter]() { appendFilter(title, filter); });
300 return;
303 // '/' need to be escaped else they are assumed to be mime types
304 QString sTitle = toQString(title).replace("/", "\\/");
306 QString sFilterName = sTitle;
307 // the Qt5 non-native file picker adds the extensions to the filter title, so strip them
308 if (m_pFileDialog->testOption(QFileDialog::DontUseNativeDialog))
310 int pos = sFilterName.indexOf(" (");
311 if (pos >= 0)
312 sFilterName.truncate(pos);
315 QString sGlobFilter = toQString(filter);
317 // LibreOffice gives us filters separated by ';' qt dialogs just want space separated
318 sGlobFilter.replace(";", " ");
320 // make sure "*.*" is not used as "all files"
321 sGlobFilter.replace("*.*", "*");
323 m_aNamedFilterList << QStringLiteral("%1 (%2)").arg(sFilterName, sGlobFilter);
324 m_aTitleToFilterMap[sTitle] = m_aNamedFilterList.constLast();
325 m_aNamedFilterToExtensionMap[m_aNamedFilterList.constLast()] = sGlobFilter;
328 void SAL_CALL Qt5FilePicker::setCurrentFilter(const OUString& title)
330 SolarMutexGuard g;
331 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
332 assert(pSalInst);
333 pSalInst->RunInMainThread([this, &title]() {
334 m_aCurrentFilter = m_aTitleToFilterMap.value(toQString(title).replace("/", "\\/"));
338 OUString SAL_CALL Qt5FilePicker::getCurrentFilter()
340 SolarMutexGuard g;
341 QString filter;
342 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
343 assert(pSalInst);
344 pSalInst->RunInMainThread([&filter, this]() {
345 filter = m_aTitleToFilterMap.key(m_pFileDialog->selectedNameFilter());
348 if (filter.isEmpty())
349 filter = "ODF Text Document (.odt)";
350 return toOUString(filter);
353 void SAL_CALL Qt5FilePicker::appendFilterGroup(const OUString& rGroupTitle,
354 const uno::Sequence<beans::StringPair>& filters)
356 SolarMutexGuard g;
357 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
358 assert(pSalInst);
359 if (!pSalInst->IsMainThread())
361 pSalInst->RunInMainThread(
362 [this, &rGroupTitle, &filters]() { appendFilterGroup(rGroupTitle, filters); });
363 return;
366 const sal_uInt16 length = filters.getLength();
367 for (sal_uInt16 i = 0; i < length; ++i)
369 beans::StringPair aPair = filters[i];
370 appendFilter(aPair.First, aPair.Second);
374 uno::Any Qt5FilePicker::handleGetListValue(const QComboBox* pWidget, sal_Int16 nControlAction)
376 uno::Any aAny;
377 switch (nControlAction)
379 case ControlActions::GET_ITEMS:
381 Sequence<OUString> aItemList(pWidget->count());
382 for (sal_Int32 i = 0; i < pWidget->count(); ++i)
383 aItemList[i] = toOUString(pWidget->itemText(i));
384 aAny <<= aItemList;
385 break;
387 case ControlActions::GET_SELECTED_ITEM:
389 if (!pWidget->currentText().isEmpty())
390 aAny <<= toOUString(pWidget->currentText());
391 break;
393 case ControlActions::GET_SELECTED_ITEM_INDEX:
395 if (pWidget->currentIndex() >= 0)
396 aAny <<= static_cast<sal_Int32>(pWidget->currentIndex());
397 break;
399 default:
400 SAL_WARN("vcl.qt5",
401 "undocumented/unimplemented ControlAction for a list " << nControlAction);
402 break;
404 return aAny;
407 void Qt5FilePicker::handleSetListValue(QComboBox* pWidget, sal_Int16 nControlAction,
408 const uno::Any& rValue)
410 switch (nControlAction)
412 case ControlActions::ADD_ITEM:
414 OUString sItem;
415 rValue >>= sItem;
416 pWidget->addItem(toQString(sItem));
417 break;
419 case ControlActions::ADD_ITEMS:
421 Sequence<OUString> aStringList;
422 rValue >>= aStringList;
423 for (auto const& sItem : std::as_const(aStringList))
424 pWidget->addItem(toQString(sItem));
425 break;
427 case ControlActions::DELETE_ITEM:
429 sal_Int32 nPos = 0;
430 rValue >>= nPos;
431 pWidget->removeItem(nPos);
432 break;
434 case ControlActions::DELETE_ITEMS:
436 pWidget->clear();
437 break;
439 case ControlActions::SET_SELECT_ITEM:
441 sal_Int32 nPos = 0;
442 rValue >>= nPos;
443 pWidget->setCurrentIndex(nPos);
444 break;
446 default:
447 SAL_WARN("vcl.qt5",
448 "undocumented/unimplemented ControlAction for a list " << nControlAction);
449 break;
452 pWidget->setEnabled(pWidget->count() > 0);
455 void SAL_CALL Qt5FilePicker::setValue(sal_Int16 controlId, sal_Int16 nControlAction,
456 const uno::Any& value)
458 SolarMutexGuard g;
459 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
460 assert(pSalInst);
461 if (!pSalInst->IsMainThread())
463 pSalInst->RunInMainThread([this, controlId, nControlAction, &value]() {
464 setValue(controlId, nControlAction, value);
466 return;
469 if (m_aCustomWidgetsMap.contains(controlId))
471 QWidget* widget = m_aCustomWidgetsMap.value(controlId);
472 QCheckBox* cb = dynamic_cast<QCheckBox*>(widget);
473 if (cb)
474 cb->setChecked(value.get<bool>());
475 else
477 QComboBox* combo = dynamic_cast<QComboBox*>(widget);
478 if (combo)
479 handleSetListValue(combo, nControlAction, value);
482 else
483 SAL_WARN("vcl.qt5", "set value on unknown control " << controlId);
486 uno::Any SAL_CALL Qt5FilePicker::getValue(sal_Int16 controlId, sal_Int16 nControlAction)
488 SolarMutexGuard g;
489 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
490 assert(pSalInst);
491 if (!pSalInst->IsMainThread())
493 uno::Any ret;
494 pSalInst->RunInMainThread([&ret, this, controlId, nControlAction]() {
495 ret = getValue(controlId, nControlAction);
497 return ret;
500 uno::Any res(false);
501 if (m_aCustomWidgetsMap.contains(controlId))
503 QWidget* widget = m_aCustomWidgetsMap.value(controlId);
504 QCheckBox* cb = dynamic_cast<QCheckBox*>(widget);
505 if (cb)
506 res <<= cb->isChecked();
507 else
509 QComboBox* combo = dynamic_cast<QComboBox*>(widget);
510 if (combo)
511 res = handleGetListValue(combo, nControlAction);
514 else
515 SAL_WARN("vcl.qt5", "get value on unknown control " << controlId);
517 return res;
520 void SAL_CALL Qt5FilePicker::enableControl(sal_Int16 controlId, sal_Bool enable)
522 SolarMutexGuard g;
523 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
524 assert(pSalInst);
525 pSalInst->RunInMainThread([this, controlId, enable]() {
526 if (m_aCustomWidgetsMap.contains(controlId))
527 m_aCustomWidgetsMap.value(controlId)->setEnabled(enable);
528 else
529 SAL_WARN("vcl.qt5", "enable unknown control " << controlId);
533 void SAL_CALL Qt5FilePicker::setLabel(sal_Int16 controlId, const OUString& label)
535 SolarMutexGuard g;
536 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
537 assert(pSalInst);
538 if (!pSalInst->IsMainThread())
540 pSalInst->RunInMainThread([this, controlId, label]() { setLabel(controlId, label); });
541 return;
544 if (m_aCustomWidgetsMap.contains(controlId))
546 QCheckBox* cb = dynamic_cast<QCheckBox*>(m_aCustomWidgetsMap.value(controlId));
547 if (cb)
548 cb->setText(toQString(label));
550 else
551 SAL_WARN("vcl.qt5", "set label on unknown control " << controlId);
554 OUString SAL_CALL Qt5FilePicker::getLabel(sal_Int16 controlId)
556 SolarMutexGuard g;
557 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
558 assert(pSalInst);
559 if (!pSalInst->IsMainThread())
561 OUString ret;
562 pSalInst->RunInMainThread([&ret, this, controlId]() { ret = getLabel(controlId); });
563 return ret;
566 QString label;
567 if (m_aCustomWidgetsMap.contains(controlId))
569 QCheckBox* cb = dynamic_cast<QCheckBox*>(m_aCustomWidgetsMap.value(controlId));
570 if (cb)
571 label = cb->text();
573 else
574 SAL_WARN("vcl.qt5", "get label on unknown control " << controlId);
576 return toOUString(label);
579 QString Qt5FilePicker::getResString(const char* pResId)
581 QString aResString;
583 if (pResId == nullptr)
584 return aResString;
586 aResString = toQString(VclResId(pResId));
588 return aResString.replace('~', '&');
591 void Qt5FilePicker::addCustomControl(sal_Int16 controlId)
593 QWidget* widget = nullptr;
594 QLabel* label = nullptr;
595 const char* resId = nullptr;
596 QCheckBox* pCheckbox = nullptr;
598 switch (controlId)
600 case CHECKBOX_AUTOEXTENSION:
601 resId = STR_FPICKER_AUTO_EXTENSION;
602 break;
603 case CHECKBOX_PASSWORD:
604 resId = STR_FPICKER_PASSWORD;
605 break;
606 case CHECKBOX_FILTEROPTIONS:
607 resId = STR_FPICKER_FILTER_OPTIONS;
608 break;
609 case CHECKBOX_READONLY:
610 resId = STR_FPICKER_READONLY;
611 break;
612 case CHECKBOX_LINK:
613 resId = STR_FPICKER_INSERT_AS_LINK;
614 break;
615 case CHECKBOX_PREVIEW:
616 resId = STR_FPICKER_SHOW_PREVIEW;
617 break;
618 case CHECKBOX_SELECTION:
619 resId = STR_FPICKER_SELECTION;
620 break;
621 case CHECKBOX_GPGENCRYPTION:
622 resId = STR_FPICKER_GPGENCRYPT;
623 break;
624 case PUSHBUTTON_PLAY:
625 resId = STR_FPICKER_PLAY;
626 break;
627 case LISTBOX_VERSION:
628 resId = STR_FPICKER_VERSION;
629 break;
630 case LISTBOX_TEMPLATE:
631 resId = STR_FPICKER_TEMPLATES;
632 break;
633 case LISTBOX_IMAGE_TEMPLATE:
634 resId = STR_FPICKER_IMAGE_TEMPLATE;
635 break;
636 case LISTBOX_IMAGE_ANCHOR:
637 resId = STR_FPICKER_IMAGE_ANCHOR;
638 break;
639 case LISTBOX_VERSION_LABEL:
640 case LISTBOX_TEMPLATE_LABEL:
641 case LISTBOX_IMAGE_TEMPLATE_LABEL:
642 case LISTBOX_IMAGE_ANCHOR_LABEL:
643 case LISTBOX_FILTER_SELECTOR:
644 break;
647 switch (controlId)
649 case CHECKBOX_AUTOEXTENSION:
650 pCheckbox = new QCheckBox(getResString(resId), m_pExtraControls);
651 // to add/remove automatic file extension based on checkbox
652 connect(pCheckbox, SIGNAL(stateChanged(int)), this,
653 SLOT(updateAutomaticFileExtension()));
654 widget = pCheckbox;
655 break;
656 case CHECKBOX_PASSWORD:
657 case CHECKBOX_FILTEROPTIONS:
658 case CHECKBOX_READONLY:
659 case CHECKBOX_LINK:
660 case CHECKBOX_PREVIEW:
661 case CHECKBOX_SELECTION:
662 case CHECKBOX_GPGENCRYPTION:
663 widget = new QCheckBox(getResString(resId), m_pExtraControls);
664 break;
665 case PUSHBUTTON_PLAY:
666 break;
667 case LISTBOX_VERSION:
668 case LISTBOX_TEMPLATE:
669 case LISTBOX_IMAGE_ANCHOR:
670 case LISTBOX_IMAGE_TEMPLATE:
671 case LISTBOX_FILTER_SELECTOR:
672 label = new QLabel(getResString(resId), m_pExtraControls);
673 widget = new QComboBox(m_pExtraControls);
674 label->setBuddy(widget);
675 break;
676 case LISTBOX_VERSION_LABEL:
677 case LISTBOX_TEMPLATE_LABEL:
678 case LISTBOX_IMAGE_TEMPLATE_LABEL:
679 case LISTBOX_IMAGE_ANCHOR_LABEL:
680 break;
683 if (widget)
685 const int row = m_pLayout->rowCount();
686 if (label)
687 m_pLayout->addWidget(label, row, 0);
688 m_pLayout->addWidget(widget, row, 1);
689 m_aCustomWidgetsMap.insert(controlId, widget);
693 void SAL_CALL Qt5FilePicker::initialize(const uno::Sequence<uno::Any>& args)
695 // parameter checking
696 uno::Any arg;
697 if (args.getLength() == 0)
698 throw lang::IllegalArgumentException("no arguments", static_cast<XFilePicker2*>(this), 1);
700 arg = args[0];
702 if ((arg.getValueType() != cppu::UnoType<sal_Int16>::get())
703 && (arg.getValueType() != cppu::UnoType<sal_Int8>::get()))
705 throw lang::IllegalArgumentException("invalid argument type",
706 static_cast<XFilePicker2*>(this), 1);
709 SolarMutexGuard g;
710 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
711 assert(pSalInst);
712 if (!pSalInst->IsMainThread())
714 pSalInst->RunInMainThread([this, args]() { initialize(args); });
715 return;
718 m_aNamedFilterToExtensionMap.clear();
719 m_aNamedFilterList.clear();
720 m_aTitleToFilterMap.clear();
721 m_aCurrentFilter.clear();
723 sal_Int16 templateId = -1;
724 arg >>= templateId;
726 QFileDialog::AcceptMode acceptMode = QFileDialog::AcceptOpen;
727 switch (templateId)
729 case FILEOPEN_SIMPLE:
730 break;
732 case FILESAVE_SIMPLE:
733 acceptMode = QFileDialog::AcceptSave;
734 break;
736 case FILESAVE_AUTOEXTENSION:
737 acceptMode = QFileDialog::AcceptSave;
738 addCustomControl(CHECKBOX_AUTOEXTENSION);
739 break;
741 case FILESAVE_AUTOEXTENSION_PASSWORD:
742 acceptMode = QFileDialog::AcceptSave;
743 addCustomControl(CHECKBOX_AUTOEXTENSION);
744 addCustomControl(CHECKBOX_PASSWORD);
745 addCustomControl(CHECKBOX_GPGENCRYPTION);
746 break;
748 case FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS:
749 acceptMode = QFileDialog::AcceptSave;
750 addCustomControl(CHECKBOX_AUTOEXTENSION);
751 addCustomControl(CHECKBOX_PASSWORD);
752 addCustomControl(CHECKBOX_GPGENCRYPTION);
753 addCustomControl(CHECKBOX_FILTEROPTIONS);
754 break;
756 case FILESAVE_AUTOEXTENSION_SELECTION:
757 acceptMode = QFileDialog::AcceptSave;
758 addCustomControl(CHECKBOX_AUTOEXTENSION);
759 addCustomControl(CHECKBOX_SELECTION);
760 break;
762 case FILESAVE_AUTOEXTENSION_TEMPLATE:
763 acceptMode = QFileDialog::AcceptSave;
764 addCustomControl(CHECKBOX_AUTOEXTENSION);
765 addCustomControl(LISTBOX_TEMPLATE);
766 break;
768 case FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE:
769 addCustomControl(CHECKBOX_LINK);
770 addCustomControl(CHECKBOX_PREVIEW);
771 addCustomControl(LISTBOX_IMAGE_TEMPLATE);
772 break;
774 case FILEOPEN_LINK_PREVIEW_IMAGE_ANCHOR:
775 addCustomControl(CHECKBOX_LINK);
776 addCustomControl(CHECKBOX_PREVIEW);
777 addCustomControl(LISTBOX_IMAGE_ANCHOR);
778 break;
780 case FILEOPEN_PLAY:
781 addCustomControl(PUSHBUTTON_PLAY);
782 break;
784 case FILEOPEN_LINK_PLAY:
785 addCustomControl(CHECKBOX_LINK);
786 addCustomControl(PUSHBUTTON_PLAY);
787 break;
789 case FILEOPEN_READONLY_VERSION:
790 addCustomControl(CHECKBOX_READONLY);
791 addCustomControl(LISTBOX_VERSION);
792 break;
794 case FILEOPEN_LINK_PREVIEW:
795 addCustomControl(CHECKBOX_LINK);
796 addCustomControl(CHECKBOX_PREVIEW);
797 break;
799 case FILEOPEN_PREVIEW:
800 addCustomControl(CHECKBOX_PREVIEW);
801 break;
803 default:
804 throw lang::IllegalArgumentException("Unknown template",
805 static_cast<XFilePicker2*>(this), 1);
808 const char* resId = nullptr;
809 switch (acceptMode)
811 case QFileDialog::AcceptOpen:
812 resId = STR_FPICKER_OPEN;
813 break;
814 case QFileDialog::AcceptSave:
815 resId = STR_FPICKER_SAVE;
816 m_pFileDialog->setFileMode(QFileDialog::AnyFile);
817 break;
820 m_pFileDialog->setAcceptMode(acceptMode);
821 m_pFileDialog->setWindowTitle(getResString(resId));
823 css::uno::Reference<css::awt::XWindow> xParentWindow;
824 if (args.getLength() > 1)
825 args[1] >>= xParentWindow;
826 if (!xParentWindow.is())
827 return;
829 css::uno::Reference<css::awt::XSystemDependentWindowPeer> xSysWinPeer(xParentWindow,
830 css::uno::UNO_QUERY);
831 if (!xSysWinPeer.is())
832 return;
834 // the sal_*Int8 handling is strange, but it's public API - no way around
835 css::uno::Sequence<sal_Int8> aProcessIdent(16);
836 rtl_getGlobalProcessId(reinterpret_cast<sal_uInt8*>(aProcessIdent.getArray()));
837 uno::Any aAny
838 = xSysWinPeer->getWindowHandle(aProcessIdent, css::lang::SystemDependent::SYSTEM_XWINDOW);
839 css::awt::SystemDependentXWindow xSysWin;
840 aAny >>= xSysWin;
842 const auto& pFrames = pSalInst->getFrames();
843 const tools::Long aWindowHandle = xSysWin.WindowHandle;
844 const auto it
845 = std::find_if(pFrames.begin(), pFrames.end(), [&aWindowHandle](auto pFrame) -> bool {
846 const SystemEnvData* pData = pFrame->GetSystemData();
847 return pData && tools::Long(pData->GetWindowHandle(pFrame)) == aWindowHandle;
849 if (it != pFrames.end())
850 m_pParentWidget = static_cast<Qt5Frame*>(*it)->asChild();
853 void SAL_CALL Qt5FilePicker::cancel() { m_pFileDialog->reject(); }
855 void SAL_CALL Qt5FilePicker::disposing(const lang::EventObject& rEvent)
857 uno::Reference<XFilePickerListener> xFilePickerListener(rEvent.Source, uno::UNO_QUERY);
859 if (xFilePickerListener.is())
861 removeFilePickerListener(xFilePickerListener);
865 void SAL_CALL Qt5FilePicker::queryTermination(const css::lang::EventObject&)
867 throw css::frame::TerminationVetoException();
870 void SAL_CALL Qt5FilePicker::notifyTermination(const css::lang::EventObject&)
872 SolarMutexGuard aGuard;
873 m_pFileDialog->reject();
876 OUString SAL_CALL Qt5FilePicker::getImplementationName()
878 return "com.sun.star.ui.dialogs.Qt5FilePicker";
881 sal_Bool SAL_CALL Qt5FilePicker::supportsService(const OUString& ServiceName)
883 return cppu::supportsService(this, ServiceName);
886 uno::Sequence<OUString> SAL_CALL Qt5FilePicker::getSupportedServiceNames()
888 return FilePicker_getSupportedServiceNames();
891 void Qt5FilePicker::updateAutomaticFileExtension()
893 bool bSetAutoExtension
894 = getValue(CHECKBOX_AUTOEXTENSION, ControlActions::GET_SELECTED_ITEM).get<bool>();
895 if (bSetAutoExtension)
897 QString sSuffix = m_aNamedFilterToExtensionMap.value(m_pFileDialog->selectedNameFilter());
898 // string is "*.<SUFFIX>" if a specific filter was selected that has exactly one possible file extension
899 if (sSuffix.lastIndexOf("*.") == 0)
901 sSuffix = sSuffix.remove("*.");
902 m_pFileDialog->setDefaultSuffix(sSuffix);
904 else
906 // fall back to setting none otherwise
907 SAL_INFO(
908 "vcl.qt5",
909 "Unable to retrieve unambiguous file extension. Will not add any automatically.");
910 bSetAutoExtension = false;
914 if (!bSetAutoExtension)
915 m_pFileDialog->setDefaultSuffix("");
918 void Qt5FilePicker::filterSelected(const QString&)
920 FilePickerEvent aEvent;
921 aEvent.ElementId = LISTBOX_FILTER;
922 SAL_INFO("vcl.qt5", "filter changed");
923 if (m_xListener.is())
924 m_xListener->controlStateChanged(aEvent);
927 void Qt5FilePicker::currentChanged(const QString&)
929 FilePickerEvent aEvent;
930 SAL_INFO("vcl.qt5", "file selection changed");
931 if (m_xListener.is())
932 m_xListener->fileSelectionChanged(aEvent);
935 OUString Qt5FilePicker::getDirectory()
937 uno::Sequence<OUString> seq = getSelectedFiles();
938 if (seq.getLength() > 1)
939 seq.realloc(1);
940 return seq[0];
943 void Qt5FilePicker::setDescription(const OUString&) {}
945 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */