1 /***************************************************************************
2 * Copyright (C) 2009 by the Quassel Project *
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 "coreconnection.h"
23 #ifndef QT_NO_NETWORKPROXY
24 # include <QNetworkProxy>
28 #include "clientsettings.h"
29 #include "coreaccountmodel.h"
32 #include "networkmodel.h"
34 #include "signalproxy.h"
37 CoreConnection::CoreConnection(CoreAccountModel *model, QObject *parent)
42 _wantReconnect(false),
47 qRegisterMetaType<ConnectionState>("CoreConnection::ConnectionState");
50 void CoreConnection::init() {
51 Client::signalProxy()->setHeartBeatInterval(30);
52 connect(Client::signalProxy(), SIGNAL(disconnected()), SLOT(coreSocketDisconnected()));
53 connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), SIGNAL(lagUpdated(int)));
55 _reconnectTimer.setSingleShot(true);
56 connect(&_reconnectTimer, SIGNAL(timeout()), SLOT(reconnectTimeout()));
59 connect(Solid::Networking::notifier(), SIGNAL(statusChanged(Solid::Networking::Status)),
60 SLOT(solidNetworkStatusChanged(Solid::Networking::Status)));
63 CoreConnectionSettings s;
64 s.initAndNotify("PingTimeoutInterval", this, SLOT(pingTimeoutIntervalChanged(QVariant)), 60);
65 s.initAndNotify("ReconnectInterval", this, SLOT(reconnectIntervalChanged(QVariant)), 60);
66 s.notify("NetworkDetectionMode", this, SLOT(networkDetectionModeChanged(QVariant)));
67 networkDetectionModeChanged(s.networkDetectionMode());
70 void CoreConnection::setProgressText(const QString &text) {
71 if(_progressText != text) {
73 emit progressTextChanged(text);
77 void CoreConnection::setProgressValue(int value) {
78 if(_progressValue != value) {
79 _progressValue = value;
80 emit progressValueChanged(value);
84 void CoreConnection::setProgressMinimum(int minimum) {
85 if(_progressMinimum != minimum) {
86 _progressMinimum = minimum;
87 emit progressRangeChanged(minimum, _progressMaximum);
91 void CoreConnection::setProgressMaximum(int maximum) {
92 if(_progressMaximum != maximum) {
93 _progressMaximum = maximum;
94 emit progressRangeChanged(_progressMinimum, maximum);
98 void CoreConnection::updateProgress(int value, int max) {
99 if(max != _progressMaximum) {
100 _progressMaximum = max;
101 emit progressRangeChanged(_progressMinimum, _progressMaximum);
103 setProgressValue(value);
106 void CoreConnection::reconnectTimeout() {
108 CoreConnectionSettings s;
109 if(_wantReconnect && s.autoReconnect()) {
112 // If using Solid, we don't want to reconnect if we're offline
113 if(s.networkDetectionMode() == CoreConnectionSettings::UseSolid) {
114 if(Solid::Networking::status() != Solid::Networking::Connected
115 && Solid::Networking::status() != Solid::Networking::Unknown) {
119 #endif /* HAVE_KDE */
126 void CoreConnection::networkDetectionModeChanged(const QVariant &vmode) {
127 CoreConnectionSettings s;
128 CoreConnectionSettings::NetworkDetectionMode mode = (CoreConnectionSettings::NetworkDetectionMode)vmode.toInt();
129 if(mode == CoreConnectionSettings::UsePingTimeout)
130 Client::signalProxy()->setMaxHeartBeatCount(s.pingTimeoutInterval() / 30);
132 Client::signalProxy()->setMaxHeartBeatCount(-1);
136 void CoreConnection::pingTimeoutIntervalChanged(const QVariant &interval) {
137 CoreConnectionSettings s;
138 if(s.networkDetectionMode() == CoreConnectionSettings::UsePingTimeout)
139 Client::signalProxy()->setMaxHeartBeatCount(interval.toInt() / 30); // interval is 30 seconds
142 void CoreConnection::reconnectIntervalChanged(const QVariant &interval) {
143 _reconnectTimer.setInterval(interval.toInt() * 1000);
148 void CoreConnection::solidNetworkStatusChanged(Solid::Networking::Status status) {
149 CoreConnectionSettings s;
150 if(s.networkDetectionMode() != CoreConnectionSettings::UseSolid)
154 case Solid::Networking::Unknown:
155 case Solid::Networking::Connected:
156 qDebug() << "Solid: Network status changed to connected or unknown";
157 if(state() == Disconnected) {
158 if(_wantReconnect && s.autoReconnect()) {
163 case Solid::Networking::Unconnected:
164 qDebug() << "Solid: Disconnected";
165 if(!isLocalConnection())
166 disconnectFromCore(tr("Network is down"), true);
175 bool CoreConnection::isEncrypted() const {
179 QSslSocket *sock = qobject_cast<QSslSocket *>(_socket);
180 return isConnected() && sock && sock->isEncrypted();
184 bool CoreConnection::isLocalConnection() const {
187 if(currentAccount().isInternal())
189 if(_socket->peerAddress().isInSubnet(QHostAddress::LocalHost, 0x00ffffff))
195 void CoreConnection::socketStateChanged(QAbstractSocket::SocketState socketState) {
198 switch(socketState) {
199 case QAbstractSocket::UnconnectedState:
200 text = tr("Disconnected");
202 case QAbstractSocket::HostLookupState:
203 text = tr("Looking up %1...").arg(currentAccount().hostName());
205 case QAbstractSocket::ConnectingState:
206 text = tr("Connecting to %1...").arg(currentAccount().hostName());
208 case QAbstractSocket::ConnectedState:
209 text = tr("Connected to %1").arg(currentAccount().hostName());
211 case QAbstractSocket::ClosingState:
212 text = tr("Disconnecting from %1...").arg(currentAccount().hostName());
219 emit progressTextChanged(text);
221 setState(socketState);
224 void CoreConnection::setState(QAbstractSocket::SocketState socketState) {
225 ConnectionState state;
227 switch(socketState) {
228 case QAbstractSocket::UnconnectedState:
229 state = Disconnected;
231 case QAbstractSocket::HostLookupState:
232 case QAbstractSocket::ConnectingState:
233 case QAbstractSocket::ConnectedState: // we'll set it to Connected in connectionReady()
237 state = Disconnected;
243 void CoreConnection::setState(ConnectionState state) {
244 if(state != _state) {
246 emit stateChanged(state);
247 if(state == Disconnected)
252 void CoreConnection::coreSocketError(QAbstractSocket::SocketError) {
253 qDebug() << "coreSocketError" << _socket << _socket->errorString();
254 disconnectFromCore(_socket->errorString(), true);
257 void CoreConnection::coreSocketDisconnected() {
258 qDebug() << Q_FUNC_INFO;
259 resetConnection(true);
260 // FIXME handle disconnects gracefully
263 void CoreConnection::coreHasData() {
265 while(SignalProxy::readDataFromDevice(_socket, _blockSize, item)) {
266 QVariantMap msg = item.toMap();
267 if(!msg.contains("MsgType")) {
268 // This core is way too old and does not even speak our init protocol...
269 emit connectionErrorPopup(tr("The Quassel Core you try to connect to is too old! Please consider upgrading."));
270 disconnectFromCore(QString(), false);
273 if(msg["MsgType"] == "ClientInitAck") {
275 } else if(msg["MsgType"] == "ClientInitReject") {
276 emit connectionErrorPopup(msg["Error"].toString());
277 disconnectFromCore(QString(), false);
279 } else if(msg["MsgType"] == "CoreSetupAck") {
280 emit coreSetupSuccess();
281 } else if(msg["MsgType"] == "CoreSetupReject") {
282 emit coreSetupFailed(msg["Error"].toString());
283 } else if(msg["MsgType"] == "ClientLoginReject") {
284 loginFailed(msg["Error"].toString());
285 } else if(msg["MsgType"] == "ClientLoginAck") {
287 } else if(msg["MsgType"] == "SessionInit") {
288 // that's it, let's hand over to the signal proxy
289 // if the socket is an orphan, the signalProxy adopts it.
290 // -> we don't need to care about it anymore
291 _socket->setParent(0);
292 Client::signalProxy()->addPeer(_socket);
294 sessionStateReceived(msg["SessionState"].toMap());
295 break; // this is definitively the last message we process here!
297 disconnectFromCore(tr("Invalid data received from core"), false);
302 updateProgress(_socket->bytesAvailable(), _blockSize);
306 void CoreConnection::disconnectFromCore() {
307 disconnectFromCore(QString(), false); // requested disconnect, so don't try to reconnect
310 void CoreConnection::disconnectFromCore(const QString &errorString, bool wantReconnect) {
312 _reconnectTimer.stop();
314 if(errorString.isEmpty())
315 emit connectionError(tr("Disconnected"));
317 emit connectionError(errorString);
319 Client::signalProxy()->removeAllPeers();
320 resetConnection(wantReconnect);
323 void CoreConnection::resetConnection(bool wantReconnect) {
324 _wantReconnect = wantReconnect;
327 disconnect(_socket, 0, this, 0);
328 _socket->deleteLater();
333 _coreMsgBuffer.clear();
338 setProgressMaximum(-1); // disable
339 setState(Disconnected);
342 emit connectionMsg(tr("Disconnected from core."));
343 emit encrypted(false);
345 // initiate if a reconnect if appropriate
346 CoreConnectionSettings s;
347 if(wantReconnect && s.autoReconnect()) {
348 _reconnectTimer.start();
352 void CoreConnection::reconnectToCore() {
353 if(currentAccount().isValid())
354 connectToCore(currentAccount().accountId());
357 bool CoreConnection::connectToCore(AccountId accId) {
361 CoreAccountSettings s;
363 // FIXME: Don't force connection to internal core in mono client
364 if(Quassel::runMode() == Quassel::Monolithic) {
365 _account = accountModel()->account(accountModel()->internalAccount());
366 Q_ASSERT(_account.isValid());
368 if(!accId.isValid()) {
369 // check our settings and figure out what to do
370 if(!s.autoConnectOnStartup())
372 if(s.autoConnectToFixedAccount())
373 accId = s.autoConnectAccount();
375 accId = s.lastAccount();
379 _account = accountModel()->account(accId);
380 if(!_account.accountId().isValid()) {
383 if(Quassel::runMode() != Quassel::Monolithic) {
384 if(_account.isInternal())
389 s.setLastAccount(accId);
390 connectToCurrentAccount();
394 void CoreConnection::connectToCurrentAccount() {
395 resetConnection(false);
397 if(currentAccount().isInternal()) {
398 if(Quassel::runMode() != Quassel::Monolithic) {
399 qWarning() << "Cannot connect to internal core in client-only mode!";
402 emit startInternalCore();
403 emit connectToInternalCore(Client::instance()->signalProxy());
407 CoreAccountSettings s;
411 QSslSocket *sock = new QSslSocket(Client::instance());
412 // make sure the warning is shown if we happen to connect without SSL support later
413 s.setAccountValue("ShowNoClientSslWarning", true);
415 if(_account.useSsl()) {
416 if(s.accountValue("ShowNoClientSslWarning", true).toBool()) {
417 bool accepted = false;
418 emit handleNoSslInClient(&accepted);
420 emit connectionError(tr("Unencrypted connection canceled"));
423 s.setAccountValue("ShowNoClientSslWarning", false);
426 QTcpSocket *sock = new QTcpSocket(Client::instance());
429 #ifndef QT_NO_NETWORKPROXY
430 if(_account.useProxy()) {
431 QNetworkProxy proxy(_account.proxyType(), _account.proxyHostName(), _account.proxyPort(), _account.proxyUser(), _account.proxyPassword());
432 sock->setProxy(proxy);
437 connect(sock, SIGNAL(readyRead()), SLOT(coreHasData()));
438 connect(sock, SIGNAL(connected()), SLOT(coreSocketConnected()));
439 connect(sock, SIGNAL(disconnected()), SLOT(coreSocketDisconnected()));
440 connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(coreSocketError(QAbstractSocket::SocketError)));
441 connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(socketStateChanged(QAbstractSocket::SocketState)));
443 emit connectionMsg(tr("Connecting to %1...").arg(currentAccount().accountName()));
444 sock->connectToHost(_account.hostName(), _account.port());
447 void CoreConnection::coreSocketConnected() {
448 // Phase One: Send client info and wait for core info
450 emit connectionMsg(tr("Synchronizing to core..."));
452 QVariantMap clientInit;
453 clientInit["MsgType"] = "ClientInit";
454 clientInit["ClientVersion"] = Quassel::buildInfo().fancyVersionString;
455 clientInit["ClientDate"] = Quassel::buildInfo().buildDate;
456 clientInit["ProtocolVersion"] = Quassel::buildInfo().protocolVersion;
457 clientInit["UseSsl"] = _account.useSsl();
458 #ifndef QT_NO_COMPRESS
459 clientInit["UseCompression"] = true;
461 clientInit["UseCompression"] = false;
464 SignalProxy::writeDataToDevice(_socket, clientInit);
467 void CoreConnection::clientInitAck(const QVariantMap &msg) {
468 // Core has accepted our version info and sent its own. Let's see if we accept it as well...
469 uint ver = msg["ProtocolVersion"].toUInt();
470 if(ver < Quassel::buildInfo().clientNeedsProtocol) {
471 emit connectionErrorPopup(tr("<b>The Quassel Core you are trying to connect to is too old!</b><br>"
472 "Need at least core/client protocol v%1 to connect.").arg(Quassel::buildInfo().clientNeedsProtocol));
473 disconnectFromCore(QString(), false);
477 Client::setCoreFeatures((Quassel::Features)msg["CoreFeatures"].toUInt());
479 #ifndef QT_NO_COMPRESS
480 if(msg["SupportsCompression"].toBool()) {
481 _socket->setProperty("UseCompression", true);
485 _coreMsgBuffer = msg;
488 CoreAccountSettings s;
489 if(currentAccount().useSsl()) {
490 if(msg["SupportSsl"].toBool()) {
491 // Make sure the warning is shown next time we don't have SSL in the core
492 s.setAccountValue("ShowNoCoreSslWarning", true);
494 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(_socket);
496 connect(sslSocket, SIGNAL(encrypted()), SLOT(sslSocketEncrypted()));
497 connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError> &)), SLOT(sslErrors()));
498 sslSocket->startClientEncryption();
500 if(s.accountValue("ShowNoCoreSslWarning", true).toBool()) {
501 bool accepted = false;
502 emit handleNoSslInCore(&accepted);
504 disconnectFromCore(tr("Unencrypted connection canceled"), false);
507 s.setAccountValue("ShowNoCoreSslWarning", false);
508 s.setAccountValue("SslCert", QString());
515 // if we use SSL we wait for the next step until every SSL warning has been cleared
521 void CoreConnection::sslSocketEncrypted() {
522 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
525 if(!socket->sslErrors().count()) {
526 // Cert is valid, so we don't want to store it as known
527 // That way, a warning will appear in case it becomes invalid at some point
528 CoreAccountSettings s;
529 s.setAccountValue("SSLCert", QString());
532 emit encrypted(true);
536 void CoreConnection::sslErrors() {
537 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
540 CoreAccountSettings s;
541 QByteArray knownDigest = s.accountValue("SslCert").toByteArray();
543 if(knownDigest != socket->peerCertificate().digest()) {
544 bool accepted = false;
545 bool permanently = false;
546 emit handleSslErrors(socket, &accepted, &permanently);
549 disconnectFromCore(tr("Unencrypted connection canceled"), false);
554 s.setAccountValue("SslCert", socket->peerCertificate().digest());
556 s.setAccountValue("SslCert", QString());
559 socket->ignoreSslErrors();
562 #endif /* HAVE_SSL */
564 void CoreConnection::connectionReady() {
566 emit connectionMsg(tr("Connected to %1").arg(currentAccount().accountName()));
568 if(!_coreMsgBuffer["Configured"].toBool()) {
570 emit startCoreSetup(_coreMsgBuffer["StorageBackends"].toList());
571 } else if(_coreMsgBuffer["LoginEnabled"].toBool()) {
574 _coreMsgBuffer.clear();
577 void CoreConnection::loginToCore(const QString &user, const QString &password, bool remember) {
578 _account.setUser(user);
579 _account.setPassword(password);
580 _account.setStorePassword(remember);
584 void CoreConnection::loginToCore(const QString &prevError) {
585 emit connectionMsg(tr("Logging in..."));
586 if(currentAccount().user().isEmpty() || currentAccount().password().isEmpty() || !prevError.isEmpty()) {
588 emit userAuthenticationRequired(&_account, &valid, prevError); // *must* be a synchronous call
589 if(!valid || currentAccount().user().isEmpty() || currentAccount().password().isEmpty()) {
590 disconnectFromCore(tr("Login canceled"), false);
595 QVariantMap clientLogin;
596 clientLogin["MsgType"] = "ClientLogin";
597 clientLogin["User"] = currentAccount().user();
598 clientLogin["Password"] = currentAccount().password();
599 SignalProxy::writeDataToDevice(_socket, clientLogin);
602 void CoreConnection::loginFailed(const QString &error) {
606 void CoreConnection::loginSuccess() {
607 updateProgress(0, 0);
609 // save current account data
610 _model->createOrUpdateAccount(currentAccount());
613 _reconnectTimer.stop();
615 setProgressText(tr("Receiving session state"));
616 setState(Synchronizing);
617 emit connectionMsg(tr("Synchronizing to %1...").arg(currentAccount().accountName()));
620 void CoreConnection::sessionStateReceived(const QVariantMap &state) {
621 updateProgress(100, 100);
623 // rest of communication happens through SignalProxy...
624 disconnect(_socket, SIGNAL(readyRead()), this, 0);
625 disconnect(_socket, SIGNAL(connected()), this, 0);
630 void CoreConnection::internalSessionStateReceived(const QVariant &packedState) {
631 updateProgress(100, 100);
633 setState(Synchronizing);
634 syncToCore(packedState.toMap());
637 void CoreConnection::syncToCore(const QVariantMap &sessionState) {
638 if(sessionState.contains("CoreFeatures"))
639 Client::setCoreFeatures((Quassel::Features)sessionState["CoreFeatures"].toUInt());
641 setProgressText(tr("Receiving network states"));
642 updateProgress(0, 100);
645 foreach(QVariant vid, sessionState["Identities"].toList()) {
646 Client::instance()->coreIdentityCreated(vid.value<Identity>());
650 // FIXME: get rid of this crap -- why?
651 QVariantList bufferinfos = sessionState["BufferInfos"].toList();
652 NetworkModel *networkModel = Client::networkModel();
653 Q_ASSERT(networkModel);
654 foreach(QVariant vinfo, bufferinfos)
655 networkModel->bufferUpdated(vinfo.value<BufferInfo>()); // create BufferItems
657 QVariantList networkids = sessionState["NetworkIds"].toList();
659 // prepare sync progress thingys...
660 // FIXME: Care about removal of networks
661 _numNetsToSync = networkids.count();
662 updateProgress(0, _numNetsToSync);
664 // create network objects
665 foreach(QVariant networkid, networkids) {
666 NetworkId netid = networkid.value<NetworkId>();
667 if(Client::network(netid))
669 Network *net = new Network(netid, Client::instance());
670 _netsToSync.insert(net);
671 connect(net, SIGNAL(initDone()), SLOT(networkInitDone()));
672 connect(net, SIGNAL(destroyed()), SLOT(networkInitDone()));
673 Client::addNetwork(net);
678 // this is also called for destroyed networks!
679 void CoreConnection::networkInitDone() {
680 QObject *net = sender();
682 disconnect(net, 0, this, 0);
683 _netsToSync.remove(net);
684 updateProgress(_numNetsToSync - _netsToSync.count(), _numNetsToSync);
688 void CoreConnection::checkSyncState() {
689 if(_netsToSync.isEmpty() && state() >= Synchronizing) {
690 setState(Synchronized);
691 setProgressText(tr("Synchronized to %1").arg(currentAccount().accountName()));
692 setProgressMaximum(-1);
697 void CoreConnection::doCoreSetup(const QVariant &setupData) {
699 setup["MsgType"] = "CoreSetupData";
700 setup["SetupData"] = setupData;
701 SignalProxy::writeDataToDevice(_socket, setup);