Fixed those nasty "Client::updateLastSeen(): Unknown buffer $bufferId" messages.
[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 "buffer.h" // needed for activity lvl
30
31 class BufferInfo;
32 class Message;
33
34 class Identity;
35 class Network;
36
37
38 class AbstractUi;
39 class AbstractUiMsg;
40 class NetworkModel;
41 class BufferModel;
42 class BufferSyncer;
43 class IrcUser;
44 class IrcChannel;
45 class SignalProxy;
46 struct NetworkInfo;
47
48 class QTimer;
49
50
51 class Client : public QObject {
52   Q_OBJECT
53
54 public:
55   static Client *instance();
56   static void destroy();
57   static void init(AbstractUi *);
58
59   static QList<BufferInfo> allBufferInfos();
60   static QList<Buffer *> buffers();
61   static Buffer *buffer(BufferId bufferUid);
62   static Buffer *buffer(BufferInfo);
63   static inline Buffer *monitorBuffer() { return instance()->_monitorBuffer; }
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 identity The identity to be updated.
81    */
82   static void updateIdentity(const Identity &identity);
83
84   //! Request removal of the identity with the given ID from the core (and all the clients, of course).
85   /** \param id The ID of the identity to be removed.
86    */
87   static void removeIdentity(IdentityId id);
88
89   static void createNetwork(const NetworkInfo &info);
90   static void updateNetwork(const NetworkInfo &info);
91   static void removeNetwork(NetworkId id);
92
93   static inline NetworkModel *networkModel() { return instance()->_networkModel; }
94   static inline BufferModel *bufferModel() { return instance()->_bufferModel; }
95   static inline SignalProxy *signalProxy() { return instance()->_signalProxy; }
96
97   static AccountId currentCoreAccount();
98
99   static AbstractUiMsg *layoutMsg(const Message &);
100
101   static bool isConnected();
102   static bool isSynced();
103
104   static void userInput(BufferInfo bufferInfo, QString message);
105
106   enum ClientMode { LocalCore, RemoteCore };
107
108   static void checkForHighlight(Message &msg);
109   static void setBufferLastSeen(BufferId id, const QDateTime &seen); // this is synced to core and other clients
110   static void removeBuffer(BufferId id);
111
112 signals:
113   void sendInput(BufferInfo, QString message);
114   void showBuffer(Buffer *);
115   void bufferUpdated(BufferInfo bufferInfo);
116   void backlogReceived(Buffer *, QList<Message>);
117   void requestBacklog(BufferInfo, QVariant, QVariant);
118   void requestNetworkStates();
119
120   void showConfigWizard(const QVariantMap &coredata);
121
122   void connected();
123   void disconnected();
124   void coreConnectionStateChanged(bool);
125
126   //! The identity with the given ID has been newly created in core and client.
127   /** \param id The ID of the newly created identity.
128    */
129   void identityCreated(IdentityId id);
130
131   //! The identity with the given ID has been removed.
132   /** Upon emitting this signal, the identity is already gone from the core, and it will
133    *  be deleted from the client immediately afterwards, so connected slots need to clean
134    *  up their stuff.
135    *  \param id The ID of the identity about to be removed.
136    */
137   void identityRemoved(IdentityId id);
138
139   //! Sent to the core when an identity shall be created. Should not be used elsewhere.
140   void requestCreateIdentity(const Identity &);
141   //! Sent to the core when an identity shall be updated. Should not be used elsewhere.
142   void requestUpdateIdentity(const Identity &);
143   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
144   void requestRemoveIdentity(IdentityId);
145
146   void networkCreated(NetworkId id);
147   void networkRemoved(NetworkId id);
148
149   void requestCreateNetwork(const NetworkInfo &info);
150   void requestUpdateNetwork(const NetworkInfo &info);
151   void requestRemoveNetwork(NetworkId);
152
153 public slots:
154   //void selectBuffer(Buffer *);
155
156   void disconnectFromCore();
157
158   void setCoreConfiguration(const QVariantMap &settings);
159
160   void bufferRemoved(BufferId bufferId);
161   void bufferRenamed(BufferId bufferId, const QString &newName);
162
163 private slots:
164   //void coreSocketError(QAbstractSocket::SocketError);
165
166   //void networkConnected(NetworkId);
167   //void networkDisconnected(NetworkId);
168
169   void recvMessage(const Message &message);
170   void recvStatusMsg(QString network, QString message);
171   void recvBacklogData(BufferInfo, QVariantList, bool);
172   void updateBufferInfo(BufferInfo);
173   void updateLastSeen(BufferId id, const QDateTime &lastSeen);
174
175   void layoutMsg();
176
177   void bufferDestroyed();
178   void networkDestroyed();
179   void coreIdentityCreated(const Identity &);
180   void coreIdentityRemoved(IdentityId);
181   void coreNetworkCreated(NetworkId);
182   void coreNetworkRemoved(NetworkId);
183
184   void setConnectedToCore(QIODevice *socket, AccountId id);
185   void setSyncedToCore();
186
187 private:
188   Client(QObject *parent = 0);
189   virtual ~Client();
190   void init();
191
192   static void addNetwork(Network *);
193   static void setCurrentCoreAccount(AccountId);
194   static inline BufferSyncer *bufferSyncer() { return instance()->_bufferSyncer; }
195
196   Buffer *statusBuffer(const NetworkId &networkid) const;
197
198   static QPointer<Client> instanceptr;
199
200   QPointer<QIODevice> socket;
201
202   SignalProxy * _signalProxy;
203   AbstractUi * mainUi;
204   NetworkModel * _networkModel;
205   BufferModel * _bufferModel;
206   BufferSyncer * _bufferSyncer;
207
208   ClientMode clientMode;
209
210   bool _connectedToCore, _syncedToCore;
211
212   QHash<BufferId, Buffer *> _buffers;
213   QHash<NetworkId, Buffer *> _statusBuffers; // fast lookup
214   QHash<NetworkId, Network *> _networks;
215   QHash<IdentityId, Identity *> _identities;
216
217   Buffer *_monitorBuffer;
218
219   QTimer *layoutTimer;
220   QList<Buffer *> layoutQueue;
221
222   static AccountId _currentCoreAccount;
223
224   friend class ClientSyncer;
225 };
226
227 #endif