cliplugin.cpp 8.81 KB
Newer Older
1 2 3
/*
 * ark -- archiver for the KDE project
 *
4
 * Copyright (C) 2009 Harald Hvaal <haraldhv@stud.ntnu.no>
5
 * Copyright (C) 2009-2011 Raphael Kubo da Costa <rakuco@FreeBSD.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
22 23

#include "cliplugin.h"
24
#include "app/logging.h"
25
#include "kerfuffle/cliinterface.h"
26
#include "kerfuffle/kerfuffle_export.h"
27

28
#include <QDateTime>
29
#include <QDebug>
30 31
#include <QDir>

Bhushan Shah's avatar
Bhushan Shah committed
32 33
#include <KPluginFactory>

34 35
Q_LOGGING_CATEGORY(KERFUFFLE_PLUGIN, "ark.kerfuffle.cli7z", QtWarningMsg)

36 37
using namespace Kerfuffle;

Bhushan Shah's avatar
Bhushan Shah committed
38 39
K_PLUGIN_FACTORY( CliPluginFactory, registerPlugin< CliPlugin >(); )

40 41
CliPlugin::CliPlugin(QObject *parent, const QVariantList & args)
        : CliInterface(parent, args)
42
        , m_archiveType(ArchiveType7z)
43
        , m_state(ReadStateHeader)
44
{
45
    qCDebug(KERFUFFLE_PLUGIN) << "Loaded cli_7z plugin";
46
}
47

48 49 50
CliPlugin::~CliPlugin()
{
}
51

52 53 54 55 56
void CliPlugin::resetParsing()
{
    m_state = ReadStateHeader;
}

57 58
ParameterList CliPlugin::parameterList() const
{
59 60 61 62
    static ParameterList p;

    if (p.isEmpty()) {
        //p[CaptureProgress] = true;
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
        p[ListProgram] = p[ExtractProgram] = p[DeleteProgram] = p[AddProgram] = QStringList() << QStringLiteral("7z")
                                                                                              << QStringLiteral("7za")
                                                                                              << QStringLiteral("7zr");
        p[ListArgs] = QStringList() << QStringLiteral("l")
                                    << QStringLiteral("-slt")
                                    << QStringLiteral("$PasswordSwitch")
                                    << QStringLiteral("$Archive");
        p[ExtractArgs] = QStringList() << QStringLiteral("$PreservePathSwitch")
                                       << QStringLiteral("$PasswordSwitch")
                                       << QStringLiteral("$Archive")
                                       << QStringLiteral("$Files");
        p[PreservePathSwitch] = QStringList() << QStringLiteral("x")
                                              << QStringLiteral("e");
        p[PasswordSwitch] = QStringList() << QStringLiteral("-p$Password");
        p[EncryptHeaderSwitch] = QStringList() << QStringLiteral("-mhe=$Enabled");
        p[WrongPasswordPatterns] = QStringList() << QStringLiteral("Wrong password");
        p[AddArgs] = QStringList() << QStringLiteral("a")
                                   << QStringLiteral("$Archive")
                                   << QStringLiteral("$EncryptHeaderSwitch")
                                   << QStringLiteral("$PasswordSwitch")
                                   << QStringLiteral("$Files");
        p[DeleteArgs] = QStringList() << QStringLiteral("d")
                                      << QStringLiteral("$Archive")
                                      << QStringLiteral("$Files");

        p[FileExistsExpression] = QStringLiteral("^\\(Y\\)es / \\(N\\)o / \\(A\\)lways / \\(S\\)kip all / A\\(u\\)to rename all / \\(Q\\)uit\\? $");
        p[FileExistsFileName] = QStringList() << QStringLiteral("^file \\./(.*)$");
        p[FileExistsInput] = QStringList() << QStringLiteral("Y")  //overwrite
                                           << QStringLiteral("N")  //skip
                                           << QStringLiteral("A")  //overwrite all
                                           << QStringLiteral("S")  //autoskip
                                           << QStringLiteral("Q"); //cancel

        p[PasswordPromptPattern] = QStringLiteral("Enter password \\(will not be echoed\\) :");
97 98 99
    }

    return p;
100
}
101

102
bool CliPlugin::readListLine(const QString& line)
103
{
104 105
    static const QLatin1String archiveInfoDelimiter1("--"); // 7z 9.13+
    static const QLatin1String archiveInfoDelimiter2("----"); // 7z 9.04
106 107
    static const QLatin1String entryInfoDelimiter("----------");

108
    switch (m_state) {
109
    case ReadStateHeader:
110
        if (line.startsWith(QStringLiteral("Listing archive:"))) {
111
            qCDebug(KERFUFFLE_PLUGIN) << "Archive name: "
112
                     << line.right(line.size() - 16).trimmed();
113 114
        } else if ((line == archiveInfoDelimiter1) ||
                   (line == archiveInfoDelimiter2)) {
115
            m_state = ReadStateArchiveInformation;
116
        } else if (line.contains(QStringLiteral("Error: "))) {
117
            qCWarning(KERFUFFLE_PLUGIN) << line.mid(7);
118 119
        }
        break;
120 121 122 123

    case ReadStateArchiveInformation:
        if (line == entryInfoDelimiter) {
            m_state = ReadStateEntryInformation;
124
        } else if (line.startsWith(QStringLiteral("Type = "))) {
125
            const QString type = line.mid(7).trimmed();
126
            qCDebug(KERFUFFLE_PLUGIN) << "Archive type: " << type;
127 128 129

            if (type == QLatin1String("7z")) {
                m_archiveType = ArchiveType7z;
130
            } else if (type == QLatin1String("bzip2")) {
131
                m_archiveType = ArchiveTypeBZip2;
132
            } else if (type == QLatin1String("gzip")) {
133
                m_archiveType = ArchiveTypeGZip;
134
            } else if (type == QLatin1String("tar")) {
135
                m_archiveType = ArchiveTypeTar;
136
            } else if (type == QLatin1String("zip")) {
137 138 139
                m_archiveType = ArchiveTypeZip;
            } else {
                // Should not happen
140
                qCWarning(KERFUFFLE_PLUGIN) << "Unsupported archive type";
141 142
                return false;
            }
143
        }
144

145 146 147
        break;

    case ReadStateEntryInformation:
148
        if (line.startsWith(QStringLiteral("Path = "))) {
149
            const QString entryFilename =
150
                QDir::fromNativeSeparators(line.mid(7).trimmed());
151 152 153
            m_currentArchiveEntry.clear();
            m_currentArchiveEntry[FileName] = entryFilename;
            m_currentArchiveEntry[InternalID] = entryFilename;
154
        } else if (line.startsWith(QStringLiteral("Size = "))) {
155
            m_currentArchiveEntry[ Size ] = line.mid(7).trimmed();
156
        } else if (line.startsWith(QStringLiteral("Packed Size = "))) {
157 158 159 160 161
            // #236696: 7z files only show a single Packed Size value
            //          corresponding to the whole archive.
            if (m_archiveType != ArchiveType7z) {
                m_currentArchiveEntry[CompressedSize] = line.mid(14).trimmed();
            }
162
        } else if (line.startsWith(QStringLiteral("Modified = "))) {
163 164
            m_currentArchiveEntry[ Timestamp ] =
                QDateTime::fromString(line.mid(11).trimmed(),
165 166
                                      QStringLiteral("yyyy-MM-dd hh:mm:ss"));
        } else if (line.startsWith(QStringLiteral("Attributes = "))) {
167
            const QString attributes = line.mid(13).trimmed();
168

169
            const bool isDirectory = attributes.startsWith(QLatin1Char('D'));
170 171
            m_currentArchiveEntry[ IsDirectory ] = isDirectory;
            if (isDirectory) {
172 173
                const QString directoryName =
                    m_currentArchiveEntry[FileName].toString();
174 175
                if (!directoryName.endsWith(QLatin1Char('/'))) {
                    const bool isPasswordProtected = (line.at(12) == QLatin1Char('+'));
176
                    m_currentArchiveEntry[FileName] =
177
                        m_currentArchiveEntry[InternalID] = QString(directoryName + QLatin1Char('/'));
178 179
                    m_currentArchiveEntry[ IsPasswordProtected ] =
                        isPasswordProtected;
180 181 182 183
                }
            }

            m_currentArchiveEntry[ Permissions ] = attributes.mid(1);
184
        } else if (line.startsWith(QStringLiteral("CRC = "))) {
185
            m_currentArchiveEntry[ CRC ] = line.mid(6).trimmed();
186
        } else if (line.startsWith(QStringLiteral("Method = "))) {
187
            m_currentArchiveEntry[ Method ] = line.mid(9).trimmed();
188
        } else if (line.startsWith(QStringLiteral("Encrypted = ")) &&
189
                   line.size() >= 13) {
190 191 192
            m_currentArchiveEntry[ IsPasswordProtected ] = (line.at(12) == QLatin1Char('+'));
        } else if (line.startsWith(QStringLiteral("Block = ")) ||
                   line.startsWith(QStringLiteral("Version = "))) {
193
            if (m_currentArchiveEntry.contains(FileName)) {
194
                emit entry(m_currentArchiveEntry);
195 196 197 198 199 200
            }
        }
        break;
    }

    return true;
201
}
202

203
#include "cliplugin.moc"