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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2017 Jarosław Staniek <staniek@kde.org>
0003    Copyright (C) 2012 Dimitrios T. Tanis <dimitrios.tanis@kdemail.net>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this program; see the file COPYING.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #ifndef KEXICONNECTIONSELECTORWIDGET_H
0022 #define KEXICONNECTIONSELECTORWIDGET_H
0023 
0024 #include <core/kexidbconnectionset.h>
0025 #include <KexiFileFilters.h>
0026 #include "kexiextwidgets_export.h"
0027 
0028 #include <QTreeWidgetItem>
0029 
0030 class QAbstractButton;
0031 class KDbDriverMetaData;
0032 
0033 //! An item for a single database connection
0034 class KEXIEXTWIDGETS_EXPORT ConnectionDataLVItem : public QTreeWidgetItem
0035 {
0036 public:
0037     ConnectionDataLVItem(KDbConnectionData *data,
0038                          const KDbDriverMetaData &driverMetaData, QTreeWidget* list);
0039     ~ConnectionDataLVItem();
0040 
0041     void update(const KDbDriverMetaData& driverMetaData);
0042 
0043     using QTreeWidgetItem::data;
0044     KDbConnectionData *data() const {
0045         return m_data;
0046     }
0047 
0048 protected:
0049     KDbConnectionData *m_data;
0050 };
0051 
0052 //! @short Widget that allows to select a database connection (file- or server-based)
0053 /*! The widget allows to select database connection without choosing database itself.
0054 */
0055 class KEXIEXTWIDGETS_EXPORT KexiConnectionSelectorWidget : public QWidget
0056 {
0057     Q_OBJECT
0058 
0059 public:
0060     //! Defines connection type
0061     enum ConnectionType {
0062         FileBased = 1, //!< the widget displays file-based connection
0063         ServerBased = 2 //!< the widget displays server-based connection
0064     };
0065 
0066     //! Defines operation mode
0067     enum OperationMode {
0068         Opening = 1,
0069         Saving = 2
0070     };
0071 
0072     /*! Constructs a new widget which contains \a conn_set as connection set.
0073      \a conn_set can be altered, because Add/Edit/Remove buttons are available
0074      to users. \a startDirOrVariable can be provided to specify a start dir for file browser
0075      (it can also contain a configuration variable name with "kfiledialog:///" prefix
0076      as described in KRecentDirs documentation). */
0077     //! @todo KEXI3 add equivalent of kfiledialog:/// for startDirOrVariable
0078     KexiConnectionSelectorWidget(KexiDBConnectionSet *conn_set,
0079                                  const QUrl& startDirOrVariable,
0080                                  OperationMode mode,
0081                                  QWidget* parent = 0);
0082 
0083     virtual ~KexiConnectionSelectorWidget();
0084 
0085     /*! After accepting this dialog this method returns wherher user selected
0086      file- or server-based connection. */
0087     ConnectionType selectedConnectionType() const;
0088 
0089     /*! \return data of selected connection, if server-based connection was selected.
0090      Returns NULL if no selection has been made or file-based connection
0091      has been selected.
0092      @see selectedConnectionType()
0093     */
0094     KDbConnectionData* selectedConnectionData() const;
0095 
0096     /*! \return the name of database file, if file-based connection was selected.
0097      Returns empty string if no selection has been made or server-based connection
0098      has been selected.
0099     //! @note Call checkSelectedFile() first
0100      @see selectedConnectionType()
0101     */
0102     QString selectedFile() const;
0103 
0104     QTreeWidget* connectionsList() const;
0105 
0106     bool confirmOverwrites() const;
0107 
0108     bool hasSelectedConnection() const;
0109 
0110     /*! @return true if the current file URL meets requied constraints (i.e. the file exists)
0111      Shows appropriate message box if needed. */
0112     bool checkSelectedFile();
0113 
0114     //! @return highlighted file
0115     QString highlightedFile() const;
0116 
0117 Q_SIGNALS:
0118     void connectionItemExecuted(ConnectionDataLVItem *item);
0119     void connectionItemHighlighted(ConnectionDataLVItem *item);
0120     void connectionSelected(bool hasSelected);
0121     void fileSelected(const QString &name);
0122 
0123 public Q_SLOTS:
0124     void showSimpleConnection();
0125     void showAdvancedConnection();
0126     virtual void setFocus();
0127 
0128     /*! Hides helpers on the server based connection page
0129       (sometimes it's convenient not to have these):
0130     - "Select existing database server's connection..." (label at the top)
0131     - "Click "Back" button" (label at the bottom)
0132     - "Back" button itself */
0133     void hideHelpers();
0134     void hideConnectonIcon();
0135     void hideDescription();
0136 
0137     /*! Sets selected filename to @a name.
0138      Only works when selectedConnectionType()==FileBased. */
0139     void setSelectedFile(const QString &name);
0140 
0141     /*! If true, user will be asked to accept overwriting existing project.
0142      This is true by default. */
0143     void setConfirmOverwrites(bool set);
0144 
0145     void setFileMode(KexiFileFilters::Mode mode);
0146 
0147     void setAdditionalMimeTypes(const QStringList &mimeTypes);
0148 
0149     //! Sets excluded mime types
0150     void setExcludedMimeTypes(const QStringList& mimeTypes);
0151 
0152     void setFileWidgetFrameVisible(bool set);
0153 
0154 protected Q_SLOTS:
0155     void slotConnectionItemExecuted(QTreeWidgetItem *item);
0156     void slotConnectionItemExecuted();
0157     void slotRemoteAddBtnClicked();
0158     void slotRemoteEditBtnClicked();
0159     void slotRemoteRemoveBtnClicked();
0160     void slotConnectionSelectionChanged();
0161     void slotPrjTypeSelected(QAbstractButton *btn);
0162     void slotFileConnectionSelected(const QString &name);
0163     void slotConnectionSelected();
0164 
0165 protected:
0166     virtual bool eventFilter(QObject* watched, QEvent* event) override;
0167 
0168 private:
0169     ConnectionDataLVItem* addConnectionData(KDbConnectionData* data);
0170     ConnectionDataLVItem* selectedConnectionDataItem() const;
0171 
0172     class Private;
0173     Private * const d;
0174 };
0175 
0176 #endif // KEXICONNECTIONSELECTORWIDGET_H