Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / qt5 / QtFilePicker.hxx
blob58824adbbd4844f2d1b9b2aa6273238d46eae488
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 #pragma once
22 #include <vclpluginapi.h>
24 #include <cppuhelper/compbase.hxx>
26 #include <com/sun/star/frame/XTerminateListener.hpp>
27 #include <com/sun/star/lang/XInitialization.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp>
30 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
31 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
32 #include <com/sun/star/ui/dialogs/XFolderPicker2.hpp>
33 #include <com/sun/star/uno/XComponentContext.hpp>
35 #include <osl/conditn.hxx>
36 #include <osl/mutex.hxx>
37 #include <unotools/resmgr.hxx>
39 #include <QtCore/QObject>
40 #include <QtCore/QString>
41 #include <QtCore/QStringList>
42 #include <QtCore/QHash>
43 #include <QtWidgets/QFileDialog>
45 #include <memory>
47 class QComboBox;
48 class QGridLayout;
49 class QLabel;
50 class QWidget;
52 typedef ::cppu::WeakComponentImplHelper<
53 css::frame::XTerminateListener, css::lang::XInitialization, css::lang::XServiceInfo,
54 css::ui::dialogs::XFilePicker3, css::ui::dialogs::XFilePickerControlAccess,
55 css::ui::dialogs::XAsynchronousExecutableDialog, css::ui::dialogs::XFolderPicker2>
56 QtFilePicker_Base;
58 class VCLPLUG_QT_PUBLIC QtFilePicker : public QObject, public QtFilePicker_Base
60 Q_OBJECT
62 private:
63 css::uno::Reference<css::uno::XComponentContext> m_context;
65 css::uno::Reference<css::ui::dialogs::XFilePickerListener> m_xListener;
66 css::uno::Reference<css::ui::dialogs::XDialogClosedListener> m_xClosedListener;
68 osl::Mutex m_aHelperMutex; ///< mutex used by the WeakComponentImplHelper
70 QStringList m_aNamedFilterList; ///< to keep the original sequence
71 QHash<QString, QString> m_aTitleToFilterMap;
72 // to retrieve the filename extension for a given filter
73 QHash<QString, QString> m_aNamedFilterToExtensionMap;
74 QString m_aCurrentFilter;
76 QGridLayout* m_pLayout; ///< layout for extra custom controls
77 QHash<sal_Int16, QWidget*> m_aCustomWidgetsMap; ///< map of SAL control ID's to widget
79 const bool m_bIsFolderPicker;
81 QWidget* m_pParentWidget;
83 protected:
84 std::unique_ptr<QFileDialog> m_pFileDialog; ///< the file picker dialog
85 QWidget* m_pExtraControls; ///< widget to contain extra custom controls
87 public:
88 // use non-native file dialog by default; there's no easy way to add custom widgets
89 // in a generic way in the native one
90 explicit QtFilePicker(css::uno::Reference<css::uno::XComponentContext> context,
91 QFileDialog::FileMode, bool bUseNative = false);
92 virtual ~QtFilePicker() override;
94 // XFilePickerNotifier
95 virtual void SAL_CALL addFilePickerListener(
96 const css::uno::Reference<css::ui::dialogs::XFilePickerListener>& xListener) override;
97 virtual void SAL_CALL removeFilePickerListener(
98 const css::uno::Reference<css::ui::dialogs::XFilePickerListener>& xListener) override;
100 // XFilterManager functions
101 virtual void SAL_CALL appendFilter(const OUString& rTitle, const OUString& rFilter) override;
102 virtual void SAL_CALL setCurrentFilter(const OUString& rTitle) override;
103 virtual OUString SAL_CALL getCurrentFilter() override;
105 // XFilterGroupManager functions
106 virtual void SAL_CALL
107 appendFilterGroup(const OUString& rGroupTitle,
108 const css::uno::Sequence<css::beans::StringPair>& rFilters) override;
110 // XCancellable
111 virtual void SAL_CALL cancel() override;
113 // XExecutableDialog functions
114 virtual void SAL_CALL setTitle(const OUString& rTitle) override;
115 virtual sal_Int16 SAL_CALL execute() override;
117 // XAsynchronousExecutableDialog functions
118 virtual void SAL_CALL setDialogTitle(const OUString&) override;
119 virtual void SAL_CALL
120 startExecuteModal(const css::uno::Reference<css::ui::dialogs::XDialogClosedListener>&) override;
122 // XFilePicker functions
123 virtual void SAL_CALL setMultiSelectionMode(sal_Bool bMode) override;
124 virtual void SAL_CALL setDefaultName(const OUString& rName) override;
125 virtual void SAL_CALL setDisplayDirectory(const OUString& rDirectory) override;
126 virtual OUString SAL_CALL getDisplayDirectory() override;
127 virtual css::uno::Sequence<OUString> SAL_CALL getFiles() override;
129 // XFilePickerControlAccess functions
130 virtual void SAL_CALL setValue(sal_Int16 nControlId, sal_Int16 nControlAction,
131 const css::uno::Any& rValue) override;
132 virtual css::uno::Any SAL_CALL getValue(sal_Int16 nControlId,
133 sal_Int16 nControlAction) override;
134 virtual void SAL_CALL enableControl(sal_Int16 nControlId, sal_Bool bEnable) override;
135 virtual void SAL_CALL setLabel(sal_Int16 nControlId, const OUString& rLabel) override;
136 virtual OUString SAL_CALL getLabel(sal_Int16 nControlId) override;
138 // XFilePicker2 functions
139 virtual css::uno::Sequence<OUString> SAL_CALL getSelectedFiles() override;
141 // XInitialization
142 virtual void SAL_CALL initialize(const css::uno::Sequence<css::uno::Any>& rArguments) override;
144 // XEventListener
145 void SAL_CALL disposing(const css::lang::EventObject& rEvent) override;
146 using cppu::WeakComponentImplHelperBase::disposing;
148 // XServiceInfo
149 virtual OUString SAL_CALL getImplementationName() override;
150 virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
151 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
153 // XFolderPicker functions
154 virtual OUString SAL_CALL getDirectory() override;
155 virtual void SAL_CALL setDescription(const OUString& rDescription) override;
157 // XTerminateListener
158 void SAL_CALL queryTermination(const css::lang::EventObject& aEvent) override;
159 void SAL_CALL notifyTermination(const css::lang::EventObject& aEvent) override;
161 protected:
162 virtual void addCustomControl(sal_Int16 controlId);
163 void setCustomControlWidgetLayout(QGridLayout* pLayout) { m_pLayout = pLayout; }
165 private:
166 QtFilePicker(const QtFilePicker&) = delete;
167 QtFilePicker& operator=(const QtFilePicker&) = delete;
169 static QString getResString(TranslateId pRedId);
170 static css::uno::Any handleGetListValue(const QComboBox* pWidget, sal_Int16 nControlAction);
171 static void handleSetListValue(QComboBox* pQComboBox, sal_Int16 nAction,
172 const css::uno::Any& rValue);
174 void prepareExecute();
176 private Q_SLOTS:
177 // emit XFilePickerListener controlStateChanged event
178 void filterSelected(const QString&);
179 // emit XFilePickerListener fileSelectionChanged event
180 void currentChanged(const QString&);
181 // (un)set automatic file extension
182 virtual void updateAutomaticFileExtension();
183 void finished(int);
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */