1 /***************************************************************************
2 * Copyright (C) 2005-08 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 ***************************************************************************/
30 class Storage : public QObject {
34 Storage(QObject *parent = 0);
35 virtual ~Storage() {};
40 //! Check if the storage type is available.
41 /** A storage subclass should return true if it can be successfully used, i.e. if all
42 * prerequisites are in place (e.g. we have an appropriate DB driver etc.).
43 * \return True if and only if the storage class can be successfully used.
45 virtual bool isAvailable() const = 0;
47 //! Returns the display name of the storage backend
48 /** \return A string that can be used by the client to name the storage backend */
49 virtual QString displayName() const = 0;
51 //! Returns a description of this storage backend
52 /** \return A string that can be displayed by the client to describe the storage backend */
53 virtual QString description() const = 0;
55 //! Setup the storage provider.
56 /** This prepares the storage provider (e.g. create tables, etc.) for use within Quassel.
57 * \param settings Hostname, port, username, password, ...
58 * \return True if and only if the storage provider was initialized successfully.
60 virtual bool setup(const QVariantMap &settings = QVariantMap()) = 0;
62 //! Initialize the storage provider
63 /** \param settings Hostname, port, username, password, ...
64 * \return True if and only if the storage provider was initialized successfully.
66 virtual bool init(const QVariantMap &settings = QVariantMap()) = 0;
68 //! Makes temp data persistent
69 /** This Method is periodically called by the Quassel Core to make temporary
70 * data persistant. This reduces the data loss drastically in the
71 * unlikely case of a Core crash.
73 virtual void sync() = 0;
75 // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
79 //! Add a new core user to the storage.
80 /** \param user The username of the new user
81 * \param password The cleartext password for the new user
82 * \return The new user's UserId
84 virtual UserId addUser(const QString &user, const QString &password) = 0;
86 //! Update a core user's password.
87 /** \param user The user's id
88 * \param password The user's new password
90 virtual void updateUser(UserId user, const QString &password) = 0;
93 /** \param user The user's id
94 * \param newName The user's new name
96 virtual void renameUser(UserId user, const QString &newName) = 0;
98 //! Validate a username with a given password.
99 /** \param user The username to validate
100 * \param password The user's alleged password
101 * \return A valid UserId if the password matches the username; 0 else
103 virtual UserId validateUser(const QString &user, const QString &password) = 0;
105 //! Remove a core user from storage.
106 /** \param user The userid to delete
108 virtual void delUser(UserId user) = 0;
110 /* Network handling */
112 //! Create a new Network in the storage backend and return it unique Id
113 /** \param user The core user who owns this network
114 * \param networkInfo The networkInfo holding the network definition
115 * \return the NetworkId of the newly created Network. Possibly invalid.
117 virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0;
119 //! Apply the changes to NetworkInfo info to the storage engine
121 * \param user The core user
122 * \param networkInfo The Updated NetworkInfo
123 * \return true if successfull.
125 virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0;
127 //! Permanently remove a Network and all the data associated with it.
128 /** \note This method is thredsafe.
130 * \param user The core user
131 * \param networkId The network to delete
132 * \return true if successfull.
134 virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0;
136 //! Returns a list of all NetworkInfos for the given UserId user
137 /** \note This method is thredsafe.
139 * \param user The core user
140 * \return QList<NetworkInfo>.
142 virtual QList<NetworkInfo> networks(UserId user) = 0;
144 //! Get the unique NetworkId of the network for a user.
145 /** \param user The core user who owns this network
146 * \param network The network name
147 * \return The NetworkId corresponding to the given network, or 0 if not found
149 virtual NetworkId getNetworkId(UserId user, const QString &network) = 0;
151 //! Get a list of Networks to restore
152 /** Return a list of networks the user was connected at the time of core shutdown
153 * \note This method is threadsafe.
155 * \param user The User Id in question
157 virtual QList<NetworkId> connectedNetworks(UserId user) = 0;
159 //! Update the connected state of a network
160 /** \note This method is threadsafe
162 * \param user The Id of the networks owner
163 * \param networkId The Id of the network
164 * \param isConnected whether the network is connected or not
166 virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0;
168 //! Get a hash of channels with their channel keys for a given network
169 /** The keys are channel names and values are passwords (possibly empty)
170 * \note This method is threadsafe
172 * \param user The id of the networks owner
173 * \param networkId The Id of the network
175 virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0;
177 //! Update the connected state of a channel
178 /** \note This method is threadsafe
180 * \param user The Id of the networks owner
181 * \param networkId The Id of the network
182 * \param channel The name of the channel
183 * \param isJoined whether the channel is connected or not
185 virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0;
187 //! Update the key of a channel
188 /** \note This method is threadsafe
190 * \param user The Id of the networks owner
191 * \param networkId The Id of the network
192 * \param channel The name of the channel
193 * \param key The key of the channel (possibly empty)
195 virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0;
197 /* Buffer handling */
199 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
200 /** \param user The core user who owns this buffername
201 * \param networkId The network id
202 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
203 * \param buffer The buffer name (if empty, the net's status buffer is returned)
204 * \return The BufferInfo corresponding to the given network and buffer name, or an invalid BufferInfo if not found
206 virtual BufferInfo getBufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "") = 0;
208 //! Get the unique BufferInfo for a bufferId
209 /** \param user The core user who owns this buffername
210 * \param bufferId The id of the buffer
211 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
213 virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0;
215 //! Request a list of all buffers known to a user.
216 /** This method is used to get a list of all buffers we have stored a backlog from.
217 * \param user The user whose buffers we request
218 * \return A list of the BufferInfos for all buffers as requested
220 virtual QList<BufferInfo> requestBuffers(UserId user) = 0;
222 //! Remove permanently a buffer and it's content from the storage backend
223 /** This call cannot be reverted!
224 * \param user The user who is the owner of the buffer
225 * \param bufferId The bufferId
226 * \return true if successfull
228 virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0;
231 /** \param user The id of the buffer owner
232 * \param networkId The id of the network the buffer belongs to
233 * \param newName The new name of the buffer
234 * \param oldName The previous name of the buffer
235 * \return the BufferId of the affected buffer or an invalid BufferId if not successfull
237 virtual BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName) = 0;
239 //! Update the LastSeenDate for a Buffer
240 /** This Method is used to make the LastSeenDate of a Buffer persistent
241 * \param user The Owner of that Buffer
242 * \param bufferId The buffer id
243 * \param MsgId The Message id of the message that has been just seen
245 virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0;
247 //! Get a Hash of all last seen message ids
248 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
249 * \param user The Owner of the buffers
251 virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0;
254 /* Message handling */
256 //! Store a Message in the backlog.
257 /** \param msg The message object to be stored
258 * \return The globally unique id for the stored message
260 virtual MsgId logMessage(Message msg) = 0;
262 //! Request a certain number (or all) messages stored in a given buffer.
263 /** \param buffer The buffer we request messages from
264 * \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
265 * \param offset Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
266 * \return The requested list of messages
268 virtual QList<Message> requestMsgs(UserId user, BufferId buffer, int lastmsgs = -1, int offset = -1) = 0;
270 //! Request messages stored in a given buffer since a certain point in time.
271 /** \param buffer The buffer we request messages from
272 * \param since Only return messages newer than this point in time
273 * \param offset Do not return messages with MsgId >= offset, if offset >= 0
274 * \return The requested list of messages
276 virtual QList<Message> requestMsgs(UserId user, BufferId buffer, QDateTime since, int offset = -1) = 0;
278 //! Request a range of messages stored in a given buffer.
279 /** \param buffer The buffer we request messages from
280 * \param first Return messages with first <= MsgId <= last
281 * \param last Return messages with first <= MsgId <= last
282 * \return The requested list of messages
284 virtual QList<Message> requestMsgRange(UserId user, BufferId buffer, int first, int last) = 0;
287 //! Sent when a new BufferInfo is created, or an existing one changed somehow.
288 void bufferInfoUpdated(UserId user, const BufferInfo &);
289 //! Sent when a Buffer was renamed
290 void bufferRenamed(const QString &newName, const QString &oldName);
291 //! Sent when a new user has been added
292 void userAdded(UserId, const QString &username);
293 //! Sent when a user has been renamed
294 void userRenamed(UserId, const QString &newname);
295 //! Sent when a user has been removed
296 void userRemoved(UserId);
299 //! when implementing a storage handler, use this method to crypt user passwords.
300 /** This guarantees compatibility with other storage handlers and allows easy migration
302 QString cryptedPassword(const QString &password);