tFix creating a new password from Passilic - sailfish-safe - Sailfish frontend for safe(1)
 (HTM) git clone git://git.z3bra.org/sailfish-safe.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 368aff1d54909eb635aabd565c766e671be8421c
 (DIR) parent f1b5077fd73c21f61dbd6a5bfc5fb93bc5be99ec
 (HTM) Author: Daniel Vrátil <daniel.vratil@avast.com>
       Date:   Mon, 19 Apr 2021 20:27:09 +0200
       
       Fix creating a new password from Passilic
       
       Diffstat:
         M src/passwordsmodel.cpp              |      38 +++++++++++++------------------
       
       1 file changed, 16 insertions(+), 22 deletions(-)
       ---
 (DIR) diff --git a/src/passwordsmodel.cpp b/src/passwordsmodel.cpp
       t@@ -19,11 +19,11 @@
        
        #include "passwordsmodel.h"
        #include "passwordprovider.h"
       +#include "gpg.h"
        
        #include <QDir>
        #include <QDebug>
        #include <QPointer>
       -#include <QProcess>
        #include <QTemporaryFile>
        #include <QFile>
        
       t@@ -230,6 +230,9 @@ void PasswordsModel::addPassword(const QModelIndex &parent, const QString &name,
                                         const QString &password, const QString &extras)
        {
            auto node = this->node(parent);
       +    if (!node) {
       +        node = mRoot;
       +    }
        
            // Escape forward slash to avoid the name "escaping" the current folder
            QString safeName = name;
       t@@ -244,27 +247,18 @@ void PasswordsModel::addPassword(const QModelIndex &parent, const QString &name,
            const auto gpgId = QString::fromUtf8(gpgIdFile.readAll()).trimmed();
            gpgIdFile.close();
        
       -
       -    const auto gpgExe = PasswordProvider::findGpgExecutable();
       -    if (gpgExe.path.isEmpty()) {
       -        qWarning() << "Failed to find GPG executable";
       -        return;
       -    }
       -
       -    QProcess process;
       -    process.setProgram(gpgExe.path);
       -    process.setArguments({ QStringLiteral("-e"),
       -                           QStringLiteral("--no-tty"),
       -                           QStringLiteral("-r%1").arg(gpgId),
       -                           QStringLiteral("-o%1/%2.gpg").arg(node->path(), safeName)
       -                         });
       -    process.start(QIODevice::ReadWrite);
       -    process.waitForStarted();
       -    process.write(password.toUtf8());
       +    QString data = password;
            if (!extras.isEmpty()) {
       -        process.write("\n");
       -        process.write(extras.toUtf8());
       +        data += QStringLiteral("\n%1").arg(extras);
            }
       -    process.closeWriteChannel();
       -    process.waitForFinished();
       +
       +    auto *task = Gpg::encrypt(QStringLiteral("%1/%2.gpg").arg(node->path(), safeName), Gpg::Key{gpgId}, data);
       +    connect(task, &Gpg::EncryptTask::finished,
       +            this, [safeName, task]() {
       +                if (task->error())  {
       +                    qWarning() << "Error:" << task->errorString();
       +                    return;
       +                }
       +                qDebug() << "Successfully encrypted password for" << safeName;
       +            });
        }