ParametersProvider: Save password and user settings.

This commit is contained in:
Timothée Leclaire-Fournier 2024-03-11 09:52:48 -04:00
parent 4c00cd8040
commit 2fee534653
5 changed files with 110 additions and 5 deletions

View File

@ -23,7 +23,9 @@ add_executable(emailQt main.cpp
Email.cpp
Email.hpp
EmailDetails.cpp
EmailDetails.hpp)
EmailDetails.hpp
KeychainClass.cpp
KeychainClass.hpp)
target_link_libraries(emailQt PRIVATE Qt6::Core
Qt6::Gui

33
KeychainClass.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "KeychainClass.hpp"
KeychainClass::KeychainClass(QObject *parent)
: QObject(parent),
readCredentialJob(QLatin1String(Keychain::url)),
writeCredentialJob(QLatin1String(Keychain::url)),
deleteCredentialJob(QLatin1String(Keychain::url)) {
readCredentialJob.setAutoDelete(false);
writeCredentialJob.setAutoDelete(false);
deleteCredentialJob.setAutoDelete(false);
}
void KeychainClass::readKey(QString const &key) {
readCredentialJob.setKey(key);
connect(&readCredentialJob, &QKeychain::ReadPasswordJob::finished, this, [this, key]() {
if (readCredentialJob.error())
return;
emit keyRead(key, readCredentialJob.textData());
});
readCredentialJob.start();
}
void KeychainClass::writeKey(QString const &key, QString const &value) {
writeCredentialJob.setKey(key);
writeCredentialJob.setTextData(value);
writeCredentialJob.start();
}
void KeychainClass::deleteKey(QString const &key) {
deleteCredentialJob.setKey(key);
deleteCredentialJob.start();
}

30
KeychainClass.hpp Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include <QObject>
#include <QString>
#include <qt6keychain/keychain.h>
namespace Keychain {
constexpr char url[]{"keychain.emailQt.kvuj.app"};
const QString passKey("Password");
}
class KeychainClass : public QObject {
Q_OBJECT
public:
explicit KeychainClass(QObject *parent = nullptr);
void readKey(const QString &key);
void writeKey(const QString &key, const QString &value);
void deleteKey(const QString &key);
signals:
void keyRead(const QString &key, const QString &value);
private:
QKeychain::ReadPasswordJob readCredentialJob;
QKeychain::WritePasswordJob writeCredentialJob;
QKeychain::DeletePasswordJob deleteCredentialJob;
};

View File

@ -12,10 +12,10 @@ ParametersProvider::ParametersProvider(QWidget *parent)
passField->setEchoMode(QLineEdit::Password);
auto *imap = new QLabel("IMAP Server", this);
imapField = new QLineEdit("imap.gmail.com");
imapField = new QLineEdit;
auto *port = new QLabel("Port", this);
portField = new QLineEdit("993");
portField = new QLineEdit;
portField->setValidator(new QIntValidator(1, 1000, this));
auto *startButton = new QPushButton("OK", this);
@ -36,6 +36,25 @@ ParametersProvider::ParametersProvider(QWidget *parent)
resize(300, 450);
connect(startButton, &QPushButton::clicked, this, &ParametersProvider::start);
// Keychain
connect(&keychain, &KeychainClass::keyRead, this, &ParametersProvider::keyRead);
keychain.readKey(Keychain::passKey);
// Ne pas overrider les defaults
userField->setText(mSettings.value("Username").toString());
imapField->setText(mSettings.value("Server").toString());
portField->setText(mSettings.value("Port").toString());
if (imapField->text().isEmpty())
imapField->setText("imap.gmail.com");
if (portField->text().isEmpty())
portField->setText("993");
}
void ParametersProvider::keyRead(const QString &key, const QString &value) {
if (key == Keychain::passKey)
passField->setText(value);
}
void ParametersProvider::start() {
@ -43,6 +62,13 @@ void ParametersProvider::start() {
sett.userS = userField->text().toStdString();
sett.passS = passField->text().toStdString();
sett.imapS = imapField->text().toStdString();
// Saved login data
keychain.writeKey(Keychain::passKey, QString::fromStdString(sett.passS));
mSettings.setValue("Username", QString::fromStdString(sett.userS));
mSettings.setValue("Server", QString::fromStdString(sett.imapS));
mSettings.setValue("Port", QString::fromStdString(std::to_string(sett.port)));
close();
emit done();
}
@ -54,4 +80,4 @@ void ParametersProvider::setFocusInternal() {
const ParametersProvider::settings &ParametersProvider::getSettings() {
return sett;
}
}

View File

@ -5,7 +5,12 @@
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSettings>
#include <QString>
#include <QVBoxLayout>
#include <QWidget>
#include "KeychainClass.hpp"
class ParametersProvider : public QDialog {
Q_OBJECT
@ -19,14 +24,23 @@ public:
explicit ParametersProvider(QWidget *parent = nullptr);
~ParametersProvider() = default;
void start();
void setFocusInternal();
const settings &getSettings();
signals:
void done();
public slots:
// Keychain to save password
void keyRead(const QString &key, const QString &value);
// When clicking OK
void start();
private:
settings sett;
QLineEdit *userField, *passField, *imapField, *portField;
KeychainClass keychain;
QSettings mSettings;
};