8b991bdaaae7cf0afb7e9cdf927d9a3ee2cf6b70
[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 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<BufferInfo> allBufferInfos();
66   static QList<Buffer *> buffers();
67   // static Buffer *buffer(BufferId bufferUid);
68   static Buffer *buffer(BufferInfo);
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 Identity &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);
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 BufferViewManager *bufferViewManager() { return instance()->_bufferViewManager; }
108
109   static AccountId currentCoreAccount();
110
111   static bool isConnected();
112   static bool isSynced();
113
114   static void userInput(BufferInfo bufferInfo, QString message);
115
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
127   void showConfigWizard(const QVariantMap &coredata);
128
129   void connected();
130   void securedConnection();
131   void disconnected();
132   void coreConnectionStateChanged(bool);
133
134   //! The identity with the given ID has been newly created in core and client.
135   /** \param id The ID of the newly created identity.
136    */
137   void identityCreated(IdentityId id);
138
139   //! The identity with the given ID has been removed.
140   /** Upon emitting this signal, the identity is already gone from the core, and it will
141    *  be deleted from the client immediately afterwards, so connected slots need to clean
142    *  up their stuff.
143    *  \param id The ID of the identity about to be removed.
144    */
145   void identityRemoved(IdentityId id);
146
147   //! Sent to the core when an identity shall be created. Should not be used elsewhere.
148   void requestCreateIdentity(const Identity &);
149   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
150   void requestRemoveIdentity(IdentityId);
151
152   void networkCreated(NetworkId id);
153   void networkRemoved(NetworkId id);
154
155   void requestCreateNetwork(const NetworkInfo &info);
156   void requestUpdateNetwork(const NetworkInfo &info);
157   void requestRemoveNetwork(NetworkId);
158
159 public slots:
160   //void selectBuffer(Buffer *);
161
162   void disconnectFromCore();
163
164   void setCoreConfiguration(const QVariantMap &settings);
165
166   void bufferRemoved(BufferId bufferId);
167   void bufferRenamed(BufferId bufferId, const QString &newName);
168
169 private slots:
170   //void coreSocketError(QAbstractSocket::SocketError);
171
172   //void networkConnected(NetworkId);
173   //void networkDisconnected(NetworkId);
174
175   void recvMessage(const Message &message);
176   void recvStatusMsg(QString network, QString message);
177   void receiveBacklog(BufferId bufferId, const QVariantList &msgs);
178   void updateBufferInfo(BufferInfo);
179
180   void bufferDestroyed();
181   void networkDestroyed();
182   void coreIdentityCreated(const Identity &);
183   void coreIdentityRemoved(IdentityId);
184   void coreNetworkCreated(NetworkId);
185   void coreNetworkRemoved(NetworkId);
186
187   void setConnectedToCore(QIODevice *socket, AccountId id);
188   void setSyncedToCore();
189   void setSecuredConnection();
190
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   Buffer *statusBuffer(const NetworkId &networkid) const;
202
203   static QPointer<Client> instanceptr;
204
205   QPointer<QIODevice> socket;
206
207   SignalProxy * _signalProxy;
208   AbstractUi * mainUi;
209   NetworkModel * _networkModel;
210   BufferModel * _bufferModel;
211   BufferSyncer * _bufferSyncer;
212   ClientBacklogManager *_backlogManager;
213   BufferViewManager *_bufferViewManager;
214   ClientIrcListHelper *_ircListHelper;
215
216   MessageModel *_messageModel;
217   AbstractMessageProcessor *_messageProcessor;
218
219   ClientMode clientMode;
220
221   bool _connectedToCore, _syncedToCore;
222
223   QHash<BufferId, Buffer *> _buffers;
224   QHash<NetworkId, Buffer *> _statusBuffers; // fast lookup
225   QHash<NetworkId, Network *> _networks;
226   QHash<IdentityId, Identity *> _identities;
227
228   static AccountId _currentCoreAccount;
229
230   friend class ClientSyncer;
231 };
232
233 #endif