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