cliplugin.cpp 9.78 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 "ark_debug.h"
25
#include "kerfuffle/cliinterface.h"
26
#include "kerfuffle/kerfuffle_export.h"
27

28
#include <QDateTime>
29
#include <QDir>
Ragnar Thomsen's avatar
Ragnar Thomsen committed
30
#include <QRegularExpression>
31

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

34 35
using namespace Kerfuffle;

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

38 39
CliPlugin::CliPlugin(QObject *parent, const QVariantList & args)
        : CliInterface(parent, args)
40
        , m_archiveType(ArchiveType7z)
Ragnar Thomsen's avatar
Ragnar Thomsen committed
41
        , m_parseState(ParseStateTitle)
42
{
43
    qCDebug(ARK) << "Loaded cli_7z plugin";
44
}
45

46 47 48
CliPlugin::~CliPlugin()
{
}
49

50 51
void CliPlugin::resetParsing()
{
Ragnar Thomsen's avatar
Ragnar Thomsen committed
52
    m_parseState = ParseStateTitle;
53 54
}

55 56
ParameterList CliPlugin::parameterList() const
{
57 58 59 60
    static ParameterList p;

    if (p.isEmpty()) {
        //p[CaptureProgress] = true;
61 62 63 64 65 66 67 68 69 70 71 72 73 74
        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");
75
        p[EncryptHeaderSwitch] = QStringList() << QStringLiteral("$Enabled");
76 77 78 79 80 81 82 83 84 85
        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");

86 87 88 89 90
        p[FileExistsExpression] = QStringList()
            << QStringLiteral("^\\(Y\\)es / \\(N\\)o / \\(A\\)lways / \\(S\\)kip all / A\\(u\\)to rename all / \\(Q\\)uit\\? $")
            << QStringLiteral("^\\? \\(Y\\)es / \\(N\\)o / \\(A\\)lways / \\(S\\)kip all / A\\(u\\)to rename all / \\(Q\\)uit\\? $");
        p[FileExistsFileName] = QStringList() << QStringLiteral("^file \\./(.*)$")
                                              << QStringLiteral("^  Path:     \\./(.*)$");
91 92 93 94 95 96 97
        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\\) :");
98 99
        p[CorruptArchivePatterns] = QStringList() << QStringLiteral("Unexpected end of archive")
                                                  << QStringLiteral("Headers Error");
100 101 102
    }

    return p;
103
}
104

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

