1
0
mirror of https://github.com/QRouland/UTPass.git synced 2026-01-10 19:36:57 +00:00

Refactor gpg for more clean job handling

This commit is contained in:
2025-01-15 23:15:00 +01:00
parent 00116aea8c
commit e589abd10c
11 changed files with 381 additions and 242 deletions

View File

@@ -1,6 +1,7 @@
#ifndef UTPASSPHRASEPROVIDER_H
#define UTPASSPHRASEPROVIDER_H
#include <QDebug>
#include <stdio.h>
#include <QObject>
#include <QQmlProperty>
@@ -14,48 +15,57 @@
class UTPassphraseProvider : public QObject, public PassphraseProvider
{
Q_OBJECT
private:
std::unique_ptr<QEventLoop> m_loop;
std::unique_ptr<QSemaphore> m_sem;
char *m_passphrase;
bool m_canceled;
public slots:
void handleResponse(bool canceled, QString p)
{
if (!canceled)
gpgrt_asprintf(&m_passphrase, "%s", p.toUtf8().constData());
else
m_canceled = true;
m_loop->quit();
};
{
qDebug() << "call handleResponse";
if (!canceled)
gpgrt_asprintf(&m_passphrase, "%s", p.toUtf8().constData());
else
m_canceled = true;
emit unlockEventLoop();
};
signals:
void unlockEventLoop();
private:
std::unique_ptr<QSemaphore> m_sem;
char *m_passphrase;
bool m_canceled;
QObject* m_window;
public:
UTPassphraseProvider():
m_loop(std::unique_ptr<QEventLoop>(new QEventLoop)),
UTPassphraseProvider(QObject* window):
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1))),
m_passphrase(nullptr),
m_canceled(false)
{}
m_canceled(false),
m_window(window)
{
qDebug() << "Initialize UTPassphraseProviderr";
}
char *getPassphrase( const char *useridHint,
const char *description,
bool previousWasBad,
bool &canceled ) Q_DECL_OVERRIDE {
if (!m_sem->tryAcquire(1, 3000))
qDebug() << "Call the getPassphrase";
if (!this->m_sem->tryAcquire(1, 500))
{
qWarning() << "Cannot acquire UTPassphraseProvider semaphore.";
canceled = true;
return nullptr;
}
m_passphrase = nullptr;
m_canceled = false;
this->m_passphrase = nullptr;
this->m_canceled = false;
qDebug() << "Call the QML Dialog Passphrase Provider";
QMetaObject::invokeMethod(
Gpg::instance()->getWindow(), "callPassphraseDialog",
this->m_window, "callPassphraseDialog",
Q_ARG(QVariant, useridHint),
Q_ARG(QVariant, description),
Q_ARG(QVariant, previousWasBad)
@@ -63,24 +73,22 @@ public:
qDebug() << "Waiting for response";
QObject::connect(
Gpg::instance()->getWindow(), SIGNAL(responsePassphraseDialog(bool, QString)),
this, SLOT(handleResponse(bool, QString))
);
m_loop->exec();
QEventLoop loop;
QObject::connect(this, &UTPassphraseProvider::unlockEventLoop, &loop, &QEventLoop::quit);
loop.exec();
qDebug() << "Prepare Returns";
char *ret;
gpgrt_asprintf(&ret, "%s", m_passphrase);
canceled = m_canceled;
canceled = this->m_canceled;
qDebug() << "Clean";
if (m_passphrase)
if (this->m_passphrase)
{
free(m_passphrase);
}
m_canceled = false;
m_sem->release(1);
this->m_canceled = false;
this->m_sem->release(1);
return ret;
};
};