Add DesktopEntry to notifyrc
[quassel.git] / src / core / sqlitestorage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 <QSqlDatabase>
24
25 #include "abstractsqlstorage.h"
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,
114                                        BufferId bufferId,
115                                        MsgId first = -1,
116                                        MsgId last = -1,
117                                        int limit = -1,
118                                        Message::Types type = Message::Types{-1},
119                                        Message::Flags flags = Message::Flags{-1}) override;
120     QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) override;
121     QList<Message> requestAllMsgsFiltered(UserId user,
122                                           MsgId first = -1,
123                                           MsgId last = -1,
124                                           int limit = -1,
125                                           Message::Types type = Message::Types{-1},
126                                           Message::Flags flags = Message::Flags{-1}) override;
127
128     /* Sysident handling */
129     QMap<UserId, QString> getAllAuthUserNames() override;
130
131 protected:
132     void setConnectionProperties(const QVariantMap& properties, const QProcessEnvironment& environment, bool loadFromEnvironment) override
133     {
134         Q_UNUSED(properties);
135         Q_UNUSED(environment);
136         Q_UNUSED(loadFromEnvironment);
137     }
138     // SQLite does not have any connection properties to set
139     QString driverName() override { return "QSQLITE"; }
140     QString databaseName() override { return backlogFile(); }
141     int installedSchemaVersion() override;
142     bool updateSchemaVersion(int newVersion, bool clearUpgradeStep) override;
143     bool setupSchemaVersion(int version) override;
144
145     /**
146      * Gets the last successful schema upgrade step, or an empty string if no upgrade is in progress
147      *
148      * @return Filename of last successful schema upgrade query, or empty string if not upgrading
149      */
150     QString schemaVersionUpgradeStep() override;
151
152     /**
153      * Sets the last successful schema upgrade step
154      *
155      * @param upgradeQuery  The filename of the last successful schema upgrade query
156      * @return True if successfully set, otherwise false
157      */
158     virtual bool setSchemaVersionUpgradeStep(QString upgradeQuery) override;
159
160     bool safeExec(QSqlQuery& query, int retryCount = 0);
161
162 private:
163     static QString backlogFile();
164     void bindNetworkInfo(QSqlQuery& query, const NetworkInfo& info);
165     void bindServerInfo(QSqlQuery& query, const Network::Server& server);
166
167     inline void lockForRead() { _dbLock.lockForRead(); }
168     inline void lockForWrite() { _dbLock.lockForWrite(); }
169     inline void unlock() { _dbLock.unlock(); }
170     QReadWriteLock _dbLock;
171     static int _maxRetryCount;
172 };
173
174 // ========================================
175 //  SqliteMigration
176 // ========================================
177 class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader
178 {
179     Q_OBJECT
180
181 public:
182     SqliteMigrationReader();
183
184     bool readMo(QuasselUserMO& user) override;
185     bool readMo(SenderMO& sender) override;
186     bool readMo(IdentityMO& identity) override;
187     bool readMo(IdentityNickMO& identityNick) override;
188     bool readMo(NetworkMO& network) override;
189     bool readMo(BufferMO& buffer) override;
190     bool readMo(BacklogMO& backlog) override;
191     bool readMo(IrcServerMO& ircserver) override;
192     bool readMo(UserSettingMO& userSetting) override;
193     bool readMo(CoreStateMO& coreState) override;
194
195     bool prepareQuery(MigrationObject mo) override;
196
197     qint64 stepSize() { return 50000; }
198
199 protected:
200     bool transaction() override { return logDb().transaction(); }
201     void rollback() override { logDb().rollback(); }
202     bool commit() override { return logDb().commit(); }
203
204 private:
205     void setMaxId(MigrationObject mo);
206     qint64 _maxId{0};
207 };
208
209 inline std::unique_ptr<AbstractSqlMigrationReader> SqliteStorage::createMigrationReader()
210 {
211     return std::unique_ptr<AbstractSqlMigrationReader>{new SqliteMigrationReader()};
212 }