core: Store session state in database, migrate old
[quassel.git] / src / core / sqlitestorage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "abstractsqlstorage.h"
24
25 #include <QSqlDatabase>
26
27 class QSqlQuery;
28
29 class SqliteStorage : public AbstractSqlStorage
30 {
31     Q_OBJECT
32
33 public:
34     SqliteStorage(QObject *parent = 0);
35     ~SqliteStorage() override;
36
37     std::unique_ptr<AbstractSqlMigrationReader> createMigrationReader() override;
38
39 public slots:
40     /* General */
41
42     bool isAvailable()  const  override;
43     QString backendId() const override;
44     QString displayName() const override;
45     QVariantList setupData() const  override { return {}; }
46     QString description() const override;
47
48     // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
49
50     /* User handling */
51     UserId addUser(const QString &user, const QString &password, const QString &authenticator = "Database") override;
52     bool updateUser(UserId user, const QString &password) override;
53     void renameUser(UserId user, const QString &newName) override;
54     UserId validateUser(const QString &user, const QString &password) override;
55     UserId getUserId(const QString &username) override;
56     QString getUserAuthenticator(const UserId userid) override;
57     UserId internalUser() override;
58     void delUser(UserId user) override;
59     void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) override;
60     QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultData = QVariant()) override;
61     void setCoreState(const QVariantList &data) override;
62     QVariantList getCoreState(const QVariantList &data) override;
63
64     /* Identity handling */
65     IdentityId createIdentity(UserId user, CoreIdentity &identity) override;
66     bool updateIdentity(UserId user, const CoreIdentity &identity) override;
67     void removeIdentity(UserId user, IdentityId identityId) override;
68     QList<CoreIdentity> identities(UserId user) override;
69
70     /* Network handling */
71     NetworkId createNetwork(UserId user, const NetworkInfo &info) override;
72     bool updateNetwork(UserId user, const NetworkInfo &info) override;
73     bool removeNetwork(UserId user, const NetworkId &networkId) override;
74     QList<NetworkInfo> networks(UserId user) override;
75     QList<NetworkId> connectedNetworks(UserId user) override;
76     void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) override;
77
78     /* persistent channels */
79     QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) override;
80     void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) override;
81     void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) override;
82
83     /* persistent user states */
84     QString awayMessage(UserId user, NetworkId networkId) override;
85     void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) override;
86     QString userModes(UserId user, NetworkId networkId) override;
87     void setUserModes(UserId user, NetworkId networkId, const QString &userModes) override;
88
89     /* Buffer handling */
90     BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) override;
91     BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) override;
92     QList<BufferInfo> requestBuffers(UserId user) override;
93     QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) override;
94     bool removeBuffer(const UserId &user, const BufferId &bufferId) override;
95     bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) override;
96     bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) override;
97     void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) override;
98     QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) override;
99     void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) override;
100     QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) override;
101     void setBufferActivity(UserId id, BufferId bufferId, Message::Types type) override;
102     QHash<BufferId, Message::Types> bufferActivities(UserId id) override;
103     Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) override;
104     void setHighlightCount(UserId id, BufferId bufferId, int count) override;
105     QHash<BufferId, int> highlightCounts(UserId id) override;
106     int highlightCount(BufferId bufferId, MsgId lastSeenMsgId) override;
107     QHash<QString, QByteArray> bufferCiphers(UserId user, const NetworkId &networkId) override;
108     void setBufferCipher(UserId user, const NetworkId &networkId, const QString &bufferName, const QByteArray &cipher) override;
109
110     /* Message handling */
111     bool logMessage(Message &msg) override;
112     bool logMessages(MessageList &msgs) override;
113     QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) override;
114     QList<Message> requestMsgsFiltered(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1,
115                                        int limit = -1, Message::Types type = Message::Types{-1},
116                                        Message::Flags flags = Message::Flags{-1}) override;
117     QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) override;
118     QList<Message> requestAllMsgsFiltered(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1,
119                                           Message::Types type = Message::Types{-1},
120                                           Message::Flags flags = Message::Flags{-1}) override;
121
122     /* Sysident handling */
123     QMap<UserId, QString> getAllAuthUserNames() override;
124     QString getAuthUserName(UserId user) override;
125
126 protected:
127     void setConnectionProperties(const QVariantMap & /* properties */)  override {}
128     QString driverName()  override { return "QSQLITE"; }
129     QString databaseName()  override { return backlogFile(); }
130     int installedSchemaVersion() override;
131     bool updateSchemaVersion(int newVersion) override;
132     bool setupSchemaVersion(int version) override;
133     bool safeExec(QSqlQuery &query, int retryCount = 0);
134
135 private:
136     static QString backlogFile();
137     void bindNetworkInfo(QSqlQuery &query, const NetworkInfo &info);
138     void bindServerInfo(QSqlQuery &query, const Network::Server &server);
139
140     inline void lockForRead() { _dbLock.lockForRead(); }
141     inline void lockForWrite() { _dbLock.lockForWrite(); }
142     inline void unlock() { _dbLock.unlock(); }
143     QReadWriteLock _dbLock;
144     static int _maxRetryCount;
145 };
146
147
148 // ========================================
149 //  SqliteMigration
150 // ========================================
151 class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader
152 {
153     Q_OBJECT
154
155 public:
156     SqliteMigrationReader();
157
158     bool readMo(QuasselUserMO &user) override;
159     bool readMo(SenderMO &sender) override;
160     bool readMo(IdentityMO &identity) override;
161     bool readMo(IdentityNickMO &identityNick) override;
162     bool readMo(NetworkMO &network) override;
163     bool readMo(BufferMO &buffer) override;
164     bool readMo(BacklogMO &backlog) override;
165     bool readMo(IrcServerMO &ircserver) override;
166     bool readMo(UserSettingMO &userSetting) override;
167     bool readMo(CoreStateMO &coreState) override;
168
169     bool prepareQuery(MigrationObject mo) override;
170
171     int stepSize() { return 50000; }
172
173 protected:
174     bool transaction()  override { return logDb().transaction(); }
175     void rollback()  override { logDb().rollback(); }
176     bool commit()  override { return logDb().commit(); }
177
178 private:
179     void setMaxId(MigrationObject mo);
180     int _maxId;
181 };
182
183
184 inline std::unique_ptr<AbstractSqlMigrationReader> SqliteStorage::createMigrationReader()
185 {
186     return std::unique_ptr<AbstractSqlMigrationReader>{new SqliteMigrationReader()};
187 }