1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "clientsyncer.h"
27 #include "ircchannel.h"
29 #include "signalproxy.h"
32 ClientSyncer::ClientSyncer(QObject *parent) : QObject(parent) {
36 connect(Client::signalProxy(), SIGNAL(disconnected()), this, SLOT(coreSocketDisconnected()));
40 ClientSyncer::~ClientSyncer() {
46 void ClientSyncer::coreHasData() {
48 while(SignalProxy::readDataFromDevice(socket, blockSize, item)) {
49 emit recvPartialItem(1,1);
50 QVariantMap msg = item.toMap();
51 if(!msg.contains("MsgType")) {
52 // This core is way too old and does not even speak our init protocol...
53 emit connectionError(tr("The Quassel Core you try to connect to is too old! Please consider upgrading."));
57 if(msg["MsgType"] == "ClientInitAck") {
59 } else if(msg["MsgType"] == "ClientInitReject") {
60 emit connectionError(msg["Error"].toString());
63 } else if(msg["MsgType"] == "ClientLoginReject") {
64 emit loginFailed(msg["Error"].toString());
65 } else if(msg["MsgType"] == "ClientLoginAck") {
66 // prevent multiple signal connections
67 disconnect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
68 connect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
70 } else if(msg["MsgType"] == "SessionInit") {
71 sessionStateReceived(msg["SessionState"].toMap());
73 emit connectionError(tr("<b>Invalid data received from core!</b><br>Disconnecting."));
78 if (!msg["StartWizard"].toBool()) {
79 recvCoreState(msg["Reply"]);
81 qWarning("Core not configured!");
82 qDebug() << "Available storage providers: " << msg["StorageProviders"].toStringList();
83 emit showConfigWizard(msg);
91 emit recvPartialItem(socket->bytesAvailable(), blockSize);
95 void ClientSyncer::coreSocketError(QAbstractSocket::SocketError) {
96 emit connectionError(socket->errorString());
97 socket->deleteLater();
100 void ClientSyncer::disconnectFromCore() {
101 if(socket) socket->close();
104 void ClientSyncer::connectToCore(const QVariantMap &conn) {
105 // TODO implement SSL
106 coreConnectionInfo = conn;
107 //if(isConnected()) {
108 // emit coreConnectionError(tr("Already connected to Core!"));
113 socket->deleteLater();
116 if(conn["Host"].toString().isEmpty()) {
117 emit connectionError(tr("Internal connections not yet supported."));
118 return; // FIXME implement internal connections
119 //clientMode = LocalCore;
120 socket = new QBuffer(this);
121 connect(socket, SIGNAL(readyRead()), this, SLOT(coreHasData()));
122 socket->open(QIODevice::ReadWrite);
123 //QVariant state = connectToLocalCore(coreConnectionInfo["User"].toString(), coreConnectionInfo["Password"].toString());
125 coreSocketConnected();
127 //clientMode = RemoteCore;
128 //emit coreConnectionMsg(tr("Connecting..."));
130 QTcpSocket *sock = new QTcpSocket(Client::instance());
132 connect(sock, SIGNAL(readyRead()), this, SLOT(coreHasData()));
133 connect(sock, SIGNAL(connected()), this, SLOT(coreSocketConnected()));
134 connect(sock, SIGNAL(disconnected()), this, SLOT(coreSocketDisconnected()));
135 connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(coreSocketError(QAbstractSocket::SocketError)));
136 connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)));
137 sock->connectToHost(conn["Host"].toString(), conn["Port"].toUInt());
141 void ClientSyncer::coreSocketConnected() {
142 //connect(this, SIGNAL(recvPartialItem(uint, uint)), this, SIGNAL(coreConnectionProgress(uint, uint)));
143 // Phase One: Send client info and wait for core info
145 //emit coreConnectionMsg(tr("Synchronizing to core..."));
146 QVariantMap clientInit;
147 clientInit["MsgType"] = "ClientInit";
148 clientInit["ClientVersion"] = Global::quasselVersion;
149 clientInit["ClientDate"] = Global::quasselDate;
150 clientInit["ClientBuild"] = Global::quasselBuild; // this is a minimum, since we probably won't update for every commit
151 clientInit["UseSsl"] = false; // FIXME implement SSL
152 SignalProxy::writeDataToDevice(socket, clientInit);
155 void ClientSyncer::coreSocketDisconnected() {
156 emit socketDisconnected();
157 Client::instance()->disconnectFromCore();
159 // FIXME handle disconnects gracefully in here as well!
161 coreConnectionInfo.clear();
163 channelsToSync.clear();
166 //restartPhaseNull();
169 void ClientSyncer::clientInitAck(const QVariantMap &msg) {
170 // Core has accepted our version info and sent its own. Let's see if we accept it as well...
171 if(msg["CoreBuild"].toUInt() < Global::coreBuildNeeded) {
172 emit connectionError(tr("<b>The Quassel Core you are trying to connect to is too old!</b><br>"
173 "Need at least a Core Version %1 (Build >= %2) to connect.").arg(Global::quasselVersion).arg(Global::quasselBuild));
174 disconnectFromCore();
177 emit connectionMsg(msg["CoreInfo"].toString());
178 if(msg["LoginEnabled"].toBool()) {
183 void ClientSyncer::loginToCore(const QString &user, const QString &passwd) {
184 emit connectionMsg(tr("Logging in..."));
185 QVariantMap clientLogin;
186 clientLogin["MsgType"] = "ClientLogin";
187 clientLogin["User"] = user;
188 clientLogin["Password"] = passwd;
189 SignalProxy::writeDataToDevice(socket, clientLogin);
192 void ClientSyncer::sessionStateReceived(const QVariantMap &state) {
193 emit sessionProgress(1, 1);
194 disconnect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
195 disconnect(socket, 0, this, 0); // rest of communication happens through SignalProxy
196 //Client::signalProxy()->addPeer(socket);
197 Client::instance()->setConnectedToCore(socket);
201 void ClientSyncer::syncToCore(const QVariantMap &sessionState) {
204 QVariantMap sessData = sessionState["SessionData"].toMap();
205 foreach(QString key, sessData.keys()) Client::instance()->recvSessionData(key, sessData[key]);
208 foreach(QVariant vid, sessionState["Identities"].toList()) {
209 Client::instance()->coreIdentityCreated(vid.value<Identity>());
213 // FIXME: get rid of this crap
214 QVariantList bufferinfos = sessionState["BufferInfos"].toList();
215 foreach(QVariant vinfo, bufferinfos) Client::buffer(vinfo.value<BufferInfo>()); // create Buffers and BufferItems
217 QVariantList networkids = sessionState["NetworkIds"].toList();
219 // prepare sync progress thingys... FIXME: Care about removal of networks
220 numNetsToSync = networkids.count();
221 numChannelsToSync = 0; //sessionState["IrcChannelCount"].toUInt();
222 numUsersToSync = 0; // sessionState["IrcUserCount"].toUInt(); qDebug() << numUsersToSync;
223 emit networksProgress(0, numNetsToSync);
224 emit channelsProgress(0, numChannelsToSync);
225 emit ircUsersProgress(0, numUsersToSync);
227 // create network objects
228 foreach(QVariant networkid, networkids) {
229 NetworkId netid = networkid.value<NetworkId>();
230 Network *net = new Network(netid, Client::instance());
231 netsToSync.insert(net);
232 connect(net, SIGNAL(initDone()), this, SLOT(networkInitDone()));
233 connect(net, SIGNAL(ircUserInitDone(IrcUser *)), this, SLOT(ircUserInitDone(IrcUser *)));
234 connect(net, SIGNAL(ircUserAdded(IrcUser *)), this, SLOT(ircUserAdded(IrcUser *)));
235 connect(net, SIGNAL(ircUserRemoved(QObject *)), this, SLOT(ircUserRemoved(QObject *)));
236 connect(net, SIGNAL(ircChannelInitDone(IrcChannel *)), this, SLOT(ircChannelInitDone(IrcChannel *)));
237 connect(net, SIGNAL(ircChannelAdded(IrcChannel *)), this, SLOT(ircChannelAdded(IrcChannel *)));
238 connect(net, SIGNAL(ircChannelRemoved(QObject *)), this, SLOT(ircChannelRemoved(QObject *)));
239 Client::addNetwork(net);
244 void ClientSyncer::networkInitDone() {
245 netsToSync.remove(sender());
246 emit networksProgress(numNetsToSync - netsToSync.count(), numNetsToSync);
250 void ClientSyncer::ircChannelInitDone(IrcChannel *chan) {
251 channelsToSync.remove(chan);
252 emit channelsProgress(numChannelsToSync - channelsToSync.count(), numChannelsToSync);
256 void ClientSyncer::ircChannelAdded(IrcChannel *chan) {
257 if(!chan->isInitialized()) {
258 channelsToSync.insert(chan);
260 emit channelsProgress(numChannelsToSync - channelsToSync.count(), numChannelsToSync);
265 void ClientSyncer::ircChannelRemoved(QObject *chan) {
266 if(channelsToSync.contains(chan)) {
268 channelsToSync.remove(chan);
269 emit channelsProgress(numChannelsToSync - channelsToSync.count(), numChannelsToSync);
274 void ClientSyncer::ircUserInitDone(IrcUser *user) {
275 usersToSync.remove(user);
276 emit ircUsersProgress(numUsersToSync - usersToSync.count(), numUsersToSync);
280 void ClientSyncer::ircUserAdded(IrcUser *user) {
281 if(!user->isInitialized()) {
282 usersToSync.insert(user);
284 emit ircUsersProgress(numUsersToSync - usersToSync.count(), numUsersToSync);
289 void ClientSyncer::ircUserRemoved(QObject *user) {
290 if(usersToSync.contains(user)) {
292 usersToSync.remove(user);
293 emit ircUsersProgress(numUsersToSync - usersToSync.count(), numUsersToSync);
298 void ClientSyncer::checkSyncState() {
299 if(usersToSync.count() + channelsToSync.count() + netsToSync.count() == 0) {
303 foreach(Network *net, _networks.values()) {
304 //disconnect(net, 0, this, SLOT(networkInitDone()));
305 //disconnect(net, 0, this, SLOT(ircUserInitDone(IrcUser *)));
306 //disconnect(net, 0, this, SLOT(ircUserAdded(IrcUser *)));
307 //disconnect(net, 0, this, SLOT(ircUserRemoved(QObject *)));
308 //disconnect(net, 0, this, SLOT(ircChannelInitDone(IrcChannel *)));
309 //disconnect(net, 0, this, SLOT(ircChannelAdded(IrcChannel *)));
310 //disconnect(net, 0, this, SLOT(ircChannelRemoved(QObject *)));
311 qDebug() << "disconnecting";
312 disconnect(net, SIGNAL(initDone()), this, SLOT(networkInitDone()));
313 disconnect(net, SIGNAL(ircUserInitDone(IrcUser *)), this, SLOT(ircUserInitDone(IrcUser *)));
314 disconnect(net, SIGNAL(ircUserAdded(IrcUser *)), this, SLOT(ircUserAdded(IrcUser *)));
315 disconnect(net, SIGNAL(ircUserRemoved(QObject *)), this, SLOT(ircUserRemoved(QObject *)));
316 disconnect(net, SIGNAL(ircChannelInitDone(IrcChannel *)), this, SLOT(ircChannelInitDone(IrcChannel *)));
317 disconnect(net, SIGNAL(ircChannelAdded(IrcChannel *)), this, SLOT(ircChannelAdded(IrcChannel *)));
318 disconnect(net, SIGNAL(ircChannelRemoved(QObject *)), this, SLOT(ircChannelRemoved(QObject *)));
322 Client::instance()->setSyncedToCore();
325 //emit connectionStateChanged(true);