fb2602808b307f29c70be9eb8f3a358ed5670f3a
[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     //! Get the unique BufferInfo for a bufferId
112     /** \note This method is threadsafe
113      *  \param user      The core user who owns this buffername
114      *  \param bufferId  The id of the buffer
115      *  \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
116      */
117     static BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
118
119   
120     //! Store a Message in the backlog.
121     /** \note This method is threadsafe.
122      *
123      *  \param msg  The message object to be stored
124      *  \return The globally unique id for the stored message
125      */
126     static MsgId storeMessage(const Message &message);
127
128     //! Request a certain number (or all) messages stored in a given buffer.
129     /** \note This method is threadsafe.
130      *
131      *  \param buffer   The buffer we request messages from
132      *  \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
133      *  \param offset   Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
134      *  \return The requested list of messages
135      */
136     static QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1);
137
138     //! Request messages stored in a given buffer since a certain point in time.
139     /** \note This method is threadsafe.
140      *
141      *  \param buffer   The buffer we request messages from
142      *  \param since    Only return messages newer than this point in time
143      *  \param offset   Do not return messages with MsgId >= offset, if offset >= 0
144      *  \return The requested list of messages
145      */
146     static QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1);
147
148     //! Request a range of messages stored in a given buffer.
149     /** \note This method is threadsafe.
150      *
151      *  \param buffer   The buffer we request messages from
152      *  \param first    Return messages with first <= MsgId <= last
153      *  \param last     Return messages with first <= MsgId <= last
154      *  \return The requested list of messages
155      */
156     static QList<Message> requestMsgRange(BufferInfo buffer, int first, int last);
157
158     //! Request a list of all buffers known to a user since a certain point in time.
159     /** This method is used to get a list of all buffers we have stored a backlog from.
160      *  Optionally, a QDateTime can be given, so that only buffers are listed that were active
161      *  since that point in time.
162      *  \note This method is threadsafe.
163      *
164      *  \param user  The user whose buffers we request
165      *  \param since If this is defined, older buffers will be ignored
166      *  \return A list of the BufferInfos for all buffers as requested
167      */
168     static QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime());
169
170     //! Remove permanently a buffer and it's content from the storage backend
171     /** This call cannot be reverted!
172      *  \note This method is threadsafe.
173      *
174      *  \param user      The user who is the owner of the buffer
175      *  \param bufferId  The bufferId
176      *  \return true if successfull
177      */
178     static bool removeBuffer(const UserId &user, const BufferId &bufferId);
179
180     //! Update the LastSeenDate for a Buffer
181     /** This Method is used to make the LastSeenDate of a Buffer persistent
182      *  \note This method is threadsafe.
183      *
184      * \param user      The Owner of that Buffer
185      * \param bufferId  The buffer id
186      * \param seenDate  Time the Buffer has been visited the last time
187      */
188     static void setBufferLastSeen(UserId user, const BufferId &bufferId, const QDateTime &seenDate);
189
190     //! Get a Hash of all last seen dates. 
191     /** This Method is called when the Quassel Core is started to restore the lastSeenDates
192      *  \note This method is threadsafe.
193      *
194      * \param user      The Owner of the buffers
195      */
196     static QHash<BufferId, QDateTime> bufferLastSeenDates(UserId user);
197
198   public slots:
199     //! Make storage data persistent
200     /** \note This method is threadsafe.
201      */
202     void syncStorage();
203   
204   signals:
205     //! Sent when a BufferInfo is updated in storage.
206     void bufferInfoUpdated(UserId user, const BufferInfo &info);
207
208   private slots:
209     bool startListening(uint port = Global::defaultPort);
210     void stopListening();
211     void incomingConnection();
212     void clientHasData();
213     void clientDisconnected();
214
215     bool initStorage(QVariantMap dbSettings, bool setup = false);
216
217   private:
218     Core();
219     ~Core();
220     void init();
221     static Core *instanceptr;
222
223     SessionThread *createSession(UserId userId, bool restoreState = false);
224     void setupClientSession(QTcpSocket *socket, UserId uid);
225     void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
226     //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
227     QString setupCore(const QVariant &setupData);
228
229     bool registerStorageBackend(Storage *);
230     void unregisterStorageBackend(Storage *);
231
232     QHash<UserId, SessionThread *> sessions;
233     Storage *storage;
234     QTimer _storageSyncTimer;
235
236     QTcpServer server; // TODO: implement SSL
237     QHash<QTcpSocket *, quint32> blocksizes;
238     QHash<QTcpSocket *, QVariantMap> clientInfo;
239
240     QHash<QString, Storage *> _storageBackends;
241
242     QDateTime startTime;
243
244     bool configured;
245
246     static QMutex mutex;
247 };
248
249 #endif