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