src: Yearly copyright bump
[quassel.git] / src / client / client.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 <memory>
24
25 #include <QList>
26 #include <QPointer>
27
28 #include "bufferinfo.h"
29 #include "coreinfo.h"
30 #include "coreaccount.h"
31 #include "coreconnection.h"
32 #include "highlightrulemanager.h"
33 #include "quassel.h"
34 #include "singleton.h"
35 #include "types.h"
36
37 class Message;
38 class MessageModel;
39 class AbstractMessageProcessor;
40
41 class Identity;
42 class CertIdentity;
43 class Network;
44
45 class AbstractUi;
46 class AbstractUiMsg;
47 class NetworkModel;
48 class BufferModel;
49 class BufferSyncer;
50 class BufferViewOverlay;
51 class ClientAliasManager;
52 class ClientBacklogManager;
53 class ClientBufferViewManager;
54 class ClientIgnoreListManager;
55 class ClientIrcListHelper;
56 class ClientTransferManager;
57 class ClientUserInputHandler;
58 class CoreAccountModel;
59 class CoreConnection;
60 class DccConfig;
61 class IrcUser;
62 class IrcChannel;
63 class NetworkConfig;
64 class SignalProxy;
65 class TransferModel;
66
67 struct NetworkInfo;
68
69 class Client : public QObject, public Singleton<Client>
70 {
71     Q_OBJECT
72
73 public:
74     enum ClientMode {
75         LocalCore,
76         RemoteCore
77     };
78
79     Client(std::unique_ptr<AbstractUi>, QObject *parent = nullptr);
80     ~Client() override;
81
82     static AbstractUi *mainUi();
83
84     static QList<NetworkId> networkIds();
85     static const Network *network(NetworkId);
86
87     static QList<IdentityId> identityIds();
88     static const Identity *identity(IdentityId);
89
90     //! Request creation of an identity with the given data.
91     /** The request will be sent to the core, and will be propagated back to all the clients
92      *  with a new valid IdentityId.
93      *  \param identity The identity template for the new identity. It does not need to have a valid ID.
94      */
95     static void createIdentity(const CertIdentity &identity);
96
97     //! Request update of an identity with the given data.
98     /** The request will be sent to the core, and will be propagated back to all the clients.
99      *  \param id The identity to be updated.
100      *  \param serializedData The identity's content (cf. SyncableObject::toVariantMap())
101      */
102     static void updateIdentity(IdentityId id, const QVariantMap &serializedData);
103
104     //! Request removal of the identity with the given ID from the core (and all the clients, of course).
105     /** \param id The ID of the identity to be removed.
106      */
107     static void removeIdentity(IdentityId id);
108
109     static void createNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
110     static void updateNetwork(const NetworkInfo &info);
111     static void removeNetwork(NetworkId id);
112
113     static inline NetworkModel *networkModel() { return instance()->_networkModel; }
114     static inline BufferModel *bufferModel() { return instance()->_bufferModel; }
115     static inline MessageModel *messageModel() { return instance()->_messageModel; }
116     static inline AbstractMessageProcessor *messageProcessor() { return instance()->_messageProcessor; }
117     static inline SignalProxy *signalProxy() { return instance()->_signalProxy; }
118
119     static inline ClientAliasManager *aliasManager() { return instance()->_aliasManager; }
120     static inline ClientBacklogManager *backlogManager() { return instance()->_backlogManager; }
121     static inline CoreInfo *coreInfo() { return instance()->_coreInfo; }
122     static inline DccConfig *dccConfig() { return instance()->_dccConfig; }
123     static inline ClientIrcListHelper *ircListHelper() { return instance()->_ircListHelper; }
124     static inline ClientBufferViewManager *bufferViewManager() { return instance()->_bufferViewManager; }
125     static inline BufferViewOverlay *bufferViewOverlay() { return instance()->_bufferViewOverlay; }
126     static inline ClientUserInputHandler *inputHandler() { return instance()->_inputHandler; }
127     static inline NetworkConfig *networkConfig() { return instance()->_networkConfig; }
128     static inline ClientIgnoreListManager *ignoreListManager() { return instance()->_ignoreListManager; }
129     static inline HighlightRuleManager *highlightRuleManager() { return instance()->_highlightRuleManager; }
130     static inline ClientTransferManager *transferManager() { return instance()->_transferManager; }
131     static inline TransferModel *transferModel() { return instance()->_transferModel; }
132
133     static inline BufferSyncer *bufferSyncer() { return instance()->_bufferSyncer; }
134
135     static inline CoreAccountModel *coreAccountModel() { return instance()->_coreAccountModel; }
136     static inline CoreConnection *coreConnection() { return instance()->_coreConnection; }
137     static inline CoreAccount currentCoreAccount() { return coreConnection()->currentAccount(); }
138     static bool isCoreFeatureEnabled(Quassel::Feature feature);
139
140     static bool isConnected();
141     static bool internalCore();
142
143     static void userInput(const BufferInfo &bufferInfo, const QString &message);
144
145     static void setBufferLastSeenMsg(BufferId id, const MsgId &msgId); // this is synced to core and other clients
146     static void setMarkerLine(BufferId id, const MsgId &msgId); // this is synced to core and other clients
147     static MsgId markerLine(BufferId id);
148
149     static void removeBuffer(BufferId id);
150     static void renameBuffer(BufferId bufferId, const QString &newName);
151     static void mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2);
152     static void purgeKnownBufferIds();
153
154     /**
155      * Requests client to resynchronize the CoreInfo object for legacy (pre-0.13) cores
156      *
157      * This provides compatibility with updating core information for legacy cores, and can be
158      * removed after protocol break.
159      *
160      * NOTE: On legacy (pre-0.13) cores, any existing connected signals will be destroyed and must
161      * be re-added after calling this, in addition to checking for existing data in coreInfo().
162      */
163     static void refreshLegacyCoreInfo();
164
165     static void changePassword(const QString &oldPassword, const QString &newPassword);
166     static void kickClient(int peerId);
167
168     void displayIgnoreList(QString ignoreRule) {
169         emit showIgnoreList(ignoreRule);
170     }
171
172     /**
173      * Request to show the channel list dialog for the network, optionally searching by channel name
174      *
175      * @see Client::showChannelList()
176      *
177      * @param networkId        Network ID for associated network
178      * @param channelFilters   Partial channel name to search for, or empty to show all
179      * @param listImmediately  If true, immediately list channels, otherwise just show dialog
180      */
181     void displayChannelList(NetworkId networkId, const QString &channelFilters = {},
182                             bool listImmediately = false)
183     {
184         emit showChannelList(networkId, channelFilters, listImmediately);
185     }
186
187 signals:
188     void requestNetworkStates();
189
190     void showConfigWizard(const QVariantMap &coredata);
191
192     /**
193      * Request to show the channel list dialog for the network, optionally searching by channel name
194      *
195      * @see MainWin::showChannelList()
196      *
197      * @param networkId        Network ID for associated network
198      * @param channelFilters   Partial channel name to search for, or empty to show all
199      * @param listImmediately  If true, immediately list channels, otherwise just show dialog
200      */
201     void showChannelList(NetworkId networkId, const QString &channelFilters = {},
202                          bool listImmediately = false);
203
204     void showIgnoreList(QString ignoreRule);
205
206     void connected();
207     void disconnected();
208     void coreConnectionStateChanged(bool);
209
210     /**
211      * Signals that core information has been resynchronized, removing existing signal handlers
212      *
213      * Whenever this is emitted, one should re-add any handlers for CoreInfo::coreDataChanged() and
214      * apply any existing information in the coreInfo() object.
215      *
216      * Only emitted on legacy (pre-0.13) cores.  Generally, one should use the
217      * CoreInfo::coreDataChanged() signal too.
218      */
219     void coreInfoResynchronized();
220
221     //! The identity with the given ID has been newly created in core and client.
222     /** \param id The ID of the newly created identity.
223      */
224     void identityCreated(IdentityId id);
225
226     //! The identity with the given ID has been removed.
227     /** Upon emitting this signal, the identity is already gone from the core, and it will
228      *  be deleted from the client immediately afterwards, so connected slots need to clean
229      *  up their stuff.
230      *  \param id The ID of the identity about to be removed.
231      */
232     void identityRemoved(IdentityId id);
233
234     //! Sent to the core when an identity shall be created. Should not be used elsewhere.
235     void requestCreateIdentity(const Identity &, const QVariantMap &);
236     //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
237     void requestRemoveIdentity(IdentityId);
238
239     void networkCreated(NetworkId id);
240     void networkRemoved(NetworkId id);
241
242     void requestCreateNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
243     void requestRemoveNetwork(NetworkId);
244
245     //! Emitted when a buffer has been marked as read
246     /** This is currently triggered by setting lastSeenMsg, either local or remote,
247      *  or by bringing the window to front.
248      *  \param id The buffer that has been marked as read
249      */
250     void bufferMarkedAsRead(BufferId id);
251
252     //! Requests a password change (user name must match the currently logged in user)
253     void requestPasswordChange(PeerPtr peer, const QString &userName, const QString &oldPassword, const QString &newPassword);
254
255     void requestKickClient(int peerId);
256     void passwordChanged(bool success);
257
258     //! Emitted when database schema upgrade starts or ends (only mono client)
259     void dbUpgradeInProgress(bool inProgress);
260
261     //! Emitted before an exit request is handled
262     void exitRequested(const QString &reason);
263
264 public slots:
265     void disconnectFromCore();
266
267     void bufferRemoved(BufferId bufferId);
268     void bufferRenamed(BufferId bufferId, const QString &newName);
269     void buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2);
270
271     void markBufferAsRead(BufferId id);
272
273     void onDbUpgradeInProgress(bool inProgress);
274     void onExitRequested(int exitCode, const QString &reason);
275
276 private slots:
277     void setSyncedToCore();
278     void setDisconnectedFromCore();
279     void connectionStateChanged(CoreConnection::ConnectionState);
280
281     void recvMessage(const Message &message);
282     void recvStatusMsg(QString network, QString message);
283
284     void networkDestroyed();
285     void coreIdentityCreated(const Identity &);
286     void coreIdentityRemoved(IdentityId);
287     void coreNetworkCreated(NetworkId);
288     void coreNetworkRemoved(NetworkId);
289
290     void corePasswordChanged(PeerPtr, bool success);
291
292     void finishConnectionInitialization();
293
294     void sendBufferedUserInput();
295
296 private:
297     void requestInitialBacklog();
298
299     /**
300      * Deletes and resynchronizes the CoreInfo object for legacy (pre-0.13) cores
301      *
302      * This provides compatibility with updating core information for legacy cores, and can be
303      * removed after protocol break.
304      *
305      * NOTE: On legacy (pre-0.13) cores, any existing connected signals will be destroyed and must
306      * be re-added after calling this, in addition to checking for existing data in coreInfo().
307      */
308     void requestLegacyCoreInfo();
309
310     static void addNetwork(Network *);
311
312     SignalProxy *_signalProxy;
313     std::unique_ptr<AbstractUi> _mainUi;
314     NetworkModel *_networkModel;
315     BufferModel *_bufferModel;
316     BufferSyncer *_bufferSyncer;
317     ClientAliasManager *_aliasManager;
318     ClientBacklogManager *_backlogManager;
319     ClientBufferViewManager *_bufferViewManager;
320     BufferViewOverlay *_bufferViewOverlay;
321     CoreInfo *_coreInfo;
322     DccConfig *_dccConfig;
323     ClientIrcListHelper *_ircListHelper;
324     ClientUserInputHandler *_inputHandler;
325     NetworkConfig *_networkConfig;
326     ClientIgnoreListManager *_ignoreListManager;
327     HighlightRuleManager *_highlightRuleManager;
328     ClientTransferManager *_transferManager;
329     TransferModel *_transferModel;
330
331     MessageModel *_messageModel;
332     AbstractMessageProcessor *_messageProcessor;
333
334     CoreAccountModel *_coreAccountModel;
335     CoreConnection *_coreConnection;
336
337     ClientMode clientMode;
338
339     QHash<NetworkId, Network *> _networks;
340     QHash<IdentityId, Identity *> _identities;
341
342     bool _connected;
343
344     QList<QPair<BufferInfo, QString> > _userInputBuffer;
345
346     friend class CoreConnection;
347 };