Use Keyring in the login dialog

This commit is contained in:
Petr Mrázek 2013-03-26 17:43:49 +01:00
parent d24c4823ef
commit 737273348f
6 changed files with 88 additions and 18 deletions

View File

@ -15,12 +15,18 @@
#include "logindialog.h" #include "logindialog.h"
#include "ui_logindialog.h" #include "ui_logindialog.h"
#include "keyring.h"
LoginDialog::LoginDialog(QWidget *parent, const QString& loginErrMsg) : LoginDialog::LoginDialog(QWidget *parent, const QString& loginErrMsg) :
QDialog(parent), QDialog(parent),
ui(new Ui::LoginDialog) ui(new Ui::LoginDialog)
{ {
ui->setupUi(this); ui->setupUi(this);
//FIXME: translateable?
ui->usernameTextBox->lineEdit()->setPlaceholderText(QApplication::translate("LoginDialog", "Name", 0));
connect(ui->usernameTextBox, SIGNAL(currentTextChanged(QString)), this, SLOT(userTextChanged(QString)));
connect(ui->forgetButton, SIGNAL(clicked(bool)), this, SLOT(forgetCurrentUser()));
if (loginErrMsg.isEmpty()) if (loginErrMsg.isEmpty())
ui->loginErrorLabel->setVisible(false); ui->loginErrorLabel->setVisible(false);
@ -33,6 +39,10 @@ LoginDialog::LoginDialog(QWidget *parent, const QString& loginErrMsg) :
resize(minimumSizeHint()); resize(minimumSizeHint());
layout()->setSizeConstraint(QLayout::SetFixedSize); layout()->setSizeConstraint(QLayout::SetFixedSize);
Keyring * k = Keyring::instance();
QStringList accounts = k->getStoredAccounts("minecraft");
ui->usernameTextBox->addItems(accounts);
} }
LoginDialog::~LoginDialog() LoginDialog::~LoginDialog()
@ -42,10 +52,49 @@ LoginDialog::~LoginDialog()
QString LoginDialog::getUsername() const QString LoginDialog::getUsername() const
{ {
return ui->usernameTextBox->text(); return ui->usernameTextBox->currentText();
} }
QString LoginDialog::getPassword() const QString LoginDialog::getPassword() const
{ {
return ui->passwordTextBox->text(); return ui->passwordTextBox->text();
} }
void LoginDialog::forgetCurrentUser()
{
Keyring * k = Keyring::instance();
QString acct = ui->usernameTextBox->currentText();
k->removeStoredAccount("minecraft", acct);
ui->passwordTextBox->clear();
int index = ui->usernameTextBox->findText(acct);
if(index != -1)
ui->usernameTextBox->removeItem(index);
}
void LoginDialog::userTextChanged ( const QString& user )
{
Keyring * k = Keyring::instance();
QString acct = ui->usernameTextBox->currentText();
QString passwd = k->getPassword("minecraft",acct);
ui->passwordTextBox->setText(passwd);
}
void LoginDialog::accept()
{
bool saveName = ui->rememberUsernameCheckbox->isChecked();
bool savePass = ui->rememberPasswordCheckbox->isChecked();
if(saveName)
{
Keyring * k = Keyring::instance();
if(savePass)
{
k->storePassword("minecraft",getUsername(),getPassword());
}
else
{
k->storePassword("minecraft",getUsername(),QString());
}
}
QDialog::accept();
}

View File

@ -33,6 +33,10 @@ public:
QString getUsername() const; QString getUsername() const;
QString getPassword() const; QString getPassword() const;
public slots:
virtual void accept();
virtual void userTextChanged(const QString& user);
virtual void forgetCurrentUser();
private: private:
Ui::LoginDialog *ui; Ui::LoginDialog *ui;
}; };

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>365</width> <width>476</width>
<height>145</height> <height>168</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -31,9 +31,9 @@
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
<widget class="QLineEdit" name="usernameTextBox"> <widget class="QComboBox" name="usernameTextBox">
<property name="placeholderText"> <property name="editable">
<string>Username</string> <bool>true</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -54,20 +54,23 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="2" rowspan="2">
<widget class="QPushButton" name="forgetButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Forget</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="checkboxLayout"> <layout class="QHBoxLayout" name="checkboxLayout">
<item>
<widget class="QPushButton" name="forceUpdateButton">
<property name="text">
<string>&amp;Force Update</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item> <item>
<widget class="QCheckBox" name="rememberUsernameCheckbox"> <widget class="QCheckBox" name="rememberUsernameCheckbox">
<property name="sizePolicy"> <property name="sizePolicy">

View File

@ -72,6 +72,14 @@ public:
*/ */
virtual QStringList getStoredAccounts(QString service) = 0; virtual QStringList getStoredAccounts(QString service) = 0;
/**
* @brief Remove the specified account from storage
* @param service the service name
* @param username the account name
* @return
*/
virtual void removeStoredAccount(QString service, QString username) = 0;
protected: protected:
/// fall back to StubKeyring if false /// fall back to StubKeyring if false
virtual bool isValid() { return false; } virtual bool isValid() { return false; }

View File

@ -90,6 +90,12 @@ QStringList StubKeyring::getStoredAccounts(QString service)
return out; return out;
} }
void StubKeyring::removeStoredAccount ( QString service, QString username )
{
QString key = generateKey(service, username);
m_settings.remove(key);
}
StubKeyring::StubKeyring() : StubKeyring::StubKeyring() :
m_settings(QSettings::UserScope, "Orochimarufan", "Keyring") m_settings(QSettings::UserScope, "Orochimarufan", "Keyring")
{ {

View File

@ -29,7 +29,7 @@ public:
virtual QString getPassword(QString service, QString username); virtual QString getPassword(QString service, QString username);
virtual bool hasPassword(QString service, QString username); virtual bool hasPassword(QString service, QString username);
virtual QStringList getStoredAccounts(QString service); virtual QStringList getStoredAccounts(QString service);
virtual void removeStoredAccount(QString service, QString username);
private: private:
friend class Keyring; friend class Keyring;
explicit StubKeyring(); explicit StubKeyring();