/* This file is part of the KDE project
   Copyright (C) 2003-2015 Jarosław Staniek <staniek@kde.org>
   Copyright (C) 2012 Dimitrios T. Tanis <dimitrios.tanis@kdemail.net>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef KEXICONNECTIONSELECTORWIDGET_H
#define KEXICONNECTIONSELECTORWIDGET_H

#include "kexiextwidgets_export.h"
#include <core/kexidbconnectionset.h>
#include <kexiutils/KexiContextMessage.h>
#include <widget/KexiServerDriverNotFoundMessage.h>

#include <KFileWidget>

#include <QPointer>
#include <QTreeWidgetItem>

class QAbstractButton;
class KexiFileWidget;
class KDbDriverMetaData;

//! An item for a single database connection
class KEXIEXTWIDGETS_EXPORT ConnectionDataLVItem : public QTreeWidgetItem
{
public:
    ConnectionDataLVItem(KDbConnectionData *data,
                         const KDbDriverMetaData &driverMetaData, QTreeWidget* list);
    ~ConnectionDataLVItem();

    void update(const KDbDriverMetaData& driverMetaData);

    using QTreeWidgetItem::data;
    KDbConnectionData *data() const {
        return m_data;
    }

protected:
    KDbConnectionData *m_data;
};

//! @short Widget that allows to select a database connection (file- or server-based)
/*! The widget allows to select database connection without choosing database itself.
*/
class KEXIEXTWIDGETS_EXPORT KexiConnectionSelectorWidget : public QWidget
{
    Q_OBJECT

public:
    //! Defines connection type
    enum ConnectionType {
        FileBased = 1, //!< the widget displays file-based connection
        ServerBased = 2 //!< the widget displays server-based connection
    };

    /*! Constructs a new widget which contains \a conn_set as connection set.
     \a conn_set can be altered, because Add/Edit/Remove buttons are available
     to users. \a startDirOrVariable can be provided to specify a start dir for file browser
     (it can also contain a configuration variable name with "kfiledialog:///" prefix
     as described in KRecentDirs documentation). */
    //! @todo KEXI3 add equivalent of kfiledialog:/// for startDirOrVariable
    KexiConnectionSelectorWidget(KexiDBConnectionSet *conn_set,
                                 const QString& startDirOrVariable,
                                 KFileWidget::OperationMode fileAccessType,
                                 QWidget* parent = 0);

    virtual ~KexiConnectionSelectorWidget();

    /*! After accepting this dialog this method returns wherher user selected
     file- or server-based connection. */
    ConnectionType selectedConnectionType() const;

    /*! \return data of selected connection, if server-based connection was selected.
     Returns NULL if no selection has been made or file-based connection
     has been selected.
     @see selectedConnectionType()
    */
    KDbConnectionData* selectedConnectionData() const;

    /*! \return the name of database file, if file-based connection was selected.
     Returns null string if no selection has been made or server-based connection
     has been selected.
     @see selectedConnectionType()
    */
    QString selectedFileName();

    /*! Sets selected filename to \a fileName.
     Only works when selectedConnectionType()==FileBased. */
    void setSelectedFileName(const QString& fileName);

    QTreeWidget* connectionsList() const;

    KexiFileWidget *fileWidget;

    /*! If true, user will be asked to accept overwriting existing project.
     This is true by default. */
    void setConfirmOverwrites(bool set);

    bool confirmOverwrites() const;

    bool hasSelectedConnection() const;

Q_SIGNALS:
    void connectionItemExecuted(ConnectionDataLVItem *item);
    void connectionItemHighlighted(ConnectionDataLVItem *item);
    void connectionSelected(bool hasSelected);

public Q_SLOTS:
    void showSimpleConn();
    void showAdvancedConn();
    virtual void setFocus();

    /*! Hides helpers on the server based connection page
      (sometimes it's convenient not to have these):
    - "Select existing database server's connection..." (label at the top)
    - "Click "Back" button" (label at the bottom)
    - "Back" button itself */
    void hideHelpers();
    void hideConnectonIcon();
    void hideDescription();

protected Q_SLOTS:
    void slotConnectionItemExecuted(QTreeWidgetItem *item);
    void slotConnectionItemExecuted();
    void slotRemoteAddBtnClicked();
    void slotRemoteEditBtnClicked();
    void slotRemoteRemoveBtnClicked();
    void slotConnectionSelectionChanged();
    void slotPrjTypeSelected(QAbstractButton *btn);
    void slotConnectionSelected();

protected:
    virtual bool eventFilter(QObject* watched, QEvent* event);

private:
    ConnectionDataLVItem* addConnectionData(KDbConnectionData* data);
    ConnectionDataLVItem* selectedConnectionDataItem() const;
    QPointer<KexiServerDriverNotFoundMessage> m_errorMessagePopup;

    class Private;
    Private * const d;
};

#endif // KEXICONNECTIONSELECTORWIDGET_H
