The BufferSyncer cache is now purged when an unknown bufferId is encountered.
[quassel.git] / src / core / storage.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 STORAGE_H
22 #define STORAGE_H
23
24 #include <QtCore>
25
26 #include "types.h"
27 #include "coreidentity.h"
28 #include "message.h"
29 #include "network.h"
30
31 class Storage : public QObject {
32   Q_OBJECT
33
34 public:
35   Storage(QObject *parent = 0);
36   virtual ~Storage() {};
37
38   enum State {
39     IsReady,        // ready to go
40     NeedsSetup,     // need basic setup (ask the user for input)
41     NotAvailable    // remove the storage backend from the list of avaliable backends
42   };
43
44 public slots:
45   /* General */
46
47   //! Check if the storage type is available.
48   /** A storage subclass should return true if it can be successfully used, i.e. if all
49    *  prerequisites are in place (e.g. we have an appropriate DB driver etc.).
50    * \return True if and only if the storage class can be successfully used.
51    */
52   virtual bool isAvailable() const = 0;
53
54   //! Returns the display name of the storage backend
55   /** \return A string that can be used by the client to name the storage backend */
56   virtual QString displayName() const = 0;
57
58   //! Returns a description of this storage backend
59   /** \return A string that can be displayed by the client to describe the storage backend */
60   virtual QString description() const = 0;
61
62   //! Setup the storage provider.
63   /** This prepares the storage provider (e.g. create tables, etc.) for use within Quassel.
64    *  \param settings   Hostname, port, username, password, ...
65    *  \return True if and only if the storage provider was initialized successfully.
66    */
67   virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
68
69   //! Initialize the storage provider
70   /** \param settings   Hostname, port, username, password, ...  
71    *  \return the State the storage backend is now in (see Storage::State)
72    */
73   virtual State init(const QVariantMap &settings = QVariantMap()) = 0;
74
75   //! Makes temp data persistent
76   /** This Method is periodically called by the Quassel Core to make temporary
77    *  data persistant. This reduces the data loss drastically in the
78    *  unlikely case of a Core crash.
79    */
80   virtual void sync() = 0;
81
82   // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
83
84   /* User handling */
85
86   //! Add a new core user to the storage.
87   /** \param user     The username of the new user
88    *  \param password The cleartext password for the new user
89    *  \return The new user's UserId
90    */
91   virtual UserId addUser(const QString &user, const QString &password) = 0;
92
93   //! Update a core user's password.
94   /** \param user     The user's id
95    *  \param password The user's new password
96    */
97   virtual void updateUser(UserId user, const QString &password) = 0;
98
99   //! Rename a user
100   /** \param user     The user's id
101    *  \param newName  The user's new name
102    */
103   virtual void renameUser(UserId user, const QString &newName) = 0;
104
105   //! Validate a username with a given password.
106   /** \param user     The username to validate
107    *  \param password The user's alleged password
108    *  \return A valid UserId if the password matches the username; 0 else
109    */
110   virtual UserId validateUser(const QString &user, const QString &password) = 0;
111
112   //! Determine the UserId of the internal user
113   /** \return A valid UserId if the password matches the username; 0 else
114    */
115   virtual UserId internalUser() = 0;
116
117   //! Remove a core user from storage.
118   /** \param user     The userid to delete
119    */
120   virtual void delUser(UserId user) = 0;
121
122   //! Store a user setting persistently
123   /**
124    * \param userId       The users Id
125    * \param settingName  The Name of the Setting
126    * \param data         The Value
127    */
128   virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) = 0;
129   
130   //! Retrieve a persistent user setting
131   /**
132    * \param userId       The users Id
133    * \param settingName  The Name of the Setting
134    * \param default      Value to return in case it's unset.
135    * \return the Value of the Setting or the default value if it is unset.
136    */
137   virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) = 0;
138
139   /* Identity handling */
140   virtual IdentityId createIdentity(UserId user, CoreIdentity &identity) = 0;
141   virtual bool updateIdentity(UserId user, const CoreIdentity &identity) = 0;
142   virtual void removeIdentity(UserId user, IdentityId identityId) = 0;
143   virtual QList<CoreIdentity> identities(UserId user) = 0;
144   
145   /* Network handling */
146
147   //! Create a new Network in the storage backend and return it unique Id
148   /** \param user        The core user who owns this network
149    *  \param networkInfo The networkInfo holding the network definition
150    *  \return the NetworkId of the newly created Network. Possibly invalid.
151    */
152   virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
153
154   //! Apply the changes to NetworkInfo info to the storage engine
155   /**
156    *  \param user        The core user
157    *  \param networkInfo The Updated NetworkInfo
158    *  \return true if successfull.
159    */
160   virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
161
162   //! Permanently remove a Network and all the data associated with it.
163   /** \note This method is thredsafe.
164    *
165    *  \param user        The core user
166    *  \param networkId   The network to delete
167    *  \return true if successfull.
168    */
169   virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
170
171   //! Returns a list of all NetworkInfos for the given UserId user
172   /** \note This method is thredsafe.
173    *
174    *  \param user        The core user
175    *  \return QList<NetworkInfo>.
176    */
177   virtual QList<NetworkInfo> networks(UserId user) = 0;
178   
179   //! Get the unique NetworkId of the network for a user.
180   /** \param user    The core user who owns this network
181    *  \param network The network name
182    *  \return The NetworkId corresponding to the given network, or 0 if not found
183    */
184   virtual NetworkId getNetworkId(UserId user, const QString &network) = 0;
185
186   //! Get a list of Networks to restore
187   /** Return a list of networks the user was connected at the time of core shutdown
188    *  \note This method is threadsafe.
189    *
190    *  \param user  The User Id in question
191    */
192   virtual QList<NetworkId> connectedNetworks(UserId user) = 0;
193
194   //! Update the connected state of a network
195   /** \note This method is threadsafe
196    *
197    *  \param user        The Id of the networks owner
198    *  \param networkId   The Id of the network
199    *  \param isConnected whether the network is connected or not
200    */
201   virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0;
202
203   //! Get a hash of channels with their channel keys for a given network
204   /** The keys are channel names and values are passwords (possibly empty)
205    *  \note This method is threadsafe
206    *
207    *  \param user       The id of the networks owner
208    *  \param networkId  The Id of the network
209    */
210   virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0;
211
212   //! Update the connected state of a channel
213   /** \note This method is threadsafe
214    *
215    *  \param user       The Id of the networks owner
216    *  \param networkId  The Id of the network
217    *  \param channel    The name of the channel
218    *  \param isJoined   whether the channel is connected or not
219    */
220   virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0;
221
222   //! Update the key of a channel
223   /** \note This method is threadsafe
224    *
225    *  \param user       The Id of the networks owner
226    *  \param networkId  The Id of the network
227    *  \param channel    The name of the channel
228    *  \param key        The key of the channel (possibly empty)
229    */
230   virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0;
231
232   //! retrieve last known away message for session restore
233   /** \note This method is threadsafe
234    *
235    *  \param user       The Id of the networks owner
236    *  \param networkId  The Id of the network
237    */
238   virtual QString awayMessage(UserId user, NetworkId networkId) = 0;
239
240   //! Make away message persistent for session restore
241   /** \note This method is threadsafe
242    *
243    *  \param user       The Id of the networks owner
244    *  \param networkId  The Id of the network
245    *  \param awayMsg    The current away message of own user
246    */
247   virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) = 0;
248
249
250   //! retrieve last known user mode for session restore
251   /** \note This method is threadsafe
252    *
253    *  \param user       The Id of the networks owner
254    *  \param networkId  The Id of the network
255    */
256   virtual QString userModes(UserId user, NetworkId networkId) = 0;
257
258   //! Make our user modes persistent for session restore
259   /** \note This method is threadsafe
260    *
261    *  \param user       The Id of the networks owner
262    *  \param networkId  The Id of the network
263    *  \param userModes  The current user modes of own user
264    */
265   virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes) = 0;
266
267
268   /* Buffer handling */
269
270   //! Get the unique BufferInfo for the given combination of network and buffername for a user.
271   /** \param user      The core user who owns this buffername
272    *  \param networkId The network id
273    *  \param type      The type of the buffer (StatusBuffer, Channel, etc.)
274    *  \param buffer  The buffer name (if empty, the net's status buffer is returned)
275    *  \param create    Whether or not the buffer should be created if it doesnt exist
276    *  \return The BufferInfo corresponding to the given network and buffer name, or an invalid BufferInfo if not found
277    */
278   virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) = 0;
279
280   //! Get the unique BufferInfo for a bufferId
281   /** \param user      The core user who owns this buffername
282    *  \param bufferId  The id of the buffer
283    *  \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
284    */
285   virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0;
286
287   //! Request a list of all buffers known to a user.
288   /** This method is used to get a list of all buffers we have stored a backlog from.
289    *  \param user  The user whose buffers we request
290    *  \return A list of the BufferInfos for all buffers as requested
291    */
292   virtual QList<BufferInfo> requestBuffers(UserId user) = 0;
293
294   //! Request a list of BufferIds for a given NetworkId
295   /** \note This method is threadsafe.
296    *
297    *  \param user  The user whose buffers we request
298    *  \param networkId  The NetworkId of the network in question
299    *  \return List of BufferIds belonging to the Network
300    */
301   virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0;
302
303   //! Remove permanently a buffer and it's content from the storage backend
304   /** This call cannot be reverted!
305    *  \param user      The user who is the owner of the buffer
306    *  \param bufferId  The bufferId
307    *  \return true if successfull
308    */
309   virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0;
310
311
312   //! Rename a Buffer
313   /** \note This method is threadsafe.
314    *  \param user      The id of the buffer owner
315    *  \param bufferId  The bufferId
316    *  \param newName   The new name of the buffer
317    *  \return true if successfull
318    */
319   virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) = 0;
320
321   //! Merge the content of two Buffers permanently. This cannot be reversed!
322   /** \note This method is threadsafe.
323    *  \param user      The id of the buffer owner
324    *  \param bufferId1 The bufferId of the remaining buffer
325    *  \param bufferId2 The buffer that is about to be removed
326    *  \return true if successfull
327    */
328   virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) = 0;
329
330   //! Update the LastSeenDate for a Buffer
331   /** This Method is used to make the LastSeenDate of a Buffer persistent
332    * \param user      The Owner of that Buffer
333    * \param bufferId  The buffer id
334    * \param MsgId     The Message id of the message that has been just seen
335    */
336   virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
337
338   //! Get a Hash of all last seen message ids
339   /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
340    * \param user      The Owner of the buffers
341    */
342   virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
343
344   
345   /* Message handling */
346
347   //! Store a Message in the backlog.
348   /** \param msg  The message object to be stored
349    *  \return The globally unique id for the stored message
350    */
351   virtual MsgId logMessage(Message msg) = 0;
352
353   //! Request a certain number messages stored in a given buffer.
354   /** \param buffer   The buffer we request messages from
355    *  \param first    if != -1 return only messages with a MsgId >= first
356    *  \param last     if != -1 return only messages with a MsgId < last
357    *  \param limit    if != -1 limit the returned list to a max of \limit entries
358    *  \return The requested list of messages
359    */
360   virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
361
362   //! Request a certain number of messages across all buffers
363   /** \param first    if != -1 return only messages with a MsgId >= first
364    *  \param last     if != -1 return only messages with a MsgId < last
365    *  \param limit    Max amount of messages
366    *  \return The requested list of messages
367    */
368   virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
369
370 signals:
371   //! Sent when a new BufferInfo is created, or an existing one changed somehow.
372   void bufferInfoUpdated(UserId user, const BufferInfo &);
373   //! Sent when a Buffer was renamed
374   void bufferRenamed(const QString &newName, const QString &oldName);
375   //! Sent when a new user has been added
376   void userAdded(UserId, const QString &username);
377   //! Sent when a user has been renamed
378   void userRenamed(UserId, const QString &newname);
379   //! Sent when a user has been removed
380   void userRemoved(UserId);
381
382 protected:
383   //! when implementing a storage handler, use this method to crypt user passwords.
384   /**  This guarantees compatibility with other storage handlers and allows easy migration
385    */
386   QString cryptedPassword(const QString &password);
387 };
388
389
390 #endif