ssl: Use QSslSocket directly to avoid redundant qobject_casts
[quassel.git] / src / core / sqlitestorage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <memory>
24
25 #include <QReadWriteLock>
26 #include <QSqlDatabase>
27
28 #include "abstractsqlstorage.h"
29
30 class QSqlQuery;
31
32 class SqliteStorage : public AbstractSqlStorage
33 {
34     Q_OBJECT
35
36 public:
37     SqliteStorage(QObject* parent = nullptr);
38
39     std::unique_ptr<AbstractSqlMigrationReader> createMigrationReader() override;
40
41     /* General */
42
43     bool isAvailable() const override;
44     QString backendId() const override;
45     QString displayName() const override;
46     QVariantList setupData() const override { return {}; }
47     QString description() const override;
48
49     // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
50
51     /* User handling */
52     UserId addUser(const QString& user, const QString& password, const QString& authenticator = "Database") override;
53     bool updateUser(UserId user, const QString& password) override;
54     void renameUser(UserId user, const QString& newName) override;
55     UserId validateUser(const QString& user, const QString& password) override;
56     UserId getUserId(const QString& username) override;
57     QString getUserAuthenticator(const UserId userid) override;
58     UserId internalUser() override;
59     void delUser(UserId user) override;
60     void setUserSetting(UserId userId, const QString& settingName, const QVariant& data) override;
61     QVariant getUserSetting(UserId userId, const QString& settingName, const QVariant& defaultData = QVariant()) override;
62     void setCoreState(const QVariantList& data) override;
63     QVariantList getCoreState(const QVariantList& data) override;
64
65     /* Identity handling */
66     IdentityId createIdentity(UserId user, CoreIdentity& identity) override;
67     bool updateIdentity(UserId user, const CoreIdentity& identity) override;
68     void removeIdentity(UserId user, IdentityId identityId) override;
69     std::vector<CoreIdentity> identities(UserId user) override;
70
71     /* Network handling */
72     NetworkId createNetwork(UserId user, const NetworkInfo& info) override;
73     bool updateNetwork(UserId user, const NetworkInfo& info) override;
74     bool removeNetwork(UserId user, const NetworkId& networkId) override;
75     std::vector<NetworkInfo> networks(UserId user) override;
76     std::vector<NetworkId> connectedNetworks(UserId user) override;
77     void setNetworkConnected(UserId user, const NetworkId& networkId, bool isConnected) override;
78
79     /* persistent channels */
80     QHash<QString, QString> persistentChannels(UserId user, const NetworkId& networkId) override;
81     void setChannelPersistent(UserId user, const NetworkId& networkId, const QString& channel, bool isJoined) override;
82     void setPersistentChannelKey(UserId user, const NetworkId& networkId, const QString& channel, const QString& key) override;
83
84     /* persistent user states */
85     QString awayMessage(UserId user, NetworkId networkId) override;
86     void setAwayMessage(UserId user, NetworkId networkId, const QString& awayMsg) override;
87     QString userModes(UserId user, NetworkId networkId) override;
88     void setUserModes(UserId user, NetworkId networkId, const QString& userModes) override;
89
90     /* Buffer handling */
91     BufferInfo bufferInfo(UserId user, const NetworkId& networkId, BufferInfo::Type type, const QString& buffer = "", bool create = true) override;
92     BufferInfo getBufferInfo(UserId user, const BufferId& bufferId) override;
93     std::vector<BufferInfo> requestBuffers(UserId user) override;
94     std::vector<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) override;
95     bool removeBuffer(const UserId& user, const BufferId& bufferId) override;
96     bool renameBuffer(const UserId& user, const BufferId& bufferId, const QString& newName) override;
97     bool mergeBuffersPermanently(const UserId& user, const BufferId& bufferId1, const BufferId& bufferId2) override;
98     QHash<BufferId, MsgId> bufferLastMsgIds(UserId user) override;
99     void setBufferLastSeenMsg(UserId user, const BufferId& bufferId, const MsgId& msgId) override;
100     QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) override;
101     void setBufferMarkerLineMsg(UserId user, const BufferId& bufferId, const MsgId& msgId) override;
102     QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) override;
103     void setBufferActivity(UserId id, BufferId bufferId, Message::Types type) override;
104     QHash<BufferId, Message::Types> bufferActivities(UserId id) override;
105     Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) override;
106     void setHighlightCount(UserId id, BufferId bufferId, int count) override;
107     QHash<BufferId, int> highlightCounts(UserId id) override;
108     int highlightCount(BufferId bufferId, MsgId lastSeenMsgId) override;
109     QHash<QString, QByteArray> bufferCiphers(UserId user, const NetworkId& networkId) override;
110     void setBufferCipher(UserId user, const NetworkId& networkId, const QString& bufferName, const QByteArray& cipher) override;
111
112     /* Message handling */
113     bool logMessage(Message& msg) override;
114     bool logMessages(MessageList& msgs) override;
115     std::vector<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) override;
116     std::vector<Message> requestMsgsFiltered(UserId user,
117                                              BufferId bufferId,
118                                              MsgId first = -1,
119                                              MsgId last = -1,
120                                              int limit = -1,
121                                              Message::Types type = Message::Types{-1},
122                                              Message::Flags flags = Message::Flags{-1}) override;
123     std::vector<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) override;
124     std::vector<Message> requestAllMsgsFiltered(UserId user,
125                                                 MsgId first = -1,
126                                                 MsgId last = -1,
127                                                 int limit = -1,
128                                                 Message::Types type = Message::Types{-1},
129                                                 Message::Flags flags = Message::Flags{-1}) override;
130
131     /* Sysident handling */
132     QMap<UserId, QString> getAllAuthUserNames() override;
133
134 protected:
135     void setConnectionProperties(const QVariantMap& properties, const QProcessEnvironment& environment, bool loadFromEnvironment) override
136     {
137         Q_UNUSED(properties);
138         Q_UNUSED(environment);
139         Q_UNUSED(loadFromEnvironment);
140     }
141     // SQLite does not have any connection properties to set
142     QString driverName() override { return "QSQLITE"; }
143     QString databaseName() override { return backlogFile(); }
144     int installedSchemaVersion() override;
145     bool updateSchemaVersion(int newVersion, bool clearUpgradeStep) override;
146     bool setupSchemaVersion(int version) override;
147
148     /**
149      * Gets the last successful schema upgrade step, or an empty string if no upgrade is in progress
150      *
151      * @return Filename of last successful schema upgrade query, or empty string if not upgrading
152      */
153     QString schemaVersionUpgradeStep() override;
154
155     /**
156      * Sets the last successful schema upgrade step
157      *
158      * @param upgradeQuery  The filename of the last successful schema upgrade query
159      * @return True if successfully set, otherwise false
160      */
161     virtual bool setSchemaVersionUpgradeStep(QString upgradeQuery) override;
162
163     bool safeExec(QSqlQuery& query, int retryCount = 0);
164
165 private:
166     static QString backlogFile();
167     void bindNetworkInfo(QSqlQuery& query, const NetworkInfo& info);
168     void bindServerInfo(QSqlQuery& query, const Network::Server& server);
169
170     inline void lockForRead() { _dbLock.lockForRead(); }
171     inline void lockForWrite() { _dbLock.lockForWrite(); }
172     inline void unlock() { _dbLock.unlock(); }
173     QReadWriteLock _dbLock;
174     static int _maxRetryCount;
175 };
176
177 // ========================================
178 //  SqliteMigration
179 // ========================================
180 class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader
181 {
182     Q_OBJECT
183
184 public:
185     SqliteMigrationReader();
186
187     bool readMo(QuasselUserMO& user) override;
188     bool readMo(SenderMO& sender) override;
189     bool readMo(IdentityMO& identity) override;
190     bool readMo(IdentityNickMO& identityNick) override;
191     bool readMo(NetworkMO& network) override;
192     bool readMo(BufferMO& buffer) override;
193     bool readMo(BacklogMO& backlog) override;
194     bool readMo(IrcServerMO& ircserver) override;
195     bool readMo(UserSettingMO& userSetting) override;
196     bool readMo(CoreStateMO& coreState) override;
197
198     bool prepareQuery(MigrationObject mo) override;
199
200     qint64 stepSize() { return 50000; }
201
202 protected:
203     bool transaction() override { return logDb().transaction(); }
204     void rollback() override { logDb().rollback(); }
205     bool commit() override { return logDb().commit(); }
206
207 private:
208     void setMaxId(MigrationObject mo);
209     qint64 _maxId{0};
210 };
211
212 inline std::unique_ptr<AbstractSqlMigrationReader> SqliteStorage::createMigrationReader()
213 {
214     return std::unique_ptr<AbstractSqlMigrationReader>{new SqliteMigrationReader()};
215 }