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