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