Ragnar Thomsen's avatar
Ragnar Thomsen committed
111 112 113 114 115 116 117 118 119 120 121 122
    if (m_parseState == ParseStateTitle) {

        const QRegularExpression rxVersionLine(QStringLiteral("^p7zip Version ([\\d\\.]+) .*$"));
        QRegularExpressionMatch matchVersion = rxVersionLine.match(line);
        if (matchVersion.hasMatch()) {
            m_parseState = ParseStateHeader;
            const QString p7zipVersion = matchVersion.captured(1);
            qCDebug(ARK) << "p7zip version" << p7zipVersion << "detected";
        }

    } else if (m_parseState == ParseStateHeader) {

123
        if (line.startsWith(QStringLiteral("Listing archive:"))) {
124
            qCDebug(ARK) << "Archive name: "
125
                     << line.right(line.size() - 16).trimmed();
126 127
        } else if ((line == archiveInfoDelimiter1) ||
                   (line == archiveInfoDelimiter2)) {
Ragnar Thomsen's avatar
Ragnar Thomsen committed
128
            m_parseState = ParseStateArchiveInformation;
129
        } else if (line.contains(QStringLiteral("Error: "))) {
130
            qCWarning(ARK) << line.mid(7);
131
        }
132

Ragnar Thomsen's avatar
Ragnar Thomsen committed
133 134
    } else if (m_parseState == ParseStateArchiveInformation) {

135
        if (line == entryInfoDelimiter) {
Ragnar Thomsen's avatar
Ragnar Thomsen committed
136
            m_parseState = ParseStateEntryInformation;
137
        } else if (line.startsWith(QStringLiteral("Type = "))) {
138
            const QString type = line.mid(7).trimmed();
139
            qCDebug(ARK) << "Archive type: " << type;
140 141 142

            if (type == QLatin1String("7z")) {
                m_archiveType = ArchiveType7z;
143
            } else if (type == QLatin1String("bzip2")) {
144
                m_archiveType = ArchiveTypeBZip2;
145
            } else if (type == QLatin1String("gzip")) {
146
                m_archiveType = ArchiveTypeGZip;
147 148
            } else if (type == QLatin1String("xz")) {
                m_archiveType = ArchiveTypeXz;
149
            } else if (type == QLatin1String("tar")) {
150
                m_archiveType = ArchiveTypeTar;
151
            } else if (type == QLatin1String("zip")) {
152
                m_archiveType = ArchiveTypeZip;
153 154
            } else if (type == QLatin1String("Rar")) {
                m_archiveType = ArchiveTypeRar;
155 156
            } else {
                // Should not happen
157
                qCWarning(ARK) << "Unsupported archive type";
158 159
                return false;
            }
160
        }
161

Ragnar Thomsen's avatar
Ragnar Thomsen committed
162
    } else if (m_parseState == ParseStateEntryInformation) {
163

164
        if (line.startsWith(QStringLiteral("Path = "))) {
165
            const QString entryFilename =
166
                QDir::fromNativeSeparators(line.mid(7).trimmed());
167 168 169
            m_currentArchiveEntry.clear();
            m_currentArchiveEntry[FileName] = entryFilename;
            m_currentArchiveEntry[InternalID] = entryFilename;
170
        } else if (line.startsWith(QStringLiteral("Size = "))) {
171
            m_currentArchiveEntry[ Size ] = line.mid(7).trimmed();
172
        } else if (line.startsWith(QStringLiteral("Packed Size = "))) {
173 174 175 176 177
            // #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();
            }
178
        } else if (line.startsWith(QStringLiteral("Modified = "))) {
179 180
            m_currentArchiveEntry[ Timestamp ] =
                QDateTime::fromString(line.mid(11).trimmed(),
181 182
                                      QStringLiteral("yyyy-MM-dd hh:mm:ss"));
        } else if (line.startsWith(QStringLiteral("Attributes = "))) {
183
            const QString attributes = line.mid(13).trimmed();
184

185
            const bool isDirectory = attributes.startsWith(QLatin1Char('D'));
186 187
            m_currentArchiveEntry[ IsDirectory ] = isDirectory;
            if (isDirectory) {
188 189
                const QString directoryName =
                    m_currentArchiveEntry[FileName].toString();
190 191
                if (!directoryName.endsWith(QLatin1Char('/'))) {
                    const bool isPasswordProtected = (line.at(12) == QLatin1Char('+'));
192
                    m_currentArchiveEntry[FileName] =
193
                        m_currentArchiveEntry[InternalID] = QString(directoryName + QLatin1Char('/'));
194 195
                    m_currentArchiveEntry[ IsPasswordProtected ] =
                        isPasswordProtected;
196 197 198 199
                }
            }

            m_currentArchiveEntry[ Permissions ] = attributes.mid(1);
200
        } else if (line.startsWith(QStringLiteral("CRC = "))) {
201
            m_currentArchiveEntry[ CRC ] = line.mid(6).trimmed();
202
        } else if (line.startsWith(QStringLiteral("Method = "))) {
203
            m_currentArchiveEntry[ Method ] = line.mid(9).trimmed();
204
        } else if (line.startsWith(QStringLiteral("Encrypted = ")) &&
205
                   line.size() >= 13) {
206 207 208
            m_currentArchiveEntry[ IsPasswordProtected ] = (line.at(12) == QLatin1Char('+'));
        } else if (line.startsWith(QStringLiteral("Block = ")) ||
                   line.startsWith(QStringLiteral("Version = "))) {
209
            if (m_currentArchiveEntry.contains(FileName)) {
210
                emit entry(m_currentArchiveEntry);
211 212 213 214 215
            }
        }
    }

    return true;
216
}
217

218
#include "cliplugin.moc"