This is it, the long-awaited huge commit with the new Network handling.
[quassel.git] / src / core / coresession.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 _CORESESSION_H_
22 #define _CORESESSION_H_
23
24 #include <QString>
25 #include <QVariant>
26
27 #include "message.h"
28
29 class Identity;
30 class NetworkConnection;  // FIXME get rid of
31 class Network;
32 struct NetworkInfo;
33 class SignalProxy;
34
35 class QScriptEngine;
36
37 class CoreSession : public QObject {
38   Q_OBJECT
39
40 public:
41   CoreSession(UserId, bool restoreState, QObject *parent = 0);
42   ~CoreSession();
43
44   QList<BufferInfo> buffers() const;
45   UserId user() const;
46   Network *network(NetworkId) const;
47   NetworkConnection *networkConnection(NetworkId) const;
48   Identity *identity(IdentityId) const;
49
50   QVariant sessionState();
51
52   //! Retrieve a piece of session-wide data.
53   QVariant retrieveSessionData(const QString &key, const QVariant &def = QVariant());
54
55   SignalProxy *signalProxy() const;
56
57   void attachNetworkConnection(NetworkConnection *conn);
58
59   //! Return necessary data for restoring the session after restarting the core
60   void saveSessionState() const;
61   void restoreSessionState();
62
63 public slots:
64   //! Store a piece session-wide data and distribute it to connected clients.
65   void storeSessionData(const QString &key, const QVariant &data);
66
67   void networkStateRequested();
68
69   void addClient(QObject *socket);
70
71   void connectToNetwork(QString, const QVariant &previousState = QVariant());
72   void connectToNetwork(NetworkId, const QVariant &previousState = QVariant());
73   void disconnectFromNetwork(NetworkId id);
74
75   //void processSignal(ClientSignal, QVariant, QVariant, QVariant);
76   void sendBacklog(BufferInfo, QVariant, QVariant);
77   void msgFromClient(BufferInfo, QString message);
78
79   //! Create an identity and propagate the changes to the clients.
80   /** \param identity The identity to be created.
81    */
82   void createIdentity(const Identity &identity);
83
84   //! Update an identity and propagate the changes to the clients.
85   /** \param identity The identity to be updated.
86    */
87   void updateIdentity(const Identity &identity);
88
89   //! Remove identity and propagate that fact to the clients.
90   /** \param identity The identity to be removed.
91    */
92   void removeIdentity(IdentityId identity);
93
94   //! Create a network and propagate the changes to the clients.
95   /** \param info The network's settings.
96    */
97   void createNetwork(const NetworkInfo &info, bool useId = false);
98
99   //! Update a network and propagate the changes to the clients.
100   /** \param info The updated network settings.
101    */
102   void updateNetwork(const NetworkInfo &info);
103
104   //! Remove identity and propagate that fact to the clients.
105   /** \param identity The identity to be removed.
106    */
107   void removeNetwork(NetworkId network);
108
109 signals:
110   void initialized();
111
112   //void msgFromGui(uint netid, QString buf, QString message);
113   void displayMsg(Message message);
114   void displayStatusMsg(QString, QString);
115
116   //void connectToIrc(QString net);
117   //void disconnectFromIrc(QString net);
118
119   void backlogData(BufferInfo, QVariantList, bool done);
120
121   void bufferInfoUpdated(BufferInfo);
122   void sessionDataChanged(const QString &key);
123   void sessionDataChanged(const QString &key, const QVariant &data);
124
125   void scriptResult(QString result);
126
127   //! Identity has been created.
128   /** This signal is propagated to the clients to tell them that the given identity has been created.
129    *  \param identity The new identity.
130    */
131   void identityCreated(const Identity &identity);
132
133   //! Identity has been removed.
134   /** This signal is propagated to the clients to inform them about the removal of the given identity.
135    *  \param identity The identity that has been removed.
136    */
137   void identityRemoved(IdentityId identity);
138
139   void networkCreated(NetworkId);
140   void networkRemoved(NetworkId);
141
142 private slots:
143   void recvStatusMsgFromServer(QString msg);
144   void recvMessageFromServer(Message::Type, QString target, QString text, QString sender = "", quint8 flags = Message::None);
145   void networkConnected(NetworkId networkid);
146   void networkDisconnected(NetworkId networkid);
147
148   //! Called when storage updated a BufferInfo.
149   /** This emits bufferInfoUpdated() via SignalProxy, iff it's one of our buffers.
150    *  \param user       The buffer's owner (not necessarily us)
151    *  \param bufferInfo The updated BufferInfo
152    */
153   void updateBufferInfo(UserId user, const BufferInfo &bufferInfo);
154
155   void scriptRequest(QString script);
156
157 private:
158   void loadSettings();
159   void initScriptEngine();
160
161   UserId _user;
162
163   SignalProxy *_signalProxy;
164   QHash<NetworkId, NetworkConnection *> _connections;
165   QHash<NetworkId, Network *> _networks;
166   QHash<IdentityId, Identity *> _identities;
167
168   QVariantMap sessionData;
169
170   QScriptEngine *scriptEngine;
171
172 };
173
174 #endif