2017-01-14 20:07:12 +01:00
|
|
|
|
#ifndef CONNECTIONLISTMODEL_H
|
|
|
|
|
|
#define CONNECTIONLISTMODEL_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
|
|
#include <QAbstractListModel>
|
|
|
|
|
|
|
2017-08-23 13:27:23 +02:00
|
|
|
|
#include "ConnectionConfig.h"
|
|
|
|
|
|
#include "Expected.h"
|
2019-08-27 20:12:00 +02:00
|
|
|
|
#include <optional>
|
|
|
|
|
|
#include <variant>
|
2019-08-24 20:47:32 +02:00
|
|
|
|
#include <QVector>
|
2017-01-14 20:07:12 +01:00
|
|
|
|
|
2019-08-27 20:12:00 +02:00
|
|
|
|
#include <QSqlError>
|
2019-08-25 15:33:51 +02:00
|
|
|
|
class QSqlDatabase;
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectionTreeModel : public QAbstractItemModel {
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
public:
|
|
|
|
|
|
enum Columns {
|
|
|
|
|
|
Name,
|
|
|
|
|
|
Host,
|
|
|
|
|
|
Port,
|
|
|
|
|
|
User,
|
|
|
|
|
|
DbName,
|
|
|
|
|
|
|
|
|
|
|
|
ColCount
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ConnectionTreeModel(QObject *parent, QSqlDatabase &db);
|
|
|
|
|
|
|
|
|
|
|
|
void load();
|
|
|
|
|
|
|
|
|
|
|
|
QVariant data(const QModelIndex &index, int role) const override;
|
|
|
|
|
|
|
|
|
|
|
|
// Qt::ItemFlags flags(const QModelIndex &index) const override;
|
|
|
|
|
|
|
|
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation,
|
|
|
|
|
|
int role = Qt::DisplayRole) const override;
|
|
|
|
|
|
|
|
|
|
|
|
QModelIndex index(int row, int column,
|
|
|
|
|
|
const QModelIndex &parent = QModelIndex()) const override;
|
|
|
|
|
|
|
|
|
|
|
|
QModelIndex parent(const QModelIndex &index) const override;
|
|
|
|
|
|
|
|
|
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
|
|
|
|
|
|
|
|
|
|
|
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
|
|
|
|
|
|
2019-08-27 20:12:00 +02:00
|
|
|
|
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
|
|
|
|
|
|
|
|
|
|
|
|
/** Matches cc to the list by looking at its uuid.
|
|
|
|
|
|
*
|
|
|
|
|
|
* If it is not in the list it is added. If the uuid is in the list that entry is updated.
|
|
|
|
|
|
* If the group has been changed it is moved to the right group.
|
|
|
|
|
|
* In both cases the data is also directly written to long term storage.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void save(const QString &group, const ConnectionConfig &cc);
|
|
|
|
|
|
/** Save changed config, group is not allowed to change
|
|
|
|
|
|
*/
|
|
|
|
|
|
void save(const ConnectionConfig &cc);
|
2019-09-01 06:42:21 +02:00
|
|
|
|
/// Create a new group in the DB and place in the tree
|
|
|
|
|
|
std::variant<int, QSqlError> addGroup(QString group_name);
|
|
|
|
|
|
std::optional<QSqlError> removeGroup(int row);
|
2019-08-25 15:33:51 +02:00
|
|
|
|
private:
|
|
|
|
|
|
using Groups = QVector<std::shared_ptr<ConnectionGroup>>;
|
|
|
|
|
|
|
|
|
|
|
|
QSqlDatabase &m_db;
|
|
|
|
|
|
Groups m_groups;
|
2019-08-27 20:12:00 +02:00
|
|
|
|
|
|
|
|
|
|
/// Finds the connection with the specified uuid and returns
|
|
|
|
|
|
/// { group_index, connection_index }
|
|
|
|
|
|
std::tuple<int, int> findConfig(const QUuid uuid) const;
|
|
|
|
|
|
int findGroup(QString name) const;
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<QSqlError> saveToDb(const ConnectionConfig &cc);
|
2019-08-25 15:33:51 +02:00
|
|
|
|
};
|
2017-01-14 20:07:12 +01:00
|
|
|
|
|
2019-08-27 20:12:00 +02:00
|
|
|
|
#if false
|
2017-02-01 20:00:03 +01:00
|
|
|
|
/** \brief Model class for the list of connections.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This class also allows for the editing of the list.
|
|
|
|
|
|
*/
|
2017-01-14 20:07:12 +01:00
|
|
|
|
class ConnectionListModel : public QAbstractListModel {
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
public:
|
2019-08-19 19:26:39 +02:00
|
|
|
|
enum Columns {
|
|
|
|
|
|
Description,
|
|
|
|
|
|
Name,
|
|
|
|
|
|
Host,
|
|
|
|
|
|
Port,
|
|
|
|
|
|
User,
|
|
|
|
|
|
Password,
|
|
|
|
|
|
DbName,
|
|
|
|
|
|
|
|
|
|
|
|
ColCount
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
ConnectionListModel(QObject *parent);
|
2017-02-26 19:29:50 +01:00
|
|
|
|
ConnectionListModel(const ConnectionListModel&) = delete;
|
2018-11-08 21:50:49 +01:00
|
|
|
|
~ConnectionListModel() override;
|
2017-01-14 20:07:12 +01:00
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
// BEGIN Model/View related functions
|
2017-01-14 20:07:12 +01:00
|
|
|
|
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
2017-01-14 22:03:58 +01:00
|
|
|
|
virtual int columnCount(const QModelIndex &/*parent*/) const override;
|
2017-01-14 20:07:12 +01:00
|
|
|
|
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
|
|
|
|
|
// virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
2017-01-14 22:29:12 +01:00
|
|
|
|
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
|
|
|
|
|
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
2019-08-24 20:47:32 +02:00
|
|
|
|
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
|
|
|
|
|
|
// END Model/View related functions
|
2017-01-14 20:07:12 +01:00
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
Expected<ConnectionConfig> get(int row);
|
2017-01-14 20:07:12 +01:00
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
void load();
|
|
|
|
|
|
// Writes all entries to storage
|
2017-01-15 12:27:36 +01:00
|
|
|
|
void save();
|
2019-08-24 20:47:32 +02:00
|
|
|
|
// Writes the specified entry to storage
|
|
|
|
|
|
void save(int index);
|
|
|
|
|
|
/** Matches cc to the list by looking at its uuid.
|
|
|
|
|
|
*
|
|
|
|
|
|
* If it is not in the list it is added. If the uuid is in the list that entry is updated.
|
|
|
|
|
|
* In both cases the data is also directly written to long term storage.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void save(const ConnectionConfig &cc);
|
2018-11-04 11:26:20 +01:00
|
|
|
|
static QString makeLongDescription(const ConnectionConfig &cfg);
|
2017-01-14 20:07:12 +01:00
|
|
|
|
private:
|
2017-02-26 19:29:50 +01:00
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
using ConnectionList = QVector<ConnectionConfig>;
|
|
|
|
|
|
ConnectionList m_connections;
|
2017-01-15 12:27:36 +01:00
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
QString iniFileName();
|
2017-01-14 20:07:12 +01:00
|
|
|
|
};
|
2019-08-27 20:12:00 +02:00
|
|
|
|
#endif
|
2017-01-14 20:07:12 +01:00
|
|
|
|
|
|
|
|
|
|
#endif // CONNECTIONLISTMODEL_H
|