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 NetworkId in the Storage and store it 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 createNetworkId(UserId user, NetworkInfo &info);
65 //! Get the NetworkId for a network name.
66 /** \note This method is threadsafe.
68 * \param user The core user
69 * \param network The name of the network
70 * \return The NetworkId corresponding to the given network.
72 static NetworkId networkId(UserId user, const QString &network);
74 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
75 /** \note This method is threadsafe.
77 * \param user The core user who owns this buffername
78 * \param networkId The network id
79 * \param buffer The buffer name (if empty, the net's status buffer is returned)
80 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
82 static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, const QString &buffer = "");
84 //! Store a Message in the backlog.
85 /** \note This method is threadsafe.
87 * \param msg The message object to be stored
88 * \return The globally unique id for the stored message
90 static MsgId storeMessage(const Message &message);
92 //! Request a certain number (or all) messages stored in a given buffer.
93 /** \note This method is threadsafe.
95 * \param buffer The buffer we request messages from
96 * \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
97 * \param offset Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
98 * \return The requested list of messages
100 static QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1);
102 //! Request messages stored in a given buffer since a certain point in time.
103 /** \note This method is threadsafe.
105 * \param buffer The buffer we request messages from
106 * \param since Only return messages newer than this point in time
107 * \param offset Do not return messages with MsgId >= offset, if offset >= 0
108 * \return The requested list of messages
110 static QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1);
112 //! Request a range of messages stored in a given buffer.
113 /** \note This method is threadsafe.
115 * \param buffer The buffer we request messages from
116 * \param first Return messages with first <= MsgId <= last
117 * \param last Return messages with first <= MsgId <= last
118 * \return The requested list of messages
120 static QList<Message> requestMsgRange(BufferInfo buffer, int first, int last);
122 //! Request a list of all buffers known to a user since a certain point in time.
123 /** This method is used to get a list of all buffers we have stored a backlog from.
124 * Optionally, a QDateTime can be given, so that only buffers are listed that were active
125 * since that point in time.
126 * \note This method is threadsafe.
128 * \param user The user whose buffers we request
129 * \param since If this is defined, older buffers will be ignored
130 * \return A list of the BufferInfos for all buffers as requested
132 static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
135 //! Make storage data persistent
136 /** \note This method is threadsafe.
141 //! Sent when a BufferInfo is updated in storage.
142 void bufferInfoUpdated(UserId user, const BufferInfo &info);
145 bool startListening(uint port = Global::defaultPort);
146 void stopListening();
147 void incomingConnection();
148 void clientHasData();
149 void clientDisconnected();
151 bool initStorage(QVariantMap dbSettings, bool setup = false);
157 static Core *instanceptr;
159 SessionThread *createSession(UserId userId, bool restoreState = false);
160 void setupClientSession(QTcpSocket *socket, UserId uid);
161 void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
163 QStringList availableStorageProviders();
166 QHash<UserId, SessionThread *> sessions;
168 QTimer _storageSyncTimer;
170 QTcpServer server; // TODO: implement SSL
171 QHash<QTcpSocket *, quint32> blocksizes;
172 QHash<QTcpSocket *, QVariantMap> clientInfo;