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 "ui_logindialog.h"
#include "keyring.h"
LoginDialog::LoginDialog(QWidget *parent, const QString& loginErrMsg) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
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())
ui->loginErrorLabel->setVisible(false);
@ -33,6 +39,10 @@ LoginDialog::LoginDialog(QWidget *parent, const QString& loginErrMsg) :
resize(minimumSizeHint());
layout()->setSizeConstraint(QLayout::SetFixedSize);
Keyring * k = Keyring::instance();
QStringList accounts = k->getStoredAccounts("minecraft");
ui->usernameTextBox->addItems(accounts);
}
LoginDialog::~LoginDialog()
@ -42,10 +52,49 @@ LoginDialog::~LoginDialog()
QString LoginDialog::getUsername() const
{
return ui->usernameTextBox->text();
return ui->usernameTextBox->currentText();
}
QString LoginDialog::getPassword() const
{
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

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

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>365</width>
<height>145</height>
<width>476</width>
<height>168</height>
</rect>
</property>
<property name="windowTitle">
@ -31,9 +31,9 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="usernameTextBox">
<property name="placeholderText">
<string>Username</string>
<widget class="QComboBox" name="usernameTextBox">
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
@ -54,20 +54,23 @@
</property>
</widget>
</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>
</item>
<item>
<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>
<widget class="QCheckBox" name="rememberUsernameCheckbox">
<property name="sizePolicy">

View File

@ -72,6 +72,14 @@ public:
*/
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:
/// fall back to StubKeyring if false
virtual bool isValid() { return false; }

View File

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

View File

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