introducing autocached settings
[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   static inline Buffer *monitorBuffer() { return instance()->_monitorBuffer; }
70
71   static QList<NetworkId> networkIds();
72   static const Network * network(NetworkId);
73
74   static QList<IdentityId> identityIds();
75   static const Identity * identity(IdentityId);
76
77   //! Request creation of an identity with the given data.
78   /** The request will be sent to the core, and will be propagated back to all the clients
79    *  with a new valid IdentityId.
80    *  \param identity The identity template for the new identity. It does not need to have a valid ID.
81    */
82   static void createIdentity(const Identity &identity);
83
84   //! Request update of an identity with the given data.
85   /** The request will be sent to the core, and will be propagated back to all the clients.
86    *  \param identity The identity to be updated.
87    */
88   static void updateIdentity(const Identity &identity);
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 updated. Should not be used elsewhere.
150   void requestUpdateIdentity(const Identity &);
151   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
152   void requestRemoveIdentity(IdentityId);
153
154   void networkCreated(NetworkId id);
155   void networkRemoved(NetworkId id);
156
157   void requestCreateNetwork(const NetworkInfo &info);
158   void requestUpdateNetwork(const NetworkInfo &info);
159   void requestRemoveNetwork(NetworkId);
160
161 public slots:
162   //void selectBuffer(Buffer *);
163
164   void disconnectFromCore();
165
166   void setCoreConfiguration(const QVariantMap &settings);
167
168   void bufferRemoved(BufferId bufferId);
169   void bufferRenamed(BufferId bufferId, const QString &newName);
170
171 private slots:
172   //void coreSocketError(QAbstractSocket::SocketError);
173
174   //void networkConnected(NetworkId);
175   //void networkDisconnected(NetworkId);
176
177   void recvMessage(const Message &message);
178   void recvStatusMsg(QString network, QString message);
179   void receiveBacklog(BufferId bufferId, const QVariantList &msgs);
180   void updateBufferInfo(BufferInfo);
181   void updateLastSeenMsg(BufferId id, const MsgId &msgId);
182
183   void bufferDestroyed();
184   void networkDestroyed();
185   void coreIdentityCreated(const Identity &);
186   void coreIdentityRemoved(IdentityId);
187   void coreNetworkCreated(NetworkId);
188   void coreNetworkRemoved(NetworkId);
189
190   void setConnectedToCore(QIODevice *socket, AccountId id);
191   void setSyncedToCore();
192   void setSecuredConnection();
193
194
195 private:
196   Client(QObject *parent = 0);
197   virtual ~Client();
198   void init();
199
200   static void addNetwork(Network *);
201   static void setCurrentCoreAccount(AccountId);
202   static inline BufferSyncer *bufferSyncer() { return instance()->_bufferSyncer; }
203
204   Buffer *statusBuffer(const NetworkId &networkid) const;
205
206   static QPointer<Client> instanceptr;
207
208   QPointer<QIODevice> socket;
209
210   SignalProxy * _signalProxy;
211   AbstractUi * mainUi;
212   NetworkModel * _networkModel;
213   BufferModel * _bufferModel;
214   BufferSyncer * _bufferSyncer;
215   ClientBacklogManager *_backlogManager;
216   BufferViewManager *_bufferViewManager;
217   ClientIrcListHelper *_ircListHelper;
218
219   MessageModel *_messageModel;
220   AbstractMessageProcessor *_messageProcessor;
221
222   ClientMode clientMode;
223
224   bool _connectedToCore, _syncedToCore;
225
226   QHash<BufferId, Buffer *> _buffers;
227   QHash<NetworkId, Buffer *> _statusBuffers; // fast lookup
228   QHash<NetworkId, Network *> _networks;
229   QHash<IdentityId, Identity *> _identities;
230
231   Buffer *_monitorBuffer;
232
233   static AccountId _currentCoreAccount;
234
235   friend class ClientSyncer;
236 };
237
238 #endif