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