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