emailQt/ParametersProvider.cpp

83 lines
2.5 KiB
C++
Raw Normal View History

2024-03-08 12:10:58 -05:00
#include "ParametersProvider.hpp"
ParametersProvider::ParametersProvider(QWidget *parent)
: QDialog(parent) {
auto *layout = new QVBoxLayout;
auto *user = new QLabel("Username", this);
2024-03-08 12:10:58 -05:00
userField = new QLineEdit;
auto *pass = new QLabel("Password", this);
2024-03-08 12:10:58 -05:00
passField = new QLineEdit;
passField->setEchoMode(QLineEdit::Password);
auto *imap = new QLabel("IMAP Server", this);
imapField = new QLineEdit;
2024-03-08 12:10:58 -05:00
auto *port = new QLabel("Port", this);
portField = new QLineEdit;
2024-03-08 12:10:58 -05:00
portField->setValidator(new QIntValidator(1, 1000, this));
auto *startButton = new QPushButton("OK", this);
layout->addWidget(user);
layout->addWidget(userField);
layout->addWidget(pass);
layout->addWidget(passField);
layout->addWidget(imap);
layout->addWidget(imapField);
layout->addWidget(port);
layout->addWidget(portField);
layout->addStretch(1);
layout->addWidget(startButton);
this->setLayout(layout);
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);
2024-03-08 12:10:58 -05:00
}
void ParametersProvider::start() {
sett.port = portField->text().toInt();
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)));
2024-03-08 12:10:58 -05:00
close();
emit done();
}
void ParametersProvider::setFocusInternal() {
activateWindow();
userField->setFocus();
}
const ParametersProvider::settings &ParametersProvider::getSettings() {
return sett;
}