Merging r732:766 from trunk to branches/0.3.
[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
31 class BufferInfo;
32 class Message;
33
34 class Identity;
35 class Network;
36
37
38 class AbstractUi;
39 class AbstractUiMsg;
40 class NetworkModel;
41 class BufferModel;
42 class BufferSyncer;
43 class ClientBacklogManager;
44 class BufferViewManager;
45 class IrcUser;
46 class IrcChannel;
47 class SignalProxy;
48 struct NetworkInfo;
49
50 class QTimer;
51
52
53 class Client : public QObject {
54   Q_OBJECT
55
56 public:
57   static Client *instance();
58   static void destroy();
59   static void init(AbstractUi *);
60
61   static QList<BufferInfo> allBufferInfos();
62   static QList<Buffer *> buffers();
63   static Buffer *buffer(BufferId bufferUid);
64   static Buffer *buffer(BufferInfo);
65   static inline Buffer *monitorBuffer() { return instance()->_monitorBuffer; }
66
67   static QList<NetworkId> networkIds();
68   static const Network * network(NetworkId);
69
70   static QList<IdentityId> identityIds();
71   static const Identity * identity(IdentityId);
72
73   //! Request creation of an identity with the given data.
74   /** The request will be sent to the core, and will be propagated back to all the clients
75    *  with a new valid IdentityId.
76    *  \param identity The identity template for the new identity. It does not need to have a valid ID.
77    */
78   static void createIdentity(const Identity &identity);
79
80   //! Request update of an identity with the given data.
81   /** The request will be sent to the core, and will be propagated back to all the clients.
82    *  \param identity The identity to be updated.
83    */
84   static void updateIdentity(const Identity &identity);
85
86   //! Request removal of the identity with the given ID from the core (and all the clients, of course).
87   /** \param id The ID of the identity to be removed.
88    */
89   static void removeIdentity(IdentityId id);
90
91   static void createNetwork(const NetworkInfo &info);
92   static void updateNetwork(const NetworkInfo &info);
93   static void removeNetwork(NetworkId id);
94
95   static inline NetworkModel *networkModel() { return instance()->_networkModel; }
96   static inline BufferModel *bufferModel() { return instance()->_bufferModel; }
97   static inline SignalProxy *signalProxy() { return instance()->_signalProxy; }
98
99   static inline ClientBacklogManager *backlogManager() { return instance()->_backlogManager; }
100   static inline BufferViewManager *bufferViewManager() { return instance()->_bufferViewManager; }
101   
102   static AccountId currentCoreAccount();
103
104   static AbstractUiMsg *layoutMsg(const Message &);
105
106   static bool isConnected();
107   static bool isSynced();
108
109   static void userInput(BufferInfo bufferInfo, QString message);
110
111   enum ClientMode { LocalCore, RemoteCore };
112
113   static void checkForHighlight(Message &msg);
114   static void setBufferLastSeenMsg(BufferId id, const MsgId &msgId); // this is synced to core and other clients
115   static void removeBuffer(BufferId id);
116
117 signals:
118   void sendInput(BufferInfo, QString message);
119   void showBuffer(Buffer *);
120   void bufferUpdated(BufferInfo bufferInfo);
121   void backlogReceived(Buffer *, QList<Message>);
122   void requestBacklog(BufferInfo, QVariant, QVariant);
123   void requestNetworkStates();
124   void messageReceived(const Message &msg);
125
126   void showConfigWizard(const QVariantMap &coredata);
127
128   void connected();
129   void securedConnection();
130   void disconnected();
131   void coreConnectionStateChanged(bool);
132
133   //! The identity with the given ID has been newly created in core and client.
134   /** \param id The ID of the newly created identity.
135    */
136   void identityCreated(IdentityId id);
137
138   //! The identity with the given ID has been removed.
139   /** Upon emitting this signal, the identity is already gone from the core, and it will
140    *  be deleted from the client immediately afterwards, so connected slots need to clean
141    *  up their stuff.
142    *  \param id The ID of the identity about to be removed.
143    */
144   void identityRemoved(IdentityId id);
145
146   //! Sent to the core when an identity shall be created. Should not be used elsewhere.
147   void requestCreateIdentity(const Identity &);
148   //! Sent to the core when an identity shall be updated. Should not be used elsewhere.
149   void requestUpdateIdentity(const Identity &);
150   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
151   void requestRemoveIdentity(IdentityId);
152
153   void networkCreated(NetworkId id);
154   void networkRemoved(NetworkId id);
155
156   void requestCreateNetwork(const NetworkInfo &info);
157   void requestUpdateNetwork(const NetworkInfo &info);
158   void requestRemoveNetwork(NetworkId);
159
160 public slots:
161   //void selectBuffer(Buffer *);
162
163   void disconnectFromCore();
164
165   void setCoreConfiguration(const QVariantMap &settings);
166
167   void bufferRemoved(BufferId bufferId);
168   void bufferRenamed(BufferId bufferId, const QString &newName);
169
170 private slots:
171   //void coreSocketError(QAbstractSocket::SocketError);
172
173   //void networkConnected(NetworkId);
174   //void networkDisconnected(NetworkId);
175
176   void recvMessage(const Message &message);
177   void recvStatusMsg(QString network, QString message);
178   void receiveBacklog(BufferId bufferId, const QVariantList &msgs);
179   void updateBufferInfo(BufferInfo);
180   void updateLastSeenMsg(BufferId id, const MsgId &msgId);
181
182   void layoutMsg();
183
184   void bufferDestroyed();
185   void networkDestroyed();
186   void coreIdentityCreated(const Identity &);
187   void coreIdentityRemoved(IdentityId);
188   void coreNetworkCreated(NetworkId);
189   void coreNetworkRemoved(NetworkId);
190
191   void setConnectedToCore(QIODevice *socket, AccountId id);
192   void setSyncedToCore();
193   void setSecuredConnection();
194
195
196 private:
197   Client(QObject *parent = 0);
198   virtual ~Client();
199   void init();
200
201   static void addNetwork(Network *);
202   static void setCurrentCoreAccount(AccountId);
203   static inline BufferSyncer *bufferSyncer() { return instance()->_bufferSyncer; }
204
205   Buffer *statusBuffer(const NetworkId &networkid) const;
206
207   static QPointer<Client> instanceptr;
208
209   QPointer<QIODevice> socket;
210
211   SignalProxy * _signalProxy;
212   AbstractUi * mainUi;
213   NetworkModel * _networkModel;
214   BufferModel * _bufferModel;
215   BufferSyncer * _bufferSyncer;
216   ClientBacklogManager *_backlogManager;
217   BufferViewManager *_bufferViewManager;
218
219   ClientMode clientMode;
220
221   bool _connectedToCore, _syncedToCore;
222
223   QHash<BufferId, Buffer *> _buffers;
224   QHash<NetworkId, Buffer *> _statusBuffers; // fast lookup
225   QHash<NetworkId, Network *> _networks;
226   QHash<IdentityId, Identity *> _identities;
227
228   Buffer *_monitorBuffer;
229
230   QTimer *layoutTimer;
231   QList<Buffer *> layoutQueue;
232
233   static AccountId _currentCoreAccount;
234
235   friend class ClientSyncer;
236 };
237
238 #endif