Make Quassel compile on MSVC.
[quassel.git] / src / core / core.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #ifndef _CORE_H_
22 #define _CORE_H_
23
24 #include <QDateTime>
25 #include <QMutex>
26 #include <QString>
27 #include <QVariant>
28 #include <QTcpServer>
29 #include <QTcpSocket>
30
31 #include "bufferinfo.h"
32 #include "message.h"
33 #include "global.h"
34 #include "sessionthread.h"
35 #include "types.h"
36
37 class CoreSession;
38 class SessionThread;
39 class Storage;
40
41 class Core : public QObject {
42   Q_OBJECT
43
44   public:
45     static Core * instance();
46     static void destroy();
47
48     static void saveState();
49     static void restoreState();
50
51     /*** Storage access ***/
52     // These methods are threadsafe.
53
54     //! Get the NetworkId for a network name.
55     /** \note This method is threadsafe.
56      *
57      *  \param user    The core user
58      *  \param network The name of the network
59      *  \return The NetworkId corresponding to the given network.
60      */
61     static NetworkId networkId(UserId user, const QString &network);
62
63     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
64     /** \note This method is threadsafe.
65      *
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
70      */
71     static BufferInfo bufferInfo(UserId user, const QString &network, const QString &buffer = "");
72
73     //! Store a Message in the backlog.
74     /** \note This method is threadsafe.
75      *
76      *  \param msg  The message object to be stored
77      *  \return The globally unique id for the stored message
78      */
79     static MsgId storeMessage(const Message &message);
80
81     //! Request a certain number (or all) messages stored in a given buffer.
82     /** \note This method is threadsafe.
83      *
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
88      */
89     static QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1);
90
91     //! Request messages stored in a given buffer since a certain point in time.
92     /** \note This method is threadsafe.
93      *
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
98      */
99     static QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1);
100
101     //! Request a range of messages stored in a given buffer.
102     /** \note This method is threadsafe.
103      *
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
108      */
109     static QList<Message> requestMsgRange(BufferInfo buffer, int first, int last);
110
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.
116      *
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
120      */
121     static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
122
123   signals:
124     //! Sent when a BufferInfo is updated in storage.
125     void bufferInfoUpdated(UserId user, const BufferInfo &info);
126
127   private slots:
128     bool startListening(uint port = Global::defaultPort);
129     void stopListening();
130     void incomingConnection();
131     void clientHasData();
132     void clientDisconnected();
133
134     bool initStorage(QVariantMap dbSettings, bool setup = false);
135
136   private:
137     Core();
138     ~Core();
139     void init();
140     static Core *instanceptr;
141
142     SessionThread *createSession(UserId userId, bool restoreState = false);
143     void setupClientSession(QTcpSocket *socket, UserId uid);
144     void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
145
146     QStringList availableStorageProviders();
147
148     UserId guiUser;
149     QHash<UserId, SessionThread *> sessions;
150     Storage *storage;
151
152     QTcpServer server; // TODO: implement SSL
153     QHash<QTcpSocket *, quint32> blocksizes;
154     QHash<QTcpSocket *, QVariantMap> clientInfo;
155
156     QDateTime startTime;
157
158     bool configured;
159
160     static QMutex mutex;
161 };
162
163 #endif