File: cliplugin.cpp

package info (click to toggle)
ark 4%3A25.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,376 kB
  • sloc: cpp: 19,286; xml: 449; ansic: 244; makefile: 5; sh: 5
file content (132 lines) | stat: -rw-r--r-- 4,814 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
    SPDX-FileCopyrightText: 2008 Claudio Bantaloukas <rockdreamer@gmail.com>
    SPDX-FileCopyrightText: 2007 Henrique Pinto <henrique.pinto@kdemail.net>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "cliplugin.h"
#include "ark_debug.h"
#include "kerfuffle/archiveentry.h"
#include "kerfuffle/kerfuffle_export.h"

#include <QDate>
#include <QDir>
#include <QTime>

K_PLUGIN_CLASS_WITH_JSON(CliPlugin, "kerfuffle_cli.json")

CliPlugin::CliPlugin(QObject *parent, const QVariantList &args)
    : CliInterface(parent, args)
    , m_isFirstLine(true)
    , m_incontent(false)
    , m_isPasswordProtected(false)
{
    qCDebug(ARK_LOG) << "Loaded cli-example plugin";
}

CliPlugin::~CliPlugin()
{
}

ParameterList CliPlugin::parameterList() const
{
    static ParameterList p;

    if (p.isEmpty()) {
        p[CaptureProgress] = true;
        p[ListProgram] = p[ExtractProgram] = p[DeleteProgram] = p[AddProgram] = QLatin1String("rar");

        p[ListArgs] = QStringList() << QLatin1String("v") << QLatin1String("-c-") << QLatin1String("$Archive");
        p[ExtractArgs] = QStringList() << QLatin1String("-p-") << QLatin1String("$PreservePathSwitch") << QLatin1String("$PasswordSwitch")
                                       << QLatin1String("$Archive") << QLatin1String("$Files");
        p[PreservePathSwitch] = QStringList() << QLatin1String("x") << QLatin1String("e");
        p[PasswordSwitch] = QStringList() << QLatin1String("-p$Password");

        p[DeleteArgs] = QStringList() << QLatin1String("d") << QLatin1String("$Archive") << QLatin1String("$Files");

        p[FileExistsExpression] = QLatin1String("^(.+) already exists. Overwrite it");
        p[FileExistsInput] = QStringList() << QLatin1String("Y") // overwrite
                                           << QLatin1String("N") // skip
                                           << QLatin1String("A") // overwrite all
                                           << QLatin1String("E") // autoskip
                                           << QLatin1String("Q") // cancel
            ;

        p[AddArgs] = QStringList() << QLatin1String("a") << QLatin1String("$Archive") << QLatin1String("$Files");

        p[WrongPasswordPatterns] = QStringList() << QLatin1String("password incorrect");
        p[ExtractionFailedPatterns] = QStringList() << QLatin1String("CRC failed");
    }

    return p;
}

bool CliPlugin::readListLine(const QString &line)
{
    const QString m_headerString = QLatin1String("-----------------------------------------");

    // skip the heading
    if (!m_incontent) {
        if (line.startsWith(m_headerString)) {
            m_incontent = true;
        }
        return true;
    }

    // catch final line
    if (line.startsWith(m_headerString)) {
        m_incontent = false;
        return true;
    }

    // rar gives one line for the filename and a line after it with some file properties
    if (m_isFirstLine) {
        m_entryFilename = line.trimmed();
        // m_entryFilename.chop(1); // handle newline
        if (!m_entryFilename.isEmpty() && m_entryFilename.at(0) == QLatin1Char('*')) {
            m_isPasswordProtected = true;
            m_entryFilename.remove(0, 1); // and the spaces in front
        } else
            m_isPasswordProtected = false;

        m_isFirstLine = false;
        return true;
    }

    QStringList fileprops = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
    m_entryFilename = QDir::fromNativeSeparators(m_entryFilename);
    bool isDirectory = (bool)(fileprops[5].contains(QLatin1Char('d'), Qt::CaseInsensitive));

    QDateTime ts(QDate::fromString(fileprops[3], QLatin1String("dd-MM-yy")), QTime::fromString(fileprops[4], QLatin1String("hh:mm")));
    // rar output date with 2 digit year but QDate takes is as 19??
    // let's take 1950 is cut-off; similar to KDateTime
    if (ts.date().year() < 1950) {
        ts = ts.addYears(100);
    }

    if (isDirectory && !m_entryFilename.endsWith(QLatin1Char('/'))) {
        m_entryFilename += QLatin1Char('/');
    }

    qCDebug(ARK_LOG) << m_entryFilename << " : " << fileprops;
    Archive::Entry *e = new Archive::Entry();
    e->setProperty("fullPath", m_entryFilename);
    e->setProperty("size", fileprops[0]);
    e->setProperty("compressedSize", fileprops[1]);
    e->setProperty("ratio", fileprops[2]);
    e->setProperty("timestamp", ts);
    e->setProperty("isDirectory", isDirectory);
    e->setProperty("permissions", fileprops[5].remove(0, 1));
    e->setProperty("CRC", fileprops[6]);
    e->setProperty("method", fileprops[7]);
    e->setProperty("version", fileprops[8]);
    e->setProperty("ssPasswordProtected", m_isPasswordProtected);
    qCDebug(ARK_LOG) << "Added entry: " << e;

    Q_EMIT entry(e);
    m_isFirstLine = true;
    return true;
}

#include "cliplugin.moc"