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