0cba29eb9539e26a18834faac9649e2107b1d48f
[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 Network in the Storage and store it's Id 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 createNetwork(UserId user, NetworkInfo &info);
64         
65     //! Apply the changes to NetworkInfo info to the storage engine
66     /** \note This method is thredsafe.
67      *
68      *  \param user        The core user
69      *  \param networkInfo The Updated NetworkInfo
70      *  \return true if successfull.
71      */
72     static bool updateNetwork(UserId user, const NetworkInfo &info);
73
74     //! Permanently remove a Network and all the data associated with it.
75     /** \note This method is thredsafe.
76      *
77      *  \param user        The core user
78      *  \param networkId   The network to delete
79      *  \return true if successfull.
80      */
81     static bool removeNetwork(UserId user, const NetworkId &networkId);
82   
83     //! Returns a list of all NetworkInfos for the given UserId user
84     /** \note This method is thredsafe.
85      *
86      *  \param user        The core user
87      *  \return QList<NetworkInfo>.
88      */
89     static QList<NetworkInfo> networks(UserId user);
90
91     //! Get the NetworkId for a network name.
92     /** \note This method is threadsafe.
93      *
94      *  \param user    The core user
95      *  \param network The name of the network
96      *  \return The NetworkId corresponding to the given network.
97      */
98     static NetworkId networkId(UserId user, const QString &network);
99
100     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
101     /** \note This method is threadsafe.
102      *
103      *  \param user      The core user who owns this buffername
104      *  \param networkId The network id
105      *  \param type      The type of the buffer (StatusBuffer, Channel, etc.)
106      *  \param buffer    The buffer name (if empty, the net's status buffer is returned)
107      *  \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
108      */
109     static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type, const QString &buffer = "");
110
111     //! Store a Message in the backlog.
112     /** \note This method is threadsafe.
113      *
114      *  \param msg  The message object to be stored
115      *  \return The globally unique id for the stored message
116      */
117     static MsgId storeMessage(const Message &message);
118
119     //! Request a certain number (or all) messages stored in a given buffer.
120     /** \note This method is threadsafe.
121      *
122      *  \param buffer   The buffer we request messages from
123      *  \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
124      *  \param offset   Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
125      *  \return The requested list of messages
126      */
127     static QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1);
128
129     //! Request messages stored in a given buffer since a certain point in time.
130     /** \note This method is threadsafe.
131      *
132      *  \param buffer   The buffer we request messages from
133      *  \param since    Only return messages newer than this point in time
134      *  \param offset   Do not return messages with MsgId >= offset, if offset >= 0
135      *  \return The requested list of messages
136      */
137     static QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1);
138
139     //! Request a range of messages stored in a given buffer.
140     /** \note This method is threadsafe.
141      *
142      *  \param buffer   The buffer we request messages from
143      *  \param first    Return messages with first <= MsgId <= last
144      *  \param last     Return messages with first <= MsgId <= last
145      *  \return The requested list of messages
146      */
147     static QList<Message> requestMsgRange(BufferInfo buffer, int first, int last);
148
149     //! Request a list of all buffers known to a user since a certain point in time.
150     /** This method is used to get a list of all buffers we have stored a backlog from.
151      *  Optionally, a QDateTime can be given, so that only buffers are listed that were active
152      *  since that point in time.
153      *  \note This method is threadsafe.
154      *
155      *  \param user  The user whose buffers we request
156      *  \param since If this is defined, older buffers will be ignored
157      *  \return A list of the BufferInfos for all buffers as requested
158      */
159     static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
160
161     //! Update the LastSeenDate for a Buffer
162     /** This Method is used to make the LastSeenDate of a Buffer persistent
163      * \param user      The Owner of that Buffer
164      * \param bufferId  The buffer id
165      * \param seenDate  Time the Buffer has been visited the last time
166      */
167     static void setBufferLastSeen(UserId user, const BufferId &bufferId, const QDateTime &seenDate);
168
169     //! Get a Hash of all last seen dates. 
170     /** This Method is called when the Quassel Core is started to restore the lastSeenDates
171      * \param user      The Owner of the buffers
172      */
173     static QHash<BufferId, QDateTime> bufferLastSeenDates(UserId user);
174
175   public slots:
176     //! Make storage data persistent
177     /** \note This method is threadsafe.
178      */
179     void syncStorage();
180   
181   signals:
182     //! Sent when a BufferInfo is updated in storage.
183     void bufferInfoUpdated(UserId user, const BufferInfo &info);
184
185   private slots:
186     bool startListening(uint port = Global::defaultPort);
187     void stopListening();
188     void incomingConnection();
189     void clientHasData();
190     void clientDisconnected();
191
192     bool initStorage(QVariantMap dbSettings, bool setup = false);
193
194   private:
195     Core();
196     ~Core();
197     void init();
198     static Core *instanceptr;
199
200     SessionThread *createSession(UserId userId, bool restoreState = false);
201     void setupClientSession(QTcpSocket *socket, UserId uid);
202     void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
203     //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
204     QString setupCore(const QVariant &setupData);
205
206     bool registerStorageBackend(Storage *);
207     void unregisterStorageBackend(Storage *);
208
209     QHash<UserId, SessionThread *> sessions;
210     Storage *storage;
211     QTimer _storageSyncTimer;
212
213     QTcpServer server; // TODO: implement SSL
214     QHash<QTcpSocket *, quint32> blocksizes;
215     QHash<QTcpSocket *, QVariantMap> clientInfo;
216
217     QHash<QString, Storage *> _storageBackends;
218
219     QDateTime startTime;
220
221     bool configured;
222
223     static QMutex mutex;
224 };
225
226 #endif