Make ping timeouts and auto-WHO configurable
[quassel.git] / src / client / client.h
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 "bufferinfo.h"
30 #include "types.h"
31
32 class Message;
33 class MessageModel;
34 class AbstractMessageProcessor;
35
36 class Identity;
37 class CertIdentity;
38 class Network;
39
40 class AbstractUi;
41 class AbstractUiMsg;
42 class NetworkModel;
43 class BufferModel;
44 class BufferSyncer;
45 class BufferViewOverlay;
46 class ClientAliasManager;
47 class ClientBacklogManager;
48 class ClientBufferViewManager;
49 class ClientIrcListHelper;
50 class ClientSyncer;
51 class ClientUserInputHandler;
52 class IrcUser;
53 class IrcChannel;
54 class NetworkConfig;
55 class SignalProxy;
56 struct NetworkInfo;
57
58 class Client : public QObject {
59   Q_OBJECT
60
61 public:
62   enum ClientMode {
63     LocalCore,
64     RemoteCore
65   };
66
67   static bool instanceExists();
68   static Client *instance();
69   static void destroy();
70   static void init(AbstractUi *);
71   static AbstractUi *mainUi();
72
73   static QList<NetworkId> networkIds();
74   static const Network * network(NetworkId);
75
76   static QList<IdentityId> identityIds();
77   static const Identity *identity(IdentityId);
78
79   //! Request creation of an identity with the given data.
80   /** The request will be sent to the core, and will be propagated back to all the clients
81    *  with a new valid IdentityId.
82    *  \param identity The identity template for the new identity. It does not need to have a valid ID.
83    */
84   static void createIdentity(const CertIdentity &identity);
85
86   //! Request update of an identity with the given data.
87   /** The request will be sent to the core, and will be propagated back to all the clients.
88    *  \param id The identity to be updated.
89    *  \param serializedData The identity's content (cf. SyncableObject::toVariantMap())
90    */
91   static void updateIdentity(IdentityId id, const QVariantMap &serializedData);
92
93   //! Request removal of the identity with the given ID from the core (and all the clients, of course).
94   /** \param id The ID of the identity to be removed.
95    */
96   static void removeIdentity(IdentityId id);
97
98   static void createNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
99   static void updateNetwork(const NetworkInfo &info);
100   static void removeNetwork(NetworkId id);
101
102   static inline NetworkModel *networkModel() { return instance()->_networkModel; }
103   static inline BufferModel *bufferModel() { return instance()->_bufferModel; }
104   static inline MessageModel *messageModel() { return instance()->_messageModel; }
105   static inline AbstractMessageProcessor *messageProcessor() { return instance()->_messageProcessor; }
106   static inline SignalProxy *signalProxy() { return instance()->_signalProxy; }
107
108   static inline ClientAliasManager *aliasManager() { return instance()->_aliasManager; }
109   static inline ClientBacklogManager *backlogManager() { return instance()->_backlogManager; }
110   static inline ClientIrcListHelper *ircListHelper() { return instance()->_ircListHelper; }
111   static inline ClientBufferViewManager *bufferViewManager() { return instance()->_bufferViewManager; }
112   static inline BufferViewOverlay *bufferViewOverlay() { return instance()->_bufferViewOverlay; }
113   static inline ClientUserInputHandler *inputHandler() { return instance()->_inputHandler; }
114   static inline NetworkConfig *networkConfig() { return instance()->_networkConfig; }
115
116   static AccountId currentCoreAccount();
117
118   static bool isConnected();
119   static bool isSynced();
120   static inline bool internalCore() { return instance()->_internalCore; }
121
122   static void userInput(const BufferInfo &bufferInfo, const QString &message);
123
124   static void setBufferLastSeenMsg(BufferId id, const MsgId &msgId); // this is synced to core and other clients
125   static void removeBuffer(BufferId id);
126   static void renameBuffer(BufferId bufferId, const QString &newName);
127   static void mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2);
128   static void purgeKnownBufferIds();
129
130   static void logMessage(QtMsgType type, const char *msg);
131   static inline const QString &debugLog() { return instance()->_debugLogBuffer; }
132
133   static inline void registerClientSyncer(ClientSyncer *syncer) { emit instance()->newClientSyncer(syncer); }
134
135 signals:
136   void requestNetworkStates();
137
138   void showConfigWizard(const QVariantMap &coredata);
139
140   void connected();
141   void disconnected();
142   void coreConnectionStateChanged(bool);
143
144   //! The identity with the given ID has been newly created in core and client.
145   /** \param id The ID of the newly created identity.
146    */
147   void identityCreated(IdentityId id);
148
149   //! The identity with the given ID has been removed.
150   /** Upon emitting this signal, the identity is already gone from the core, and it will
151    *  be deleted from the client immediately afterwards, so connected slots need to clean
152    *  up their stuff.
153    *  \param id The ID of the identity about to be removed.
154    */
155   void identityRemoved(IdentityId id);
156
157   //! Sent to the core when an identity shall be created. Should not be used elsewhere.
158   void requestCreateIdentity(const Identity &, const QVariantMap &);
159   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
160   void requestRemoveIdentity(IdentityId);
161
162   void networkCreated(NetworkId id);
163   void networkRemoved(NetworkId id);
164
165   void requestCreateNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
166   void requestRemoveNetwork(NetworkId);
167
168   void newClientSyncer(ClientSyncer *);
169
170   void logUpdated(const QString &msg);
171
172 public slots:
173   void disconnectFromCore();
174
175   void bufferRemoved(BufferId bufferId);
176   void bufferRenamed(BufferId bufferId, const QString &newName);
177   void buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2);
178
179 private slots:
180   void disconnectedFromCore();
181
182   void recvMessage(const Message &message);
183   void recvStatusMsg(QString network, QString message);
184
185   void networkDestroyed();
186   void coreIdentityCreated(const Identity &);
187   void coreIdentityRemoved(IdentityId);
188   void coreNetworkCreated(NetworkId);
189   void coreNetworkRemoved(NetworkId);
190
191   void setConnectedToCore(AccountId id, QIODevice *socket = 0);
192   void setSyncedToCore();
193   void requestInitialBacklog();
194   void createDefaultBufferView();
195
196   void sendBufferedUserInput();
197
198 private:
199   Client(QObject *parent = 0);
200   virtual ~Client();
201   void init();
202
203   static void addNetwork(Network *);
204   static void setCurrentCoreAccount(AccountId);
205   static inline BufferSyncer *bufferSyncer() { return instance()->_bufferSyncer; }
206
207   static QPointer<Client> instanceptr;
208
209   SignalProxy * _signalProxy;
210   AbstractUi * _mainUi;
211   NetworkModel * _networkModel;
212   BufferModel * _bufferModel;
213   BufferSyncer * _bufferSyncer;
214   ClientAliasManager *_aliasManager;
215   ClientBacklogManager *_backlogManager;
216   ClientBufferViewManager *_bufferViewManager;
217   BufferViewOverlay *_bufferViewOverlay;
218   ClientIrcListHelper *_ircListHelper;
219   ClientUserInputHandler *_inputHandler;
220   NetworkConfig *_networkConfig;
221
222   MessageModel *_messageModel;
223   AbstractMessageProcessor *_messageProcessor;
224
225   ClientMode clientMode;
226
227   bool _connectedToCore, _syncedToCore;
228   bool _internalCore;
229
230   QHash<NetworkId, Network *> _networks;
231   QHash<IdentityId, Identity *> _identities;
232
233   static AccountId _currentCoreAccount;
234
235   QString _debugLogBuffer;
236   QTextStream _debugLog;
237
238   QList<QPair<BufferInfo, QString> > _userInputBuffer;
239
240   friend class ClientSyncer;
241 };
242
243 #endif