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 ***************************************************************************/
31 #include "bufferinfo.h"
34 #include "sessionthread.h"
41 class Core : public QObject {
45 static Core * instance();
46 static void destroy();
48 static void saveState();
49 static void restoreState();
51 /*** Storage access ***/
52 // These methods are threadsafe.
54 //! Get the NetworkId for a network name.
55 /** \note This method is threadsafe.
57 * \param user The core user
58 * \param network The name of the network
59 * \return The NetworkId corresponding to the given network.
61 static NetworkId networkId(UserId user, const QString &network);
63 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
64 /** \note This method is threadsafe.
66 * \param user The core user who owns this buffername
67 * \param network The network name
68 * \param buffer The buffer name (if empty, the net's status buffer is returned)
69 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
71 static BufferInfo bufferInfo(UserId user, const QString &network, const QString &buffer = "");
73 //! Store a Message in the backlog.
74 /** \note This method is threadsafe.
76 * \param msg The message object to be stored
77 * \return The globally unique id for the stored message
79 static MsgId storeMessage(const Message &message);
81 //! Request a certain number (or all) messages stored in a given buffer.
82 /** \note This method is threadsafe.
84 * \param buffer The buffer we request messages from
85 * \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
86 * \param offset Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
87 * \return The requested list of messages
89 static QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1);
91 //! Request messages stored in a given buffer since a certain point in time.
92 /** \note This method is threadsafe.
94 * \param buffer The buffer we request messages from
95 * \param since Only return messages newer than this point in time
96 * \param offset Do not return messages with MsgId >= offset, if offset >= 0
97 * \return The requested list of messages
99 static QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1);
101 //! Request a range of messages stored in a given buffer.
102 /** \note This method is threadsafe.
104 * \param buffer The buffer we request messages from
105 * \param first Return messages with first <= MsgId <= last
106 * \param last Return messages with first <= MsgId <= last
107 * \return The requested list of messages
109 static QList<Message> requestMsgRange(BufferInfo buffer, int first, int last);
111 //! Request a list of all buffers known to a user since a certain point in time.
112 /** This method is used to get a list of all buffers we have stored a backlog from.
113 * Optionally, a QDateTime can be given, so that only buffers are listed that were active
114 * since that point in time.
115 * \note This method is threadsafe.
117 * \param user The user whose buffers we request
118 * \param since If this is defined, older buffers will be ignored
119 * \return A list of the BufferInfos for all buffers as requested
121 static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
124 //! Sent when a BufferInfo is updated in storage.
125 void bufferInfoUpdated(UserId user, const BufferInfo &info);
128 bool startListening(uint port = Global::defaultPort);
129 void stopListening();
130 void incomingConnection();
131 void clientHasData();
132 void clientDisconnected();
134 bool initStorage(QVariantMap dbSettings, bool setup = false);
140 static Core *instanceptr;
142 SessionThread *createSession(UserId userId, bool restoreState = false);
143 void setupClientSession(QTcpSocket *socket, UserId uid);
144 void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
146 QStringList availableStorageProviders();
149 QHash<UserId, SessionThread *> sessions;
152 QTcpServer server; // TODO: implement SSL
153 QHash<QTcpSocket *, quint32> blocksizes;
154 QHash<QTcpSocket *, QVariantMap> clientInfo;