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