added missing sql queries... OOPS
[quassel.git] / src / core / storage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 _STORAGE_H_
22 #define _STORAGE_H_
23
24 #include <QtCore>
25
26 #include "types.h"
27 #include "message.h"
28 #include "network.h"
29
30 class Storage : public QObject {
31   Q_OBJECT
32
33   public:
34     Storage(QObject *parent = 0);
35     virtual ~Storage() {};
36
37   public slots:
38     /* General */
39
40     //! Check if the storage type is available.
41     /** A storage subclass should return true if it can be successfully used, i.e. if all
42      *  prerequisites are in place (e.g. we have an appropriate DB driver etc.).
43      * \return True if and only if the storage class can be successfully used.
44      */
45     virtual bool isAvailable() const = 0;
46
47     //! Returns the display name of the storage backend
48     /** \return A string that can be used by the client to name the storage backend */
49     virtual QString displayName() const = 0;
50
51     //! Returns a description of this storage backend
52     /** \return A string that can be displayed by the client to describe the storage backend */
53     virtual QString description() const = 0;
54
55     //! Setup the storage provider.
56     /** This prepares the storage provider (e.g. create tables, etc.) for use within Quassel.
57      *  \param settings   Hostname, port, username, password, ...
58      *  \return True if and only if the storage provider was initialized successfully.
59      */
60     virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
61
62     //! Initialize the storage provider
63     /** \param settings   Hostname, port, username, password, ...  
64      *  \return True if and only if the storage provider was initialized successfully.
65      */
66     virtual bool init(const QVariantMap &settings = QVariantMap()) = 0;
67
68     //! Makes temp data persistent
69     /** This Method is periodically called by the Quassel Core to make temporary
70     *  data persistant. This reduces the data loss drastically in the
71     *  unlikely case of a Core crash.
72     */
73     virtual void sync() = 0;
74
75     // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
76
77     /* User handling */
78
79     //! Add a new core user to the storage.
80     /** \param user     The username of the new user
81      *  \param password The cleartext password for the new user
82      *  \return The new user's UserId
83      */
84     virtual UserId addUser(const QString &user, const QString &password) = 0;
85
86     //! Update a core user's password.
87     /** \param user     The user's id
88      *  \param password The user's new password
89      */
90     virtual void updateUser(UserId user, const QString &password) = 0;
91
92     //! Rename a user
93     /** \param user     The user's id
94      *  \param newName  The user's new name
95      */
96     virtual void renameUser(UserId user, const QString &newName) = 0;
97
98     //! Validate a username with a given password.
99     /** \param user     The username to validate
100      *  \param password The user's alleged password
101      *  \return A valid UserId if the password matches the username; 0 else
102      */
103     virtual UserId validateUser(const QString &user, const QString &password) = 0;
104
105     //! Remove a core user from storage.
106     /** \param user     The userid to delete
107      */
108     virtual void delUser(UserId user) = 0;
109
110     /* Network handling */
111
112     //! Create a new Network in the storage backend and return it unique Id
113     /** \param user        The core user who owns this network
114      *  \param networkInfo The networkInfo holding the network definition
115      *  \return the NetworkId of the newly created Network. Possibly invalid.
116      */
117     virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
118
119     //! Apply the changes to NetworkInfo info to the storage engine
120     /** \note This method is thredsafe.
121      *
122      *  \param user        The core user
123      *  \param networkInfo The Updated NetworkInfo
124      *  \return true if successfull.
125      */
126     virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
127
128     //! Permanently remove a Network and all the data associated with it.
129     /** \note This method is thredsafe.
130      *
131      *  \param user        The core user
132      *  \param networkId   The network to delete
133      *  \return true if successfull.
134      */
135     virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
136
137     //! Returns a list of all NetworkInfos for the given UserId user
138     /** \note This method is thredsafe.
139      *
140      *  \param user        The core user
141      *  \return QList<NetworkInfo>.
142      */
143     virtual QList<NetworkInfo> networks(UserId user) = 0;
144   
145     //! Get the unique NetworkId of the network for a user.
146     /** \param user    The core user who owns this network
147      *  \param network The network name
148      *  \return The NetworkId corresponding to the given network, or 0 if not found
149      */
150     virtual NetworkId getNetworkId(UserId user, const QString &network) = 0;
151
152     /* Buffer handling */
153
154     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
155     /** \param user      The core user who owns this buffername
156      *  \param networkId The network id
157      *  \param type      The type of the buffer (StatusBuffer, Channel, etc.)
158      *  \param buffer  The buffer name (if empty, the net's status buffer is returned)
159      *  \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
160      */
161     virtual BufferInfo getBufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "") = 0;
162
163     //! Request a list of all buffers known to a user since a certain point in time.
164     /** This method is used to get a list of all buffers we have stored a backlog from.
165      *  Optionally, a QDateTime can be given, so that only buffers are listed that where active
166      *  since that point in time.
167      *  \param user  The user whose buffers we request
168      *  \param since If this is defined, older buffers will be ignored
169      *  \return A list of the BufferInfos for all buffers as requested
170      */
171     virtual QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime()) = 0;
172
173     //! Update the LastSeenDate for a Buffer
174     /** This Method is used to make the LastSeenDate of a Buffer persistent
175      * \param user      The Owner of that Buffer
176      * \param bufferId  The buffer id
177      * \param seenDate  Time the Buffer has been visited the last time
178      */
179     virtual void setBufferLastSeen(UserId user, const BufferId &bufferId, const QDateTime &seenDate) = 0;
180
181     //! Get a Hash of all last seen dates. 
182     /** This Method is called when the Quassel Core is started to restore the lastSeenDates
183      * \param user      The Owner of the buffers
184      */
185     virtual QHash<BufferId, QDateTime> bufferLastSeenDates(UserId user) = 0;
186
187   
188     /* Message handling */
189
190     //! Store a Message in the backlog.
191     /** \param msg  The message object to be stored
192      *  \return The globally unique id for the stored message
193      */
194     virtual MsgId logMessage(Message msg) = 0;
195
196     //! Request a certain number (or all) messages stored in a given buffer.
197     /** \param buffer   The buffer we request messages from
198      *  \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
199      *  \param offset   Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
200      *  \return The requested list of messages
201      */
202     virtual QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1) = 0;
203
204     //! Request messages stored in a given buffer since a certain point in time.
205     /** \param buffer   The buffer we request messages from
206      *  \param since    Only return messages newer than this point in time
207      *  \param offset   Do not return messages with MsgId >= offset, if offset >= 0
208      *  \return The requested list of messages
209      */
210     virtual QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1) = 0;
211
212     //! Request a range of messages stored in a given buffer.
213     /** \param buffer   The buffer we request messages from
214      *  \param first    Return messages with first <= MsgId <= last
215      *  \param last     Return messages with first <= MsgId <= last
216      *  \return The requested list of messages
217      */
218     virtual QList<Message> requestMsgRange(BufferInfo buffer, int first, int last) = 0;
219
220   signals:
221     //! Sent when a new BufferInfo is created, or an existing one changed somehow.
222     void bufferInfoUpdated(UserId user, const BufferInfo &);
223     //! Sent when a new user has been added
224     void userAdded(UserId, const QString &username);
225     //! Sent when a user has been renamed
226     void userRenamed(UserId, const QString &newname);
227     //! Sent when a user has been removed
228     void userRemoved(UserId);
229
230   public:
231
232 };
233
234
235 #endif