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