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 ***************************************************************************/
32 #include "bufferinfo.h"
35 #include "sessionthread.h"
43 class Core : public QObject {
47 static Core * instance();
48 static void destroy();
50 static void saveState();
51 static void restoreState();
53 /*** Storage access ***/
54 // These methods are threadsafe.
56 //! Create a Network in the Storage and store it's Id in the given NetworkInfo
57 /** \note This method is thredsafe.
59 * \param user The core user
60 * \param networkInfo a NetworkInfo definition to store the newly created ID in
61 * \return true if successfull.
63 static bool createNetwork(UserId user, NetworkInfo &info);
65 //! Apply the changes to NetworkInfo info to the storage engine
66 /** \note This method is thredsafe.
68 * \param user The core user
69 * \param networkInfo The Updated NetworkInfo
70 * \return true if successfull.
72 static bool updateNetwork(UserId user, const NetworkInfo &info);
74 //! Permanently remove a Network and all the data associated with it.
75 /** \note This method is thredsafe.
77 * \param user The core user
78 * \param networkId The network to delete
79 * \return true if successfull.
81 static bool removeNetwork(UserId user, const NetworkId &networkId);
83 //! Returns a list of all NetworkInfos for the given UserId user
84 /** \note This method is thredsafe.
86 * \param user The core user
87 * \return QList<NetworkInfo>.
89 static QList<NetworkInfo> networks(UserId user);
91 //! Get the NetworkId for a network name.
92 /** \note This method is threadsafe.
94 * \param user The core user
95 * \param network The name of the network
96 * \return The NetworkId corresponding to the given network.
98 static NetworkId networkId(UserId user, const QString &network);
100 //! Get a list of Networks to restore
101 /** Return a list of networks the user was connected at the time of core shutdown
102 * \note This method is threadsafe.
104 * \param user The User Id in question
106 static QList<NetworkId> connectedNetworks(UserId user);
108 //! Update the connected state of a network
109 /** \note This method is threadsafe
111 * \param user The Id of the networks owner
112 * \param networkId The Id of the network
113 * \param isConnected whether the network is connected or not
115 static void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
117 //! Get a hash of channels with their channel keys for a given network
118 /** The keys are channel names and values are passwords (possibly empty)
119 * \note This method is threadsafe
121 * \param user The id of the networks owner
122 * \param networkId The Id of the network
124 static QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
126 //! Update the connected state of a channel
127 /** \note This method is threadsafe
129 * \param user The Id of the networks owner
130 * \param networkId The Id of the network
131 * \param channel The name of the channel
132 * \param isJoined whether the channel is connected or not
134 static void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
136 //! Update the key of a channel
137 /** \note This method is threadsafe
139 * \param user The Id of the networks owner
140 * \param networkId The Id of the network
141 * \param channel The name of the channel
142 * \param key The key of the channel (possibly empty)
144 static void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
146 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
147 /** \note This method is threadsafe.
149 * \param user The core user who owns this buffername
150 * \param networkId The network id
151 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
152 * \param buffer The buffer name (if empty, the net's status buffer is returned)
153 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
155 static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type, const QString &buffer = "");
157 //! Get the unique BufferInfo for a bufferId
158 /** \note This method is threadsafe
159 * \param user The core user who owns this buffername
160 * \param bufferId The id of the buffer
161 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
163 static BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
166 //! Store a Message in the backlog.
167 /** \note This method is threadsafe.
169 * \param msg The message object to be stored
170 * \return The globally unique id for the stored message
172 static MsgId storeMessage(const Message &message);
174 //! Request a certain number (or all) messages stored in a given buffer.
175 /** \note This method is threadsafe.
177 * \param buffer The buffer we request messages from
178 * \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
179 * \param offset Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
180 * \return The requested list of messages
182 static QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1);
184 //! Request messages stored in a given buffer since a certain point in time.
185 /** \note This method is threadsafe.
187 * \param buffer The buffer we request messages from
188 * \param since Only return messages newer than this point in time
189 * \param offset Do not return messages with MsgId >= offset, if offset >= 0
190 * \return The requested list of messages
192 static QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1);
194 //! Request a range of messages stored in a given buffer.
195 /** \note This method is threadsafe.
197 * \param buffer The buffer we request messages from
198 * \param first Return messages with first <= MsgId <= last
199 * \param last Return messages with first <= MsgId <= last
200 * \return The requested list of messages
202 static QList<Message> requestMsgRange(BufferInfo buffer, int first, int last);
204 //! Request a list of all buffers known to a user since a certain point in time.
205 /** This method is used to get a list of all buffers we have stored a backlog from.
206 * Optionally, a QDateTime can be given, so that only buffers are listed that were active
207 * since that point in time.
208 * \note This method is threadsafe.
210 * \param user The user whose buffers we request
211 * \param since If this is defined, older buffers will be ignored
212 * \return A list of the BufferInfos for all buffers as requested
214 static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
216 //! Remove permanently a buffer and it's content from the storage backend
217 /** This call cannot be reverted!
218 * \note This method is threadsafe.
220 * \param user The user who is the owner of the buffer
221 * \param bufferId The bufferId
222 * \return true if successfull
224 static bool removeBuffer(const UserId &user, const BufferId &bufferId);
227 /** \note This method is threadsafe.
228 * \param user The id of the buffer owner
229 * \param networkId The id of the network the buffer belongs to
230 * \param newName The new name of the buffer
231 * \param oldName The previous name of the buffer
232 * \return the BufferId of the affected buffer or an invalid BufferId if not successfull
234 static BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName);
236 //! Update the LastSeenDate for a Buffer
237 /** This Method is used to make the LastSeenDate of a Buffer persistent
238 * \note This method is threadsafe.
240 * \param user The Owner of that Buffer
241 * \param bufferId The buffer id
242 * \param MsgId The Message id of the message that has been just seen
244 static void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
246 //! Get a Hash of all last seen message ids
247 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
248 * \note This method is threadsafe.
250 * \param user The Owner of the buffers
252 static QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
255 //! Make storage data persistent
256 /** \note This method is threadsafe.
261 //! Sent when a BufferInfo is updated in storage.
262 void bufferInfoUpdated(UserId user, const BufferInfo &info);
265 bool startListening(uint port = Global::defaultPort);
266 void stopListening();
267 void incomingConnection();
268 void clientHasData();
269 void clientDisconnected();
271 bool initStorage(QVariantMap dbSettings, bool setup = false);
277 static Core *instanceptr;
279 SessionThread *createSession(UserId userId, bool restoreState = false);
280 void setupClientSession(QTcpSocket *socket, UserId uid);
281 void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
282 //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
283 QString setupCore(const QVariant &setupData);
285 bool registerStorageBackend(Storage *);
286 void unregisterStorageBackend(Storage *);
288 QHash<UserId, SessionThread *> sessions;
290 QTimer _storageSyncTimer;
292 QTcpServer server; // TODO: implement SSL
293 QHash<QTcpSocket *, quint32> blocksizes;
294 QHash<QTcpSocket *, QVariantMap> clientInfo;
296 QHash<QString, Storage *> _storageBackends;