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