Added getNetworkId(UserId user, const QString &network) to make the transition to...
[quassel.git] / src / core / storage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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 _STORAGE_H_
22 #define _STORAGE_H_
23
24 #include <QtCore>
25 #include <QSqlDatabase>
26
27 //#include "global.h"
28 #include "message.h"
29
30 class Storage : public QObject {
31   Q_OBJECT
32
33   public:
34     Storage() {};
35     virtual ~Storage() {};
36
37     //! Initialize the static parts of the storage class
38     /** This is called by the core before any other method of the storage backend is used.
39      *  This should be used to perform any static initialization that might be necessary.
40      *  DO NOT use this for creating database connection or similar stuff, since init() might be
41      *  called even if the storage backend is never be actually used (because no user selected it).
42      *  For anything like this, the constructor (which is called if and when we actually create an instance
43      *  of the storage backend) is the right place.
44      */
45     static void init() {};
46
47     /* General */
48
49     //! Check if the storage type is available.
50     /** A storage subclass should return true if it can be successfully used, i.e. if all
51      *  prerequisites are in place (e.g. we have an appropriate DB driver etc.).
52      * \return True if and only if the storage class can be successfully used.
53      */
54     static bool isAvailable() { return false; }
55
56     //! Returns the display name of the storage backend
57     /** \return A string that can be used by the GUI to describe the storage backend */
58     static QString displayName() { return ""; }
59
60     // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
61
62     /* User handling */
63
64     //! Add a new core user to the storage.
65     /** \param user     The username of the new user
66      *  \param password The cleartext password for the new user
67      *  \return The new user's UserId
68      */
69     virtual UserId addUser(const QString &user, const QString &password) = 0;
70
71     //! Update a core user's password.
72     /** \param user     The user's id
73      *  \param password The user's new password
74      */
75     virtual void updateUser(UserId user, const QString &password) = 0;
76
77     //! Rename a user
78     /** \param user     The user's id
79      *  \param newName  The user's new name
80      */
81     virtual void renameUser(UserId user, const QString &newName) = 0;
82
83     //! Validate a username with a given password.
84     /** \param user     The username to validate
85      *  \param password The user's alleged password
86      *  \return A valid UserId if the password matches the username; 0 else
87      */
88     virtual UserId validateUser(const QString &user, const QString &password) = 0;
89
90     //! Remove a core user from storage.
91     /** \param user     The userid to delete
92      */
93     virtual void delUser(UserId user) = 0;
94
95     /* Network handling */
96
97     //! Get the unique NetworkId of the network for a user.
98     /** \param user    The core user who owns this buffername
99      *  \param network The network name
100      *  \return The BufferId corresponding to the given network and buffer name, or 0 if not found
101      */
102     virtual uint getNetworkId(UserId user, const QString &network) = 0;
103
104     /* Buffer handling */
105
106     //! Get the unique BufferId for the given combination of network and buffername for a user.
107     /** \param user    The core user who owns this buffername
108      *  \param network The network name
109      *  \param buffer  The buffer name (if empty, the net's status buffer is returned)
110      *  \return The BufferId corresponding to the given network and buffer name, or 0 if not found
111      */
112     virtual BufferId getBufferId(UserId user, const QString &network, const QString &buffer = "") = 0;
113
114     //! Request a list of all buffers known to a user since a certain point in time.
115     /** This method is used to get a list of all buffers we have stored a backlog from.
116      *  Optionally, a QDateTime can be given, so that only buffers are listed that where active
117      *  since that point in time.
118      *  \param user  The user whose buffers we request
119      *  \param since If this is defined, older buffers will be ignored
120      *  \return A list of the BufferIds for all buffers as requested
121      */
122     virtual QList<BufferId> requestBuffers(UserId user, QDateTime since = QDateTime()) = 0;
123
124     /* Message handling */
125
126     //! Store a Message in the backlog.
127     /** \param msg  The message object to be stored
128      *  \return The globally unique id for the stored message
129      */
130     virtual MsgId logMessage(Message msg) = 0;
131
132     //! Request a certain number (or all) messages stored in a given buffer.
133     /** \param buffer   The buffer we request messages from
134      *  \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
135      *  \param offset   Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
136      *  \return The requested list of messages
137      */
138     virtual QList<Message> requestMsgs(BufferId buffer, int lastmsgs = -1, int offset = -1) = 0;
139
140     //! Request messages stored in a given buffer since a certain point in time.
141     /** \param buffer   The buffer we request messages from
142      *  \param since    Only return messages newer than this point in time
143      *  \param offset   Do not return messages with MsgId >= offset, if offset >= 0
144      *  \return The requested list of messages
145      */
146     virtual QList<Message> requestMsgs(BufferId buffer, QDateTime since, int offset = -1) = 0;
147
148     //! Request a range of messages stored in a given buffer.
149     /** \param buffer   The buffer we request messages from
150      *  \param first    Return messages with first <= MsgId <= last
151      *  \param last     Return messages with first <= MsgId <= last
152      *  \return The requested list of messages
153      */
154     virtual QList<Message> requestMsgRange(BufferId buffer, int first, int last) = 0;
155
156   public slots:
157     //! This is just for importing the old file-based backlog */
158     /** This slot needs to be implemented in the storage backends.
159      *  It should first prepare (delete?) the database, then call initBackLogOld(UserId id).
160      *  If the importing was successful, backLogEnabledOld will be true afterwards.
161      */
162     virtual void importOldBacklog() = 0;
163
164   signals:
165     //! Sent when a new BufferId is created, or an existing one changed somehow.
166     void bufferIdUpdated(BufferId);
167     //! Sent when a new user has been added
168     void userAdded(UserId, const QString &username);
169     //! Sent when a user has been renamed
170     void userRenamed(UserId, const QString &newname);
171     //! Sent when a user has been removed
172     void userRemoved(UserId);
173
174   public:
175     /* Exceptions */
176     struct AuthError : public Exception {};
177
178   protected:
179     // Old stuff, just for importing old file-based data
180     void initBackLogOld(UserId id);
181
182     QSqlDatabase logDb; // FIXME this does not belong in the base class!
183       
184     bool backLogEnabledOld;
185     QDir backLogDir;
186     QHash<QString, QList<Message> > backLog;
187     QHash<QString, QFile *> logFiles;
188     QHash<QString, QDataStream *> logStreams;
189     QHash<QString, QDate> logFileDates;
190     QHash<QString, QDir> logFileDirs;
191
192 };
193
194
195 #endif