#endif
#include "client.h"
+#include "clientsettings.h"
#include "coreaccountmodel.h"
#include "identity.h"
#include "network.h"
: QObject(parent),
_model(model),
_blockSize(0),
+ _state(Disconnected),
_progressMinimum(0),
_progressMaximum(-1),
_progressValue(-1)
}
-void CoreConnection::start() {
- connectToCore(1);
-}
-
void CoreConnection::init() {
connect(Client::signalProxy(), SIGNAL(disconnected()), SLOT(coreSocketDisconnected()));
}
_numNetsToSync = 0;
setProgressMaximum(-1); // disable
+ setState(Disconnected);
emit connectionMsg(tr("Disconnected from core."));
}
if(state != _state) {
_state = state;
emit stateChanged(state);
+ if(state == Disconnected)
+ emit disconnected();
}
}
}
void CoreConnection::coreSocketDisconnected() {
- setState(Disconnected);
emit disconnected();
resetConnection();
// FIXME handle disconnects gracefully
}
void CoreConnection::disconnectFromCore() {
- if(isConnected()) {
- Client::signalProxy()->removeAllPeers();
- resetConnection();
- }
+ Client::signalProxy()->removeAllPeers();
+ resetConnection();
}
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::connectToCore(AccountId accId) {
+void CoreConnection::connectToCurrentAccount() {
resetConnection();
- _account = accountModel()->account(accId);
- if(!_account.accountId().isValid()) {
- emit connectionError(tr("Invalid core account, cannot connect!"));
+ if(currentAccount().isInternal()) {
+ if(Quassel::runMode() != Quassel::Monolithic) {
+ qWarning() << "Cannot connect to internal core in client-only mode!";
+ return;
+ }
+ emit startInternalCore();
+ emit connectToInternalCore(Client::instance()->signalProxy());
return;
}
resetWarningsHandler();
}
-void CoreConnection::loginToCore() {
+void CoreConnection::loginToCore(const QString &prevError) {
emit connectionMsg(tr("Logging in..."));
- if(currentAccount().user().isEmpty() || currentAccount().password().isEmpty()) {
- emit userAuthenticationRequired(&_account); // *must* be a synchronous call
- if(currentAccount().user().isEmpty() || currentAccount().password().isEmpty()) {
+ if(currentAccount().user().isEmpty() || currentAccount().password().isEmpty() || !prevError.isEmpty()) {
+ bool valid = false;
+ emit userAuthenticationRequired(&_account, &valid, prevError); // *must* be a synchronous call
+ if(!valid || currentAccount().user().isEmpty() || currentAccount().password().isEmpty()) {
disconnectFromCore();
+ emit connectionError(tr("Login canceled"));
return;
}
}
}
void CoreConnection::loginFailed(const QString &error) {
- emit userAuthenticationRequired(&_account, error); // *must* be a synchronous call
- if(currentAccount().user().isEmpty() || currentAccount().password().isEmpty()) {
- disconnectFromCore();
- return;
- }
- loginToCore();
+ loginToCore(error);
}
void CoreConnection::loginSuccess() {
updateProgress(0, 0);
+
+ // save current account data
+ _model->createOrUpdateAccount(currentAccount());
+ _model->save();
+
setProgressText(tr("Receiving session state"));
setState(Synchronizing);
emit connectionMsg(tr("Synchronizing to %1...").arg(currentAccount().accountName()));
disconnect(_socket, SIGNAL(readyRead()), this, 0);
disconnect(_socket, SIGNAL(connected()), this, 0);
- //Client::instance()->setConnectedToCore(currentAccount().accountId(), _socket);
syncToCore(state);
}
+void CoreConnection::internalSessionStateReceived(const QVariant &packedState) {
+ updateProgress(100, 100);
+
+ setState(Synchronizing);
+ syncToCore(packedState.toMap());
+}
+
void CoreConnection::syncToCore(const QVariantMap &sessionState) {
setProgressText(tr("Receiving network states"));
updateProgress(0, 100);