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