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 the unique BufferInfo for the given combination of network and buffername for a user.
101 /** \note This method is threadsafe.
103 * \param user The core user who owns this buffername
104 * \param networkId The network id
105 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
106 * \param buffer The buffer name (if empty, the net's status buffer is returned)
107 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
109 static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type, const QString &buffer = "");
111 //! Get the unique BufferInfo for a bufferId
112 /** \note This method is threadsafe
113 * \param user The core user who owns this buffername
114 * \param bufferId The id of the buffer
115 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
117 static BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
120 //! Store a Message in the backlog.
121 /** \note This method is threadsafe.
123 * \param msg The message object to be stored
124 * \return The globally unique id for the stored message
126 static MsgId storeMessage(const Message &message);
128 //! Request a certain number (or all) messages stored in a given buffer.
129 /** \note This method is threadsafe.
131 * \param buffer The buffer we request messages from
132 * \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
133 * \param offset Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
134 * \return The requested list of messages
136 static QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1);
138 //! Request messages stored in a given buffer since a certain point in time.
139 /** \note This method is threadsafe.
141 * \param buffer The buffer we request messages from
142 * \param since Only return messages newer than this point in time
143 * \param offset Do not return messages with MsgId >= offset, if offset >= 0
144 * \return The requested list of messages
146 static QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1);
148 //! Request a range of messages stored in a given buffer.
149 /** \note This method is threadsafe.
151 * \param buffer The buffer we request messages from
152 * \param first Return messages with first <= MsgId <= last
153 * \param last Return messages with first <= MsgId <= last
154 * \return The requested list of messages
156 static QList<Message> requestMsgRange(BufferInfo buffer, int first, int last);
158 //! Request a list of all buffers known to a user since a certain point in time.
159 /** This method is used to get a list of all buffers we have stored a backlog from.
160 * Optionally, a QDateTime can be given, so that only buffers are listed that were active
161 * since that point in time.
162 * \note This method is threadsafe.
164 * \param user The user whose buffers we request
165 * \param since If this is defined, older buffers will be ignored
166 * \return A list of the BufferInfos for all buffers as requested
168 static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
170 //! Remove permanently a buffer and it's content from the storage backend
171 /** This call cannot be reverted!
172 * \note This method is threadsafe.
174 * \param user The user who is the owner of the buffer
175 * \param bufferId The bufferId
176 * \return true if successfull
178 static bool removeBuffer(const UserId &user, const BufferId &bufferId);
181 /** \note This method is threadsafe.
182 * \param user The id of the buffer owner
183 * \param networkId The id of the network the buffer belongs to
184 * \param newName The new name of the buffer
185 * \param oldName The previous name of the buffer
186 * \return the BufferId of the affected buffer or an invalid BufferId if not successfull
188 static BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName);
190 //! Update the LastSeenDate for a Buffer
191 /** This Method is used to make the LastSeenDate of a Buffer persistent
192 * \note This method is threadsafe.
194 * \param user The Owner of that Buffer
195 * \param bufferId The buffer id
196 * \param seenDate Time the Buffer has been visited the last time
198 static void setBufferLastSeen(UserId user, const BufferId &bufferId, const QDateTime &seenDate);
200 //! Get a Hash of all last seen dates.
201 /** This Method is called when the Quassel Core is started to restore the lastSeenDates
202 * \note This method is threadsafe.
204 * \param user The Owner of the buffers
206 static QHash<BufferId, QDateTime> bufferLastSeenDates(UserId user);
209 //! Make storage data persistent
210 /** \note This method is threadsafe.
215 //! Sent when a BufferInfo is updated in storage.
216 void bufferInfoUpdated(UserId user, const BufferInfo &info);
219 bool startListening(uint port = Global::defaultPort);
220 void stopListening();
221 void incomingConnection();
222 void clientHasData();
223 void clientDisconnected();
225 bool initStorage(QVariantMap dbSettings, bool setup = false);
231 static Core *instanceptr;
233 SessionThread *createSession(UserId userId, bool restoreState = false);
234 void setupClientSession(QTcpSocket *socket, UserId uid);
235 void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
236 //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
237 QString setupCore(const QVariant &setupData);
239 bool registerStorageBackend(Storage *);
240 void unregisterStorageBackend(Storage *);
242 QHash<UserId, SessionThread *> sessions;
244 QTimer _storageSyncTimer;
246 QTcpServer server; // TODO: implement SSL
247 QHash<QTcpSocket *, quint32> blocksizes;
248 QHash<QTcpSocket *, QVariantMap> clientInfo;
250 QHash<QString, Storage *> _storageBackends;