common: Represent core/client features as string list in the protocol
[quassel.git] / src / client / client.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <QList>
24 #include <QPointer>
25
26 #include "bufferinfo.h"
27 #include "coreaccount.h"
28 #include "coreconnection.h"
29 #include "highlightrulemanager.h"
30 #include "quassel.h"
31 #include "types.h"
32
33 class Message;
34 class MessageModel;
35 class AbstractMessageProcessor;
36
37 class Identity;
38 class CertIdentity;
39 class Network;
40
41 class AbstractUi;
42 class AbstractUiMsg;
43 class NetworkModel;
44 class BufferModel;
45 class BufferSyncer;
46 class BufferViewOverlay;
47 class ClientAliasManager;
48 class ClientBacklogManager;
49 class ClientBufferViewManager;
50 class ClientIgnoreListManager;
51 class ClientIrcListHelper;
52 class ClientTransferManager;
53 class ClientUserInputHandler;
54 class CoreAccountModel;
55 class CoreConnection;
56 class DccConfig;
57 class IrcUser;
58 class IrcChannel;
59 class NetworkConfig;
60 class SignalProxy;
61 class TransferModel;
62
63 struct NetworkInfo;
64
65 class Client : public QObject
66 {
67     Q_OBJECT
68
69 public:
70     enum ClientMode {
71         LocalCore,
72         RemoteCore
73     };
74
75     static bool instanceExists();
76     static Client *instance();
77     static void destroy();
78     static void init(AbstractUi *);
79     static AbstractUi *mainUi();
80
81     static QList<NetworkId> networkIds();
82     static const Network *network(NetworkId);
83
84     static QList<IdentityId> identityIds();
85     static const Identity *identity(IdentityId);
86
87     //! Request creation of an identity with the given data.
88     /** The request will be sent to the core, and will be propagated back to all the clients
89      *  with a new valid IdentityId.
90      *  \param identity The identity template for the new identity. It does not need to have a valid ID.
91      */
92     static void createIdentity(const CertIdentity &identity);
93
94     //! Request update of an identity with the given data.
95     /** The request will be sent to the core, and will be propagated back to all the clients.
96      *  \param id The identity to be updated.
97      *  \param serializedData The identity's content (cf. SyncableObject::toVariantMap())
98      */
99     static void updateIdentity(IdentityId id, const QVariantMap &serializedData);
100
101     //! Request removal of the identity with the given ID from the core (and all the clients, of course).
102     /** \param id The ID of the identity to be removed.
103      */
104     static void removeIdentity(IdentityId id);
105
106     static void createNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
107     static void updateNetwork(const NetworkInfo &info);
108     static void removeNetwork(NetworkId id);
109
110     static inline NetworkModel *networkModel() { return instance()->_networkModel; }
111     static inline BufferModel *bufferModel() { return instance()->_bufferModel; }
112     static inline MessageModel *messageModel() { return instance()->_messageModel; }
113     static inline AbstractMessageProcessor *messageProcessor() { return instance()->_messageProcessor; }
114     static inline SignalProxy *signalProxy() { return instance()->_signalProxy; }
115
116     static inline ClientAliasManager *aliasManager() { return instance()->_aliasManager; }
117     static inline ClientBacklogManager *backlogManager() { return instance()->_backlogManager; }
118     static inline DccConfig *dccConfig() { return instance()->_dccConfig; }
119     static inline ClientIrcListHelper *ircListHelper() { return instance()->_ircListHelper; }
120     static inline ClientBufferViewManager *bufferViewManager() { return instance()->_bufferViewManager; }
121     static inline BufferViewOverlay *bufferViewOverlay() { return instance()->_bufferViewOverlay; }
122     static inline ClientUserInputHandler *inputHandler() { return instance()->_inputHandler; }
123     static inline NetworkConfig *networkConfig() { return instance()->_networkConfig; }
124     static inline ClientIgnoreListManager *ignoreListManager() { return instance()->_ignoreListManager; }
125     static inline HighlightRuleManager *highlightRuleManager() { return instance()->_highlightRuleManager; }
126     static inline ClientTransferManager *transferManager() { return instance()->_transferManager; }
127     static inline TransferModel *transferModel() { return instance()->_transferModel; }
128
129     static inline BufferSyncer *bufferSyncer() { return instance()->_bufferSyncer; }
130
131     static inline CoreAccountModel *coreAccountModel() { return instance()->_coreAccountModel; }
132     static inline CoreConnection *coreConnection() { return instance()->_coreConnection; }
133     static inline CoreAccount currentCoreAccount() { return coreConnection()->currentAccount(); }
134     static bool isCoreFeatureEnabled(Quassel::Feature feature);
135
136     static bool isConnected();
137     static bool internalCore();
138
139     static void userInput(const BufferInfo &bufferInfo, const QString &message);
140
141     static void setBufferLastSeenMsg(BufferId id, const MsgId &msgId); // this is synced to core and other clients
142     static void setMarkerLine(BufferId id, const MsgId &msgId); // this is synced to core and other clients
143     static MsgId markerLine(BufferId id);
144
145     static void removeBuffer(BufferId id);
146     static void renameBuffer(BufferId bufferId, const QString &newName);
147     static void mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2);
148     static void purgeKnownBufferIds();
149
150     static void changePassword(const QString &oldPassword, const QString &newPassword);
151     static void kickClient(int peerId);
152
153     void displayIgnoreList(QString ignoreRule) {
154         emit showIgnoreList(ignoreRule);
155     }
156
157 #if QT_VERSION < 0x050000
158     static void logMessage(QtMsgType type, const char *msg);
159 #else
160     static void logMessage(QtMsgType, const QMessageLogContext&, const QString&);
161 #endif
162     static inline const QString &debugLog() { return instance()->_debugLogBuffer; }
163
164 signals:
165     void requestNetworkStates();
166
167     void showConfigWizard(const QVariantMap &coredata);
168     void showIgnoreList(QString ignoreRule);
169
170     void connected();
171     void disconnected();
172     void coreConnectionStateChanged(bool);
173
174     //! The identity with the given ID has been newly created in core and client.
175     /** \param id The ID of the newly created identity.
176      */
177     void identityCreated(IdentityId id);
178
179     //! The identity with the given ID has been removed.
180     /** Upon emitting this signal, the identity is already gone from the core, and it will
181      *  be deleted from the client immediately afterwards, so connected slots need to clean
182      *  up their stuff.
183      *  \param id The ID of the identity about to be removed.
184      */
185     void identityRemoved(IdentityId id);
186
187     //! Sent to the core when an identity shall be created. Should not be used elsewhere.
188     void requestCreateIdentity(const Identity &, const QVariantMap &);
189     //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
190     void requestRemoveIdentity(IdentityId);
191
192     void networkCreated(NetworkId id);
193     void networkRemoved(NetworkId id);
194
195     void requestCreateNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
196     void requestRemoveNetwork(NetworkId);
197
198     void logUpdated(const QString &msg);
199
200     //! Emitted when a buffer has been marked as read
201     /** This is currently triggered by setting lastSeenMsg, either local or remote,
202      *  or by bringing the window to front.
203      *  \param id The buffer that has been marked as read
204      */
205     void bufferMarkedAsRead(BufferId id);
206
207     //! Requests a password change (user name must match the currently logged in user)
208     void requestPasswordChange(PeerPtr peer, const QString &userName, const QString &oldPassword, const QString &newPassword);
209
210     void requestKickClient(int peerId);
211     void passwordChanged(bool success);
212
213 public slots:
214     void disconnectFromCore();
215
216     void bufferRemoved(BufferId bufferId);
217     void bufferRenamed(BufferId bufferId, const QString &newName);
218     void buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2);
219
220     void markBufferAsRead(BufferId id);
221
222 private slots:
223     void setSyncedToCore();
224     void setDisconnectedFromCore();
225     void connectionStateChanged(CoreConnection::ConnectionState);
226
227     void recvMessage(const Message &message);
228     void recvStatusMsg(QString network, QString message);
229
230     void networkDestroyed();
231     void coreIdentityCreated(const Identity &);
232     void coreIdentityRemoved(IdentityId);
233     void coreNetworkCreated(NetworkId);
234     void coreNetworkRemoved(NetworkId);
235
236     void corePasswordChanged(PeerPtr, bool success);
237
238     void finishConnectionInitialization();
239
240     void sendBufferedUserInput();
241
242 private:
243     Client(QObject *parent = 0);
244     virtual ~Client();
245     void init();
246
247     void requestInitialBacklog();
248
249     static void addNetwork(Network *);
250
251     static QPointer<Client> instanceptr;
252
253     SignalProxy *_signalProxy;
254     AbstractUi *_mainUi;
255     NetworkModel *_networkModel;
256     BufferModel *_bufferModel;
257     BufferSyncer *_bufferSyncer;
258     ClientAliasManager *_aliasManager;
259     ClientBacklogManager *_backlogManager;
260     ClientBufferViewManager *_bufferViewManager;
261     BufferViewOverlay *_bufferViewOverlay;
262     DccConfig *_dccConfig;
263     ClientIrcListHelper *_ircListHelper;
264     ClientUserInputHandler *_inputHandler;
265     NetworkConfig *_networkConfig;
266     ClientIgnoreListManager *_ignoreListManager;
267     HighlightRuleManager *_highlightRuleManager;
268     ClientTransferManager *_transferManager;
269     TransferModel *_transferModel;
270
271     MessageModel *_messageModel;
272     AbstractMessageProcessor *_messageProcessor;
273
274     CoreAccountModel *_coreAccountModel;
275     CoreConnection *_coreConnection;
276
277     ClientMode clientMode;
278
279     QHash<NetworkId, Network *> _networks;
280     QHash<IdentityId, Identity *> _identities;
281
282     bool _connected;
283
284     QString _debugLogBuffer;
285     QTextStream _debugLog;
286
287     QList<QPair<BufferInfo, QString> > _userInputBuffer;
288
289     friend class CoreConnection;
290 };