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

0001 /*
0002     SPDX-FileCopyrightText: 2011-2022 Rolf Eike Beer <kde@opensource.sf-tec.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "kgpgencrypt.h"
0007 
0008 #include "kgpgsettings.h"
0009 #include "gpgproc.h"
0010 
0011 static QStringList trustOptions(const QString &binary)
0012 {
0013     const int gpgver = GPGProc::gpgVersion(GPGProc::gpgVersionString(binary));
0014     QStringList args;
0015     if (gpgver >= 0x10302)
0016         args << QLatin1String("--trust-model")
0017                 << QLatin1String("always");
0018     else
0019         args << QLatin1String("--always-trust");
0020 
0021     return args;
0022 }
0023 
0024 KGpgEncrypt::KGpgEncrypt(QObject *parent, const QStringList &userIds, const QString &text, const EncryptOptions &options, const QStringList &extraOptions)
0025     : KGpgTextOrFileTransaction(parent, text),
0026     m_fileIndex(-1),
0027     m_options(options),
0028     m_userIds(userIds),
0029     m_extraOptions(extraOptions)
0030 {
0031     if ((m_options & AllowUntrustedEncryption) && !m_userIds.isEmpty())
0032         m_extraOptions << trustOptions(getProcess()->program().at(0));
0033 }
0034 
0035 KGpgEncrypt::KGpgEncrypt(QObject *parent, const QStringList &userIds, const QList<QUrl> &files, const EncryptOptions &options, const QStringList &extraOptions)
0036     : KGpgTextOrFileTransaction(parent, files),
0037     m_fileIndex(0),
0038     m_options(options),
0039     m_userIds(userIds),
0040     m_extraOptions(extraOptions)
0041 {
0042     if ((m_options & AllowUntrustedEncryption) && !m_userIds.isEmpty())
0043         m_extraOptions << trustOptions(getProcess()->program().at(0));
0044 }
0045 
0046 QStringList
0047 KGpgEncrypt::command() const
0048 {
0049     QStringList ret = m_extraOptions;
0050 
0051     if (m_options.testFlag(AsciiArmored))
0052         ret << QLatin1String("--armor");
0053 
0054     if (m_userIds.isEmpty()) {
0055         ret << QLatin1String( "--symmetric" );
0056     } else {
0057         if (m_options.testFlag(HideKeyId))
0058             ret << QLatin1String("--throw-keyid");
0059 
0060         ret.reserve(ret.size() + 2 * m_userIds.size() + 1);
0061         for (const QString &uid : m_userIds)
0062             ret << QLatin1String( "--recipient" ) << uid;
0063         ret << QLatin1String( "--encrypt" );
0064     }
0065 
0066     return ret;
0067 }
0068 
0069 QStringList
0070 KGpgEncrypt::encryptedText() const
0071 {
0072     QStringList result;
0073 
0074     for (const QString &line : getMessages())
0075         if (!line.startsWith(QLatin1String("[GNUPG:] ")))
0076             result.append(line);
0077 
0078     return result;
0079 }
0080 
0081 bool
0082 KGpgEncrypt::nextLine(const QString &line)
0083 {
0084     const QList<QUrl> &inputFiles = getInputFiles();
0085 
0086     if (!inputFiles.isEmpty()) {
0087         static const QString encStart = QLatin1String("[GNUPG:] FILE_START 2 ");
0088         static const QString encDone = QLatin1String("[GNUPG:] FILE_DONE");
0089 
0090         if (line.startsWith(encStart)) {
0091             m_currentFile = line.mid(encStart.length());
0092             Q_EMIT statusMessage(i18nc("Status message 'Encrypting <filename>' (operation starts)", "Encrypting %1", m_currentFile));
0093             Q_EMIT infoProgress(2 * m_fileIndex + 1, inputFiles.count() * 2);
0094         } else if (line == encDone) {
0095             Q_EMIT statusMessage(i18nc("Status message 'Encrypted <filename>' (operation was completed)", "Encrypted %1", m_currentFile));
0096             m_fileIndex++;
0097             Q_EMIT infoProgress(2 * m_fileIndex, inputFiles.count() * 2);
0098         }
0099     }
0100 
0101     return KGpgTextOrFileTransaction::nextLine(line);
0102 }
0103 
0104 KGpgTransaction::ts_boolanswer
0105 KGpgEncrypt::confirmOverwrite(QUrl &currentFile)
0106 {
0107     const QString ext = encryptExtension(m_options.testFlag(AsciiArmored));
0108 
0109     if (m_currentFile.isEmpty())
0110         currentFile = QUrl::fromLocalFile(getInputFiles().at(m_fileIndex).toLocalFile() + ext);
0111     else
0112         currentFile = QUrl::fromLocalFile(m_currentFile + ext);
0113     return BA_UNKNOWN;
0114 }
0115 
0116 
0117 QString
0118 KGpgEncrypt::encryptExtension(const bool ascii)
0119 {
0120     if (ascii)
0121         return QLatin1String( ".asc" );
0122     else if (KGpgSettings::pgpExtension())
0123         return QLatin1String( ".pgp" );
0124     else
0125         return QLatin1String( ".gpg" );
0126 }