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