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