- if(!msg.contains("MsgType")) {
- // This core is way too old and does not even speak our init protocol...
- emit connectionError(tr("The Quassel Core you try to connect to is too old! Please consider upgrading."));
- disconnectFromCore();
- return;
- }
- if(msg["MsgType"] == "ClientInitAck") {
- clientInitAck(msg);
- } else if(msg["MsgType"] == "ClientInitReject") {
- emit connectionError(msg["Error"].toString());
- disconnectFromCore();
- return;
- } else if(msg["MsgType"] == "CoreSetupAck") {
- //emit coreSetupSuccess();
- } else if(msg["MsgType"] == "CoreSetupReject") {
- //emit coreSetupFailed(msg["Error"].toString());
- } else if(msg["MsgType"] == "ClientLoginReject") {
- loginFailed(msg["Error"].toString());
- } else if(msg["MsgType"] == "ClientLoginAck") {
- loginSuccess();
- } else if(msg["MsgType"] == "SessionInit") {
- // that's it, let's hand over to the signal proxy
- // if the socket is an orphan, the signalProxy adopts it.
- // -> we don't need to care about it anymore
- _socket->setParent(0);
- Client::signalProxy()->addPeer(_socket);
-
- sessionStateReceived(msg["SessionState"].toMap());
- break; // this is definitively the last message we process here!
- } else {
- emit connectionError(tr("<b>Invalid data received from core!</b><br>Disconnecting."));
- disconnectFromCore();
- return;
- }
- }
- if(_blockSize > 0) {
- updateProgress(_socket->bytesAvailable(), _blockSize);
- }
-}
-
-void CoreConnection::disconnectFromCore() {
- if(isConnected()) {
- Client::signalProxy()->removeAllPeers();
- resetConnection();
- }
-}
-
-void CoreConnection::reconnectToCore() {
-
-}
-
-void CoreConnection::connectToCore(AccountId accId) {
- resetConnection();
- _account = accountModel()->account(accId);
-
- if(!_account.accountId().isValid()) {
- emit connectionError(tr("Invalid core account, cannot connect!"));
- return;
- }
-
- Q_ASSERT(!_socket);
+ if (!msg.contains("MsgType")) {
+ // This core is way too old and does not even speak our init protocol...
+ emit connectionErrorPopup(tr("The Quassel Core you try to connect to is too old! Please consider upgrading."));
+ disconnectFromCore(QString(), false);
+ return;
+ }
+ if (msg["MsgType"] == "ClientInitAck") {
+ clientInitAck(msg);
+ }
+ else if (msg["MsgType"] == "ClientInitReject") {
+ emit connectionErrorPopup(msg["Error"].toString());
+ disconnectFromCore(QString(), false);
+ return;
+ }
+ else if (msg["MsgType"] == "CoreSetupAck") {
+ emit coreSetupSuccess();
+ }
+ else if (msg["MsgType"] == "CoreSetupReject") {
+ emit coreSetupFailed(msg["Error"].toString());
+ }
+ else if (msg["MsgType"] == "ClientLoginReject") {
+ loginFailed(msg["Error"].toString());
+ }
+ else if (msg["MsgType"] == "ClientLoginAck") {
+ loginSuccess();
+ }
+ else if (msg["MsgType"] == "SessionInit") {
+ // that's it, let's hand over to the signal proxy
+ // if the connection is an orphan, the signalProxy adopts it.
+ // -> we don't need to care about it anymore
+
+ disconnect(_peer, 0, this, 0);
+
+ _peer->setParent(0);
+ Client::signalProxy()->addPeer(_peer);
+
+ sessionStateReceived(msg["SessionState"].toMap());
+ }
+ else {
+ disconnectFromCore(tr("Invalid data received from core"), false);
+ return;
+ }
+}
+
+
+void CoreConnection::disconnectFromCore()
+{
+ if (_socket) {
+ _requestedDisconnect = true;
+ disconnectFromCore(QString(), false); // requested disconnect, so don't try to reconnect
+ }
+}
+
+
+void CoreConnection::disconnectFromCore(const QString &errorString, bool wantReconnect)
+{
+ if (!wantReconnect)
+ _reconnectTimer.stop();
+
+ _wasReconnect = wantReconnect; // store if disconnect was requested
+
+ resetConnection(wantReconnect);
+
+ if (errorString.isEmpty())
+ emit connectionError(tr("Disconnected"));
+ else
+ emit connectionError(errorString);
+}
+
+
+void CoreConnection::resetConnection(bool wantReconnect)
+{
+ if (_resetting)
+ return;
+ _resetting = true;
+
+ _wantReconnect = wantReconnect;
+
+ if (_peer) {
+ disconnect(_socket, 0, this, 0);
+ disconnect(_peer, 0, this, 0);
+ _peer->close();
+
+ if (_peer->parent() == this)
+ _peer->deleteLater(); // if it's not us, it belongs to the sigproxy which will delete it
+ _socket = 0; // socket is owned and will be deleted by RemoteConnection
+ _peer = 0;
+ }
+ else if (_socket) {
+ disconnect(_socket, 0, this, 0);
+ _socket->deleteLater();
+ _socket = 0;
+ }
+
+ _requestedDisconnect = false;
+
+ _coreMsgBuffer.clear();
+ _netsToSync.clear();
+ _numNetsToSync = 0;
+
+ setProgressMaximum(-1); // disable
+ setState(Disconnected);
+ emit lagUpdated(-1);
+
+ emit connectionMsg(tr("Disconnected from core."));
+ emit encrypted(false);
+
+ // initiate if a reconnect if appropriate
+ CoreConnectionSettings s;
+ if (wantReconnect && s.autoReconnect()) {
+ _reconnectTimer.start();
+ }
+
+ _resetting = false;
+}
+
+
+void CoreConnection::reconnectToCore()
+{
+ if (currentAccount().isValid())
+ connectToCore(currentAccount().accountId());
+}
+
+
+bool CoreConnection::connectToCore(AccountId accId)
+{
+ if (isConnected())
+ return false;
+
+ CoreAccountSettings s;
+
+ // FIXME: Don't force connection to internal core in mono client
+ if (Quassel::runMode() == Quassel::Monolithic) {
+ _account = accountModel()->account(accountModel()->internalAccount());
+ Q_ASSERT(_account.isValid());
+ }
+ else {
+ if (!accId.isValid()) {
+ // check our settings and figure out what to do
+ if (!s.autoConnectOnStartup())
+ return false;
+ if (s.autoConnectToFixedAccount())
+ accId = s.autoConnectAccount();
+ else
+ accId = s.lastAccount();
+ if (!accId.isValid())
+ return false;
+ }
+ _account = accountModel()->account(accId);
+ if (!_account.accountId().isValid()) {
+ return false;
+ }
+ if (Quassel::runMode() != Quassel::Monolithic) {
+ if (_account.isInternal())
+ return false;
+ }
+ }
+
+ s.setLastAccount(accId);
+ connectToCurrentAccount();
+ return true;
+}
+
+
+void CoreConnection::connectToCurrentAccount()
+{
+ resetConnection(false);
+
+ if (currentAccount().isInternal()) {
+ if (Quassel::runMode() != Quassel::Monolithic) {
+ qWarning() << "Cannot connect to internal core in client-only mode!";
+ return;
+ }
+ emit startInternalCore();
+
+ InternalPeer *peer = new InternalPeer();
+ Client::instance()->signalProxy()->addPeer(peer); // sigproxy will take ownership
+ emit connectToInternalCore(peer);
+
+ return;
+ }
+
+ CoreAccountSettings s;
+
+ Q_ASSERT(!_socket);