1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-01-26 00:26:40 +00:00
UTPass/plugins/Pass/pass.cpp

141 lines
3.8 KiB
C++
Raw Normal View History

2019-09-20 21:29:39 +02:00
#include <QUrl>
#include <QtCore/QStandardPaths>
#include <QtCore/QDir>
#include "pass.h"
#include "gpg.h"
#include "passkeymodel.h"
Pass::Pass():
m_password_store (QStandardPaths::writableLocation(
2025-01-15 23:40:35 +01:00
QStandardPaths::AppDataLocation).append("/.password-store")),
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1))),
m_show_filename(QString())
2019-09-20 21:29:39 +02:00
{}
2025-01-14 08:15:03 +01:00
void Pass::initialize(QObject *window)
2019-09-20 21:29:39 +02:00
{
if (!window) {
qFatal("window is invalid. Abording.");
}
this->m_gpg = std::unique_ptr<Gpg>(new Gpg(window));
2025-01-15 23:40:35 +01:00
QObject::connect(this, &Pass::responsePassphraseDialogPropagate, this->m_gpg->passphrase_provider(),
&UTPassphraseProvider::handleResponse);
QObject::connect(this->m_gpg.get(), &Gpg::importKeysFromFileResult, this, &Pass::importGPGKeyResult);
QObject::connect(this->m_gpg.get(), &Gpg::getKeysResult, this, &Pass::getAllGPGKeysResult);
QObject::connect(this->m_gpg.get(), &Gpg::deleteKeyResult, this, &Pass::deleteGPGKeyResult);
QObject::connect(this->m_gpg.get(), &Gpg::decryptResult, this, &Pass::showResult);
2019-09-20 21:29:39 +02:00
QDir dir(m_password_store);
2025-01-10 13:48:38 +01:00
if (!dir.exists()) {
2019-09-20 21:29:39 +02:00
dir.mkpath(".");
2025-01-10 13:48:38 +01:00
}
qInfo() << "Password Store is :" << m_password_store;
2019-09-20 21:29:39 +02:00
}
bool Pass::show(QUrl url)
2019-09-20 21:29:39 +02:00
{
2025-01-15 23:40:35 +01:00
if (!this->m_sem->tryAcquire(1, 500)) {
return false;
}
auto path = url.toLocalFile();
2025-01-20 11:23:40 +01:00
qInfo() << "Pass show " << path;
QFileInfo file_info(path);
this->m_show_filename = file_info.completeBaseName();
return this->m_gpg->decryptFromFile(path);
}
2025-01-15 23:40:35 +01:00
void Pass::showResult(Error err, QString plain_text)
{
2025-01-20 11:23:40 +01:00
qDebug() << "Pass show Result";
if (err) {
2025-01-20 11:23:40 +01:00
qInfo() << "Pass show Failed";
emit showFailed(err.asString());
2025-01-17 10:40:54 +01:00
2025-01-20 11:23:40 +01:00
} else if (err.isCanceled()) {
qInfo() << "Pass show Cancelled";
emit showCancelled();
2019-09-20 21:29:39 +02:00
} else {
2025-01-20 11:23:40 +01:00
qInfo() << "Pass show Succeed";
emit showSucceed(this->m_show_filename, plain_text);
2019-09-20 21:29:39 +02:00
}
this->m_show_filename = QString();
this->m_sem->release(1);
2019-09-20 21:29:39 +02:00
}
2025-01-20 11:23:40 +01:00
bool Pass::deleteGPGKey(PassKeyModel* key)
2019-09-20 21:29:39 +02:00
{
2025-01-15 23:40:35 +01:00
if (!this->m_sem->tryAcquire(1, 500)) {
return false;
}
2025-01-20 11:23:40 +01:00
qInfo() << "Delete Key " << key->uid();
return this->m_gpg->deleteKey(key->key());
}
2025-01-15 23:40:35 +01:00
void Pass::deleteGPGKeyResult(Error err)
{
2025-01-20 11:23:40 +01:00
qDebug() << "Delete Ke yResult";
2025-01-15 23:40:35 +01:00
if (err) {
2025-01-20 11:23:40 +01:00
qInfo() << "Delete Key Failed";
emit deleteGPGKeyFailed(err.asString());
} else {
2025-01-20 11:23:40 +01:00
qInfo() << "Delete Key Succeed";
emit deleteGPGKeySucceed();
}
this->m_sem->release(1);
2019-09-20 21:29:39 +02:00
}
2025-01-14 08:15:03 +01:00
bool Pass::importGPGKey(QUrl url)
2019-09-20 21:29:39 +02:00
{
2025-01-15 23:40:35 +01:00
if (!this->m_sem->tryAcquire(1, 500)) {
return false;
}
2025-01-20 11:23:40 +01:00
qInfo() << "Import GPG Key from " << url;
return this->m_gpg->importKeysFromFile(url.toLocalFile());
}
2025-01-15 23:40:35 +01:00
void Pass::importGPGKeyResult(Error err)
{
2025-01-20 11:23:40 +01:00
qDebug() << "Import GPG Key Result";
2025-01-15 23:40:35 +01:00
if (err) {
2025-01-20 11:23:40 +01:00
qInfo() << "Delete Key Failed";
emit importGPGKeyFailed(err.asString());
} else {
2025-01-20 11:23:40 +01:00
qInfo() << "Delete Key Succeed";
emit importGPGKeySucceed();
}
this->m_sem->release(1);
2019-09-20 21:29:39 +02:00
}
bool Pass::getAllGPGKeys()
2019-09-20 21:29:39 +02:00
{
2025-01-15 23:40:35 +01:00
if (!this->m_sem->tryAcquire(1, 500)) {
return false;
}
2025-01-20 11:23:40 +01:00
qInfo() << "Get GPG keys";
return this->m_gpg->getAllKeys();
}
2025-01-15 23:40:35 +01:00
void Pass::getAllGPGKeysResult(Error err, std::vector<GpgME::Key> keys_info)
{
2025-01-20 11:23:40 +01:00
qDebug() << "Get GPG keys Result";
2025-01-15 23:40:35 +01:00
if (err) {
2025-01-20 11:23:40 +01:00
qInfo() << "Get GPG Failed";
emit getAllGPGKeysFailed(err.asString());
} else {
2025-01-20 11:23:40 +01:00
qInfo() << "Get GPG Succeed";
emit getAllGPGKeysSucceed(QVariant::fromValue(PassKeyModel::keysToPassKey(keys_info)));
}
this->m_sem->release(1);
2019-09-20 21:29:39 +02:00
}
2025-01-15 23:40:35 +01:00
void Pass::responsePassphraseDialog(bool cancel, QString passphrase)
{
2025-01-20 11:23:40 +01:00
qDebug() << "Propagate responsePassphraseDialog";
emit responsePassphraseDialogPropagate(cancel, passphrase);
}