Changed QVariant::fromValue() to qVariantFromValue, since MSVC6 supposedly cannot...
[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 class BufferInfo;
31 class Message;
32
33 class Identity;
34 class Network;
35
36
37 class AbstractUi;
38 class AbstractUiMsg;
39 class NetworkModel;
40 class BufferModel;
41 class IrcUser;
42 class IrcChannel;
43 class NickModel;
44 class SignalProxy;
45
46 class QTimer;
47
48
49 class Client : public QObject {
50   Q_OBJECT
51
52 public:
53   static Client *instance();
54   static void destroy();
55   static void init(AbstractUi *);
56
57   static QList<Network *> networks();
58   static Network *network(NetworkId networkid);
59
60   static QList<BufferInfo> allBufferInfos();
61   static QList<Buffer *> buffers();
62   static Buffer *buffer(BufferId bufferUid);
63   static Buffer *buffer(BufferInfo);
64
65   static QList<IdentityId> identityIds();
66   static const Identity * identity(IdentityId);
67
68   //! Request creation of an identity with the given data.
69   /** The request will be sent to the core, and will be propagated back to all the clients
70    *  with a new valid IdentityId.
71    *  \param identity The identity template for the new identity. It does not need to have a valid ID.
72    */
73   static void createIdentity(const Identity &identity);
74
75   //! Request update of an identity with the given data.
76   /** The request will be sent to the core, and will be propagated back to all the clients.
77    *  \param identity The identity to be updated.
78    */
79   static void updateIdentity(const Identity &identity);
80
81   //! Request removal of the identity with the given ID from the core (and all the clients, of course).
82   /** \param id The ID of the identity to be removed.
83    */
84   static void removeIdentity(IdentityId id);
85
86   static void addNetwork(NetworkId id);
87   static void addNetwork(Network *);
88
89
90   static NetworkModel *networkModel();
91   static BufferModel *bufferModel();
92   static NickModel *nickModel();
93   static SignalProxy *signalProxy();
94
95   static AbstractUiMsg *layoutMsg(const Message &);
96
97   static bool isConnected();
98   static bool isSynced();
99
100   static void fakeInput(BufferId bufferUid, QString message);
101   static void fakeInput(BufferInfo bufferInfo, QString message);
102
103   static void storeSessionData(const QString &key, const QVariant &data);
104   static QVariant retrieveSessionData(const QString &key, const QVariant &def = QVariant());
105   static QStringList sessionDataKeys();
106
107   enum ClientMode { LocalCore, RemoteCore };
108
109 signals:
110   void sendInput(BufferInfo, QString message);
111   void showBuffer(Buffer *);
112   void bufferUpdated(BufferInfo bufferInfo);
113   void backlogReceived(Buffer *, QList<Message>);
114   void requestBacklog(BufferInfo, QVariant, QVariant);
115   void requestNetworkStates();
116
117   void showConfigWizard(const QVariantMap &coredata);
118
119   void connected();
120   void disconnected();
121   void coreConnectionStateChanged(bool);
122
123   void sessionDataChanged(const QString &key);
124   void sessionDataChanged(const QString &key, const QVariant &data);
125   void sendSessionData(const QString &key, const QVariant &data);
126
127   //! The identity with the given ID has been newly created in core and client.
128   /** \param id The ID of the newly created identity.
129    */
130   void identityCreated(IdentityId id);
131
132   //! The identity with the given ID has been removed.
133   /** Upon emitting this signal, the identity is already gone from the core, and it will
134    *  be deleted from the client immediately afterwards, so connected slots need to clean
135    *  up their stuff.
136    *  \param id The ID of the identity about to be removed.
137    */
138   void identityRemoved(IdentityId id);
139
140   //! Sent to the core when an identity shall be created. Should not be used elsewhere.
141   void requestCreateIdentity(const Identity &);
142   //! Sent to the core when an identity shall be updated. Should not be used elsewhere.
143   void requestUpdateIdentity(const Identity &);
144   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
145   void requestRemoveIdentity(IdentityId);
146
147 public slots:
148   //void selectBuffer(Buffer *);
149
150   void setConnectedToCore(QIODevice *socket);
151   void setSyncedToCore();
152   void disconnectFromCore();
153
154   void setCoreConfiguration(const QVariantMap &settings);
155
156
157 private slots:
158   void recvSessionData(const QString &key, const QVariant &data);
159
160   //void coreSocketError(QAbstractSocket::SocketError);
161
162   void userInput(BufferInfo, QString);
163
164   //void networkConnected(NetworkId);
165   //void networkDisconnected(NetworkId);
166
167   void recvMessage(const Message &message);
168   void recvStatusMsg(QString network, QString message);
169   void recvBacklogData(BufferInfo, QVariantList, bool);
170   void updateBufferInfo(BufferInfo);
171
172   void layoutMsg();
173
174   void bufferDestroyed();
175   void networkDestroyed();
176   void coreIdentityCreated(const Identity &);
177   void coreIdentityRemoved(IdentityId);
178
179 private:
180   Client(QObject *parent = 0);
181   virtual ~Client();
182   void init();
183
184   void syncToCore(const QVariantMap &sessionState);
185
186   static QPointer<Client> instanceptr;
187
188   QPointer<QIODevice> socket;
189   QPointer<SignalProxy> _signalProxy;
190   QPointer<AbstractUi> mainUi;
191   QPointer<NetworkModel> _networkModel;
192   QPointer<BufferModel> _bufferModel;
193   QPointer<NickModel> _nickModel;
194
195   ClientMode clientMode;
196
197   bool _connectedToCore, _syncedToCore;
198
199   QHash<BufferId, Buffer *> _buffers;
200   QHash<NetworkId, Network *> _networks;
201   QHash<IdentityId, Identity *> _identities;
202
203   QTimer *layoutTimer;
204   QList<Buffer *> layoutQueue;
205
206   QVariantMap sessionData;
207
208   friend class ClientSyncer;
209 };
210
211 #endif