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
#include <QDir>
31 32
#include <QLatin1String>
#include <QString>
33

Bhushan Shah's avatar
Bhushan Shah committed
34 35
#include <KPluginFactory>

36 37
Q_LOGGING_CATEGORY(KERFUFFLE_PLUGIN, "ark.kerfuffle.cli7z", QtWarningMsg)

38 39
using namespace Kerfuffle;

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

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

50 51 52
CliPlugin::~CliPlugin()
{
}
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
        p[ListProgram] = p[ExtractProgram] = p[DeleteProgram] = p[AddProgram] = QStringList() << QLatin1String( "7z" )
                                                                                              << QLatin1String( "7za" )
                                                                                              << QLatin1String( "7zr" );
        p[ListArgs] = QStringList() << QLatin1String( "l" )
                                    << QLatin1String( "-slt" )
                                    << QLatin1String("$PasswordSwitch")
                                    << QLatin1String( "$Archive" );
        p[ExtractArgs] = QStringList() << QLatin1String( "$PreservePathSwitch" )
                                       << QLatin1String( "$PasswordSwitch" )
                                       << QLatin1String( "$Archive" )
                                       << QLatin1String( "$Files" );
        p[PreservePathSwitch] = QStringList() << QLatin1String( "x" )
                                              << QLatin1String( "e" );
73
        p[PasswordSwitch] = QStringList() << QLatin1String( "-p$Password" );
74
        p[EncryptHeaderSwitch] = QStringList() << QLatin1String("-mhe=$Enabled");
75
        p[WrongPasswordPatterns] = QStringList() << QLatin1String( "Wrong password" );
76 77 78 79 80 81 82 83 84
        p[AddArgs] = QStringList() << QLatin1String( "a" )
                                   << QLatin1String( "$Archive" )
                                   << QLatin1String("$EncryptHeaderSwitch")
                                   << QLatin1String( "$PasswordSwitch" )
                                   << QLatin1String( "$Files" );
        p[DeleteArgs] = QStringList() << QLatin1String( "d" )
                                      << QLatin1String( "$Archive" )
                                      << QLatin1String( "$Files" );

85 86
        p[FileExistsExpression] = QLatin1String( "^\\(Y\\)es / \\(N\\)o / \\(A\\)lways / \\(S\\)kip all / A\\(u\\)to rename all / \\(Q\\)uit\\? $" );
        p[FileExistsFileName] = QStringList() << QLatin1String("^file \\./(.*)$");
87 88 89 90 91
        p[FileExistsInput] = QStringList() << QLatin1String( "Y" )  //overwrite
                                           << QLatin1String( "N" )  //skip
                                           << QLatin1String( "A" )  //overwrite all
                                           << QLatin1String( "S" )  //autoskip
                                           << QLatin1String( "Q" ); //cancel
92 93

        p[PasswordPromptPattern] = QLatin1String("Enter password \\(will not be echoed\\) :");
94 95 96
    }

    return p;
97
}
98

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

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

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

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

142 143 144
        break;

    case ReadStateEntryInformation:
145
        if (line.startsWith(QLatin1String("Path = "))) {
146
            const QString entryFilename =
147
                QDir::fromNativeSeparators(line.mid(7).trimmed());
148 149 150
            m_currentArchiveEntry.clear();
            m_currentArchiveEntry[FileName] = entryFilename;
            m_currentArchiveEntry[InternalID] = entryFilename;
151
        } else if (line.startsWith(QLatin1String("Size = "))) {
152 153
            m_currentArchiveEntry[ Size ] = line.mid(7).trimmed();
        } else if (line.startsWith(QLatin1String("Packed Size = "))) {
154 155 156 157 158
            // #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();
            }
159
        } else if (line.startsWith(QLatin1String("Modified = "))) {
160 161
            m_currentArchiveEntry[ Timestamp ] =
                QDateTime::fromString(line.mid(11).trimmed(),
162
                                      QLatin1String( "yyyy-MM-dd hh:mm:ss" ));
163
        } else if (line.startsWith(QLatin1String("Attributes = "))) {
164
            const QString attributes = line.mid(13).trimmed();
165

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

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

    return true;
198
}
199

200
#include "cliplugin.moc"