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() {
45 void ClientSyncer::coreHasData() {
47 while(SignalProxy::readDataFromDevice(socket, blockSize, item)) {
48 emit recvPartialItem(1,1);
49 QVariantMap msg = item.toMap();
50 if(!msg.contains("MsgType")) {
51 // This core is way too old and does not even speak our init protocol...
52 emit connectionError(tr("The Quassel Core you try to connect to is too old! Please consider upgrading."));
56 if(msg["MsgType"] == "ClientInitAck") {
58 } else if(msg["MsgType"] == "ClientInitReject") {
59 emit connectionError(msg["Error"].toString());
62 } else if(msg["MsgType"] == "ClientLoginReject") {
63 emit loginFailed(msg["Error"].toString());
64 } else if(msg["MsgType"] == "ClientLoginAck") {
65 // prevent multiple signal connections
66 disconnect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
67 connect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
69 } else if(msg["MsgType"] == "SessionInit") {
70 sessionStateReceived(msg["SessionState"].toMap());
72 emit connectionError(tr("<b>Invalid data received from core!</b><br>Disconnecting."));
77 if (!msg["StartWizard"].toBool()) {
78 recvCoreState(msg["Reply"]);
80 qWarning("Core not configured!");
81 qDebug() << "Available storage providers: " << msg["StorageProviders"].toStringList();
82 emit showConfigWizard(msg);
90 emit recvPartialItem(socket->bytesAvailable(), blockSize);
94 void ClientSyncer::coreSocketError(QAbstractSocket::SocketError) {
95 emit connectionError(socket->errorString());
96 socket->deleteLater();
99 void ClientSyncer::disconnectFromCore() {
100 if(socket) socket->close();
103 void ClientSyncer::connectToCore(const QVariantMap &conn) {
104 // TODO implement SSL
105 coreConnectionInfo = conn;
106 //if(isConnected()) {
107 // emit coreConnectionError(tr("Already connected to Core!"));
112 socket->deleteLater();
115 if(conn["Host"].toString().isEmpty()) {
116 emit connectionError(tr("Internal connections not yet supported."));
117 return; // FIXME implement internal connections
118 //clientMode = LocalCore;
119 socket = new QBuffer(this);
120 connect(socket, SIGNAL(readyRead()), this, SLOT(coreHasData()));
121 socket->open(QIODevice::ReadWrite);
122 //QVariant state = connectToLocalCore(coreConnectionInfo["User"].toString(), coreConnectionInfo["Password"].toString());
124 coreSocketConnected();
126 //clientMode = RemoteCore;
127 //emit coreConnectionMsg(tr("Connecting..."));
129 QTcpSocket *sock = new QTcpSocket(Client::instance());
131 connect(sock, SIGNAL(readyRead()), this, SLOT(coreHasData()));
132 connect(sock, SIGNAL(connected()), this, SLOT(coreSocketConnected()));
133 connect(sock, SIGNAL(disconnected()), this, SLOT(coreSocketDisconnected()));
134 connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(coreSocketError(QAbstractSocket::SocketError)));
135 connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)));
136 sock->connectToHost(conn["Host"].toString(), conn["Port"].toUInt());
140 void ClientSyncer::coreSocketConnected() {
141 //connect(this, SIGNAL(recvPartialItem(uint, uint)), this, SIGNAL(coreConnectionProgress(uint, uint)));
142 // Phase One: Send client info and wait for core info
144 //emit coreConnectionMsg(tr("Synchronizing to core..."));
145 QVariantMap clientInit;
146 clientInit["MsgType"] = "ClientInit";
147 clientInit["ClientVersion"] = Global::quasselVersion;
148 clientInit["ClientDate"] = Global::quasselDate;
149 clientInit["ClientBuild"] = Global::quasselBuild; // this is a minimum, since we probably won't update for every commit
150 clientInit["UseSsl"] = false; // FIXME implement SSL
151 SignalProxy::writeDataToDevice(socket, clientInit);
154 void ClientSyncer::coreSocketDisconnected() {
155 emit socketDisconnected();
156 Client::instance()->disconnectFromCore();
158 // FIXME handle disconnects gracefully in here as well!
160 coreConnectionInfo.clear();
162 channelsToSync.clear();
165 //restartPhaseNull();
168 void ClientSyncer::clientInitAck(const QVariantMap &msg) {
169 // Core has accepted our version info and sent its own. Let's see if we accept it as well...
170 if(msg["CoreBuild"].toUInt() < Global::coreBuildNeeded) {
171 emit connectionError(tr("<b>The Quassel Core you are trying to connect to is too old!</b><br>"
172 "Need at least a Core Version %1 (Build >= %2) to connect.").arg(Global::quasselVersion).arg(Global::quasselBuild));
173 disconnectFromCore();
176 emit connectionMsg(msg["CoreInfo"].toString());
177 if(msg["LoginEnabled"].toBool()) {
182 void ClientSyncer::loginToCore(const QString &user, const QString &passwd) {
183 emit connectionMsg(tr("Logging in..."));
184 QVariantMap clientLogin;
185 clientLogin["MsgType"] = "ClientLogin";
186 clientLogin["User"] = user;
187 clientLogin["Password"] = passwd;
188 SignalProxy::writeDataToDevice(socket, clientLogin);
191 void ClientSyncer::sessionStateReceived(const QVariantMap &state) {
192 emit sessionProgress(1, 1);
193 disconnect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
194 disconnect(socket, 0, this, 0); // rest of communication happens through SignalProxy
195 //Client::signalProxy()->addPeer(socket);
196 Client::instance()->setConnectedToCore(socket);
200 void ClientSyncer::syncToCore(const QVariantMap &sessionState) {
203 foreach(QVariant vid, sessionState["Identities"].toList()) {
204 Client::instance()->coreIdentityCreated(vid.value<Identity>());
208 // FIXME: get rid of this crap
209 QVariantList bufferinfos = sessionState["BufferInfos"].toList();
210 foreach(QVariant vinfo, bufferinfos) Client::buffer(vinfo.value<BufferInfo>()); // create Buffers and BufferItems
212 QVariantList networkids = sessionState["NetworkIds"].toList();
214 // prepare sync progress thingys... FIXME: Care about removal of networks
215 numNetsToSync = networkids.count();
216 numChannelsToSync = 0; //sessionState["IrcChannelCount"].toUInt();
217 numUsersToSync = 0; // sessionState["IrcUserCount"].toUInt(); qDebug() << numUsersToSync;
218 emit networksProgress(0, numNetsToSync);
219 emit channelsProgress(0, numChannelsToSync);
220 emit ircUsersProgress(0, numUsersToSync);
222 // create network objects
223 foreach(QVariant networkid, networkids) {
224 NetworkId netid = networkid.value<NetworkId>();
225 Network *net = new Network(netid, Client::instance());
226 netsToSync.insert(net);
227 connect(net, SIGNAL(initDone()), this, SLOT(networkInitDone()));
228 connect(net, SIGNAL(ircUserInitDone(IrcUser *)), this, SLOT(ircUserInitDone(IrcUser *)));
229 connect(net, SIGNAL(ircUserAdded(IrcUser *)), this, SLOT(ircUserAdded(IrcUser *)));
230 connect(net, SIGNAL(ircUserRemoved(QObject *)), this, SLOT(ircUserRemoved(QObject *)));
231 connect(net, SIGNAL(ircChannelInitDone(IrcChannel *)), this, SLOT(ircChannelInitDone(IrcChannel *)));
232 connect(net, SIGNAL(ircChannelAdded(IrcChannel *)), this, SLOT(ircChannelAdded(IrcChannel *)));
233 connect(net, SIGNAL(ircChannelRemoved(QObject *)), this, SLOT(ircChannelRemoved(QObject *)));
234 Client::addNetwork(net);
239 void ClientSyncer::networkInitDone() {
240 netsToSync.remove(sender());
241 emit networksProgress(numNetsToSync - netsToSync.count(), numNetsToSync);
245 void ClientSyncer::ircChannelInitDone(IrcChannel *chan) {
246 channelsToSync.remove(chan);
247 emit channelsProgress(numChannelsToSync - channelsToSync.count(), numChannelsToSync);
251 void ClientSyncer::ircChannelAdded(IrcChannel *chan) {
252 if(!chan->isInitialized()) {
253 channelsToSync.insert(chan);
255 emit channelsProgress(numChannelsToSync - channelsToSync.count(), numChannelsToSync);
260 void ClientSyncer::ircChannelRemoved(QObject *chan) {
261 if(channelsToSync.contains(chan)) {
263 channelsToSync.remove(chan);
264 emit channelsProgress(numChannelsToSync - channelsToSync.count(), numChannelsToSync);
269 void ClientSyncer::ircUserInitDone(IrcUser *user) {
270 usersToSync.remove(user);
271 emit ircUsersProgress(numUsersToSync - usersToSync.count(), numUsersToSync);
275 void ClientSyncer::ircUserAdded(IrcUser *user) {
276 if(!user->isInitialized()) {
277 usersToSync.insert(user);
279 emit ircUsersProgress(numUsersToSync - usersToSync.count(), numUsersToSync);
284 void ClientSyncer::ircUserRemoved(QObject *user) {
285 if(usersToSync.contains(user)) {
287 usersToSync.remove(user);
288 emit ircUsersProgress(numUsersToSync - usersToSync.count(), numUsersToSync);
293 void ClientSyncer::checkSyncState() {
294 if(usersToSync.count() + channelsToSync.count() + netsToSync.count() == 0) {
298 foreach(Network *net, _networks.values()) {
299 //disconnect(net, 0, this, SLOT(networkInitDone()));
300 //disconnect(net, 0, this, SLOT(ircUserInitDone(IrcUser *)));
301 //disconnect(net, 0, this, SLOT(ircUserAdded(IrcUser *)));
302 //disconnect(net, 0, this, SLOT(ircUserRemoved(QObject *)));
303 //disconnect(net, 0, this, SLOT(ircChannelInitDone(IrcChannel *)));
304 //disconnect(net, 0, this, SLOT(ircChannelAdded(IrcChannel *)));
305 //disconnect(net, 0, this, SLOT(ircChannelRemoved(QObject *)));
306 qDebug() << "disconnecting";
307 disconnect(net, SIGNAL(initDone()), this, SLOT(networkInitDone()));
308 disconnect(net, SIGNAL(ircUserInitDone(IrcUser *)), this, SLOT(ircUserInitDone(IrcUser *)));
309 disconnect(net, SIGNAL(ircUserAdded(IrcUser *)), this, SLOT(ircUserAdded(IrcUser *)));
310 disconnect(net, SIGNAL(ircUserRemoved(QObject *)), this, SLOT(ircUserRemoved(QObject *)));
311 disconnect(net, SIGNAL(ircChannelInitDone(IrcChannel *)), this, SLOT(ircChannelInitDone(IrcChannel *)));
312 disconnect(net, SIGNAL(ircChannelAdded(IrcChannel *)), this, SLOT(ircChannelAdded(IrcChannel *)));
313 disconnect(net, SIGNAL(ircChannelRemoved(QObject *)), this, SLOT(ircChannelRemoved(QObject *)));
317 Client::instance()->setSyncedToCore();
320 //emit connectionStateChanged(true);