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