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 //! Store a Message in the backlog.
112 /** \note This method is threadsafe.
114 * \param msg The message object to be stored
115 * \return The globally unique id for the stored message
117 static MsgId storeMessage(const Message &message);
119 //! Request a certain number (or all) messages stored in a given buffer.
120 /** \note This method is threadsafe.
122 * \param buffer The buffer we request messages from
123 * \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
124 * \param offset Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
125 * \return The requested list of messages
127 static QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1);
129 //! Request messages stored in a given buffer since a certain point in time.
130 /** \note This method is threadsafe.
132 * \param buffer The buffer we request messages from
133 * \param since Only return messages newer than this point in time
134 * \param offset Do not return messages with MsgId >= offset, if offset >= 0
135 * \return The requested list of messages
137 static QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1);
139 //! Request a range of messages stored in a given buffer.
140 /** \note This method is threadsafe.
142 * \param buffer The buffer we request messages from
143 * \param first Return messages with first <= MsgId <= last
144 * \param last Return messages with first <= MsgId <= last
145 * \return The requested list of messages
147 static QList<Message> requestMsgRange(BufferInfo buffer, int first, int last);
149 //! Request a list of all buffers known to a user since a certain point in time.
150 /** This method is used to get a list of all buffers we have stored a backlog from.
151 * Optionally, a QDateTime can be given, so that only buffers are listed that were active
152 * since that point in time.
153 * \note This method is threadsafe.
155 * \param user The user whose buffers we request
156 * \param since If this is defined, older buffers will be ignored
157 * \return A list of the BufferInfos for all buffers as requested
159 static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
162 //! Make storage data persistent
163 /** \note This method is threadsafe.
168 //! Sent when a BufferInfo is updated in storage.
169 void bufferInfoUpdated(UserId user, const BufferInfo &info);
172 bool startListening(uint port = Global::defaultPort);
173 void stopListening();
174 void incomingConnection();
175 void clientHasData();
176 void clientDisconnected();
178 bool initStorage(QVariantMap dbSettings, bool setup = false);
184 static Core *instanceptr;
186 SessionThread *createSession(UserId userId, bool restoreState = false);
187 void setupClientSession(QTcpSocket *socket, UserId uid);
188 void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
189 //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
190 QString setupCore(const QVariant &setupData);
192 bool registerStorageBackend(Storage *);
193 void unregisterStorageBackend(Storage *);
195 QHash<UserId, SessionThread *> sessions;
197 QTimer _storageSyncTimer;
199 QTcpServer server; // TODO: implement SSL
200 QHash<QTcpSocket *, quint32> blocksizes;
201 QHash<QTcpSocket *, QVariantMap> clientInfo;
203 QHash<QString, Storage *> _storageBackends;