Introducing the all-new all-fancy bufferviews.
[quassel.git] / src / qtui / coreconnectdlg.cpp
index 16d4552..4811e2e 100644 (file)
@@ -1,11 +1,11 @@
 /***************************************************************************
- *   Copyright (C) 2005-07 by The Quassel Team                             *
+ *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
+ *   (at your option) version 3.                                           *
  *                                                                         *
  *   This program is distributed in the hope that it will be useful,       *
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
-#include <QtGui>
+#include <QDebug>
+#include <QMessageBox>
+#include <QNetworkProxy>
+
 #include "coreconnectdlg.h"
-#include "client.h"
+
 #include "clientsettings.h"
-#include "configwizard.h"
-#include "global.h"
+#include "clientsyncer.h"
+#include "coreconfigwizard.h"
+
+CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool autoconnect)
+  : QDialog(parent)
+{
+  ui.setupUi(this);
+
+  // make it look more native under Mac OS X:
+  setWindowFlags(Qt::Sheet);
 
-CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool /*doAutoConnect*/) : QDialog(parent) {
-  ui.setupUi(this); //qDebug() << "new dlg";
+  clientSyncer = new ClientSyncer(this);
+  wizard = 0;
 
   setAttribute(Qt::WA_DeleteOnClose);
 
-  coreState = 0;
-  /* We show ui.internalCore in any case, because we might want to run as monolithic client anyway at another time
-  if(Global::runMode == Global::Monolithic) {
-    connect(ui.internalCore, SIGNAL(toggled(bool)), ui.hostEdit, SLOT(setDisabled(bool)));
-    connect(ui.internalCore, SIGNAL(toggled(bool)), ui.port, SLOT(setDisabled(bool)));
-    ui.internalCore->setChecked(true);
-  } else {
-    //ui.internalCore->hide();
+  doingAutoConnect = false;
+
+  ui.stackedWidget->setCurrentWidget(ui.accountPage);
+
+  CoreAccountSettings s;
+  AccountId lastacc = s.lastAccount();
+  autoConnectAccount = s.autoConnectAccount();
+  QListWidgetItem *currentItem = 0;
+  foreach(AccountId id, s.knownAccounts()) {
+    if(!id.isValid()) continue;
+    QVariantMap data = s.retrieveAccountData(id);
+    accounts[id] = data;
+    QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
+    item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
+    if(id == lastacc) currentItem = item;
   }
-  */
-  connect(ui.newAccount, SIGNAL(clicked()), this, SLOT(createAccount()));
-  connect(ui.delAccount, SIGNAL(clicked()), this, SLOT(removeAccount()));
-  connect(ui.buttonBox1, SIGNAL(accepted()), this, SLOT(doConnect()));
-  connect(ui.hostEdit, SIGNAL(textChanged(const QString &)), this, SLOT(checkInputValid()));
-  connect(ui.userEdit, SIGNAL(textChanged(const QString &)), this, SLOT(checkInputValid()));
-  connect(ui.internalCore, SIGNAL(toggled(bool)), this, SLOT(checkInputValid()));
-  connect(ui.internalCore, SIGNAL(toggled(bool)), ui.hostEdit, SLOT(setDisabled(bool)));
-  connect(ui.internalCore, SIGNAL(toggled(bool)), ui.port, SLOT(setDisabled(bool)));
-  connect(ui.accountList, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(accountChanged(const QString &)));
-  connect(ui.autoConnect, SIGNAL(clicked(bool)), this, SLOT(autoConnectToggled(bool)));
-
-  connect(Client::instance(), SIGNAL(coreConnectionMsg(const QString &)), ui.connectionStatus, SLOT(setText(const QString &)));
-  connect(Client::instance(), SIGNAL(coreConnectionProgress(uint, uint)), this, SLOT(updateProgressBar(uint, uint)));
-  connect(Client::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
-  connect(Client::instance(), SIGNAL(connected()), this, SLOT(coreConnected()));
-  
-  connect(Client::instance(), SIGNAL(showConfigWizard(const QVariantMap &)), this, SLOT(showConfigWizard(const QVariantMap &)));
-
-  AccountSettings s;
-  ui.accountList->addItems(s.knownAccounts());
-  curacc = s.lastAccount();
-  if(!ui.accountList->count()) {
-    //if(doAutoConnect) reject();
-
-    setAccountEditEnabled(false);
-    QString newacc = QInputDialog::getText(this, tr("Create Account"), tr(
-                                           "In order to connect to a Quassel Core, you need to create an account.<br>"
-                                           "Please enter a name for this account now:"), QLineEdit::Normal, tr("Default"));
-    if(!newacc.isEmpty()) {
-      ui.accountList->addItem(newacc);
-      ui.hostEdit->setText("localhost");
-      ui.port->setValue(DEFAULT_PORT);
-      ui.internalCore->setChecked(false);
-      setAccountEditEnabled(true);
-    }
-    /*
-    // FIXME We create a default account here that just connects to the internal core
-    curacc = "Default";
-    ui.accountList->addItem("Default");
-    ui.internalCore->setChecked(true);
-    ui.userEdit->setText("Default");
-    ui.passwdEdit->setText("password");
-    ui.rememberPasswd->setChecked(true);
-    accountChanged(curacc);
-    ui.passwdEdit->setText("password");
-    ui.accountList->setCurrentIndex(0);
-    ui.autoConnect->setChecked(true);
-    autoConnectToggled(true);
-    */
-  } else {
-    if(!curacc.isEmpty()) {
-      //if(doAutoConnect) { qDebug() << "auto";
-      //  AccountSettings s;
-      //  int idx = ui.accountList->findText(s.autoConnectAccount());
-      //  if(idx < 0) reject();
-      //  else {
-      //    ui.accountList->setCurrentIndex(idx);
-      //    doConnect();
-      //  }
-      //} else {
-        int idx = ui.accountList->findText(curacc);
-        ui.accountList->setCurrentIndex(idx);
-      //}
-    }
+  if(currentItem) ui.accountList->setCurrentItem(currentItem);
+  else ui.accountList->setCurrentRow(0);
+
+  setAccountWidgetStates();
+
+  ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
+  //ui.accountButtonBox->button(QDialogButtonBox::Ok)->setAutoDefault(true);
+
+  connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
+  connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
+  connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
+  connect(clientSyncer, SIGNAL(encrypted(bool)), this, SLOT(encrypted(bool)));
+  connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
+  connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
+  connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
+  connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
+  connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
+  connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
+  connect(clientSyncer, SIGNAL(channelsProgress(quint32, quint32)), this, SLOT(coreChannelsProgress(quint32, quint32)));
+  connect(clientSyncer, SIGNAL(ircUsersProgress(quint32, quint32)), this, SLOT(coreIrcUsersProgress(quint32, quint32)));
+  connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
+
+  connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
+  connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
+
+  connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
+  connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
+
+  if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
+     && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
+    doingAutoConnect = true;
+    on_accountButtonBox_accepted();
   }
 }
 
 CoreConnectDlg::~CoreConnectDlg() {
-  //qDebug() << "destroy";
-}
-
-void CoreConnectDlg::setAccountEditEnabled(bool en) {
-  ui.accountList->setEnabled(en);
-  ui.hostEdit->setEnabled(en && !ui.internalCore->isChecked());
-  ui.userEdit->setEnabled(en);
-  ui.passwdEdit->setEnabled(en);
-  ui.port->setEnabled(en && !ui.internalCore->isChecked());
-  ui.editAccount->setEnabled(en);
-  ui.delAccount->setEnabled(en);
-  ui.internalCore->setEnabled(en);
-  ui.rememberPasswd->setEnabled(en);
-  ui.autoConnect->setEnabled(en);
-  ui.buttonBox1->button(QDialogButtonBox::Ok)->setEnabled(en && checkInputValid());
-}
-
-void CoreConnectDlg::accountChanged(const QString &text) {
-  AccountSettings s;
-  if(!curacc.isEmpty()) {
-    QVariantMap oldAcc;
-    oldAcc["User"] = ui.userEdit->text();
-    oldAcc["Host"] = ui.hostEdit->text();
-    oldAcc["Port"] = ui.port->value();
-    oldAcc["InternalCore"] = ui.internalCore->isChecked();
-    if(ui.rememberPasswd->isChecked()) oldAcc["Password"] = ui.passwdEdit->text();
-    s.setValue(curacc, "AccountData", oldAcc);
+  if(ui.accountList->selectedItems().count()) {
+    CoreAccountSettings s;
+    s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
   }
-  ui.autoConnect->setChecked(false);
-  if(!text.isEmpty()) { // empty text: just save stuff
-    curacc = text;
-    s.setLastAccount(curacc);
-    QVariantMap newAcc = s.value(curacc, "AccountData").toMap();
-    ui.userEdit->setText(newAcc["User"].toString());
-    ui.hostEdit->setText(newAcc["Host"].toString());
-    ui.port->setValue(newAcc["Port"].toInt());
-    ui.internalCore->setChecked(newAcc["InternalCore"].toBool());
-    if(newAcc.contains("Password")) {
-      ui.passwdEdit->setText(newAcc["Password"].toString());
-      ui.rememberPasswd->setChecked(true);
-    } else ui.rememberPasswd->setChecked(false);
-    if(s.autoConnectAccount() == curacc) ui.autoConnect->setChecked(true);
+}
+
+
+/****************************************************
+ * Account Management
+ ***************************************************/
+
+void CoreConnectDlg::on_accountList_itemSelectionChanged() {
+  setAccountWidgetStates();
+}
+
+void CoreConnectDlg::setAccountWidgetStates() {
+  QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
+  ui.editAccount->setEnabled(selectedItems.count());
+  ui.deleteAccount->setEnabled(selectedItems.count());
+  ui.autoConnect->setEnabled(selectedItems.count());
+  if(selectedItems.count()) {
+    ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
   }
+  ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
 }
 
-void CoreConnectDlg::autoConnectToggled(bool autoConnect) {
-  AccountSettings s;
-  if(autoConnect) s.setAutoConnectAccount(curacc);
-  else s.setAutoConnectAccount("");
+void CoreConnectDlg::on_autoConnect_clicked(bool state) {
+  if(!state) {
+    autoConnectAccount = 0;
+  } else {
+    if(ui.accountList->selectedItems().count()) {
+      autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
+    } else {
+      qWarning() << "Checked auto connect without an enabled item!";  // should never happen!
+      autoConnectAccount = 0;
+    }
+  }
+  setAccountWidgetStates();
 }
 
-bool CoreConnectDlg::checkInputValid() {
-  bool res = (ui.internalCore->isChecked() || ui.hostEdit->text().count()) && ui.userEdit->text().count();
-  ui.buttonBox1->button(QDialogButtonBox::Ok)->setEnabled(res);
-  return res;
+void CoreConnectDlg::on_addAccount_clicked() {
+  QStringList existing;
+  for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
+  CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
+  if(dlg.exec() == QDialog::Accepted) {
+    // find free ID
+    AccountId id = accounts.count() + 1;
+    for(AccountId i = 1; i <= accounts.count(); i++) {
+      if(!accounts.keys().contains(i)) {
+        id = i;
+        break;
+      }
+    }
+    QVariantMap data = dlg.accountData();
+    data["AccountId"] = QVariant::fromValue<AccountId>(id);
+    accounts[id] = data;
+    QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
+    item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
+    ui.accountList->setCurrentItem(item);
+  }
 }
 
-void CoreConnectDlg::createAccount() {
-  QString accname = QInputDialog::getText(this, tr("Create Account"), tr("Please enter a name for the new account:"));
-  if(accname.isEmpty()) return;
-  if(ui.accountList->findText(accname) >= 0) {
-    QMessageBox::warning(this, tr("Account name already exists!"), tr("An account named '%1' already exists, and account names must be unique!").arg(accname));
-    return;
+void CoreConnectDlg::on_editAccount_clicked() {
+  QStringList existing;
+  for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
+  AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
+  QVariantMap acct = accounts[id];
+  CoreAccountEditDlg dlg(id, acct, existing, this);
+  if(dlg.exec() == QDialog::Accepted) {
+    QVariantMap data = dlg.accountData();
+    ui.accountList->currentItem()->setText(data["AccountName"].toString());
+    accounts[id] = data;
   }
-  QVariantMap defdata;
-  ui.accountList->addItem(accname);
-  ui.accountList->setCurrentIndex(ui.accountList->findText(accname));
-  setAccountEditEnabled(true);
-}
-
-void CoreConnectDlg::removeAccount() {
-  QString acc = ui.accountList->currentText();
-  int res = QMessageBox::warning(this, tr("Delete account?"), tr("Do you really want to delete the data for the account '%1'?<br>"
-                                                       "Note that this only affects your local account settings and will not remove "
-                                                       "any data from the core.").arg(acc),
-                             QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
-  if(res == QMessageBox::Yes) {
-    AccountSettings s;
-    s.removeAccount(acc);
-    curacc = "";
-    ui.accountList->removeItem(ui.accountList->findText(acc));
-    if(!ui.accountList->count()) setAccountEditEnabled(false);
+}
+
+void CoreConnectDlg::on_deleteAccount_clicked() {
+  AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
+  int ret = QMessageBox::question(this, tr("Remove Account Settings"),
+                                  tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
+                                  "Note: This will <em>not</em> remove or change any data on the Core itself!"),
+                                  QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
+  if(ret == QMessageBox::Yes) {
+    int idx = ui.accountList->currentRow();
+    delete ui.accountList->takeItem(idx);
+    ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
+    accounts[id]["Delete"] = true;  // we only flag this here, actual deletion happens on accept!
+    setAccountWidgetStates();
   }
 }
 
-bool CoreConnectDlg::willDoInternalAutoConnect() {
-  AccountSettings s;
-  if(Global::runMode != Global::Monolithic) return false;
-  if(ui.autoConnect->isChecked() && s.autoConnectAccount() == curacc && ui.internalCore->isChecked()) {
-    return true;
+void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
+  Q_UNUSED(item);
+  on_accountButtonBox_accepted();
+}
+
+void CoreConnectDlg::on_accountButtonBox_accepted() {
+  // save accounts
+  CoreAccountSettings s;
+  foreach(QVariantMap acct, accounts.values()) {
+    AccountId id = acct["AccountId"].value<AccountId>();
+    if(acct.contains("Delete")) {
+      s.removeAccount(id);
+    } else {
+      s.storeAccountData(id, acct);
+    }
   }
-  return false;
+  s.setAutoConnectAccount(autoConnectAccount);
+
+  ui.stackedWidget->setCurrentWidget(ui.loginPage);
+  account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
+  accountData = accounts[account];
+  s.setLastAccount(account);
+  connectToCore();
+}
+
+/*****************************************************
+ * Connecting to the Core
+ ****************************************************/
+
+/*** Phase One: initializing the core connection ***/
+
+void CoreConnectDlg::connectToCore() {
+  ui.secureConnection->hide();
+  ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-disconnect")));
+  ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
+  ui.coreInfoLabel->setText("");
+  ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
+  ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
+  ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
+  ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
+  disconnect(ui.loginButtonBox, 0, this, 0);
+  connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
+
+  clientSyncer->connectToCore(accountData);
 }
 
-void CoreConnectDlg::doAutoConnect() {
-  AccountSettings s;
-  if(s.autoConnectAccount() == curacc) {
-    doConnect();
+void CoreConnectDlg::initPhaseError(const QString &error) {
+  doingAutoConnect = false;
+  ui.secureConnection->hide();
+  ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/status/dialog-error")));
+  //ui.connectLabel->setBrush(QBrush("red"));
+  ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
+  ui.coreInfoLabel->setText(error);
+  ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
+  ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
+  disconnect(ui.loginButtonBox, 0, this, 0);
+  connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
+  connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
+}
+
+void CoreConnectDlg::initPhaseMsg(const QString &msg) {
+  ui.coreInfoLabel->setText(msg);
+}
+
+void CoreConnectDlg::encrypted(bool useSsl) {
+  if(useSsl)
+    ui.secureConnection->show();
+  else
+    ui.secureConnection->hide();
+}
+
+void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
+  QString s;
+  QString host = accountData["Host"].toString();
+  switch(state) {
+    case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
+    case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
+    case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
+    case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
+    default: s = tr("Unknown connection state to %1"); break;
   }
+  ui.connectLabel->setText(s);
 }
 
-void CoreConnectDlg::doConnect() {
-  accountChanged(); // save current account info
-  QVariantMap conninfo;
-  ui.stackedWidget->setCurrentIndex(1);
-  if(ui.internalCore->isChecked()) {
-    // FIXME
-    coreConnectionError(tr("Can't connect to internal core at the moment [serious breakage due to switch to dynamic signals]. Please check back later."));
-    return;
-    if(Global::runMode != Global::Monolithic) {
-      coreConnectionError(tr("Can't connect to internal core, since we are running as a standalone GUI!"));
-      return;
+void CoreConnectDlg::restartPhaseNull() {
+  doingAutoConnect = false;
+  ui.stackedWidget->setCurrentWidget(ui.accountPage);
+  clientSyncer->disconnectFromCore();
+}
+
+/*********************************************************
+ * Phase Two: Login
+ *********************************************************/
+
+void CoreConnectDlg::startLogin() {
+  ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-connect")));
+  ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
+  //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
+  ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
+  ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
+  ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
+  if(!accountData["User"].toString().isEmpty()) {
+    ui.user->setText(accountData["User"].toString());
+    if(accountData["RememberPasswd"].toBool()) {
+      ui.password->setText(accountData["Password"].toString());
+      ui.rememberPasswd->setChecked(true);
+      ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
+    } else {
+      ui.rememberPasswd->setChecked(false);
+      ui.password->setFocus();
     }
-    ui.connectionGroupBox->setTitle(tr("Connecting to internal core"));
-    ui.connectionProgress->hide();
+  } else ui.user->setFocus();
+  disconnect(ui.loginButtonBox, 0, this, 0);
+  connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
+  connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
+  if(doingAutoConnect) doLogin();
+}
+
+void CoreConnectDlg::doLogin() {
+  QVariantMap loginData;
+  loginData["User"] = ui.user->text();
+  loginData["Password"] = ui.password->text();
+  loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
+  doLogin(loginData);
+}
+
+void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
+  disconnect(ui.loginButtonBox, 0, this, 0);
+  connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
+  connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
+  ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
+  ui.loginGroup->setTitle(tr("Logging in..."));
+  ui.user->setDisabled(true);
+  ui.password->setDisabled(true);
+  ui.rememberPasswd->setDisabled(true);
+  ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
+  accountData["User"] = loginData["User"];
+  accountData["RememberPasswd"] = loginData["RememberPasswd"];
+  if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
+  else accountData.remove("Password");
+  ui.user->setText(loginData["User"].toString());
+  ui.password->setText(loginData["Password"].toString());
+  ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
+  CoreAccountSettings s;
+  s.storeAccountData(account, accountData);
+  clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
+}
+
+void CoreConnectDlg::setLoginWidgetStates() {
+  ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
+}
+
+void CoreConnectDlg::loginFailed(const QString &error) {
+  if(wizard) {
+    wizard->reject();
+  }
+  ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
+  ui.loginGroup->setTitle(tr("Login"));
+  ui.user->setEnabled(true);
+  ui.password->setEnabled(true);
+  ui.rememberPasswd->setEnabled(true);
+  ui.coreInfoLabel->setText(error);
+  ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
+  ui.password->setFocus();
+  doingAutoConnect = false;
+}
+
+void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
+  storageBackends = backends;
+  ui.loginStack->setCurrentWidget(ui.coreConfigPage);
+
+  //on_launchCoreConfigWizard_clicked();
+
+}
+
+void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
+  Q_ASSERT(!wizard);
+  wizard = new CoreConfigWizard(storageBackends, this);
+  connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
+  connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
+  connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
+  connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
+  connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
+  connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
+  connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
+  connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
+  wizard->show();
+}
+
+void CoreConnectDlg::configWizardAccepted() {
+
+  wizard->deleteLater();
+  wizard = 0;
+}
+
+void CoreConnectDlg::configWizardRejected() {
+
+  wizard->deleteLater();
+  wizard = 0;
+  //exit(1); // FIXME
+}
+
+
+/************************************************************
+ * Phase Three: Syncing
+ ************************************************************/
+
+void CoreConnectDlg::startSync() {
+  ui.sessionProgress->setRange(0, 1);
+  ui.sessionProgress->setValue(0);
+  ui.networksProgress->setRange(0, 1);
+  ui.networksProgress->setValue(0);
+  ui.channelsProgress->setRange(0, 1);
+  ui.channelsProgress->setValue(0);
+  ui.ircUsersProgress->setRange(0, 1);
+  ui.ircUsersProgress->setValue(0);
+
+  ui.stackedWidget->setCurrentWidget(ui.syncPage);
+  // clean up old page
+  ui.loginGroup->setTitle(tr("Login"));
+  ui.user->setEnabled(true);
+  ui.password->setEnabled(true);
+  ui.rememberPasswd->setEnabled(true);
+  ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
+}
+
+void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
+  ui.sessionProgress->setRange(0, max);
+  ui.sessionProgress->setValue(val);
+
+}
+
+void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
+  if(max == 0) {
+    ui.networksProgress->setFormat("0/0");
+    ui.networksProgress->setRange(0, 1);
+    ui.networksProgress->setValue(1);
   } else {
-    ui.connectionGroupBox->setTitle(tr("Connecting to %1").arg(ui.hostEdit->text()));
-    conninfo["Host"] = ui.hostEdit->text();
-    conninfo["Port"] = ui.port->value();
+    ui.networksProgress->setFormat("%v/%m");
+    ui.networksProgress->setRange(0, max);
+    ui.networksProgress->setValue(val);
   }
-  conninfo["User"] = ui.userEdit->text();
-  conninfo["Password"] = ui.passwdEdit->text();
-  ui.profileLabel->hide(); ui.guiProfile->hide();
-  ui.newGuiProfile->hide(); ui.alwaysUseProfile->hide();
-  ui.connectionProgress->show();
-  try {
-    Client::instance()->connectToCore(conninfo);
-  } catch(Exception e) {
-    QString msg;
-    //if(!e.msg().isEmpty()) msg = tr("<br>%1").arg(e.msg()); // FIXME throw more detailed (vulgo: any) error msg
-    coreConnectionError(tr("Invalid user or password. Pleasy try again.%1").arg(msg));
-    //QMessageBox::warning(this, tr("Unknown account"), tr("Invalid user or password. Pleasy try again.%1").arg(msg));
-    //cancelConnect();
-    return;
+}
+
+void CoreConnectDlg::coreChannelsProgress(quint32 val, quint32 max) {
+  if(max == 0) {
+    ui.channelsProgress->setFormat("0/0");
+    ui.channelsProgress->setRange(0, 1);
+    ui.channelsProgress->setValue(1);
+  } else {
+    ui.channelsProgress->setFormat("%v/%m");
+    ui.channelsProgress->setRange(0, max);
+    ui.channelsProgress->setValue(val);
   }
 }
 
-void CoreConnectDlg::cancelConnect() {
-  ui.stackedWidget->setCurrentIndex(0);
-}
-
-void CoreConnectDlg::setStartState() { /*
-  ui.hostName->show(); ui.hostPort->show(); ui.hostLabel->show(); ui.portLabel->show();
-  ui.statusText->setText(tr("Connect to Quassel Core running on:"));
-  ui.buttonBox->button(QDialogButtonBox::Ok)->show();
-  ui.hostName->setEnabled(true); ui.hostPort->setEnabled(true);
-  ui.hostName->setSelection(0, ui.hostName->text().length()); */
-  ui.stackedWidget->setCurrentIndex(0);
-}
-
-void CoreConnectDlg::hostEditChanged(QString /*txt*/) {
-  //ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(txt.length());
-}
-
-void CoreConnectDlg::hostSelected() { /*
-  ui.hostName->hide(); ui.hostPort->hide(); ui.hostLabel->hide(); ui.portLabel->hide();
-  ui.statusText->setText(tr("Connecting to %1:%2" ).arg(ui.hostName->text()).arg(ui.hostPort->value()));
-  ui.buttonBox->button(QDialogButtonBox::Ok)->hide();
-  connect(ClientProxy::instance(), SIGNAL(coreConnected()), this, SLOT(coreConnected()));
-  connect(ClientProxy::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
-  Client::instance()->connectToCore(ui.hostName->text(), ui.hostPort->value());
-*/
-}
-
-void CoreConnectDlg::coreConnected() { /*
-  ui.hostLabel->hide(); ui.hostName->hide(); ui.portLabel->hide(); ui.hostPort->hide();
-  ui.statusText->setText(tr("Synchronizing..."));
-  QSettings s;
-  s.setValue("GUI/CoreHost", ui.hostName->text());
-  s.setValue("GUI/CorePort", ui.hostPort->value());
-  s.setValue("GUI/CoreAutoConnect", ui.autoConnect->isChecked());
-  connect(ClientProxy::instance(), SIGNAL(recvPartialItem(quint32, quint32)), this, SLOT(updateProgressBar(quint32, quint32)));
-  connect(ClientProxy::instance(), SIGNAL(csCoreState(QVariant)), this, SLOT(recvCoreState(QVariant)));
-  ui.progressBar->show();
-  QVariantMap initmsg;
-  initmsg["GUIProtocol"] = GUI_PROTOCOL;
-  // FIXME guiProxy->send(GS_CLIENT_INIT, QVariant(initmsg)); */
-  ui.connectionStatus->setText(tr("Connected to core."));
-  accept();
-}
-
-void CoreConnectDlg::coreConnectionError(QString err) {
-  ui.stackedWidget->setCurrentIndex(0);
-  show(); // just in case we started hidden
-  QMessageBox::warning(this, tr("Connection Error"), tr("<b>Could not connect to Quassel Core!</b><br>\n") + err, QMessageBox::Retry);
-  //disconnect(ClientProxy::instance(), 0, this, 0); FIXME?
-  //ui.autoConnect->setChecked(false);
-  setStartState();
-}
-
-void CoreConnectDlg::updateProgressBar(uint partial, uint total) {
-  ui.connectionProgress->setMaximum(total);
-  ui.connectionProgress->setValue(partial);
-  //qDebug() << "progress:" << partial << total;
-}
-
-void CoreConnectDlg::recvCoreState(QVariant state) {
-  //ui.progressBar->hide();
-  coreState = state;
-  accept();
-}
-
-QVariant CoreConnectDlg::getCoreState() {
-  return coreState;
-}
-
-void CoreConnectDlg::showConfigWizard(const QVariantMap &coredata) {
-  QStringList storageProviders = coredata["StorageProviders"].toStringList();
-  ConfigWizard *wizard = new ConfigWizard(storageProviders, this);
-  wizard->exec();
-  QVariantMap reply;
-  reply["GuiProtocol"] = GUI_PROTOCOL;
-  reply["HasSettings"] = true;
-  reply["User"] = wizard->field("adminuser.name").toString();
-  reply["Password"] = wizard->field("adminuser.password").toString();
-  QString sp = storageProviders.value(wizard->field("storage.provider").toInt());
-  reply["Type"] = sp;
-  if (sp.compare("Sqlite", Qt::CaseInsensitive)) {
-    reply["Host"] = wizard->field("storage.host").toString();
-    reply["Port"] = wizard->field("storage.port").toString();
-    reply["Database"] = wizard->field("storage.database").toString();
-    reply["User"] = wizard->field("storage.user").toString();
-    reply["Password"] = wizard->field("storage.password").toString();
+void CoreConnectDlg::coreIrcUsersProgress(quint32 val, quint32 max) {
+  if(max == 0) {
+    ui.ircUsersProgress->setFormat("0/0");
+    ui.ircUsersProgress->setRange(0, 1);
+    ui.ircUsersProgress->setValue(1);
+  } else {
+    if(val % 100) return;
+    ui.ircUsersProgress->setFormat("%v/%m");
+    ui.ircUsersProgress->setRange(0, max);
+    ui.ircUsersProgress->setValue(val);
+  }
+}
+
+void CoreConnectDlg::syncFinished() {
+  if(!wizard) accept();
+  else {
+    hide();
+    disconnect(wizard, 0, this, 0);
+    connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
   }
-  Client::instance()->setCoreConfiguration(reply);
+}
+
+/*****************************************************************************************
+ * CoreAccountEditDlg
+ *****************************************************************************************/
+CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent)
+  : QDialog(parent)
+{
+  ui.setupUi(this);
+  existing = _existing;
+  if(id.isValid()) {
+    existing.removeAll(acct["AccountName"].toString());
+    ui.host->setText(acct["Host"].toString());
+    ui.port->setValue(acct["Port"].toUInt());
+    ui.useInternal->setChecked(acct["UseInternal"].toBool());
+    ui.accountName->setText(acct["AccountName"].toString());
+#ifndef QT_NO_OPENSSL
+    ui.useSsl->setChecked(acct["useSsl"].toBool());
+#else
+    ui.useSsl->setChecked(false);
+    ui.useSsl->setEnabled(false);
+#endif
+    ui.useProxy->setChecked(acct["useProxy"].toBool());
+    ui.proxyHost->setText(acct["proxyHost"].toString());
+    ui.proxyPort->setValue(acct["proxyPort"].toUInt());
+    ui.proxyType->setCurrentIndex(acct["proxyType"].toInt() == QNetworkProxy::Socks5Proxy ? 0 : 1);
+    ui.proxyUser->setText(acct["proxyUser"].toString());
+    ui.proxyPassword->setText(acct["proxyPassword"].toString());
+  } else {
+    setWindowTitle(tr("Add Core Account"));
+#ifdef QT_NO_OPENSSL
+    ui.useSsl->setChecked(false);
+    ui.useSsl->setEnabled(false);
+#endif
+  }
+}
+
+QVariantMap CoreAccountEditDlg::accountData() {
+  account["AccountName"] = ui.accountName->text().trimmed();
+  account["Host"] = ui.host->text().trimmed();
+  account["Port"] = ui.port->value();
+  account["UseInternal"] = ui.useInternal->isChecked();
+  account["useSsl"] = ui.useSsl->isChecked();
+  account["useProxy"] = ui.useProxy->isChecked();
+  account["proxyHost"] = ui.proxyHost->text().trimmed();
+  account["proxyPort"] = ui.proxyPort->value();
+  account["proxyType"] = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
+  account["proxyUser"] = ui.proxyUser->text().trimmed();
+  account["proxyPassword"] = ui.proxyPassword->text().trimmed();
+  return account;
+}
+
+void CoreAccountEditDlg::setWidgetStates() {
+  bool ok = !ui.accountName->text().trimmed().isEmpty() && !existing.contains(ui.accountName->text()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
+  ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
+}
+
+void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
+  Q_UNUSED(text);
+  setWidgetStates();
+}
+
+void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
+  Q_UNUSED(text);
+  setWidgetStates();
+}
+
+void CoreAccountEditDlg::on_useRemote_toggled(bool state) {
+  Q_UNUSED(state);
+  setWidgetStates();
 }