Move the client's AliasManager into Client
[quassel.git] / src / client / client.h
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 CertIdentity;
38 class Network;
39
40 class AbstractUi;
41 class AbstractUiMsg;
42 class NetworkModel;
43 class BufferModel;
44 class BufferSyncer;
45 class ClientAliasManager;
46 class ClientBacklogManager;
47 class ClientBufferViewManager;
48 class ClientIrcListHelper;
49 class ClientSyncer;
50 class ClientUserInputHandler;
51 class IrcUser;
52 class IrcChannel;
53 class SignalProxy;
54 struct NetworkInfo;
55
56 class Client : public QObject {
57   Q_OBJECT
58
59 public:
60   enum ClientMode {
61     LocalCore,
62     RemoteCore
63   };
64
65   static Client *instance();
66   static void destroy();
67   static void init(AbstractUi *);
68   static AbstractUi *mainUi();
69
70   static QList<NetworkId> networkIds();
71   static const Network * network(NetworkId);
72
73   static QList<IdentityId> identityIds();
74   static const Identity *identity(IdentityId);
75
76   //! Request creation 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    *  with a new valid IdentityId.
79    *  \param identity The identity template for the new identity. It does not need to have a valid ID.
80    */
81   static void createIdentity(const CertIdentity &identity);
82
83   //! Request update of an identity with the given data.
84   /** The request will be sent to the core, and will be propagated back to all the clients.
85    *  \param id The identity to be updated.
86    *  \param serializedData The identity's content (cf. SyncableObject::toVariantMap())
87    */
88   static void updateIdentity(IdentityId id, const QVariantMap &serializedData);
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, const QStringList &persistentChannels = QStringList());
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 ClientAliasManager *aliasManager() { return instance()->_aliasManager; }
106   static inline ClientBacklogManager *backlogManager() { return instance()->_backlogManager; }
107   static inline ClientIrcListHelper *ircListHelper() { return instance()->_ircListHelper; }
108   static inline ClientBufferViewManager *bufferViewManager() { return instance()->_bufferViewManager; }
109   static inline ClientUserInputHandler *inputHandler() { return instance()->_inputHandler; }
110
111   static AccountId currentCoreAccount();
112
113   static bool isConnected();
114   static bool isSynced();
115   static inline bool internalCore() { return instance()->_internalCore; }
116
117   static void userInput(const BufferInfo &bufferInfo, const QString &message);
118
119   static void setBufferLastSeenMsg(BufferId id, const MsgId &msgId); // this is synced to core and other clients
120   static void removeBuffer(BufferId id);
121   static void renameBuffer(BufferId bufferId, const QString &newName);
122   static void mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2);
123   static void purgeKnownBufferIds();
124
125   static void logMessage(QtMsgType type, const char *msg);
126   static inline const QString &debugLog() { return instance()->_debugLogBuffer; }
127
128   static inline void registerClientSyncer(ClientSyncer *syncer) { emit instance()->newClientSyncer(syncer); }
129
130 signals:
131   void requestNetworkStates();
132
133   void showConfigWizard(const QVariantMap &coredata);
134
135   void connected();
136   void disconnected();
137   void coreConnectionStateChanged(bool);
138
139   //! The identity with the given ID has been newly created in core and client.
140   /** \param id The ID of the newly created identity.
141    */
142   void identityCreated(IdentityId id);
143
144   //! The identity with the given ID has been removed.
145   /** Upon emitting this signal, the identity is already gone from the core, and it will
146    *  be deleted from the client immediately afterwards, so connected slots need to clean
147    *  up their stuff.
148    *  \param id The ID of the identity about to be removed.
149    */
150   void identityRemoved(IdentityId id);
151
152   //! Sent to the core when an identity shall be created. Should not be used elsewhere.
153   void requestCreateIdentity(const Identity &, const QVariantMap &);
154   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
155   void requestRemoveIdentity(IdentityId);
156
157   void networkCreated(NetworkId id);
158   void networkRemoved(NetworkId id);
159
160   void requestCreateNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
161   void requestRemoveNetwork(NetworkId);
162
163   void newClientSyncer(ClientSyncer *);
164
165   void logUpdated(const QString &msg);
166
167 public slots:
168   void disconnectFromCore();
169
170   void bufferRemoved(BufferId bufferId);
171   void bufferRenamed(BufferId bufferId, const QString &newName);
172   void buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2);
173
174 private slots:
175   void disconnectedFromCore();
176
177   void recvMessage(const Message &message);
178   void recvStatusMsg(QString network, QString message);
179
180   void networkDestroyed();
181   void coreIdentityCreated(const Identity &);
182   void coreIdentityRemoved(IdentityId);
183   void coreNetworkCreated(NetworkId);
184   void coreNetworkRemoved(NetworkId);
185
186   void setConnectedToCore(AccountId id, QIODevice *socket = 0);
187   void setSyncedToCore();
188   void requestInitialBacklog();
189   void createDefaultBufferView();
190
191   void sendBufferedUserInput();
192
193 private:
194   Client(QObject *parent = 0);
195   virtual ~Client();
196   void init();
197
198   static void addNetwork(Network *);
199   static void setCurrentCoreAccount(AccountId);
200   static inline BufferSyncer *bufferSyncer() { return instance()->_bufferSyncer; }
201
202   static QPointer<Client> instanceptr;
203
204   SignalProxy * _signalProxy;
205   AbstractUi * _mainUi;
206   NetworkModel * _networkModel;
207   BufferModel * _bufferModel;
208   BufferSyncer * _bufferSyncer;
209   ClientAliasManager *_aliasManager;
210   ClientBacklogManager *_backlogManager;
211   ClientBufferViewManager *_bufferViewManager;
212   ClientIrcListHelper *_ircListHelper;
213   ClientUserInputHandler *_inputHandler;
214
215   MessageModel *_messageModel;
216   AbstractMessageProcessor *_messageProcessor;
217
218   ClientMode clientMode;
219
220   bool _connectedToCore, _syncedToCore;
221   bool _internalCore;
222
223   QHash<NetworkId, Network *> _networks;
224   QHash<IdentityId, Identity *> _identities;
225
226   static AccountId _currentCoreAccount;
227
228   QString _debugLogBuffer;
229   QTextStream _debugLog;
230
231   QList<QPair<BufferInfo, QString> > _userInputBuffer;
232
233   friend class ClientSyncer;
234 };
235
236 #endif