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