File indexing completed on 2024-05-12 16:40:55

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2018 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KEXIFILEWIDGETINTERFACE_H
0021 #define KEXIFILEWIDGETINTERFACE_H
0022 
0023 #include "kexiextwidgets_export.h"
0024 #include <KexiFileFilters.h>
0025 #include <QWidget>
0026 
0027 //! @brief An interface for file widget supporting opening/saving files known by Kexi
0028 class KEXIEXTWIDGETS_EXPORT KexiFileWidgetInterface
0029 {
0030 public:
0031     virtual ~KexiFileWidgetInterface();
0032 
0033     /**
0034      * @brief Creates a file widget
0035      * @param startDirOrVariable A URL specifying the initial directory and/or filename,
0036      *                 or using the @c kfiledialog:/// syntax to specify a last used location.
0037      *                 Refer to the KFileWidget::KFileWidget() documentation
0038      *                 for the @c kfiledialog:/// URL syntax.
0039      * @param mode File widget's mode
0040      * @param fileName Optional file name that is added to the resulting URL.
0041      * @param parent File widget's parent widget
0042      *
0043      * Depending on settings one of two file widget implementations is used:
0044      * - if the KEXI_USE_KFILEWIDGET build option is on and KDE Plasma desktop is detected as the
0045      *   current desktop, KF5's KFileWidget-based widget is created,
0046      * - if the KEXI_USE_KFILEWIDGET build option is off or if non-KDE Plasma desktop is detected
0047      *   as the current desktop, a simple KexiFileRequester widget is created.
0048      *
0049      * In addition, if the KEXI_USE_KFILEWIDGET build option is on, defaults can be overridden by
0050      * "UseKFileWidget" boolean option in the "File Dialogs" group of the application's config file:
0051      * - if "UseKFileWidget" is @c true, KF5's KFileWidget-based widget is created,
0052      * - if "UseKFileWidget" is @c false a simple KexiFileRequester widget is created.
0053      *
0054      * To delete the override, delete the "UseKFileWidget" option in the aplication's config file.
0055      *
0056      * @return the new file widget.
0057      *
0058      * @todo Share this code with KReport and Kexi
0059      */
0060     Q_REQUIRED_RESULT static KexiFileWidgetInterface *createWidget(const QUrl &startDirOrVariable,
0061                                                  KexiFileFilters::Mode mode,
0062                                                  const QString &fileName,
0063                                                  QWidget *parent = nullptr);
0064 
0065     /**
0066      * @overload
0067      */
0068     Q_REQUIRED_RESULT static KexiFileWidgetInterface *createWidget(const QUrl &startDirOrVariable,
0069                                                  KexiFileFilters::Mode mode,
0070                                                  QWidget *parent = nullptr);
0071 
0072     /**
0073      * @brief returns this object casted to QWidget pointer
0074      */
0075     inline QWidget *widget() { return dynamic_cast<QWidget*>(this); }
0076 
0077     //! @return mode for filters used in this widget
0078     KexiFileFilters::Mode mode() const;
0079 
0080     //! Sets mode for filters to be used in this widget
0081     void setMode(KexiFileFilters::Mode mode);
0082 
0083     //! @return additional mime types
0084     QStringList additionalMimeTypes() const;
0085 
0086     //! Sets additional mime types, e.g. "text/x-csv"
0087     void setAdditionalMimeTypes(const QStringList &mimeTypes);
0088 
0089     //! @return excluded mime types
0090     QStringList excludedMimeTypes() const;
0091 
0092     //! Set excluded mime types
0093     void setExcludedMimeTypes(const QStringList &mimeTypes);
0094 
0095     /**
0096      * Returns the full path of the selected file in the local filesystem.
0097      * (Local files only)
0098      */
0099     virtual QString selectedFile() const = 0;
0100 
0101     /**
0102      * @brief Sets the file name to preselect to @p name
0103      *
0104      * This takes absolute URLs and relative file names.
0105      */
0106     virtual void setSelectedFile(const QString &name) = 0;
0107 
0108     /**
0109      * @brief Returns @c true if the current URL meets requied constraints, e.g. exists
0110      *
0111      * Shows appropriate message box if needed.
0112      */
0113     bool checkSelectedFile();
0114 
0115     /**
0116      * @brief Returns the full path of the highlighted file in the local filesystem
0117      *
0118      * (Local files only)
0119      */
0120     virtual QString highlightedFile() const = 0;
0121 
0122     //! Sets location text
0123     //! @todo
0124     //virtual void setLocationText(const QString& text) = 0;
0125 
0126     //! @return default extension
0127     QString defaultExtension() const;
0128 
0129     /**
0130      * @brief Sets default extension which will be added after accepting if user didn't provided one
0131      * This method is usable when there is more than one filter so there is no rule what extension
0132      * should be selected. By default first one is selected.
0133      */
0134     void setDefaultExtension(const QString& ext);
0135 
0136     /**
0137      * @return @c true if user should be asked to accept overwriting existing file.
0138      * @see setConfirmOverwrites
0139      */
0140     bool confirmOverwrites() const;
0141 
0142     /*! If true, user will be asked to accept overwriting existing file.
0143     This is true by default. */
0144     void setConfirmOverwrites(bool set);
0145 
0146     /**
0147      * Sets whether the line edit draws itself with a frame.
0148      */
0149     virtual void setWidgetFrame(bool set) = 0;
0150 
0151     /**
0152      * @returns the currently shown directory.
0153      * Reimplement it.
0154      */
0155     virtual QString currentDir() const;
0156 
0157     /**
0158      * @brief Connects "file hightlighted" signal to specific receiver
0159      *
0160      * Connects widget's "fileHighlighted(QString)" signal to @a receiver and @a slot. The signal
0161      * is emit when a file item is selected or highlighted.
0162      *
0163      * @note Highlighting happens mostly when user single clicks a file item and
0164      * double-click-to-select mode is enabled (see KexiUtils::activateItemsOnSingleClick()). Rather
0165      * depend on file delecting than file highlighting.
0166      *
0167      * @see connectFileSelectedSignal
0168      */
0169     void connectFileHighlightedSignal(QObject *receiver, const char *slot);
0170 
0171     /**
0172      * @brief Connects "file selected" signal to specific receiver
0173      *
0174      * Connects "fileSelected(QString)" signal of widget's returned by widget() to
0175      * @a receiver and @a slot.
0176      */
0177     void connectFileSelectedSignal(QObject *receiver, const char *slot);
0178 
0179 protected:
0180     KexiFileWidgetInterface(const QUrl &startDirOrVariable, const QString &fileName);
0181 
0182     /**
0183      * @brief Updates filters in the widget based on current filter selection.
0184      */
0185     virtual void updateFilters() = 0;
0186 
0187     /**
0188      * @brief Applies filename entered in the location edit
0189      *
0190      * Matching file item is selected on the files list if possible.
0191      */
0192     virtual void applyEnteredFileName() = 0;
0193 
0194     virtual QStringList currentFilters() const = 0;
0195 
0196     QUrl startUrl() const;
0197 
0198     void addRecentDir(const QString &name);
0199 
0200     KexiFileFilters* filters();
0201 
0202     const KexiFileFilters* filters() const;
0203 
0204     void setFiltersUpdated(bool set);
0205 
0206     bool filtersUpdated() const;
0207 
0208     void done();
0209 
0210 private:
0211     class Private;
0212     Private * const d;
0213 };
0214 
0215 #endif // KEXIFILEWIDGETINTERFACE_H