rsyncui/password.cpp

56 lines
1.3 KiB
C++
Raw Normal View History

2023-03-05 13:39:16 +01:00
#include "mainwindow.h"
#include "password.h"
#include <qt5keychain/keychain.h>
#include <qt5keychain/qkeychain_export.h>
2023-03-09 18:13:46 +01:00
#include <QSettings>
2023-03-05 13:39:16 +01:00
using namespace std;
2023-03-09 18:13:46 +01:00
extern QApplication a;
2023-03-05 13:39:16 +01:00
2023-03-09 18:13:46 +01:00
QString appName = "RsyncUI";
2023-03-09 18:13:46 +01:00
static QSettings sett{appName, appName};
2023-03-09 18:13:46 +01:00
void setPassword(QString account, QString pass)
{
2023-03-09 18:13:46 +01:00
QKeychain::WritePasswordJob write(appName);
write.setSettings(&sett);
// write.setAutoDelete(false);
write.setKey(account);
write.setTextData(pass);
2023-03-09 18:13:46 +01:00
QEventLoop loop;
write.connect(&write, &QKeychain::WritePasswordJob::finished, &loop, &QEventLoop::quit);
2023-03-09 18:13:46 +01:00
write.start();
loop.exec();
2023-03-05 13:39:16 +01:00
2023-03-09 18:13:46 +01:00
if(write.error())
cout << "Error writing key1. Error: " << write.errorString().toStdString();
}
2023-03-09 18:13:46 +01:00
QString getPassword(const QString& name)
{
2023-03-09 18:13:46 +01:00
QKeychain::ReadPasswordJob readPass(appName);
readPass.setSettings(&sett);
// readPass.setAutoDelete(false);
readPass.setKey(name);
2023-03-09 18:13:46 +01:00
QEventLoop loop;
readPass.connect(&readPass, &QKeychain::ReadPasswordJob::finished, &loop, &QEventLoop::quit);
readPass.start();
loop.exec();
if(readPass.error())
{
qDebug()<<"KeyHandler~Error reading decrypt Pass. Error: " + readPass.errorString();
return "";
}
else
{
QString data = readPass.textData();
return data;
}
}