Merge pull request #105 from merlin1991/qdatastream
[quassel.git] / src / core / coresession.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef CORESESSION_H
22 #define CORESESSION_H
23
24 #include <QString>
25 #include <QVariant>
26
27 #include "corecoreinfo.h"
28 #include "corealiasmanager.h"
29 #include "coreignorelistmanager.h"
30 #include "protocol.h"
31 #include "message.h"
32 #include "storage.h"
33
34 class CoreBacklogManager;
35 class CoreBufferSyncer;
36 class CoreBufferViewManager;
37 class CoreIdentity;
38 class CoreIrcListHelper;
39 class CoreNetwork;
40 class CoreNetworkConfig;
41 class CoreSessionEventProcessor;
42 class CoreTransferManager;
43 class CtcpParser;
44 class EventManager;
45 class EventStringifier;
46 class InternalPeer;
47 class IrcParser;
48 class Peer;
49 class MessageEvent;
50 class NetworkConnection;
51 class RemotePeer;
52 class SignalProxy;
53
54 struct NetworkInfo;
55
56 class QScriptEngine;
57
58 class CoreSession : public QObject
59 {
60     Q_OBJECT
61
62 public:
63     CoreSession(UserId, bool restoreState, QObject *parent = 0);
64     ~CoreSession();
65
66     QList<BufferInfo> buffers() const;
67     inline UserId user() const { return _user; }
68     CoreNetwork *network(NetworkId) const;
69     CoreIdentity *identity(IdentityId) const;
70     inline CoreNetworkConfig *networkConfig() const { return _networkConfig; }
71     NetworkConnection *networkConnection(NetworkId) const;
72
73     Protocol::SessionState sessionState() const;
74
75     inline SignalProxy *signalProxy() const { return _signalProxy; }
76
77     const AliasManager &aliasManager() const { return _aliasManager; }
78     AliasManager &aliasManager() { return _aliasManager; }
79
80     inline EventManager *eventManager() const { return _eventManager; }
81     inline EventStringifier *eventStringifier() const { return _eventStringifier; }
82     inline CoreSessionEventProcessor *sessionEventProcessor() const { return _sessionEventProcessor; }
83     inline CtcpParser *ctcpParser() const { return _ctcpParser; }
84     inline IrcParser *ircParser() const { return _ircParser; }
85
86     inline CoreIrcListHelper *ircListHelper() const { return _ircListHelper; }
87
88     inline CoreIgnoreListManager *ignoreListManager() { return &_ignoreListManager; }
89     inline CoreTransferManager *transferManager() const { return _transferManager; }
90
91 //   void attachNetworkConnection(NetworkConnection *conn);
92
93     //! Return necessary data for restoring the session after restarting the core
94     void restoreSessionState();
95
96 public slots:
97     void addClient(RemotePeer *peer);
98     void addClient(InternalPeer *peer);
99
100     void msgFromClient(BufferInfo, QString message);
101
102     //! Create an identity and propagate the changes to the clients.
103     /** \param identity The identity to be created.
104      */
105     void createIdentity(const Identity &identity, const QVariantMap &additional);
106     void createIdentity(const CoreIdentity &identity);
107
108     //! Remove identity and propagate that fact to the clients.
109     /** \param identity The identity to be removed.
110      */
111     void removeIdentity(IdentityId identity);
112
113     //! Create a network and propagate the changes to the clients.
114     /** \param info The network's settings.
115      */
116     void createNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
117
118     //! Remove network and propagate that fact to the clients.
119     /** \param network The id of the network to be removed.
120      */
121     void removeNetwork(NetworkId network);
122
123     //! Rename a Buffer for a given network
124     /* \param networkId The id of the network the buffer belongs to
125      * \param newName   The new name of the buffer
126      * \param oldName   The old name of the buffer
127      */
128     void renameBuffer(const NetworkId &networkId, const QString &newName, const QString &oldName);
129
130     QHash<QString, QString> persistentChannels(NetworkId) const;
131
132     //! Marks us away (or unaway) on all networks
133     void globalAway(const QString &msg = QString());
134
135 signals:
136     void initialized();
137     void sessionState(const Protocol::SessionState &sessionState);
138
139     //void msgFromGui(uint netid, QString buf, QString message);
140     void displayMsg(Message message);
141     void displayStatusMsg(QString, QString);
142
143     void scriptResult(QString result);
144
145     //! Identity has been created.
146     /** This signal is propagated to the clients to tell them that the given identity has been created.
147      *  \param identity The new identity.
148      */
149     void identityCreated(const Identity &identity);
150
151     //! Identity has been removed.
152     /** This signal is propagated to the clients to inform them about the removal of the given identity.
153      *  \param identity The identity that has been removed.
154      */
155     void identityRemoved(IdentityId identity);
156
157     void networkCreated(NetworkId);
158     void networkRemoved(NetworkId);
159     void networkDisconnected(NetworkId);
160
161 protected:
162     virtual void customEvent(QEvent *event);
163
164 private slots:
165     void removeClient(Peer *peer);
166
167     void recvStatusMsgFromServer(QString msg);
168     void recvMessageFromServer(NetworkId networkId, Message::Type, BufferInfo::Type, const QString &target, const QString &text, const QString &sender = "", Message::Flags flags = Message::None);
169
170     void destroyNetwork(NetworkId);
171
172     void scriptRequest(QString script);
173
174     void clientsConnected();
175     void clientsDisconnected();
176
177     void updateIdentityBySender();
178
179     void saveSessionState() const;
180
181 private:
182     void processMessages();
183
184     void loadSettings();
185     void initScriptEngine();
186
187     /// Hook for converting events to the old displayMsg() handlers
188     Q_INVOKABLE void processMessageEvent(MessageEvent *event);
189
190     UserId _user;
191
192     SignalProxy *_signalProxy;
193     CoreAliasManager _aliasManager;
194     // QHash<NetworkId, NetworkConnection *> _connections;
195     QHash<NetworkId, CoreNetwork *> _networks;
196     //  QHash<NetworkId, CoreNetwork *> _networksToRemove;
197     QHash<IdentityId, CoreIdentity *> _identities;
198
199     CoreBufferSyncer *_bufferSyncer;
200     CoreBacklogManager *_backlogManager;
201     CoreBufferViewManager *_bufferViewManager;
202     CoreIrcListHelper *_ircListHelper;
203     CoreNetworkConfig *_networkConfig;
204     CoreCoreInfo _coreInfo;
205     CoreTransferManager *_transferManager;
206
207     EventManager *_eventManager;
208     EventStringifier *_eventStringifier; // should eventually move into client
209     CoreSessionEventProcessor *_sessionEventProcessor;
210     CtcpParser *_ctcpParser;
211     IrcParser *_ircParser;
212
213     QScriptEngine *scriptEngine;
214
215     QList<RawMessage> _messageQueue;
216     bool _processMessages;
217     CoreIgnoreListManager _ignoreListManager;
218 };
219
220
221 struct RawMessage {
222     NetworkId networkId;
223     Message::Type type;
224     BufferInfo::Type bufferType;
225     QString target;
226     QString text;
227     QString sender;
228     Message::Flags flags;
229     RawMessage(NetworkId networkId, Message::Type type, BufferInfo::Type bufferType, const QString &target, const QString &text, const QString &sender, Message::Flags flags)
230         : networkId(networkId), type(type), bufferType(bufferType), target(target), text(text), sender(sender), flags(flags) {}
231 };
232
233 #endif