File indexing completed on 2025-02-02 05:37:34

0001 /*
0002     SPDX-FileCopyrightText: 2008 Claudio Bantaloukas <rockdreamer@gmail.com>
0003     SPDX-FileCopyrightText: 2007 Henrique Pinto <henrique.pinto@kdemail.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "cliplugin.h"
0009 #include "ark_debug.h"
0010 #include "kerfuffle/archiveentry.h"
0011 #include "kerfuffle/kerfuffle_export.h"
0012 
0013 #include <QDate>
0014 #include <QDir>
0015 #include <QTime>
0016 
0017 K_PLUGIN_CLASS_WITH_JSON(CliPlugin, "kerfuffle_cli.json")
0018 
0019 CliPlugin::CliPlugin(QObject *parent, const QVariantList &args)
0020     : CliInterface(parent, args)
0021     , m_isFirstLine(true)
0022     , m_incontent(false)
0023     , m_isPasswordProtected(false)
0024 {
0025     qCDebug(ARK) << "Loaded cli-example plugin";
0026 }
0027 
0028 CliPlugin::~CliPlugin()
0029 {
0030 }
0031 
0032 ParameterList CliPlugin::parameterList() const
0033 {
0034     static ParameterList p;
0035 
0036     if (p.isEmpty()) {
0037         p[CaptureProgress] = true;
0038         p[ListProgram] = p[ExtractProgram] = p[DeleteProgram] = p[AddProgram] = QLatin1String("rar");
0039 
0040         p[ListArgs] = QStringList() << QLatin1String("v") << QLatin1String("-c-") << QLatin1String("$Archive");
0041         p[ExtractArgs] = QStringList() << QLatin1String("-p-") << QLatin1String("$PreservePathSwitch") << QLatin1String("$PasswordSwitch")
0042                                        << QLatin1String("$Archive") << QLatin1String("$Files");
0043         p[PreservePathSwitch] = QStringList() << QLatin1String("x") << QLatin1String("e");
0044         p[PasswordSwitch] = QStringList() << QLatin1String("-p$Password");
0045 
0046         p[DeleteArgs] = QStringList() << QLatin1String("d") << QLatin1String("$Archive") << QLatin1String("$Files");
0047 
0048         p[FileExistsExpression] = QLatin1String("^(.+) already exists. Overwrite it");
0049         p[FileExistsInput] = QStringList() << QLatin1String("Y") // overwrite
0050                                            << QLatin1String("N") // skip
0051                                            << QLatin1String("A") // overwrite all
0052                                            << QLatin1String("E") // autoskip
0053                                            << QLatin1String("Q") // cancel
0054             ;
0055 
0056         p[AddArgs] = QStringList() << QLatin1String("a") << QLatin1String("$Archive") << QLatin1String("$Files");
0057 
0058         p[WrongPasswordPatterns] = QStringList() << QLatin1String("password incorrect");
0059         p[ExtractionFailedPatterns] = QStringList() << QLatin1String("CRC failed");
0060     }
0061 
0062     return p;
0063 }
0064 
0065 bool CliPlugin::readListLine(const QString &line)
0066 {
0067     const QString m_headerString = QLatin1String("-----------------------------------------");
0068 
0069     // skip the heading
0070     if (!m_incontent) {
0071         if (line.startsWith(m_headerString)) {
0072             m_incontent = true;
0073         }
0074         return true;
0075     }
0076 
0077     // catch final line
0078     if (line.startsWith(m_headerString)) {
0079         m_incontent = false;
0080         return true;
0081     }
0082 
0083     // rar gives one line for the filename and a line after it with some file properties
0084     if (m_isFirstLine) {
0085         m_entryFilename = line.trimmed();
0086         // m_entryFilename.chop(1); // handle newline
0087         if (!m_entryFilename.isEmpty() && m_entryFilename.at(0) == QLatin1Char('*')) {
0088             m_isPasswordProtected = true;
0089             m_entryFilename.remove(0, 1); // and the spaces in front
0090         } else
0091             m_isPasswordProtected = false;
0092 
0093         m_isFirstLine = false;
0094         return true;
0095     }
0096 
0097     QStringList fileprops = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
0098     m_entryFilename = QDir::fromNativeSeparators(m_entryFilename);
0099     bool isDirectory = (bool)(fileprops[5].contains(QLatin1Char('d'), Qt::CaseInsensitive));
0100 
0101     QDateTime ts(QDate::fromString(fileprops[3], QLatin1String("dd-MM-yy")), QTime::fromString(fileprops[4], QLatin1String("hh:mm")));
0102     // rar output date with 2 digit year but QDate takes is as 19??
0103     // let's take 1950 is cut-off; similar to KDateTime
0104     if (ts.date().year() < 1950) {
0105         ts = ts.addYears(100);
0106     }
0107 
0108     if (isDirectory && !m_entryFilename.endsWith(QLatin1Char('/'))) {
0109         m_entryFilename += QLatin1Char('/');
0110     }
0111 
0112     qCDebug(ARK) << m_entryFilename << " : " << fileprops;
0113     Archive::Entry *e = new Archive::Entry();
0114     e->setProperty("fullPath", m_entryFilename);
0115     e->setProperty("size", fileprops[0]);
0116     e->setProperty("compressedSize", fileprops[1]);
0117     e->setProperty("ratio", fileprops[2]);
0118     e->setProperty("timestamp", ts);
0119     e->setProperty("isDirectory", isDirectory);
0120     e->setProperty("permissions", fileprops[5].remove(0, 1));
0121     e->setProperty("CRC", fileprops[6]);
0122     e->setProperty("method", fileprops[7]);
0123     e->setProperty("version", fileprops[8]);
0124     e->setProperty("ssPasswordProtected", m_isPasswordProtected);
0125     qCDebug(ARK) << "Added entry: " << e;
0126 
0127     Q_EMIT entry(e);
0128     m_isFirstLine = true;
0129     return true;
0130 }
0131 
0132 #include "cliplugin.moc"