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