Merge branch 'seezer'
[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 class MessageModel;
34
35 class Identity;
36 class Network;
37
38 class AbstractUi;
39 class AbstractUiMsg;
40 class NetworkModel;
41 class BufferModel;
42 class BufferSyncer;
43 class ClientBacklogManager;
44 class ClientIrcListHelper;
45 class BufferViewManager;
46 class IrcUser;
47 class IrcChannel;
48 class SignalProxy;
49 struct NetworkInfo;
50
51 class QTimer;
52
53 class Client : public QObject {
54   Q_OBJECT
55
56 public:
57   static Client *instance();
58   static void destroy();
59   static void init(AbstractUi *);
60
61   static QList<BufferInfo> allBufferInfos();
62   static QList<Buffer *> buffers();
63   static Buffer *buffer(BufferId bufferUid);
64   static Buffer *buffer(BufferInfo);
65   static inline Buffer *monitorBuffer() { return instance()->_monitorBuffer; }
66
67   static QList<NetworkId> networkIds();
68   static const Network * network(NetworkId);
69
70   static QList<IdentityId> identityIds();
71   static const Identity * identity(IdentityId);
72
73   //! Request creation of an identity with the given data.
74   /** The request will be sent to the core, and will be propagated back to all the clients
75    *  with a new valid IdentityId.
76    *  \param identity The identity template for the new identity. It does not need to have a valid ID.
77    */
78   static void createIdentity(const Identity &identity);
79
80   //! Request update of an identity with the given data.
81   /** The request will be sent to the core, and will be propagated back to all the clients.
82    *  \param identity The identity to be updated.
83    */
84   static void updateIdentity(const Identity &identity);
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 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 AbstractUiMsg *layoutMsg(const Message &);
107
108   static bool isConnected();
109   static bool isSynced();
110
111   static void userInput(BufferInfo bufferInfo, QString message);
112
113   enum ClientMode { LocalCore, RemoteCore };
114
115   static void checkForHighlight(Message &msg);
116   static void setBufferLastSeenMsg(BufferId id, const MsgId &msgId); // this is synced to core and other clients
117   static void removeBuffer(BufferId id);
118
119 signals:
120   void sendInput(BufferInfo, QString message);
121   void showBuffer(Buffer *);
122   void bufferUpdated(BufferInfo bufferInfo);
123   void backlogReceived(Buffer *, QList<Message>);
124   void requestBacklog(BufferInfo, QVariant, QVariant);
125   void requestNetworkStates();
126   void messageReceived(const Message &msg);
127
128   void showConfigWizard(const QVariantMap &coredata);
129
130   void connected();
131   void securedConnection();
132   void disconnected();
133   void coreConnectionStateChanged(bool);
134
135   //! The identity with the given ID has been newly created in core and client.
136   /** \param id The ID of the newly created identity.
137    */
138   void identityCreated(IdentityId id);
139
140   //! The identity with the given ID has been removed.
141   /** Upon emitting this signal, the identity is already gone from the core, and it will
142    *  be deleted from the client immediately afterwards, so connected slots need to clean
143    *  up their stuff.
144    *  \param id The ID of the identity about to be removed.
145    */
146   void identityRemoved(IdentityId id);
147
148   //! Sent to the core when an identity shall be created. Should not be used elsewhere.
149   void requestCreateIdentity(const Identity &);
150   //! Sent to the core when an identity shall be updated. Should not be used elsewhere.
151   void requestUpdateIdentity(const Identity &);
152   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
153   void requestRemoveIdentity(IdentityId);
154
155   void networkCreated(NetworkId id);
156   void networkRemoved(NetworkId id);
157
158   void requestCreateNetwork(const NetworkInfo &info);
159   void requestUpdateNetwork(const NetworkInfo &info);
160   void requestRemoveNetwork(NetworkId);
161
162 public slots:
163   //void selectBuffer(Buffer *);
164
165   void disconnectFromCore();
166
167   void setCoreConfiguration(const QVariantMap &settings);
168
169   void bufferRemoved(BufferId bufferId);
170   void bufferRenamed(BufferId bufferId, const QString &newName);
171
172 private slots:
173   //void coreSocketError(QAbstractSocket::SocketError);
174
175   //void networkConnected(NetworkId);
176   //void networkDisconnected(NetworkId);
177
178   void recvMessage(const Message &message);
179   void recvStatusMsg(QString network, QString message);
180   void receiveBacklog(BufferId bufferId, const QVariantList &msgs);
181   void updateBufferInfo(BufferInfo);
182   void updateLastSeenMsg(BufferId id, const MsgId &msgId);
183
184   void layoutMsg();
185
186   void bufferDestroyed();
187   void networkDestroyed();
188   void coreIdentityCreated(const Identity &);
189   void coreIdentityRemoved(IdentityId);
190   void coreNetworkCreated(NetworkId);
191   void coreNetworkRemoved(NetworkId);
192
193   void setConnectedToCore(QIODevice *socket, AccountId id);
194   void setSyncedToCore();
195   void setSecuredConnection();
196
197
198 private:
199   Client(QObject *parent = 0);
200   virtual ~Client();
201   void init();
202
203   static void addNetwork(Network *);
204   static void setCurrentCoreAccount(AccountId);
205   static inline BufferSyncer *bufferSyncer() { return instance()->_bufferSyncer; }
206
207   Buffer *statusBuffer(const NetworkId &networkid) const;
208
209   static QPointer<Client> instanceptr;
210
211   QPointer<QIODevice> socket;
212
213   SignalProxy * _signalProxy;
214   AbstractUi * mainUi;
215   NetworkModel * _networkModel;
216   BufferModel * _bufferModel;
217   BufferSyncer * _bufferSyncer;
218   ClientBacklogManager *_backlogManager;
219   BufferViewManager *_bufferViewManager;
220   ClientIrcListHelper *_ircListHelper;
221
222   MessageModel *_messageModel;
223
224   ClientMode clientMode;
225
226   bool _connectedToCore, _syncedToCore;
227
228   QHash<BufferId, Buffer *> _buffers;
229   QHash<NetworkId, Buffer *> _statusBuffers; // fast lookup
230   QHash<NetworkId, Network *> _networks;
231   QHash<IdentityId, Identity *> _identities;
232
233   Buffer *_monitorBuffer;
234
235   QTimer *layoutTimer;
236   QLinkedList<Buffer *> layoutQueue;
237
238   static AccountId _currentCoreAccount;
239
240   friend class ClientSyncer;
241 };
242
243 #endif