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 //! Returns a list of properties required to use the storage backend
63 virtual QStringList setupKeys() const = 0;
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;
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.
75 virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
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)
81 virtual State init(const QVariantMap &settings = QVariantMap()) = 0;
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.
88 virtual void sync() = 0;
90 // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
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
99 virtual UserId addUser(const QString &user, const QString &password) = 0;
101 //! Update a core user's password.
102 /** \param user The user's id
103 * \param password The user's new password
105 virtual void updateUser(UserId user, const QString &password) = 0;
108 /** \param user The user's id
109 * \param newName The user's new name
111 virtual void renameUser(UserId user, const QString &newName) = 0;
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
118 virtual UserId validateUser(const QString &user, const QString &password) = 0;
120 //! Determine the UserId of the internal user
121 /** \return A valid UserId if the password matches the username; 0 else
123 virtual UserId internalUser() = 0;
125 //! Remove a core user from storage.
126 /** \param user The userid to delete
128 virtual void delUser(UserId user) = 0;
130 //! Store a user setting persistently
132 * \param userId The users Id
133 * \param settingName The Name of the Setting
134 * \param data The Value
136 virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) = 0;
138 //! Retrieve a persistent user setting
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.
145 virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) = 0;
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;
153 /* Network handling */
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.
160 virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
162 //! Apply the changes to NetworkInfo info to the storage engine
164 * \param user The core user
165 * \param networkInfo The Updated NetworkInfo
166 * \return true if successfull.
168 virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
170 //! Permanently remove a Network and all the data associated with it.
171 /** \note This method is thredsafe.
173 * \param user The core user
174 * \param networkId The network to delete
175 * \return true if successfull.
177 virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
179 //! Returns a list of all NetworkInfos for the given UserId user
180 /** \note This method is thredsafe.
182 * \param user The core user
183 * \return QList<NetworkInfo>.
185 virtual QList<NetworkInfo> networks(UserId user) = 0;
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.
191 * \param user The User Id in question
193 virtual QList<NetworkId> connectedNetworks(UserId user) = 0;
195 //! Update the connected state of a network
196 /** \note This method is threadsafe
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
202 virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0;
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
208 * \param user The id of the networks owner
209 * \param networkId The Id of the network
211 virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0;
213 //! Update the connected state of a channel
214 /** \note This method is threadsafe
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
221 virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0;
223 //! Update the key of a channel
224 /** \note This method is threadsafe
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)
231 virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0;
233 //! retrieve last known away message for session restore
234 /** \note This method is threadsafe
236 * \param user The Id of the networks owner
237 * \param networkId The Id of the network
239 virtual QString awayMessage(UserId user, NetworkId networkId) = 0;
241 //! Make away message persistent for session restore
242 /** \note This method is threadsafe
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
248 virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) = 0;
251 //! retrieve last known user mode for session restore
252 /** \note This method is threadsafe
254 * \param user The Id of the networks owner
255 * \param networkId The Id of the network
257 virtual QString userModes(UserId user, NetworkId networkId) = 0;
259 //! Make our user modes persistent for session restore
260 /** \note This method is threadsafe
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
266 virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes) = 0;
269 /* Buffer handling */
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
279 virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) = 0;
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.
286 virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0;
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
293 virtual QList<BufferInfo> requestBuffers(UserId user) = 0;
295 //! Request a list of BufferIds for a given NetworkId
296 /** \note This method is threadsafe.
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
302 virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0;
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
310 virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0;
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
320 virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) = 0;
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
329 virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) = 0;
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
337 virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
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
343 virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
346 /* Message handling */
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
352 virtual bool logMessage(Message &msg) = 0;
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
358 virtual bool logMessages(MessageList &msgs) = 0;
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
367 virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
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
375 virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) = 0;
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);
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
393 QString cryptedPassword(const QString &password);