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