modernize: Use '= default' instead of empty ctor/dtor bodies
[quassel.git] / src / core / abstractsqlstorage.h
index 4b54f4f..efd1ce3 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2013 by the Quassel Project                        *
+ *   Copyright (C) 2005-2018 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
-#ifndef ABSTRACTSQLSTORAGE_H
-#define ABSTRACTSQLSTORAGE_H
+#pragma once
 
 #include "storage.h"
 
+#include <memory>
+
 #include <QSqlDatabase>
 #include <QSqlQuery>
 #include <QSqlError>
@@ -35,23 +36,43 @@ class AbstractSqlStorage : public Storage
     Q_OBJECT
 
 public:
-    AbstractSqlStorage(QObject *parent = 0);
-    virtual ~AbstractSqlStorage();
+    AbstractSqlStorage(QObject *parent = nullptr);
+    ~AbstractSqlStorage() override;
 
-    virtual inline AbstractSqlMigrationReader *createMigrationReader() { return 0; }
-    virtual inline AbstractSqlMigrationWriter *createMigrationWriter() { return 0; }
+    virtual std::unique_ptr<AbstractSqlMigrationReader> createMigrationReader() { return {}; }
+    virtual std::unique_ptr<AbstractSqlMigrationWriter> createMigrationWriter() { return {}; }
 
 public slots:
-    virtual State init(const QVariantMap &settings = QVariantMap());
-    virtual bool setup(const QVariantMap &settings = QVariantMap());
+    State init(const QVariantMap &settings = QVariantMap(),
+                       const QProcessEnvironment &environment = {},
+                       bool loadFromEnvironment = false) override;
+    bool setup(const QVariantMap &settings = QVariantMap(),
+                       const QProcessEnvironment &environment = {},
+                       bool loadFromEnvironment = false) override;
 
 protected:
-    inline virtual void sync() {};
+    inline void sync() override {};
 
     QSqlDatabase logDb();
 
-    QString queryString(const QString &queryName, int version);
-    inline QString queryString(const QString &queryName) { return queryString(queryName, 0); }
+    /**
+     * Fetch an SQL query string by name and optional schema version
+     *
+     * Loads the named SQL query from the built-in SQL resource collection, returning it as a
+     * string.  If a version is specified, it'll be loaded from the schema version-specific folder
+     * instead.
+     *
+     * @see schemaVersion()
+     *
+     * @param[in] queryName  File name of the SQL query, minus the .sql extension
+     * @param[in] version
+     * @parblock
+     * SQL schema version; if 0, fetches from current version, otherwise loads from the specified
+     * schema version instead of the current schema files.
+     * @endparblock
+     * @return String with the requested SQL query, ready for parameter substitution
+     */
+    QString queryString(const QString &queryName, int version = 0);
 
     QStringList setupQueries();
 
@@ -65,7 +86,9 @@ protected:
     virtual bool updateSchemaVersion(int newVersion) = 0;
     virtual bool setupSchemaVersion(int version) = 0;
 
-    virtual void setConnectionProperties(const QVariantMap &properties) = 0;
+    virtual void setConnectionProperties(const QVariantMap &properties,
+                                         const QProcessEnvironment &environment,
+                                         bool loadFromEnvironment) = 0;
     virtual QString driverName() = 0;
     inline virtual QString hostName() { return QString(); }
     inline virtual int port() { return -1; }
@@ -87,8 +110,9 @@ private slots:
 
 private:
     void addConnectionToPool();
+    void dbConnect(QSqlDatabase &db);
 
-    int _schemaVersion;
+    int _schemaVersion{0};
     bool _debug;
 
     static int _nextConnectionId;
@@ -100,6 +124,14 @@ private:
     QHash<QThread *, Connection *> _connectionPool;
 };
 
+struct SenderData {
+    QString sender;
+    QString realname;
+    QString avatarurl;
+
+    friend uint qHash(const SenderData &key);
+    friend bool operator==(const SenderData &a, const SenderData &b);
+};
 
 // ========================================
 //  AbstractSqlStorage::Connection
@@ -109,8 +141,8 @@ class AbstractSqlStorage::Connection : public QObject
     Q_OBJECT
 
 public:
-    Connection(const QString &name, QObject *parent = 0);
-    ~Connection();
+    Connection(const QString &name, QObject *parent = nullptr);
+    ~Connection() override;
 
     inline QLatin1String name() const { return QLatin1String(_name); }
 
@@ -130,12 +162,15 @@ public:
         UserId id;
         QString username;
         QString password;
