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