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