c3172a7f9e446d7f90b32f4fc51026b27dd82a0a
[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     void displayChannelList(NetworkId networkId) {
165         emit showChannelList(networkId);
166     }
167
168 signals:
169     void requestNetworkStates();
170
171     void showConfigWizard(const QVariantMap &coredata);
172     void showChannelList(NetworkId networkId);
173     void showIgnoreList(QString ignoreRule);
174
175     void connected();
176     void disconnected();
177     void coreConnectionStateChanged(bool);
178
179     //! The identity with the given ID has been newly created in core and client.
180     /** \param id The ID of the newly created identity.
181      */
182     void identityCreated(IdentityId id);
183
184     //! The identity with the given ID has been removed.
185     /** Upon emitting this signal, the identity is already gone from the core, and it will
186      *  be deleted from the client immediately afterwards, so connected slots need to clean
187      *  up their stuff.
188      *  \param id The ID of the identity about to be removed.
189      */
190     void identityRemoved(IdentityId id);
191
192     //! Sent to the core when an identity shall be created. Should not be used elsewhere.
193     void requestCreateIdentity(const Identity &, const QVariantMap &);
194     //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
195     void requestRemoveIdentity(IdentityId);
196
197     void networkCreated(NetworkId id);
198     void networkRemoved(NetworkId id);
199
200     void requestCreateNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
201     void requestRemoveNetwork(NetworkId);
202
203     void logUpdated(const QString &msg);
204
205     //! Emitted when a buffer has been marked as read
206     /** This is currently triggered by setting lastSeenMsg, either local or remote,
207      *  or by bringing the window to front.
208      *  \param id The buffer that has been marked as read
209      */
210     void bufferMarkedAsRead(BufferId id);
211
212     //! Requests a password change (user name must match the currently logged in user)
213     void requestPasswordChange(PeerPtr peer, const QString &userName, const QString &oldPassword, const QString &newPassword);
214
215     void requestKickClient(int peerId);
216     void passwordChanged(bool success);
217
218 public slots:
219     void disconnectFromCore();
220
221     void bufferRemoved(BufferId bufferId);
222     void bufferRenamed(BufferId bufferId, const QString &newName);
223     void buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2);
224
225     void markBufferAsRead(BufferId id);
226
227 private slots:
228     void setSyncedToCore();
229     void setDisconnectedFromCore();
230     void connectionStateChanged(CoreConnection::ConnectionState);
231
232     void recvMessage(const Message &message);
233     void recvStatusMsg(QString network, QString message);
234
235     void networkDestroyed();
236     void coreIdentityCreated(const Identity &);
237     void coreIdentityRemoved(IdentityId);
238     void coreNetworkCreated(NetworkId);
239     void coreNetworkRemoved(NetworkId);
240
241     void corePasswordChanged(PeerPtr, bool success);
242
243     void finishConnectionInitialization();
244
245     void sendBufferedUserInput();
246
247 private:
248     Client(QObject *parent = 0);
249     virtual ~Client();
250     void init();
251
252     void requestInitialBacklog();
253
254     static void addNetwork(Network *);
255
256     static QPointer<Client> instanceptr;
257
258     SignalProxy *_signalProxy;
259     AbstractUi *_mainUi;
260     NetworkModel *_networkModel;
261     BufferModel *_bufferModel;
262     BufferSyncer *_bufferSyncer;
263     ClientAliasManager *_aliasManager;
264     ClientBacklogManager *_backlogManager;
265     ClientBufferViewManager *_bufferViewManager;
266     BufferViewOverlay *_bufferViewOverlay;
267     DccConfig *_dccConfig;
268     ClientIrcListHelper *_ircListHelper;
269     ClientUserInputHandler *_inputHandler;
270     NetworkConfig *_networkConfig;
271     ClientIgnoreListManager *_ignoreListManager;
272     HighlightRuleManager *_highlightRuleManager;
273     ClientTransferManager *_transferManager;
274     TransferModel *_transferModel;
275
276     MessageModel *_messageModel;
277     AbstractMessageProcessor *_messageProcessor;
278
279     CoreAccountModel *_coreAccountModel;
280     CoreConnection *_coreConnection;
281
282     ClientMode clientMode;
283
284     QHash<NetworkId, Network *> _networks;
285     QHash<IdentityId, Identity *> _identities;
286
287     bool _connected;
288
289     QString _debugLogBuffer;
290     QTextStream _debugLog;
291
292     QList<QPair<BufferInfo, QString> > _userInputBuffer;
293
294     friend class CoreConnection;
295 };