some minor improvements to the NetworkModel and added a sanity check to TreeModel
[quassel.git] / src / client / client.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 _CLIENT_H_
22 #define _CLIENT_H_
23
24 #include <QAbstractSocket>
25 #include <QTcpSocket>
26 #include <QList>
27 #include <QPointer>
28
29 #include "buffer.h" // needed for activity lvl
30 class BufferInfo;
31 class Message;
32
33 class Identity;
34 class Network;
35
36
37 class AbstractUi;
38 class AbstractUiMsg;
39 class NetworkModel;
40 class BufferModel;
41 class IrcUser;
42 class IrcChannel;
43 class SignalProxy;
44 struct NetworkInfo;
45
46 class QTimer;
47
48
49 class Client : public QObject {
50   Q_OBJECT
51
52 public:
53   static Client *instance();
54   static void destroy();
55   static void init(AbstractUi *);
56
57   static QList<BufferInfo> allBufferInfos();
58   static QList<Buffer *> buffers();
59   static Buffer *buffer(BufferId bufferUid);
60   static Buffer *buffer(BufferInfo);
61   static Buffer *monitorBuffer();
62
63   static QList<NetworkId> networkIds();
64   static const Network * network(NetworkId);
65
66   static QList<IdentityId> identityIds();
67   static const Identity * identity(IdentityId);
68
69   //! Request creation of an identity with the given data.
70   /** The request will be sent to the core, and will be propagated back to all the clients
71    *  with a new valid IdentityId.
72    *  \param identity The identity template for the new identity. It does not need to have a valid ID.
73    */
74   static void createIdentity(const Identity &identity);
75
76   //! Request update of an identity with the given data.
77   /** The request will be sent to the core, and will be propagated back to all the clients.
78    *  \param identity The identity to be updated.
79    */
80   static void updateIdentity(const Identity &identity);
81
82   //! Request removal of the identity with the given ID from the core (and all the clients, of course).
83   /** \param id The ID of the identity to be removed.
84    */
85   static void removeIdentity(IdentityId id);
86
87   static void createNetwork(const NetworkInfo &info);
88   static void updateNetwork(const NetworkInfo &info);
89   static void removeNetwork(NetworkId id);
90
91   static NetworkModel *networkModel();
92   static BufferModel *bufferModel();
93   static SignalProxy *signalProxy();
94
95   static AbstractUiMsg *layoutMsg(const Message &);
96
97   static bool isConnected();
98   static bool isSynced();
99
100   static void userInput(BufferInfo bufferInfo, QString message);
101
102   static void storeSessionData(const QString &key, const QVariant &data);
103   static QVariant retrieveSessionData(const QString &key, const QVariant &def = QVariant());
104   static QStringList sessionDataKeys();
105
106   static void disconnectFromNetwork(NetworkId);
107
108   enum ClientMode { LocalCore, RemoteCore };
109
110 signals:
111   void sendInput(BufferInfo, QString message);
112   void showBuffer(Buffer *);
113   void bufferUpdated(BufferInfo bufferInfo);
114   void backlogReceived(Buffer *, QList<Message>);
115   void requestBacklog(BufferInfo, QVariant, QVariant);
116   void requestNetworkStates();
117
118   void showConfigWizard(const QVariantMap &coredata);
119
120   void connected();
121   void disconnected();
122   void coreConnectionStateChanged(bool);
123
124   void sessionDataChanged(const QString &key);
125   void sessionDataChanged(const QString &key, const QVariant &data);
126   void sendSessionData(const QString &key, const QVariant &data);
127
128   //! The identity with the given ID has been newly created in core and client.
129   /** \param id The ID of the newly created identity.
130    */
131   void identityCreated(IdentityId id);
132
133   //! The identity with the given ID has been removed.
134   /** Upon emitting this signal, the identity is already gone from the core, and it will
135    *  be deleted from the client immediately afterwards, so connected slots need to clean
136    *  up their stuff.
137    *  \param id The ID of the identity about to be removed.
138    */
139   void identityRemoved(IdentityId id);
140
141   //! Sent to the core when an identity shall be created. Should not be used elsewhere.
142   void requestCreateIdentity(const Identity &);
143   //! Sent to the core when an identity shall be updated. Should not be used elsewhere.
144   void requestUpdateIdentity(const Identity &);
145   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
146   void requestRemoveIdentity(IdentityId);
147
148   void networkCreated(NetworkId id);
149   void networkRemoved(NetworkId id);
150
151   void requestCreateNetwork(const NetworkInfo &info);
152   void requestUpdateNetwork(const NetworkInfo &info);
153   void requestRemoveNetwork(const NetworkInfo &info);
154
155 public slots:
156   //void selectBuffer(Buffer *);
157
158   void setConnectedToCore(QIODevice *socket);
159   void setSyncedToCore();
160   void disconnectFromCore();
161
162   void setCoreConfiguration(const QVariantMap &settings);
163
164 private slots:
165   void recvSessionData(const QString &key, const QVariant &data);
166
167   //void coreSocketError(QAbstractSocket::SocketError);
168
169   //void networkConnected(NetworkId);
170   //void networkDisconnected(NetworkId);
171
172   void recvMessage(const Message &message);
173   void recvStatusMsg(QString network, QString message);
174   void recvBacklogData(BufferInfo, QVariantList, bool);
175   void updateBufferInfo(BufferInfo);
176
177   void layoutMsg();
178
179   void bufferDestroyed();
180   void networkDestroyed();
181   void coreIdentityCreated(const Identity &);
182   void coreIdentityRemoved(IdentityId);
183
184 private:
185   Client(QObject *parent = 0);
186   virtual ~Client();
187   void init();
188
189   static void addNetwork(Network *);
190
191   static QPointer<Client> instanceptr;
192
193   QPointer<QIODevice> socket;
194   QPointer<SignalProxy> _signalProxy;
195   QPointer<AbstractUi> mainUi;
196   QPointer<NetworkModel> _networkModel;
197   QPointer<BufferModel> _bufferModel;
198
199   ClientMode clientMode;
200
201   bool _connectedToCore, _syncedToCore;
202
203   QHash<BufferId, Buffer *> _buffers;
204   QHash<NetworkId, Network *> _networks;
205   QHash<IdentityId, Identity *> _identities;
206
207   Buffer *_monitorBuffer;
208
209   QTimer *layoutTimer;
210   QList<Buffer *> layoutQueue;
211
212   QVariantMap sessionData;
213
214   friend class ClientSyncer;
215 };
216
217 #endif