cliplugin.cpp 9.06 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 30
#include <QDir>

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

33 34
using namespace Kerfuffle;

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

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

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

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

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

    if (p.isEmpty()) {
        //p[CaptureProgress] = true;
60 61 62 63 64 65 66 67 68 69 70 71 72 73
        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");
74
        p[EncryptHeaderSwitch] = QStringList() << QStringLiteral("$Enabled");
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        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\\) :");
94 95
        p[CorruptArchivePatterns] = QStringList() << QStringLiteral("Unexpected end of archive")
                                                  << QStringLiteral("Headers Error");
96 97 98
    }

    return p;
99
}
100

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

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

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

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

148 149
        break;

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

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

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

    return true;
204
}
205

206
#include "cliplugin.moc"