+        int hashversion;
+        QString authenticator;
     };
 
     struct SenderMO {
-        int senderId;
+        qint64 senderId{0};
         QString sender;
-        SenderMO() : senderId(0) {}
+        QString realname;
+        QString avatarurl;
     };
 
     struct IdentityMO {
@@ -153,7 +188,7 @@ public:
         bool autoAwayReasonEnabled;
         bool detachAwayEnabled;
         QString detachAwayReason;
-        bool detchAwayReasonEnabled;
+        bool detachAwayReasonEnabled;
         QString ident;
         QString kickReason;
         QString partReason;
@@ -169,31 +204,35 @@ public:
     };
 
     struct NetworkMO {
-        NetworkId networkid;
         UserId userid;
         QString networkname;
-        IdentityId identityid;
-        QString encodingcodec;
-        QString decodingcodec;
-        QString servercodec;
-        bool userandomserver;
         QString perform;
-        bool useautoidentify;
         QString autoidentifyservice;
         QString autoidentifypassword;
-        bool useautoreconnect;
-        int autoreconnectinterval;
-        int autoreconnectretries;
-        bool unlimitedconnectretries;
-        bool rejoinchannels;
-        bool connected;
+        QString saslaccount;
+        QString saslpassword;
+        QString servercodec;
+        QString encodingcodec;
+        QString decodingcodec;
         QString usermode;
         QString awaymessage;
         QString attachperform;
         QString detachperform;
+        NetworkId networkid;
+        IdentityId identityid;
+        int messagerateburstsize;
+        int messageratedelay;
+        int autoreconnectinterval;
+        int autoreconnectretries;
+        bool rejoinchannels;
+        bool userandomserver;
+        bool useautoidentify;
         bool usesasl;
-        QString saslaccount;
-        QString saslpassword;
+        bool useautoreconnect;
+        bool unlimitedconnectretries;
+        bool usecustommessagerate;
+        bool unlimitedmessagerate;
+        bool connected;
     };
 
     struct BufferMO {
@@ -204,10 +243,14 @@ public:
         QString buffername;
         QString buffercname;
         int buffertype;
-        int lastseenmsgid;
-        int markerlinemsgid;
+        qint64 lastmsgid;
+        qint64 lastseenmsgid;
+        qint64 markerlinemsgid;
+        int bufferactivity;
+        int highlightcount;
         QString key;
         bool joined;
+        QString cipher;
     };
 
     struct BacklogMO {
@@ -216,7 +259,8 @@ public:
         BufferId bufferid;
         int type;
         int flags;
-        int senderid;
+        qint64 senderid;
+        QString senderprefixes;
         QString message;
     };
 
@@ -228,6 +272,7 @@ public:
         int port;
         QString password;
         bool ssl;
+        bool sslverify;     /// If true, validate SSL certificates
         int sslversion;
         bool useproxy;
         int proxytype;
@@ -243,6 +288,11 @@ public:
         QByteArray settingvalue;
     };
 
+    struct CoreStateMO {
+        QString key;
+        QByteArray value;
+    };
+
     enum MigrationObject {
         QuasselUser,
         Sender,
@@ -252,11 +302,11 @@ public:
         Buffer,
         Backlog,
         IrcServer,
-        UserSetting
+        UserSetting,
+        CoreState
     };
 
-    AbstractSqlMigrator();
-    virtual ~AbstractSqlMigrator() {}
+    virtual ~AbstractSqlMigrator() = default;
 
     static QString migrationObject(MigrationObject moType);
 
@@ -280,7 +330,7 @@ protected:
     virtual bool commit() = 0;
 
 private:
-    QSqlQuery *_query;
+    QSqlQuery *_query{nullptr};
 };
 
 
@@ -298,6 +348,7 @@ public:
     virtual bool readMo(BacklogMO &backlog) = 0;
     virtual bool readMo(IrcServerMO &ircserver) = 0;
     virtual bool readMo(UserSettingMO &userSetting) = 0;
+    virtual bool readMo(CoreStateMO &coreState) = 0;
 
     bool migrateTo(AbstractSqlMigrationWriter *writer);
 
@@ -307,7 +358,7 @@ private:
 
     template<typename T> bool transferMo(MigrationObject moType, T &mo);
 
-    AbstractSqlMigrationWriter *_writer;
+    AbstractSqlMigrationWriter *_writer{nullptr};
 };
 
 
@@ -323,6 +374,7 @@ public:
     virtual bool writeMo(const BacklogMO &backlog) = 0;
     virtual bool writeMo(const IrcServerMO &ircserver) = 0;
     virtual bool writeMo(const UserSettingMO &userSetting) = 0;
+    virtual bool writeMo(const CoreStateMO &coreState) = 0;
 
     inline bool migrateFrom(AbstractSqlMigrationReader *reader) { return reader->migrateTo(this); }
 
@@ -330,6 +382,3 @@ public:
     virtual inline bool postProcess() { return true; }
     friend class AbstractSqlMigrationReader;
 };
-
-
-#endif