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