cliplugin.cpp 9.53 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 86 87 88 89 90 91 92 93 94
        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\\) :");
95 96
        p[CorruptArchivePatterns] = QStringList() << QStringLiteral("Unexpected end of archive")
                                                  << QStringLiteral("Headers Error");
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("----------");

Ragnar Thomsen's avatar
Ragnar Thomsen committed
108 109 110 111 112 113 114 115 116 117 118 119
    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) {

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

Ragnar Thomsen's avatar
Ragnar Thomsen committed
130 131
    } else if (m_parseState == ParseStateArchiveInformation) {

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

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

Ragnar Thomsen's avatar
Ragnar Thomsen committed
159
    } else if (m_parseState == ParseStateEntryInformation) {
160

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

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

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

    return true;
213
}
214

215
#include "cliplugin.moc"