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