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