#ifndef CONNECTIONLISTMODEL_H #define CONNECTIONLISTMODEL_H #include #include #include #include "ConnectionConfig.h" #include "Expected.h" #include #include #include #include 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; 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); /// Create a new group in the DB and place in the tree std::variant addGroup(QString group_name); std::optional removeGroup(int row); int findGroup(int conngroup_id) const; private: using Groups = QVector>; QSqlDatabase &m_db; Groups m_groups; /// Finds the connection with the specified uuid and returns /// { group_index, connection_index } std::tuple findConfig(const QUuid uuid) const; int findGroup(QString name) const; std::optional saveToDb(const ConnectionConfig &cc); }; #if false /** \brief Model class for the list of connections. * * This class also allows for the editing of the list. */ class ConnectionListModel : public QAbstractListModel { Q_OBJECT public: enum Columns { Description, Name, Host, Port, User, Password, DbName, ColCount }; ConnectionListModel(QObject *parent); ConnectionListModel(const ConnectionListModel&) = delete; ~ConnectionListModel() override; // BEGIN Model/View related functions virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override; virtual int columnCount(const QModelIndex &/*parent*/) const override; 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; virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; virtual Qt::ItemFlags flags(const QModelIndex &index) const override; virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; // END Model/View related functions Expected get(int row); void load(); // Writes all entries to storage void save(); // 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); static QString makeLongDescription(const ConnectionConfig &cfg); private: using ConnectionList = QVector; ConnectionList m_connections; QString iniFileName(); }; #endif #endif // CONNECTIONLISTMODEL_H