File indexing completed on 2024-05-05 17:43:12

0001 /*
0002  *   SPDX-FileCopyrightText: 2017 Ivan Cukic <ivan.cukic (at) kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "cryfscypherchooserwidget.h"
0008 
0009 #include "ui_cryfscypherchooserwidget.h"
0010 
0011 #include <QProcess>
0012 #include <QStringList>
0013 #include <QTimer>
0014 
0015 #include <asynqt/basic/all.h>
0016 #include <asynqt/operations/transform.h>
0017 #include <asynqt/wrappers/process.h>
0018 
0019 class CryfsCypherChooserWidget::Private
0020 {
0021 public:
0022     Ui::CryfsCypherChooserWidget ui;
0023 };
0024 
0025 CryfsCypherChooserWidget::CryfsCypherChooserWidget()
0026     : DialogDsl::DialogModule(true)
0027     , d(new Private())
0028 {
0029     d->ui.setupUi(this);
0030 
0031     QTimer::singleShot(0, this, &CryfsCypherChooserWidget::initializeCyphers);
0032 }
0033 
0034 void CryfsCypherChooserWidget::initializeCyphers()
0035 {
0036     using namespace AsynQt;
0037     using namespace AsynQt::operators;
0038 
0039     // TODO: This needs to be prettier -- for modules to be able
0040     // to reach their backends directly
0041     auto process = new QProcess();
0042     process->setProgram("cryfs");
0043     process->setArguments({"--show-ciphers"});
0044 
0045     auto env = process->processEnvironment();
0046     env.insert("CRYFS_FRONTEND", "noninteractive");
0047     process->setProcessEnvironment(env);
0048 
0049     auto combo = d->ui.comboCypher;
0050 
0051     process->start();
0052 
0053     while (!process->waitForFinished(10)) {
0054         QCoreApplication::processEvents();
0055     }
0056 
0057     const auto err = process->readAllStandardError();
0058 
0059     combo->addItem(i18n("Use the default cipher"), QString());
0060 
0061     for (const auto &item : QString::fromLatin1(err).split('\n')) {
0062         if (!item.isEmpty()) {
0063             combo->addItem(item, item);
0064         }
0065     }
0066 }
0067 
0068 CryfsCypherChooserWidget::~CryfsCypherChooserWidget()
0069 {
0070 }
0071 
0072 PlasmaVault::Vault::Payload CryfsCypherChooserWidget::fields() const
0073 {
0074     return {{"cryfs-cipher", d->ui.comboCypher->currentData()}};
0075 }