fixing auto expand issues with new networks
[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 <QAbstractSocket>
25 #include <QTcpSocket>
26 #include <QList>
27 #include <QPointer>
28
29 #include "bufferinfo.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 ClientBacklogManager;
46 class ClientIrcListHelper;
47 class ClientSyncer;
48 class ClientBufferViewManager;
49 class IrcUser;
50 class IrcChannel;
51 class SignalProxy;
52 struct NetworkInfo;
53
54 class Client : public QObject {
55   Q_OBJECT
56
57 public:
58   enum ClientMode {
59     LocalCore,
60     RemoteCore
61   };
62
63   static Client *instance();
64   static void destroy();
65   static void init(AbstractUi *);
66   static AbstractUi *mainUi();
67
68   static QList<NetworkId> networkIds();
69   static const Network * network(NetworkId);
70
71   static QList<IdentityId> identityIds();
72   static const Identity *identity(IdentityId);
73
74   //! Request creation of an identity with the given data.
75   /** The request will be sent to the core, and will be propagated back to all the clients
76    *  with a new valid IdentityId.
77    *  \param identity The identity template for the new identity. It does not need to have a valid ID.
78    */
79   static void createIdentity(const CertIdentity &identity);
80
81   //! Request update 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    *  \param id The identity to be updated.
84    *  \param serializedData The identity's content (cf. SyncableObject::toVariantMap())
85    */
86   static void updateIdentity(IdentityId id, const QVariantMap &serializedData);
87
88   //! Request removal of the identity with the given ID from the core (and all the clients, of course).
89   /** \param id The ID of the identity to be removed.
90    */
91   static void removeIdentity(IdentityId id);
92
93   static void createNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
94   static void updateNetwork(const NetworkInfo &info);
95   static void removeNetwork(NetworkId id);
96
97   static inline NetworkModel *networkModel() { return instance()->_networkModel; }
98   static inline BufferModel *bufferModel() { return instance()->_bufferModel; }
99   static inline MessageModel *messageModel() { return instance()->_messageModel; }
100   static inline AbstractMessageProcessor *messageProcessor() { return instance()->_messageProcessor; }
101   static inline SignalProxy *signalProxy() { return instance()->_signalProxy; }
102
103   static inline ClientBacklogManager *backlogManager() { return instance()->_backlogManager; }
104   static inline ClientIrcListHelper *ircListHelper() { return instance()->_ircListHelper; }
105   static inline ClientBufferViewManager *bufferViewManager() { return instance()->_bufferViewManager; }
106
107   static AccountId currentCoreAccount();
108
109   static bool isConnected();
110   static bool isSynced();
111   static inline bool internalCore() { return instance()->_internalCore; }
112
113   static void userInput(BufferInfo bufferInfo, QString message);
114
115   static void setBufferLastSeenMsg(BufferId id, const MsgId &msgId); // this is synced to core and other clients
116   static void removeBuffer(BufferId id);
117   static void renameBuffer(BufferId bufferId, const QString &newName);
118   static void mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2);
119
120   static void logMessage(QtMsgType type, const char *msg);
121   static inline const QString &debugLog() { return instance()->_debugLogBuffer; }
122
123   static inline void registerClientSyncer(ClientSyncer *syncer) { emit instance()->newClientSyncer(syncer); }
124
125 signals:
126   void sendInput(BufferInfo, QString message);
127   void requestNetworkStates();
128
129   void showConfigWizard(const QVariantMap &coredata);
130
131   void connected();
132   void disconnected();
133   void coreConnectionStateChanged(bool);
134
135   //! The identity with the given ID has been newly created in core and client.
136   /** \param id The ID of the newly created identity.
137    */
138   void identityCreated(IdentityId id);
139
140   //! The identity with the given ID has been removed.
141   /** Upon emitting this signal, the identity is already gone from the core, and it will
142    *  be deleted from the client immediately afterwards, so connected slots need to clean
143    *  up their stuff.
144    *  \param id The ID of the identity about to be removed.
145    */
146   void identityRemoved(IdentityId id);
147
148   //! Sent to the core when an identity shall be created. Should not be used elsewhere.
149   void requestCreateIdentity(const Identity &, const QVariantMap &);
150   //! Sent to the core when an identity shall be removed. Should not be used elsewhere.
151   void requestRemoveIdentity(IdentityId);
152
153   void networkCreated(NetworkId id);
154   void networkRemoved(NetworkId id);
155
156   void requestCreateNetwork(const NetworkInfo &info, const QStringList &persistentChannels = QStringList());
157   void requestRemoveNetwork(NetworkId);
158
159   void newClientSyncer(ClientSyncer *);
160
161   void logUpdated(const QString &msg);
162
163 public slots:
164   //void selectBuffer(Buffer *);
165
166   void disconnectFromCore();
167
168   void bufferRemoved(BufferId bufferId);
169   void bufferRenamed(BufferId bufferId, const QString &newName);
170   void buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2);
171
172 private slots:
173   void disconnectedFromCore();
174
175   void recvMessage(const Message &message);
176   void recvStatusMsg(QString network, QString message);
177
178   void networkDestroyed();
179   void coreIdentityCreated(const Identity &);
180   void coreIdentityRemoved(IdentityId);
181   void coreNetworkCreated(NetworkId);
182   void coreNetworkRemoved(NetworkId);
183
184   void setConnectedToCore(AccountId id, QIODevice *socket = 0);
185   void setSyncedToCore();
186   void requestInitialBacklog();
187   void createDefaultBufferView();
188
189 private:
190   Client(QObject *parent = 0);
191   virtual ~Client();
192   void init();
193
194   static void addNetwork(Network *);
195   static void setCurrentCoreAccount(AccountId);
196   static inline BufferSyncer *bufferSyncer() { return instance()->_bufferSyncer; }
197
198   static QPointer<Client> instanceptr;
199
200   SignalProxy * _signalProxy;
201   AbstractUi * _mainUi;
202   NetworkModel * _networkModel;
203   BufferModel * _bufferModel;
204   BufferSyncer * _bufferSyncer;
205   ClientBacklogManager *_backlogManager;
206   ClientBufferViewManager *_bufferViewManager;
207   ClientIrcListHelper *_ircListHelper;
208
209   MessageModel *_messageModel;
210   AbstractMessageProcessor *_messageProcessor;
211
212   ClientMode clientMode;
213
214   bool _connectedToCore, _syncedToCore;
215   bool _internalCore;
216
217   QHash<NetworkId, Network *> _networks;
218   QHash<IdentityId, Identity *> _identities;
219
220   static AccountId _currentCoreAccount;
221
222   QString _debugLogBuffer;
223   QTextStream _debugLog;
224
225   friend class ClientSyncer;
226 };
227
228 #endif