From a2f1c206f3468553da879e903c9cc3f175b8e52c Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sat, 12 Sep 2009 22:53:57 +0100
Subject: Multiple profiles in the login window mostly working.


diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index 15ca5f4..1de4011 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -200,6 +200,7 @@ void MainController::handleLoginRequest(const String &username, const String &pa
 	profileSettings->storeString("jid", username);
 	profileSettings->storeString("certificate", certificateFile);
 	profileSettings->storeString("pass", remember ? password : "");
+	loginWindow_->addAvailableAccount(profileSettings->getStringSetting("jid"), profileSettings->getStringSetting("pass"), profileSettings->getStringSetting("certificate"));
 	delete profileSettings;
 
 	resetClient();
diff --git a/Swift/QtUI/QtLoginWindow.cpp b/Swift/QtUI/QtLoginWindow.cpp
index d25f732..e9e135c 100644
--- a/Swift/QtUI/QtLoginWindow.cpp
+++ b/Swift/QtUI/QtLoginWindow.cpp
@@ -6,6 +6,7 @@
 
 #include <QApplication>
 #include <QBoxLayout>
+#include <QComboBox>
 #include <QFileDialog>
 #include <QStatusBar>
 #include <QToolButton>
@@ -38,7 +39,8 @@ QtLoginWindow::QtLoginWindow() : QMainWindow() {
 	layout->addWidget(logo);
 	layout->addStretch();
 
-	username_ = new QLineEdit(this);
+	username_ = new QComboBox(this);
+	username_->setEditable(true);
 	layout->addWidget(username_);
 
 	QWidget* w = new QWidget(this);
@@ -51,8 +53,8 @@ QtLoginWindow::QtLoginWindow() : QMainWindow() {
 	password_ = new QLineEdit(this);
 	password_->setEchoMode(QLineEdit::Password);
 	connect(password_, SIGNAL(returnPressed()), this, SLOT(loginClicked()));
-	connect(username_, SIGNAL(returnPressed()), password_, SLOT(setFocus()));
-	connect(username_, SIGNAL(returnPressed()), password_, SLOT(selectAll()));
+	connect(username_->lineEdit(), SIGNAL(returnPressed()), password_, SLOT(setFocus()));
+	connect(username_, SIGNAL(editTextChanged(const QString&)), this, SLOT(handleUsernameTextChanged()));
 	credentialsLayout->addWidget(password_);
 
 	certificateButton_ = new QToolButton(this);
@@ -100,13 +102,37 @@ QtLoginWindow::QtLoginWindow() : QMainWindow() {
 }
 
 void QtLoginWindow::addAvailableAccount(const String& defaultJID, const String& defaultPassword, const String& defaultCertificate) {
-	username_->setText(P2QSTRING(defaultJID));
-	password_->setText(P2QSTRING(defaultPassword));
-	certificateFile_ = P2QSTRING(defaultCertificate);
+	QString username = P2QSTRING(defaultJID);
+	int index = -1;
+	for (int i = 0; i < usernames_.count(); i++) {
+		if (username_->currentText() == usernames_[i]) {
+			index = i;
+		}
+	}
+	if (index == -1) {
+		usernames_.append(username);
+		passwords_.append(P2QSTRING(defaultPassword));
+		certificateFiles_.append(P2QSTRING(defaultCertificate));
+		username_->addItem(username);
+	} else {
+		usernames_[index] = username;
+		passwords_[index] = P2QSTRING(defaultPassword);
+		certificateFiles_[index] = P2QSTRING(defaultCertificate);
+	}
+}
+
+void QtLoginWindow::handleUsernameTextChanged() {
+	QString username = username_->currentText();
+	for (int i = 0; i < usernames_.count(); i++) {
+		if (username_->currentText() == usernames_[i]) {
+			certificateFile_ == certificateFiles_[i];
+			password_->setText(passwords_[i]);
+		}
+	}
 	if (!certificateFile_.isEmpty()) {
 		certificateButton_->setChecked(true);
 	}
-	remember_->setChecked(defaultPassword != "");
+	remember_->setChecked(password_->text() != "");
 }
 
 void QtLoginWindow::loggedOut() {
@@ -121,7 +147,7 @@ void QtLoginWindow::loggedOut() {
 
 void QtLoginWindow::loginClicked() {
 	setEnabled(false);
-	onLoginRequest(Q2PSTRING(username_->text()), Q2PSTRING(password_->text()), Q2PSTRING(certificateFile_), remember_->isChecked());
+	onLoginRequest(Q2PSTRING(username_->currentText()), Q2PSTRING(password_->text()), Q2PSTRING(certificateFile_), remember_->isChecked());
 }
 
 void QtLoginWindow::handleCertficateChecked(bool checked) {
diff --git a/Swift/QtUI/QtLoginWindow.h b/Swift/QtUI/QtLoginWindow.h
index a4ec606..ea40c71 100644
--- a/Swift/QtUI/QtLoginWindow.h
+++ b/Swift/QtUI/QtLoginWindow.h
@@ -15,6 +15,7 @@
 
 class QLabel;
 class QToolButton;
+class QComboBox;
 
 namespace Swift {
 	class QtLoginWindow : public QMainWindow, public LoginWindow {
@@ -32,14 +33,18 @@ namespace Swift {
 			void handleQuit();
 			void handleAbout();
 			void bringToFront();
+			void handleUsernameTextChanged();
 
 		private:
 			void setInitialMenus();
-			QLineEdit *username_;
-			QLineEdit *password_;
-			QPushButton *loginButton_;
-			QCheckBox *remember_;
-			QStackedWidget *stack_;
+			QStringList usernames_;
+			QStringList passwords_;
+			QStringList certificateFiles_;
+			QComboBox* username_;
+			QLineEdit* password_;
+			QPushButton* loginButton_;
+			QCheckBox* remember_;
+			QStackedWidget* stack_;
 			QLabel* message_;
 			QString certificateFile_;
 			QToolButton* certificateButton_;
-- 
cgit v0.10.2-6-g49f6