Make Client::mainUi() accessible, needed for some things in uisupport
[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   static AbstractUi *mainUi();
66
67   static QList<NetworkId> networkIds();
68   static const Network * network(NetworkId);
69
70   static QList<IdentityId> identityIds();
71   static const Identity * identity(IdentityId);
72
73   //! Request creation of an identity with the given data.
74   /** The request will be sent to the core, and will be propagated back to all the clients
75    *  with a new valid IdentityId.
76    *  \param identity The identity template for the new identity. It does not need to have a valid ID.
77    */
78   static void createIdentity(const Identity &identity);
79
80   //! Request update of an identity with the given data.
81   /** The request will be sent to the core, and will be propagated back to all the clients.
82    *  \param id The identity to be updated.
83    *  \param serializedData The identity's content (cf. SyncableObject::toVariantMap())
84    */
85   static void updateIdentity(IdentityId id, const QVariantMap &serializedData);
86
87   //! Request removal of the identity with the given ID from the core (and all the clients, of course).
88   /** \param id The ID of the identity to be removed.
89    */
90   static void removeIdentity(IdentityId id);
91
92   static void createNetwork(const NetworkInfo &info);
93   static void updateNetwork(const NetworkInfo &info);
94   static void removeNetwork(NetworkId id);
95
96   static inline NetworkModel *networkModel() { return instance()->_networkModel; }
97   static inline BufferModel *bufferModel() { return instance()->_bufferModel; }
98   static inline MessageModel *messageModel() { return instance()->_messageModel; }
99   static inline AbstractMessageProcessor *messageProcessor() { return instance()->_messageProcessor; }
100   static inline SignalProxy *signalProxy() { return instance()->_signalProxy; }
101
102   static inline ClientBacklogManager *backlogManager() { return instance()->_backlogManager; }
103   static inline ClientIrcListHelper *ircListHelper() { return instance()->_ircListHelper; }
104   static inline BufferViewManager *bufferViewManager() { return instance()->_bufferViewManager; }
105
106   static AccountId currentCoreAccount();
107
108   static bool isConnected();
109   static bool isSynced();
110
111   static void userInput(BufferInfo bufferInfo, QString message);
112
113   static void setBufferLastSeenMsg(BufferId id, const MsgId &msgId); // this is synced to core and other clients
114   static void removeBuffer(BufferId id);
115
116   static void logMessage(QtMsgType type, const char *msg);
117   static inline const QString &debugLog() { return instance()->_debugLogBuffer; }
118
119 signals:
120   void sendInput(BufferInfo, QString message);
121   void requestNetworkStates();
122
123   void showConfigWizard(const QVariantMap &coredata);
124
125   void connected();
126   void securedConnection();
127   void disconnected();
128   void coreConnectionStateChanged(bool);
129
130   //! The identity with the given ID has been newly created in core and client.
131   /** \param id The ID of the newly created identity.
132    */
133   void identityCreated(IdentityId id);
134
135   //! The identity with the given ID has been removed.
136   /** Upon emitting this signal, the identity is already gone from the core, and it will
137    *  be deleted from the client immediately afterwards, so connected slots need to clean
138    *  up their stuff.
139    *  \param id The ID of the identity about to be removed.
140    */
141   void identityRemoved(IdentityId id);
142
143   //! Sent to the core when an identity shall be created. Should not be used elsewhere.
144   void requestCreateIdentity(const Identity &);
145   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
146   void requestRemoveIdentity(IdentityId);
147
148   void networkCreated(NetworkId id);
149   void networkRemoved(NetworkId id);
150
151   void requestCreateNetwork(const NetworkInfo &info);
152   void requestRemoveNetwork(NetworkId);
153
154   void newClientSyncer(ClientSyncer *);
155
156   void logUpdated(const QString &msg);
157
158 public slots:
159   //void selectBuffer(Buffer *);
160
161   void disconnectFromCore();
162
163   void bufferRemoved(BufferId bufferId);
164   void bufferRenamed(BufferId bufferId, const QString &newName);
165
166 private slots:
167   void disconnectedFromCore();
168
169   void recvMessage(const Message &message);
170   void recvStatusMsg(QString network, QString message);
171
172   void networkDestroyed();
173   void coreIdentityCreated(const Identity &);
174   void coreIdentityRemoved(IdentityId);
175   void coreNetworkCreated(NetworkId);
176   void coreNetworkRemoved(NetworkId);
177
178   void setConnectedToCore(AccountId id, QIODevice *socket = 0);
179   void setSyncedToCore();
180   void requestInitialBacklog();
181   void setSecuredConnection();
182
183
184 private:
185   Client(QObject *parent = 0);
186   virtual ~Client();
187   void init();
188
189   static void addNetwork(Network *);
190   static void setCurrentCoreAccount(AccountId);
191   static inline BufferSyncer *bufferSyncer() { return instance()->_bufferSyncer; }
192
193   static QPointer<Client> instanceptr;
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   QString _debugLogBuffer;
217   QTextStream _debugLog;
218
219   friend class ClientSyncer;
220 };
221
222 #endif