cliplugin.cpp 9.22 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)
Ragnar Thomsen's avatar
Ragnar Thomsen committed
43
        , m_parseState(ParseStateHeader)
44
{
45
    qCDebug(KERFUFFLE_PLUGIN) << "Loaded cli_7z plugin";
46
}
47

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

52 53
void CliPlugin::resetParsing()
{
Ragnar Thomsen's avatar
Ragnar Thomsen committed
54
    m_parseState = ParseStateHeader;
55 56
}

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
        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");
77
        p[EncryptHeaderSwitch] = QStringList() << QStringLiteral("$Enabled");
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        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
        p[CorruptArchivePatterns] = QStringList() << QStringLiteral("Unexpected end of archive")
                                                  << QStringLiteral("Headers Error");
99 100 101
    }

    return p;
102
}
103

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

Ragnar Thomsen's avatar
Ragnar Thomsen committed
110 111
    switch (m_parseState) {
    case ParseStateHeader:
112
        if (line.startsWith(QStringLiteral("Listing archive:"))) {
113
            qCDebug(KERFUFFLE_PLUGIN) << "Archive name: "
114
                     << line.right(line.size() - 16).trimmed();
115 116
        } else if ((line == archiveInfoDelimiter1) ||
                   (line == archiveInfoDelimiter2)) {
Ragnar Thomsen's avatar
Ragnar Thomsen committed
117
            m_parseState = ParseStateArchiveInformation;
118
        } else if (line.contains(QStringLiteral("Error: "))) {
119
            qCWarning(KERFUFFLE_PLUGIN) << line.mid(7);
120 121
        }
        break;
122

Ragnar Thomsen's avatar
Ragnar Thomsen committed
123
    case ParseStateArchiveInformation:
124
        if (line == entryInfoDelimiter) {
Ragnar Thomsen's avatar
Ragnar Thomsen committed
125
            m_parseState = ParseStateEntryInformation;
126
        } else if (line.startsWith(QStringLiteral("Type = "))) {
127
            const QString type = line.mid(7).trimmed();
128
            qCDebug(KERFUFFLE_PLUGIN) << "Archive type: " << type;
129 130 131

            if (type == QLatin1String("7z")) {
                m_archiveType = ArchiveType7z;
132
            } else if (type == QLatin1String("bzip2")) {
133
                m_archiveType = ArchiveTypeBZip2;
134
            } else if (type == QLatin1String("gzip")) {
135
                m_archiveType = ArchiveTypeGZip;
136 137
            } else if (type == QLatin1String("xz")) {
                m_archiveType = ArchiveTypeXz;
138
            } else if (type == QLatin1String("tar")) {
139
                m_archiveType = ArchiveTypeTar;
140
            } else if (type == QLatin1String("zip")) {
141
                m_archiveType = ArchiveTypeZip;
142 143
            } else if (type == QLatin1String("Rar")) {
                m_archiveType = ArchiveTypeRar;
144 145
            } else {
                // Should not happen
146
                qCWarning(KERFUFFLE_PLUGIN) << "Unsupported archive type";
147 148
                return false;
            }
149
        }
150

151 152
        break;

Ragnar Thomsen's avatar
Ragnar Thomsen committed
153
    case ParseStateEntryInformation:
154
        if (line.startsWith(QStringLiteral("Path = "))) {
155
            const QString entryFilename =
156
                QDir::fromNativeSeparators(line.mid(7).trimmed());
157 158 159
            m_currentArchiveEntry.clear();
            m_currentArchiveEntry[FileName] = entryFilename;
            m_currentArchiveEntry[InternalID] = entryFilename;
160
        } else if (line.startsWith(QStringLiteral("Size = "))) {
161
            m_currentArchiveEntry[ Size ] = line.mid(7).trimmed();
162
        } else if (line.startsWith(QStringLiteral("Packed Size = "))) {
163 164 165 166 167
            // #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();
            }
168
        } else if (line.startsWith(QStringLiteral("Modified = "))) {
169 170
            m_currentArchiveEntry[ Timestamp ] =
                QDateTime::fromString(line.mid(11).trimmed(),
171 172
                                      QStringLiteral("yyyy-MM-dd hh:mm:ss"));
        } else if (line.startsWith(QStringLiteral("Attributes = "))) {
173
            const QString attributes = line.mid(13).trimmed();
174

175
            const bool isDirectory = attributes.startsWith(QLatin1Char('D'));
176 177
            m_currentArchiveEntry[ IsDirectory ] = isDirectory;
            if (isDirectory) {
178 179
                const QString directoryName =
                    m_currentArchiveEntry[FileName].toString();
180 181
                if (!directoryName.endsWith(QLatin1Char('/'))) {
                    const bool isPasswordProtected = (line.at(12) == QLatin1Char('+'));
182
                    m_currentArchiveEntry[FileName] =
183
                        m_currentArchiveEntry[InternalID] = QString(directoryName + QLatin1Char('/'));
184 185
                    m_currentArchiveEntry[ IsPasswordProtected ] =
                        isPasswordProtected;
186 187 188 189
                }
            }

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

    return true;
207
}
208

209
#include "cliplugin.moc"