1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
27 #include "coreidentity.h"
31 class Storage : public QObject {
35 Storage(QObject *parent = 0);
36 virtual ~Storage() {};
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
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.
52 virtual bool isAvailable() const = 0;
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;
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;
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.
67 virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
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)
73 virtual State init(const QVariantMap &settings = QVariantMap()) = 0;
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.
80 virtual void sync() = 0;
82 // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
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
91 virtual UserId addUser(const QString &user, const QString &password) = 0;
93 //! Update a core user's password.
94 /** \param user The user's id
95 * \param password The user's new password
97 virtual void updateUser(UserId user, const QString &password) = 0;
100 /** \param user The user's id
101 * \param newName The user's new name
103 virtual void renameUser(UserId user, const QString &newName) = 0;
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
110 virtual UserId validateUser(const QString &user, const QString &password) = 0;
112 //! Determine the UserId of the internal user
113 /** \return A valid UserId if the password matches the username; 0 else
115 virtual UserId internalUser() = 0;
117 //! Remove a core user from storage.
118 /** \param user The userid to delete
120 virtual void delUser(UserId user) = 0;
122 //! Store a user setting persistently
124 * \param userId The users Id
125 * \param settingName The Name of the Setting
126 * \param data The Value
128 virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) = 0;
130 //! Retrieve a persistent user setting
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.
137 virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) = 0;
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;
145 /* Network handling */
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.
152 virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
154 //! Apply the changes to NetworkInfo info to the storage engine
156 * \param user The core user
157 * \param networkInfo The Updated NetworkInfo
158 * \return true if successfull.
160 virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
162 //! Permanently remove a Network and all the data associated with it.
163 /** \note This method is thredsafe.
165 * \param user The core user
166 * \param networkId The network to delete
167 * \return true if successfull.
169 virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
171 //! Returns a list of all NetworkInfos for the given UserId user
172 /** \note This method is thredsafe.
174 * \param user The core user
175 * \return QList<NetworkInfo>.
177 virtual QList<NetworkInfo> networks(UserId user) = 0;
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
184 virtual NetworkId getNetworkId(UserId user, const QString &network) = 0;
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.
190 * \param user The User Id in question
192 virtual QList<NetworkId> connectedNetworks(UserId user) = 0;
194 //! Update the connected state of a network
195 /** \note This method is threadsafe
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
201 virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0;
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
207 * \param user The id of the networks owner
208 * \param networkId The Id of the network
210 virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0;
212 //! Update the connected state of a channel
213 /** \note This method is threadsafe
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
220 virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0;
222 //! Update the key of a channel
223 /** \note This method is threadsafe
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)
230 virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0;
232 //! retrieve last known away message for session restore
233 /** \note This method is threadsafe
235 * \param user The Id of the networks owner
236 * \param networkId The Id of the network
238 virtual QString awayMessage(UserId user, NetworkId networkId) = 0;
240 //! Make away message persistent for session restore
241 /** \note This method is threadsafe
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
247 virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) = 0;
250 //! retrieve last known user mode for session restore
251 /** \note This method is threadsafe
253 * \param user The Id of the networks owner
254 * \param networkId The Id of the network
256 virtual QString userModes(UserId user, NetworkId networkId) = 0;
258 //! Make our user modes persistent for session restore
259 /** \note This method is threadsafe
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
265 virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes) = 0;
268 /* Buffer handling */
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
278 virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) = 0;
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.
285 virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0;
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
292 virtual QList<BufferInfo> requestBuffers(UserId user) = 0;
294 //! Request a list of BufferIds for a given NetworkId
295 /** \note This method is threadsafe.
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
301 virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0;
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
309 virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0;
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
319 virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) = 0;
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
328 virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) = 0;
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
336 virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
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
342 virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
345 /* Message handling */
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
351 virtual MsgId logMessage(Message msg) = 0;
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
360 virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
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
368 virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
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);
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
386 QString cryptedPassword(const QString &password);