Lots of new internal stuff (breaking protocol):
[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 a list of Networks to restore
101     /** Return a list of networks the user was connected at the time of core shutdown
102      *  \note This method is threadsafe.
103      *
104      *  \param user  The User Id in question
105      */
106     static QList<NetworkId> connectedNetworks(UserId user);
107
108     //! Update the connected state of a network
109     /** \note This method is threadsafe
110      *
111      *  \param user        The Id of the networks owner
112      *  \param networkId   The Id of the network
113      *  \param isConnected whether the network is connected or not
114      */
115     static void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
116
117     //! Get a hash of channels with their channel keys for a given network
118     /** The keys are channel names and values are passwords (possibly empty)
119      *  \note This method is threadsafe
120      *
121      *  \param user       The id of the networks owner
122      *  \param networkId  The Id of the network
123      */
124     static QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
125
126     //! Update the connected state of a channel
127     /** \note This method is threadsafe
128      *
129      *  \param user       The Id of the networks owner
130      *  \param networkId  The Id of the network
131      *  \param channel    The name of the channel
132      *  \param isJoined   whether the channel is connected or not
133      */
134     static void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
135
136     //! Update the key of a channel
137     /** \note This method is threadsafe
138      *
139      *  \param user       The Id of the networks owner
140      *  \param networkId  The Id of the network
141      *  \param channel    The name of the channel
142      *  \param key        The key of the channel (possibly empty)
143      */
144     static void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
145
146     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
147     /** \note This method is threadsafe.
148      *
149      *  \param user      The core user who owns this buffername
150      *  \param networkId The network id
151      *  \param type      The type of the buffer (StatusBuffer, Channel, etc.)
152      *  \param buffer    The buffer name (if empty, the net's status buffer is returned)
153      *  \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
154      */
155     static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type, const QString &buffer = "");
156
157     //! Get the unique BufferInfo for a bufferId
158     /** \note This method is threadsafe
159      *  \param user      The core user who owns this buffername
160      *  \param bufferId  The id of the buffer
161      *  \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
162      */
163     static BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
164
165   
166     //! Store a Message in the backlog.
167     /** \note This method is threadsafe.
168      *
169      *  \param msg  The message object to be stored
170      *  \return The globally unique id for the stored message
171      */
172     static MsgId storeMessage(const Message &message);
173
174     //! Request a certain number (or all) messages stored in a given buffer.
175     /** \note This method is threadsafe.
176      *
177      *  \param buffer   The buffer we request messages from
178      *  \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
179      *  \param offset   Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
180      *  \return The requested list of messages
181      */
182     static QList<Message> requestMsgs(UserId user, BufferId buffer, int lastmsgs = -1, int offset = -1);
183
184     //! Request messages stored in a given buffer since a certain point in time.
185     /** \note This method is threadsafe.
186      *
187      *  \param buffer   The buffer we request messages from
188      *  \param since    Only return messages newer than this point in time
189      *  \param offset   Do not return messages with MsgId >= offset, if offset >= 0
190      *  \return The requested list of messages
191      */
192     static QList<Message> requestMsgs(UserId user, BufferId buffer, QDateTime since, int offset = -1);
193
194     //! Request a range of messages stored in a given buffer.
195     /** \note This method is threadsafe.
196      *
197      *  \param buffer   The buffer we request messages from
198      *  \param first    Return messages with first <= MsgId <= last
199      *  \param last     Return messages with first <= MsgId <= last
200      *  \return The requested list of messages
201      */
202     static QList<Message> requestMsgRange(UserId user, BufferId buffer, int first, int last);
203
204     //! Request a list of all buffers known to a user since a certain point in time.
205     /** This method is used to get a list of all buffers we have stored a backlog from.
206      *  Optionally, a QDateTime can be given, so that only buffers are listed that were active
207      *  since that point in time.
208      *  \note This method is threadsafe.
209      *
210      *  \param user  The user whose buffers we request
211      *  \param since If this is defined, older buffers will be ignored
212      *  \return A list of the BufferInfos for all buffers as requested
213      */
214     static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
215
216     //! Remove permanently a buffer and it's content from the storage backend
217     /** This call cannot be reverted!
218      *  \note This method is threadsafe.
219      *
220      *  \param user      The user who is the owner of the buffer
221      *  \param bufferId  The bufferId
222      *  \return true if successfull
223      */
224     static bool removeBuffer(const UserId &user, const BufferId &bufferId);
225
226     //! Rename a Buffer
227     /** \note This method is threadsafe.
228      *  \param user      The id of the buffer owner
229      *  \param networkId The id of the network the buffer belongs to
230      *  \param newName   The new name of the buffer
231      *  \param oldName   The previous name of the buffer
232      *  \return the BufferId of the affected buffer or an invalid BufferId if not successfull
233      */
234     static BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName);
235
236     //! Update the LastSeenDate for a Buffer
237     /** This Method is used to make the LastSeenDate of a Buffer persistent
238      *  \note This method is threadsafe.
239      *
240      * \param user      The Owner of that Buffer
241      * \param bufferId  The buffer id
242      * \param MsgId     The Message id of the message that has been just seen
243      */
244     static void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
245
246     //! Get a Hash of all last seen message ids
247     /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
248      *  \note This method is threadsafe.
249      *
250      * \param user      The Owner of the buffers
251      */
252     static QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
253
254   public slots:
255     //! Make storage data persistent
256     /** \note This method is threadsafe.
257      */
258     void syncStorage();
259   
260   signals:
261     //! Sent when a BufferInfo is updated in storage.
262     void bufferInfoUpdated(UserId user, const BufferInfo &info);
263
264   private slots:
265     bool startListening(uint port = Global::defaultPort);
266     void stopListening();
267     void incomingConnection();
268     void clientHasData();
269     void clientDisconnected();
270
271     bool initStorage(QVariantMap dbSettings, bool setup = false);
272
273   private:
274     Core();
275     ~Core();
276     void init();
277     static Core *instanceptr;
278
279     SessionThread *createSession(UserId userId, bool restoreState = false);
280     void setupClientSession(QTcpSocket *socket, UserId uid);
281     void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
282     //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
283     QString setupCore(const QVariant &setupData);
284
285     bool registerStorageBackend(Storage *);
286     void unregisterStorageBackend(Storage *);
287
288     QHash<UserId, SessionThread *> sessions;
289     Storage *storage;
290     QTimer _storageSyncTimer;
291
292     QTcpServer server; // TODO: implement SSL
293     QHash<QTcpSocket *, quint32> blocksizes;
294     QHash<QTcpSocket *, QVariantMap> clientInfo;
295
296     QHash<QString, Storage *> _storageBackends;
297
298     QDateTime startTime;
299
300     bool configured;
301
302     static QMutex mutex;
303 };
304
305 #endif