updating sequences after mirgration
[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   inline QVariantMap setupKeys() const { return QVariantMap(); }
45   QString description() const;
46
47   // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
48
49   /* User handling */
50
51   virtual UserId addUser(const QString &user, const QString &password);
52   virtual void 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 internalUser();
56   virtual void delUser(UserId user);
57   virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data);
58   virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultData = QVariant());
59
60   /* Identity handling */
61   virtual IdentityId createIdentity(UserId user, CoreIdentity &identity);
62   virtual bool updateIdentity(UserId user, const CoreIdentity &identity);
63   virtual void removeIdentity(UserId user, IdentityId identityId);
64   virtual QList<CoreIdentity> identities(UserId user);
65
66   /* Network handling */
67   virtual NetworkId createNetwork(UserId user, const NetworkInfo &info);
68   virtual bool updateNetwork(UserId user, const NetworkInfo &info);
69   virtual bool removeNetwork(UserId user, const NetworkId &networkId);
70   virtual QList<NetworkInfo> networks(UserId user);
71   virtual QList<NetworkId> connectedNetworks(UserId user);
72   virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
73
74   /* persistent channels */
75   virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
76   virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
77   virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
78
79   /* persistent user states */
80   virtual QString awayMessage(UserId user, NetworkId networkId);
81   virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg);
82   virtual QString userModes(UserId user, NetworkId networkId);
83   virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes);
84
85   /* Buffer handling */
86   virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true);
87   virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
88   virtual QList<BufferInfo> requestBuffers(UserId user);
89   virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId);
90   virtual bool removeBuffer(const UserId &user, const BufferId &bufferId);
91   virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName);
92   virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2);
93   virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
94   virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
95
96   /* Message handling */
97   virtual bool logMessage(Message &msg);
98   virtual bool logMessages(MessageList &msgs);
99   virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1);
100   virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1);
101
102 protected:
103   inline virtual void setConnectionProperties(const QVariantMap & /* properties */) {}
104   inline virtual QString driverName() { return "QSQLITE"; }
105   inline virtual QString databaseName() { return backlogFile(); }
106   virtual int installedSchemaVersion();
107   virtual bool updateSchemaVersion(int newVersion);
108   virtual bool setupSchemaVersion(int version);
109   bool safeExec(QSqlQuery &query, int retryCount = 0);
110
111 private:
112   static QString backlogFile();
113   bool isValidNetwork(UserId user, const NetworkId &networkId);
114   bool isValidBuffer(const UserId &user, const BufferId &bufferId);
115   NetworkId getNetworkId(UserId user, const QString &network);
116   void createBuffer(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer);
117
118   static int _maxRetryCount;
119 };
120
121 // ========================================
122 //  SqliteMigration
123 // ========================================
124 class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader {
125   Q_OBJECT
126
127 public:
128   SqliteMigrationReader();
129
130   virtual bool readMo(QuasselUserMO &user);
131   virtual bool readMo(SenderMO &sender);
132   virtual bool readMo(IdentityMO &identity);
133   virtual bool readMo(IdentityNickMO &identityNick);
134   virtual bool readMo(NetworkMO &network);
135   virtual bool readMo(BufferMO &buffer);
136   virtual bool readMo(BacklogMO &backlog);
137   virtual bool readMo(IrcServerMO &ircserver);
138   virtual bool readMo(UserSettingMO &userSetting);
139
140   virtual bool prepareQuery(MigrationObject mo);
141
142 protected:
143   virtual inline bool transaction() { return logDb().transaction(); }
144   virtual inline void rollback() { logDb().rollback(); }
145   virtual inline bool commit() { return logDb().commit(); }
146 };
147
148 inline AbstractSqlMigrationReader *SqliteStorage::createMigrationReader() {
149   return new SqliteMigrationReader();
150 }
151
152 #endif