clean++
[quassel.git] / src / client / client.h
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel IRC Development Team             *
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) any later version.                                   *
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 "buffer.h"
30 #include "message.h"
31
32 class AbstractUi;
33 class BufferTreeModel;
34 class QtGui;
35 class SignalProxy;
36
37 class QTimer;
38
39 class Client : public QObject {
40   Q_OBJECT
41
42   public:
43     static Client *instance();
44     static void init(AbstractUi *);
45     static void destroy();
46
47     static QList<BufferId> allBufferIds();
48     static Buffer *buffer(BufferId);
49     static BufferId statusBufferId(QString net);
50     static BufferId bufferId(QString net, QString buf);
51
52     static BufferTreeModel *bufferModel();
53     static SignalProxy *signalProxy();
54
55     static AbstractUiMsg *layoutMsg(const Message &);
56
57     static bool isConnected();
58
59     static void storeSessionData(const QString &key, const QVariant &data);
60     static QVariant retrieveSessionData(const QString &key, const QVariant &def = QVariant());
61     static QStringList sessionDataKeys();
62
63     enum ClientMode { LocalCore, RemoteCore };
64     static ClientMode clientMode;
65
66   signals:
67     void sendInput(BufferId, QString message);
68     void showBuffer(Buffer *);
69     void bufferSelected(Buffer *);
70     void bufferUpdated(Buffer *);
71     void bufferActivity(Buffer::ActivityLevel, Buffer *);
72     void bufferDestroyed(Buffer *);
73     void backlogReceived(Buffer *, QList<Message>);
74     void requestBacklog(BufferId, QVariant, QVariant);
75     void requestNetworkStates();
76
77     void recvPartialItem(uint avail, uint size);
78     void coreConnectionError(QString errorMsg);
79     void coreConnectionMsg(const QString &msg);
80     void coreConnectionProgress(uint part, uint total);
81
82     void connected();
83     void disconnected();
84
85     void sessionDataChanged(const QString &key);
86     void sessionDataChanged(const QString &key, const QVariant &data);
87     void sendSessionData(const QString &key, const QVariant &data);
88
89   public slots:
90     //void selectBuffer(Buffer *);
91     //void connectToLocalCore();
92     void connectToCore(const QVariantMap &);
93     void disconnectFromCore();
94
95   private slots:
96     void recvCoreState(const QVariant &state);
97     void recvSessionData(const QString &key, const QVariant &data);
98
99     void coreSocketError(QAbstractSocket::SocketError);
100     void coreHasData();
101     void coreSocketConnected();
102     void coreSocketDisconnected();
103     void coreSocketStateChanged(QAbstractSocket::SocketState);
104
105     void userInput(BufferId, QString);
106     void networkConnected(QString);
107     void networkDisconnected(QString);
108     void recvNetworkState(QString, QVariant);
109     void recvMessage(const Message &message);
110     void recvStatusMsg(QString network, QString message);
111     void setTopic(QString net, QString buf, QString);
112     void addNick(QString net, QString nick, QVariantMap props);
113     void removeNick(QString net, QString nick);
114     void renameNick(QString net, QString oldnick, QString newnick);
115     void updateNick(QString net, QString nick, QVariantMap props);
116     void setOwnNick(QString net, QString nick);
117     void recvBacklogData(BufferId, QVariantList, bool);
118     void updateBufferId(BufferId);
119
120     void removeBuffer(Buffer *);
121
122     void layoutMsg();
123
124   private:
125     Client();
126     ~Client();
127     void init();
128     static Client *instanceptr;
129
130     void syncToCore(const QVariant &coreState);
131     QVariant connectToLocalCore(QString user, QString passwd);  // defined in main.cpp
132     void disconnectFromLocalCore();                             // defined in main.cpp
133
134     AbstractUi *mainUi;
135     SignalProxy *_signalProxy;
136     BufferTreeModel *_bufferModel;
137
138     QPointer<QIODevice> socket;
139     quint32 blockSize;
140
141     static bool connectedToCore;
142     static QVariantMap coreConnectionInfo;
143     static QHash<BufferId, Buffer *> buffers;
144     static QHash<uint, BufferId> bufferIds;
145     static QHash<QString, QHash<QString, QVariantMap> > nicks;
146     static QHash<QString, bool> netConnected;
147     static QStringList netsAwaitingInit;
148     static QHash<QString, QString> ownNick;
149
150     QTimer *layoutTimer;
151     QList<Buffer *> layoutQueue;
152
153     QVariantMap sessionData;
154 };
155
156 #endif