51237ec76dac9fcb6833238100bcce35b4c85fef
[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 struct NetworkInfo;
41
42 class Core : public QObject {
43   Q_OBJECT
44
45   public:
46     static Core * instance();
47     static void destroy();
48
49     static void saveState();
50     static void restoreState();
51
52     /*** Storage access ***/
53     // These methods are threadsafe.
54
55     //! Create a NetworkId in the Storage and store it in the given NetworkInfo
56     /** \note This method is thredsafe.
57      *
58      *  \param user        The core user
59      *  \param networkInfo a NetworkInfo definition to store the newly created ID in
60      *  \return true if successfull.
61      */
62     static bool createNetworkId(UserId user, NetworkInfo &info);
63         
64     //! Get the NetworkId for a network name.
65     /** \note This method is threadsafe.
66      *
67      *  \param user    The core user
68      *  \param network The name of the network
69      *  \return The NetworkId corresponding to the given network.
70      */
71     static NetworkId networkId(UserId user, const QString &network);
72
73     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
74     /** \note This method is threadsafe.
75      *
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
80      */
81     static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, const QString &buffer = "");
82
83     //! Store a Message in the backlog.
84     /** \note This method is threadsafe.
85      *
86      *  \param msg  The message object to be stored
87      *  \return The globally unique id for the stored message
88      */
89     static MsgId storeMessage(const Message &message);
90
91     //! Request a certain number (or all) messages stored in a given buffer.
92     /** \note This method is threadsafe.
93      *
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
98      */
99     static QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1);
100
101     //! Request messages stored in a given buffer since a certain point in time.
102     /** \note This method is threadsafe.
103      *
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
108      */
109     static QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1);
110
111     //! Request a range of messages stored in a given buffer.
112     /** \note This method is threadsafe.
113      *
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
118      */
119     static QList<Message> requestMsgRange(BufferInfo buffer, int first, int last);
120
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.
126      *
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
130      */
131     static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
132
133   signals:
134     //! Sent when a BufferInfo is updated in storage.
135     void bufferInfoUpdated(UserId user, const BufferInfo &info);
136
137   private slots:
138     bool startListening(uint port = Global::defaultPort);
139     void stopListening();
140     void incomingConnection();
141     void clientHasData();
142     void clientDisconnected();
143
144     bool initStorage(QVariantMap dbSettings, bool setup = false);
145
146   private:
147     Core();
148     ~Core();
149     void init();
150     static Core *instanceptr;
151
152     SessionThread *createSession(UserId userId, bool restoreState = false);
153     void setupClientSession(QTcpSocket *socket, UserId uid);
154     void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
155
156     QStringList availableStorageProviders();
157
158     UserId guiUser;
159     QHash<UserId, SessionThread *> sessions;
160     Storage *storage;
161
162     QTcpServer server; // TODO: implement SSL
163     QHash<QTcpSocket *, quint32> blocksizes;
164     QHash<QTcpSocket *, QVariantMap> clientInfo;
165
166     QDateTime startTime;
167
168     bool configured;
169
170     static QMutex mutex;
171 };
172
173 #endif