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

182 lines
5.5 KiB
C++
Raw Normal View History

2019-09-20 21:29:39 +02:00
#include <QUrl>
#include <QtCore/QStandardPaths>
#include <QtCore/QDir>
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")),
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1))),
m_show_filename(QString())
2025-01-29 16:42:37 +01:00
{
qRegisterMetaType<rnp_result_t>("rnp_result_t");
}
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) {
2025-01-29 16:42:37 +01:00
qWarning("Window should not be null unless your in testing");
2019-09-20 21:29:39 +02:00
}
2025-01-29 16:42:37 +01:00
// this->m_gpg = std::unique_ptr<Gpg>(new Gpg(window));
// UTPassphraseProvider *passphrase_provider = dynamic_cast<UTPassphraseProvider*>(this->m_gpg->passphrase_provider());
// QObject::connect(this, &Pass::responsePassphraseDialogPropagate, passphrase_provider,
// &UTPassphraseProvider::handleResponse);
2025-01-29 16:42:37 +01:00
// 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
}
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
{
2025-01-29 16:42:37 +01:00
qInfo() << "Import GPG Key from " << url;
2025-01-20 15:46:57 +01:00
if (!this->m_sem->tryAcquire(1, 500)) {
2025-01-29 16:42:37 +01:00
qInfo() << "A job 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
{
2025-01-29 16:42:37 +01:00
qDebug() << "Import GPG Key Failed";
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
{
2025-01-29 16:42:37 +01:00
qDebug() << "Import GPG Key Failed";
emit importGPGKeySucceed();
this->m_sem->release(1);
2019-09-20 21:29:39 +02:00
}
2025-01-29 16:42:37 +01:00
// bool Pass::getAllGPGKeys()
// {
// if (!this->m_sem->tryAcquire(1, 500)) {
// return false;
// }
// qInfo() << "Get GPG keys";
// return this->m_gpg->getAllKeys();
// }
// void Pass::getAllGPGKeysResult(Error err, std::vector<GpgME::Key> keys_info)
// {
// qDebug() << "Get GPG keys Result";
// if (err) {
// qInfo() << "Get GPG Failed";
// emit getAllGPGKeysFailed(err.asString());
// } else {
// qInfo() << "Get GPG Succeed";
// emit getAllGPGKeysSucceed(QVariant::fromValue(PassKeyModel::keysToPassKey(keys_info)));
// }
// this->m_sem->release(1);
// }
// void Pass::responsePassphraseDialog(bool cancel, QString passphrase)
// {
// qDebug() << "Propagate responsePassphraseDialog";
// emit responsePassphraseDialogPropagate(cancel, passphrase);
// }