1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-02-11 23:17:15 +00:00
UTPass/plugins/Pass/pass.cpp

205 lines
5.9 KiB
C++
Raw Normal View History

2019-09-20 21:29:39 +02:00
#include <QUrl>
#include <QtCore/QStandardPaths>
#include <QtCore/QDir>
#include "jobs/getkeysjob.h"
2025-01-29 16:42:37 +01:00
#include "jobs/importkeyjob.h"
2019-09-20 21:29:39 +02:00
#include "pass.h"
Pass::Pass():
m_password_store (QStandardPaths::writableLocation(
2025-01-15 23:40:35 +01:00
QStandardPaths::AppDataLocation).append("/.password-store")),
2025-01-29 16:42:37 +01:00
m_gpg_home (QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.rnp")),
2025-01-30 22:46:46 +01:00
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1)))
2025-01-29 16:42:37 +01:00
{
2025-01-29 16:42:37 +01:00
}
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) {
qWarning("[Pass] Window should be null only for testing");
2019-09-20 21:29:39 +02:00
}
this->initGpgHome();
this->initPasswordStore();
}
void Pass::initGpgHome()
{
// delete gpghome from previous version using GPGME
QString path = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.gpghome");
QDir dir(path);
dir.removeRecursively();
// create gpghome for rnp
QDir dir_gpg_home(this->m_gpg_home);
if (!dir_gpg_home.exists()) {
dir_gpg_home.mkpath(".");
}
qInfo() << "[Pass] GPG Home is :" << m_gpg_home;
}
2019-09-20 21:29:39 +02:00
void Pass::initPasswordStore()
{
QDir dir_password_store(this->m_password_store);
if (!dir_password_store.exists()) {
dir_password_store.mkpath(".");
2025-01-10 13:48:38 +01:00
}
qInfo() << "[Pass] Password Store is :" << m_password_store;
2019-09-20 21:29:39 +02:00
}
2025-01-29 16:42:37 +01:00
// bool Pass::show(QUrl url)
// {
// if (!this->m_sem->tryAcquire(1, 500)) {
// return false;
// }
// auto path = url.toLocalFile();
// qInfo() << "Pass show " << path;
// QFileInfo file_info(path);
// this->m_show_filename = file_info.completeBaseName();
// return this->m_gpg->decryptFromFile(path);
// }
// void Pass::showResult(Error err, QString plain_text)
// {
// qDebug() << "Pass show Result";
// if (err) {
// qInfo() << "Pass show Failed";
// emit showFailed(err.asString());
// } else if (err.isCanceled()) {
// qInfo() << "Pass show Cancelled";
// emit showCancelled();
// } else {
// qInfo() << "Pass show Succeed";
// emit showSucceed(this->m_show_filename, plain_text);
// }
// this->m_show_filename = QString();
// this->m_sem->release(1);
// }
// bool Pass::deletePasswordStore()
// {
// if (!this->m_sem->tryAcquire(1, 500)) {
// return false;
// }
// qInfo() << "Pass delete Password Store";
// auto job = new RmJob(this->password_store());
// qDebug() << "Delete Password Store at " << this->password_store();
// connect(job, &RmJob::resultReady, this, &Pass::deletePasswordStoreResult);
// connect(job, &RmJob::finished, job, &QObject::deleteLater);
// job->start();
// return true;
// }
// void Pass::deletePasswordStoreResult(bool err)
// {
// qDebug() << "Pass delete Password StoreResult";
// if (err) { //dir.removeRecursively()) {
// qInfo() << "Pass delete Password Store Failed";
// emit deletePasswordStoreFailed("failed to delete password store");
// } else {
// qInfo() << "Pass delete Password Store Succeed";
// emit deletePasswordStoreSucceed();
// }
// this->m_sem->release(1);
// }
// bool Pass::deleteGPGKey(PassKeyModel* key)
// {
// if (!this->m_sem->tryAcquire(1, 500)) {
// return false;
// }
// qInfo() << "Delete Key " << key->uid();
// return this->m_gpg->deleteKey(key->key());
// }
// void Pass::deleteGPGKeyResult(Error err)
// {
// qDebug() << "Delete Ke yResult";
// if (err) {
// qInfo() << "Delete Key Failed";
// emit deleteGPGKeyFailed(err.asString());
// } else {
// qInfo() << "Delete Key Succeed";
// emit deleteGPGKeySucceed();
// }
// this->m_sem->release(1);
// }
2019-09-20 21:29:39 +02:00
2025-01-29 16:42:37 +01:00
bool Pass::importGPGKey(QUrl url)
2025-01-20 14:46:47 +01:00
{
qInfo() << "[Pass] Import GPG Key from " << url;
2025-01-20 15:46:57 +01:00
if (!this->m_sem->tryAcquire(1, 500)) {
2025-02-01 13:45:55 +01:00
qInfo() << "[Pass] A command is already running";
2025-01-20 15:46:57 +01:00
return false;
}
2025-01-29 16:42:37 +01:00
auto job = new ImportKeyJob(this->m_gpg_home, url.toLocalFile());
QObject::connect(job, &ImportKeyJob::resultError, this, &Pass::slotImportGPGKeyError);
QObject::connect(job, &ImportKeyJob::resultSuccess, this, &Pass::slotImportGPGKeySucceed);
connect(job, &ImportKeyJob::finished, job, &QObject::deleteLater);
2025-01-20 14:46:47 +01:00
job->start();
return true;
}
2025-01-29 16:42:37 +01:00
void Pass::slotImportGPGKeyError(rnp_result_t err)
2025-01-15 23:40:35 +01:00
{
qInfo() << "[Pass] Import GPG Key Failed";
2025-01-29 16:42:37 +01:00
emit importGPGKeyFailed(rnp_result_to_string(err));
this->m_sem->release(1);
2019-09-20 21:29:39 +02:00
}
2025-01-29 16:42:37 +01:00
void Pass::slotImportGPGKeySucceed()
2019-09-20 21:29:39 +02:00
{
qInfo() << "[Pass] Import GPG Key Succesfull";
2025-01-29 16:42:37 +01:00
emit importGPGKeySucceed();
this->m_sem->release(1);
2019-09-20 21:29:39 +02:00
}
bool Pass::getAllGPGKeys()
{
qInfo() << "[Pass] Get all GPG Keys";
if (!this->m_sem->tryAcquire(1, 500)) {
2025-02-01 13:45:55 +01:00
qInfo() << "[Pass] A command is already running";
return false;
}
2025-01-30 22:46:46 +01:00
this->m_keyring_model = nullptr;
auto job = new GetKeysJob(this->m_gpg_home);
QObject::connect(job, &GetKeysJob::resultError, this, &Pass::slotGetAllGPGKeysError);
QObject::connect(job, &GetKeysJob::resultSuccess, this, &Pass::slotGetAllGPGKeysSucceed);
connect(job, &ImportKeyJob::finished, job, &QObject::deleteLater);
job->start();
return true;
}
void Pass::slotGetAllGPGKeysError(rnp_result_t err)
{
qInfo() << "[Pass] Get all GPG Keys Failed";
2025-01-30 22:46:46 +01:00
this->m_keyring_model = nullptr;
emit getAllGPGKeysFailed(rnp_result_to_string(err));
this->m_sem->release(1);
}
2025-02-01 13:45:55 +01:00
void Pass::slotGetAllGPGKeysSucceed(QList<QJsonDocument> result)
{
qInfo() << "[Pass] Get all GPG Keys Succeed";
2025-01-30 22:46:46 +01:00
this->m_keyring_model = std::unique_ptr<PassKeyringModel>(new PassKeyringModel(result));
emit getAllGPGKeysSucceed(this->m_keyring_model.get());
this->m_sem->release(1);
}
2025-01-29 16:42:37 +01:00
// void Pass::responsePassphraseDialog(bool cancel, QString passphrase)
// {
// qDebug() << "Propagate responsePassphraseDialog";
// emit responsePassphraseDialogPropagate(cancel, passphrase);
// }