File indexing completed on 2024-04-14 05:43:29

0001 /*
0002     SPDX-FileCopyrightText: 2009-2022 Rolf Eike Beer <kde@opensource.sf-tec.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "kgpgexport.h"
0007 
0008 #include "gpgproc.h"
0009 
0010 #include <QFile>
0011 #include <QProcess>
0012 
0013 KGpgExport::KGpgExport(QObject *parent, const QStringList &ids, QProcess *outp, const QStringList &options, const bool secret)
0014     : KGpgTransaction(parent),
0015     m_keyids(ids),
0016     m_outp(outp),
0017     m_outputmode(ModeProcess)
0018 {
0019     procSetup(options, secret);
0020 }
0021 
0022 KGpgExport::KGpgExport(QObject *parent, const QStringList &ids, const QString &file, const QStringList &options, const bool secret)
0023     : KGpgTransaction(parent),
0024     m_keyids(ids),
0025     m_outp(nullptr),
0026     m_outf(file),
0027     m_outputmode(ModeFile)
0028 {
0029     procSetup(options, secret);
0030 }
0031 
0032 KGpgExport::KGpgExport(QObject *parent, const QStringList &ids, const QStringList &options, const bool secret)
0033     : KGpgTransaction(parent),
0034     m_keyids(ids),
0035     m_outp(nullptr),
0036     m_outputmode(ModeStdout)
0037 {
0038     procSetup(options, secret);
0039 }
0040 
0041 KGpgExport::KGpgExport(QObject *parent, const QStringList &ids, KGpgTransaction *outt, const QStringList &options, const bool secret)
0042     : KGpgTransaction(parent),
0043     m_keyids(ids),
0044     m_outp(nullptr),
0045     m_outputmode(ModeTransaction)
0046 {
0047     procSetup(options, secret);
0048     outt->setInputTransaction(this);
0049 }
0050 
0051 void
0052 KGpgExport::setKeyId(const QString &id)
0053 {
0054     m_keyids.clear();
0055     m_keyids.append(id);
0056 }
0057 
0058 void
0059 KGpgExport::setKeyIds(const QStringList &ids)
0060 {
0061     m_keyids = ids;
0062 }
0063 
0064 const QStringList &
0065 KGpgExport::getKeyIds() const
0066 {
0067     return m_keyids;
0068 }
0069 
0070 void
0071 KGpgExport::setOutputProcess(QProcess *outp)
0072 {
0073     m_outf.clear();
0074     m_outp = outp;
0075     m_outputmode = ModeProcess;
0076 }
0077 
0078 void
0079 KGpgExport::setOutputFile(const QString &filename)
0080 {
0081     m_outp = nullptr;
0082     m_outf = filename;
0083     if (filename.isEmpty())
0084         m_outputmode = ModeStdout;
0085     else
0086         m_outputmode = ModeFile;
0087 }
0088 
0089 void
0090 KGpgExport::setOutputTransaction(KGpgTransaction *outt)
0091 {
0092     m_outp = nullptr;
0093     m_outf.clear();
0094     m_outputmode = ModeTransaction;
0095     outt->setInputTransaction(this);
0096 }
0097 
0098 const QString &
0099 KGpgExport::getOutputFile() const
0100 {
0101     return m_outf;
0102 }
0103 
0104 const QByteArray &
0105 KGpgExport::getOutputData() const
0106 {
0107     return m_data;
0108 }
0109 
0110 bool
0111 KGpgExport::preStart()
0112 {
0113     setSuccess(TS_OK);
0114 
0115     switch (m_outputmode) {
0116     case ModeFile:
0117         {
0118         Q_ASSERT(!m_outf.isEmpty());
0119         Q_ASSERT(m_outp == nullptr);
0120 
0121         addArgument(QLatin1String( "--output" ));
0122         addArgument(m_outf);
0123 
0124         QFile ofile(m_outf);
0125         if (ofile.exists())
0126             ofile.remove();
0127 
0128         break;
0129         }
0130     case ModeProcess:
0131         Q_ASSERT(m_outf.isEmpty());
0132         Q_ASSERT(m_outp != nullptr);
0133 
0134         getProcess()->setStandardOutputProcess(m_outp);
0135 
0136         break;
0137     case ModeStdout:
0138         Q_ASSERT(m_outf.isEmpty());
0139         Q_ASSERT(m_outp == nullptr);
0140         break;
0141     case ModeTransaction:
0142         Q_ASSERT(m_outf.isEmpty());
0143         Q_ASSERT(m_outp == nullptr);
0144         break;
0145     default:
0146         Q_ASSERT(0);
0147     }
0148 
0149     addArguments(m_keyids);
0150 
0151     m_data.clear();
0152 
0153     return true;
0154 }
0155 
0156 bool
0157 KGpgExport::nextLine(const QString &line)
0158 {
0159     // key exporting does not send any messages
0160 
0161     m_data.append(line.toLatin1() + '\n');
0162 
0163     if (m_outputmode != 2)
0164         setSuccess(TS_MSG_SEQUENCE);
0165 
0166     return false;
0167 }
0168 
0169 void
0170 KGpgExport::procSetup(const QStringList &options, const bool secret)
0171 {
0172     getProcess()->resetProcess();
0173 
0174     if (secret)
0175         addArgument(QLatin1String( "--export-secret-key" ));
0176     else
0177         addArgument(QLatin1String( "--export" ));
0178 
0179     if ((m_outputmode == 2) && !options.contains(QLatin1String( "--armor" )))
0180         addArgument(QLatin1String( "--armor" ));
0181 
0182     addArguments(options);
0183 }