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