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