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