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