File indexing completed on 2022-07-03 17:05:59

0001 /*
0002     SPDX-FileCopyrightText: 2002 Jean-Baptiste Mardelle <bj@altern.org>
0003     SPDX-FileCopyrightText: 2007, 2008, 2009, 2010, 2011, 2012 Rolf Eike Beer <kde@opensource.sf-tec.de>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kgpgtextinterface.h"
0008 
0009 #include "gpgproc.h"
0010 #include "kgpgsettings.h"
0011 
0012 #include <QString>
0013 
0014 class KGpgTextInterfacePrivate
0015 {
0016     Q_DISABLE_COPY(KGpgTextInterfacePrivate)
0017 public:
0018     KGpgTextInterfacePrivate(QObject *parent, const QString &keyID, const QStringList &options);
0019 
0020     GPGProc * const m_process;
0021     int m_step;
0022     const QStringList m_gpgopts;
0023     QList<QUrl> m_files;
0024 
0025     void signFile(const QString &fileName);
0026 };
0027 
0028 static QStringList
0029 buildCmdLine(const QString &keyID, const QStringList &options)
0030 {
0031     return QStringList(QLatin1String("-u")) <<
0032         keyID <<
0033         options <<
0034         QLatin1String("--detach-sign") <<
0035         QLatin1String("--output");
0036 }
0037 
0038 KGpgTextInterfacePrivate::KGpgTextInterfacePrivate(QObject *parent, const QString &keyID, const QStringList &options)
0039     : m_process(new GPGProc(parent)),
0040     m_step(0),
0041     m_gpgopts(buildCmdLine(keyID, options))
0042 {
0043 }
0044 
0045 void
0046 KGpgTextInterfacePrivate::signFile(const QString &fileName)
0047 {
0048     m_process->resetProcess();
0049     *m_process <<
0050             m_gpgopts <<
0051             fileName + QLatin1String(".sig") <<
0052             fileName;
0053 
0054     m_process->start();
0055 }
0056 
0057 KGpgTextInterface::KGpgTextInterface(QObject *parent, const QString &keyID, const QStringList &options)
0058     : QObject(parent),
0059     d(new KGpgTextInterfacePrivate(parent, keyID, options))
0060 {
0061     connect(d->m_process, &GPGProc::processExited, this, &KGpgTextInterface::slotSignFile);
0062 }
0063 
0064 KGpgTextInterface::~KGpgTextInterface()
0065 {
0066     delete d->m_process;
0067     delete d;
0068 }
0069 
0070 // signatures
0071 void
0072 KGpgTextInterface::signFiles(const QList<QUrl> &srcUrls)
0073 {
0074     d->m_files = srcUrls;
0075 
0076     slotSignFile();
0077 }
0078 
0079 void
0080 KGpgTextInterface::slotSignFile()
0081 {
0082     const QString fileName = d->m_files.takeFirst().path();
0083 
0084     if (d->m_files.isEmpty()) {
0085         disconnect(d->m_process, &GPGProc::processExited, this, &KGpgTextInterface::slotSignFile);
0086         connect(d->m_process, &GPGProc::processExited, this, &KGpgTextInterface::slotSignFinished);
0087     }
0088 
0089     d->signFile(fileName);
0090 }
0091 
0092 void
0093 KGpgTextInterface::slotSignFinished()
0094 {
0095     Q_EMIT fileSignFinished();
0096 }