1 | /*************************************************************************** |
2 | * Copyright (C) 2005-2014 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 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 | { |
33 | Q_OBJECT |
34 | |
35 | public: |
36 | Storage(QObject *parent = 0); |
37 | virtual ~Storage() {}; |
38 | |
39 | enum State { |
40 | IsReady, // ready to go |
41 | NeedsSetup, // need basic setup (ask the user for input) |
42 | NotAvailable // remove the storage backend from the list of avaliable backends |
43 | }; |
44 | |
45 | public slots: |
46 | /* General */ |
47 | |
48 | //! Check if the storage type is available. |
49 | /** A storage subclass should return true if it can be successfully used, i.e. if all |
50 | * prerequisites are in place (e.g. we have an appropriate DB driver etc.). |
51 | * \return True if and only if the storage class can be successfully used. |
52 | */ |
53 | virtual bool isAvailable() const = 0; |
54 | |
55 | //! Returns the display name of the storage backend |
56 | /** \return A string that can be used by the client to name the storage backend */ |
57 | virtual QString displayName() const = 0; |
58 | |
59 | //! Returns a description of this storage backend |
60 | /** \return A string that can be displayed by the client to describe the storage backend */ |
61 | virtual QString description() const = 0; |
62 | |
63 | //! Returns a list of properties required to use the storage backend |
64 | virtual QStringList setupKeys() const = 0; |
65 | |
66 | //! Returns a map where the keys are are properties to use the storage backend |
67 | /* the values are QVariants with default values */ |
68 | virtual QVariantMap setupDefaults() const = 0; |
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 | * \return true on success. |
105 | */ |
106 | virtual bool updateUser(UserId user, const QString &password) = 0; |
107 | |
108 | //! Rename a user |
109 | /** \param user The user's id |
110 | * \param newName The user's new name |
111 | */ |
112 | virtual void renameUser(UserId user, const QString &newName) = 0; |
113 | |
114 | //! Validate a username with a given password. |
115 | /** \param user The username to validate |
116 | * \param password The user's alleged password |
117 | * \return A valid UserId if the password matches the username; 0 else |
118 | */ |
119 | virtual UserId validateUser(const QString &user, const QString &password) = 0; |
120 | |
121 | //! Check if a user with given username exists. Do not use for login purposes! |
122 | /** \param username The username to validate |
123 | * \return A valid UserId if the user exists; 0 else |
124 | */ |
125 | virtual UserId getUserId(const QString &username) = 0; |
126 | |
127 | //! Determine the UserId of the internal user |
128 | /** \return A valid UserId if the password matches the username; 0 else |
129 | */ |
130 | virtual UserId internalUser() = 0; |
131 | |
132 | //! Remove a core user from storage. |
133 | /** \param user The userid to delete |
134 | */ |
135 | virtual void delUser(UserId user) = 0; |
136 | |
137 | //! Store a user setting persistently |
138 | /** |
139 | * \param userId The users Id |
140 | * \param settingName The Name of the Setting |
141 | * \param data The Value |
142 | */ |
143 | virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) = 0; |
144 | |
145 | //! Retrieve a persistent user setting |
146 | /** |
147 | * \param userId The users Id |
148 | * \param settingName The Name of the Setting |
149 | * \param default Value to return in case it's unset. |
150 | * \return the Value of the Setting or the default value if it is unset. |
151 | */ |
152 | virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) = 0; |
153 | |
154 | /* Identity handling */ |
155 | virtual IdentityId createIdentity(UserId user, CoreIdentity &identity) = 0; |
156 | virtual bool updateIdentity(UserId user, const CoreIdentity &identity) = 0; |
157 | virtual void removeIdentity(UserId user, IdentityId identityId) = 0; |
158 | virtual QList<CoreIdentity> identities(UserId user) = 0; |
159 | |
160 | /* Network handling */ |
161 | |
162 | //! Create a new Network in the storage backend and return it unique Id |
163 | /** \param user The core user who owns this network |
164 | * \param networkInfo The networkInfo holding the network definition |
165 | * \return the NetworkId of the newly created Network. Possibly invalid. |
166 | */ |
167 | virtual NetworkId createNetwork(UserId user, const NetworkInfo &info) = 0; |
168 | |
169 | //! Apply the changes to NetworkInfo info to the storage engine |
170 | /** |
171 | * \param user The core user |
172 | * \param networkInfo The Updated NetworkInfo |
173 | * \return true if successfull. |
174 | */ |
175 | virtual bool updateNetwork(UserId user, const NetworkInfo &info) = 0; |
176 | |
177 | //! Permanently remove a Network and all the data associated with it. |
178 | /** \note This method is thredsafe. |
179 | * |
180 | * \param user The core user |
181 | * \param networkId The network to delete |
182 | * \return true if successfull. |
183 | */ |
184 | virtual bool removeNetwork(UserId user, const NetworkId &networkId) = 0; |
185 | |
186 | //! Returns a list of all NetworkInfos for the given UserId user |
187 | /** \note This method is thredsafe. |
188 | * |
189 | * \param user The core user |
190 | * \return QList<NetworkInfo>. |
191 | */ |
192 | virtual QList<NetworkInfo> networks(UserId user) = 0; |
193 | |
194 | //! Get a list of Networks to restore |
195 | /** Return a list of networks the user was connected at the time of core shutdown |
196 | * \note This method is threadsafe. |
197 | * |
198 | * \param user The User Id in question |
199 | */ |
200 | virtual QList<NetworkId> connectedNetworks(UserId user) = 0; |
201 | |
202 | //! Update the connected state of a network |
203 | /** \note This method is threadsafe |
204 | * |
205 | * \param user The Id of the networks owner |
206 | * \param networkId The Id of the network |
207 | * \param isConnected whether the network is connected or not |
208 | */ |
209 | virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) = 0; |
210 | |
211 | //! Get a hash of channels with their channel keys for a given network |
212 | /** The keys are channel names and values are passwords (possibly empty) |
213 | * \note This method is threadsafe |
214 | * |
215 | * \param user The id of the networks owner |
216 | * \param networkId The Id of the network |
217 | */ |
218 | virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) = 0; |
219 | |
220 | //! Update the connected state of a channel |
221 | /** \note This method is threadsafe |
222 | * |
223 | * \param user The Id of the networks owner |
224 | * \param networkId The Id of the network |
225 | * \param channel The name of the channel |
226 | * \param isJoined whether the channel is connected or not |
227 | */ |
228 | virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) = 0; |
229 | |
230 | //! Update the key of a channel |
231 | /** \note This method is threadsafe |
232 | * |
233 | * \param user The Id of the networks owner |
234 | * \param networkId The Id of the network |
235 | * \param channel The name of the channel |
236 | * \param key The key of the channel (possibly empty) |
237 | */ |
238 | virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) = 0; |
239 | |
240 | //! retrieve last known away message for session restore |
241 | /** \note This method is threadsafe |
242 | * |
243 | * \param user The Id of the networks owner |
244 | * \param networkId The Id of the network |
245 | */ |
246 | virtual QString awayMessage(UserId user, NetworkId networkId) = 0; |
247 | |
248 | //! Make away message persistent for session restore |
249 | /** \note This method is threadsafe |
250 | * |
251 | * \param user The Id of the networks owner |
252 | * \param networkId The Id of the network |
253 | * \param awayMsg The current away message of own user |
254 | */ |
255 | virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) = 0; |
256 | |
257 | //! retrieve last known user mode for session restore |
258 | /** \note This method is threadsafe |
259 | * |
260 | * \param user The Id of the networks owner |
261 | * \param networkId The Id of the network |
262 | */ |
263 | virtual QString userModes(UserId user, NetworkId networkId) = 0; |
264 | |
265 | //! Make our user modes persistent for session restore |
266 | /** \note This method is threadsafe |
267 | * |
268 | * \param user The Id of the networks owner |
269 | * \param networkId The Id of the network |
270 | * \param userModes The current user modes of own user |
271 | */ |
272 | virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes) = 0; |
273 | |
274 | /* Buffer handling */ |
275 | |
276 | //! Get the unique BufferInfo for the given combination of network and buffername for a user. |
277 | /** \param user The core user who owns this buffername |
278 | * \param networkId The network id |
279 | * \param type The type of the buffer (StatusBuffer, Channel, etc.) |
280 | * \param buffer The buffer name (if empty, the net's status buffer is returned) |
281 | * \param create Whether or not the buffer should be created if it doesnt exist |
282 | * \return The BufferInfo corresponding to the given network and buffer name, or an invalid BufferInfo if not found |
283 | */ |
284 | virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "" , bool create = true) = 0; |
285 | |
286 | //! Get the unique BufferInfo for a bufferId |
287 | /** \param user The core user who owns this buffername |
288 | * \param bufferId The id of the buffer |
289 | * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found. |
290 | */ |
291 | virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) = 0; |
292 | |
293 | //! Request a list of all buffers known to a user. |
294 | /** This method is used to get a list of all buffers we have stored a backlog from. |
295 | * \param user The user whose buffers we request |
296 | * \return A list of the BufferInfos for all buffers as requested |
297 | */ |
298 | virtual QList<BufferInfo> requestBuffers(UserId user) = 0; |
299 | |
300 | //! Request a list of BufferIds for a given NetworkId |
301 | /** \note This method is threadsafe. |
302 | * |
303 | * \param user The user whose buffers we request |
304 | * \param networkId The NetworkId of the network in question |
305 | * \return List of BufferIds belonging to the Network |
306 | */ |
307 | virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) = 0; |
308 | |
309 | //! Remove permanently a buffer and it's content from the storage backend |
310 | /** This call cannot be reverted! |
311 | * \param user The user who is the owner of the buffer |
312 | * \param bufferId The bufferId |
313 | * \return true if successfull |
314 | */ |
315 | virtual bool removeBuffer(const UserId &user, const BufferId &bufferId) = 0; |
316 | |
317 | //! Rename a Buffer |
318 | /** \note This method is threadsafe. |
319 | * \param user The id of the buffer owner |
320 | * \param bufferId The bufferId |
321 | * \param newName The new name of the buffer |
322 | * \return true if successfull |
323 | */ |
324 | virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) = 0; |
325 | |
326 | //! Merge the content of two Buffers permanently. This cannot be reversed! |
327 | /** \note This method is threadsafe. |
328 | * \param user The id of the buffer owner |
329 | * \param bufferId1 The bufferId of the remaining buffer |
330 | * \param bufferId2 The buffer that is about to be removed |
331 | * \return true if successfull |
332 | */ |
333 | virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) = 0; |
334 | |
335 | //! Update the LastSeenDate for a Buffer |
336 | /** This Method is used to make the LastSeenDate of a Buffer persistent |
337 | * \param user The Owner of that Buffer |
338 | * \param bufferId The buffer id |
339 | * \param MsgId The Message id of the message that has been just seen |
340 | */ |
341 | virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0; |
342 | |
343 | //! Get a Hash of all last seen message ids |
344 | /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds |
345 | * \param user The Owner of the buffers |
346 | */ |
347 | virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) = 0; |
348 | |
349 | //! Update the MarkerLineMsgId for a Buffer |
350 | /** This Method is used to make the marker line position of a Buffer persistent |
351 | * \note This method is threadsafe. |
352 | * |
353 | * \param user The Owner of that Buffer |
354 | * \param bufferId The buffer id |
355 | * \param MsgId The Message id where the marker line should be placed |
356 | */ |
357 | virtual void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) = 0; |
358 | |
359 | //! Get a Hash of all marker line message ids |
360 | /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds |
361 | * \note This method is threadsafe. |
362 | * |
363 | * \param user The Owner of the buffers |
364 | */ |
365 | virtual QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) = 0; |
366 | |
367 | /* Message handling */ |
368 | |
369 | //! Store a Message in the storage backend and set its unique Id. |
370 | /** \param msg The message object to be stored |
371 | * \return true on success |
372 | */ |
373 | virtual bool logMessage(Message &msg) = 0; |
374 | |
375 | //! Store a list of Messages in the storage backend and set their unique Id. |
376 | /** \param msgs The list message objects to be stored |
377 | * \return true on success |
378 | */ |
379 | virtual bool logMessages(MessageList &msgs) = 0; |
380 | |
381 | //! Request a certain number messages stored in a given buffer. |
382 | /** \param buffer The buffer we request messages from |
383 | * \param first if != -1 return only messages with a MsgId >= first |
384 | * \param last if != -1 return only messages with a MsgId < last |
385 | * \param limit if != -1 limit the returned list to a max of \limit entries |
386 | * \return The requested list of messages |
387 | */ |
388 | virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) = 0; |
389 | |
390 | //! Request a certain number of messages across all buffers |
391 | /** \param first if != -1 return only messages with a MsgId >= first |
392 | * \param last if != -1 return only messages with a MsgId < last |
393 | * \param limit Max amount of messages |
394 | * \return The requested list of messages |
395 | */ |
396 | virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) = 0; |
397 | |
398 | signals: |
399 | //! Sent when a new BufferInfo is created, or an existing one changed somehow. |
400 | void bufferInfoUpdated(UserId user, const BufferInfo &); |
401 | //! Sent when a Buffer was renamed |
402 | void bufferRenamed(const QString &newName, const QString &oldName); |
403 | //! Sent when a new user has been added |
404 | void userAdded(UserId, const QString &username); |
405 | //! Sent when a user has been renamed |
406 | void userRenamed(UserId, const QString &newname); |
407 | //! Sent when a user has been removed |
408 | void userRemoved(UserId); |
409 | |
410 | protected: |
411 | //! when implementing a storage handler, use this method to crypt user passwords. |
412 | /** This guarantees compatibility with other storage handlers and allows easy migration |
413 | */ |
414 | QString cryptedPassword(const QString &password); |
415 | }; |
416 | |
417 | |
418 | #endif |
419 | |