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