rsyncui/password.cpp

56 lines
1.3 KiB
C++

#include "mainwindow.h"
#include "password.h"
#include <qt5keychain/keychain.h>
#include <qt5keychain/qkeychain_export.h>
#include <QSettings>
using namespace std;
extern QApplication a;
QString appName = "RsyncUI";
static QSettings sett{appName, appName};
void setPassword(QString account, QString pass)
{
QKeychain::WritePasswordJob write(appName);
write.setSettings(&sett);
// write.setAutoDelete(false);
write.setKey(account);
write.setTextData(pass);
QEventLoop loop;
write.connect(&write, &QKeychain::WritePasswordJob::finished, &loop, &QEventLoop::quit);
write.start();
loop.exec();
if(write.error())
cout << "Error writing key1. Error: " << write.errorString().toStdString();
}
QString getPassword(const QString& name)
{
QKeychain::ReadPasswordJob readPass(appName);
readPass.setSettings(&sett);
// readPass.setAutoDelete(false);
readPass.setKey(name);
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;
}
}