cliplugin.cpp 7.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 24

#include "cliplugin.h"
#include "kerfuffle/cliinterface.h"
25
#include "kerfuffle/kerfuffle_export.h"
Laurent Montel's avatar
Laurent Montel committed
26
#include "kerfuffle_macro.h"
27

28
#include <QDateTime>
29
#include <QDebug>
30
#include <QDir>
31 32
#include <QLatin1String>
#include <QString>
33 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)
41
        , m_state(ReadStateHeader)
42
{
43
}
44

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

49 50
ParameterList CliPlugin::parameterList() const
{
51 52 53 54
    static ParameterList p;

    if (p.isEmpty()) {
        //p[CaptureProgress] = true;
55
        p[ListProgram] = p[ExtractProgram] = p[DeleteProgram] = p[AddProgram] = QStringList() << QLatin1String( "7z" ) << QLatin1String( "7za" ) << QLatin1String( "7zr" );
56 57

        p[ListArgs] = QStringList() << QLatin1String( "l" ) << QLatin1String( "-slt" ) << QLatin1String( "$Archive" );
58
        p[ExtractArgs] = QStringList() << QLatin1String( "$PreservePathSwitch" ) << QLatin1String( "$PasswordSwitch" ) << QLatin1String( "$Archive" ) << QLatin1String( "$Files" );
59 60 61 62 63 64
        p[PreservePathSwitch] = QStringList() << QLatin1String( "x" ) << QLatin1String( "e" );
        p[PasswordSwitch] = QStringList() << QLatin1String( "-p$Password" );
        p[FileExistsExpression] = QLatin1String( "already exists. Overwrite with" );
        p[WrongPasswordPatterns] = QStringList() << QLatin1String( "Wrong password" );
        p[AddArgs] = QStringList() << QLatin1String( "a" ) << QLatin1String( "$Archive" ) << QLatin1String( "$Files" );
        p[DeleteArgs] = QStringList() << QLatin1String( "d" ) << QLatin1String( "$Archive" ) << QLatin1String( "$Files" );
65 66

        p[FileExistsInput] = QStringList()
67 68 69 70 71
                             << QLatin1String( "Y" ) //overwrite
                             << QLatin1String( "N" )//skip
                             << QLatin1String( "A" ) //overwrite all
                             << QLatin1String( "S" ) //autoskip
                             << QLatin1String( "Q" ) //cancel
72
                             ;
73 74

        p[PasswordPromptPattern] = QLatin1String("Enter password \\(will not be echoed\\) :");
75 76 77
    }

    return p;
78
}
79

80
bool CliPlugin::readListLine(const QString& line)
81
{
82 83
    static const QLatin1String archiveInfoDelimiter1("--"); // 7z 9.13+
    static const QLatin1String archiveInfoDelimiter2("----"); // 7z 9.04
84 85
    static const QLatin1String entryInfoDelimiter("----------");

86
    switch (m_state) {
87
    case ReadStateHeader:
88
        if (line.startsWith(QLatin1String("Listing archive:"))) {
89
            qDebug() << "Archive name: "
90
                     << line.right(line.size() - 16).trimmed();
91 92
        } else if ((line == archiveInfoDelimiter1) ||
                   (line == archiveInfoDelimiter2)) {
93
            m_state = ReadStateArchiveInformation;
94
        } else if (line.contains(QLatin1String( "Error:" ))) {
95
            qDebug() << line.mid(6);
96 97
        }
        break;
98 99 100 101

    case ReadStateArchiveInformation:
        if (line == entryInfoDelimiter) {
            m_state = ReadStateEntryInformation;
102 103
        } else if (line.startsWith(QLatin1String("Type ="))) {
            const QString type = line.mid(7).trimmed();
104
            qDebug() << "Archive type: " << type;
105 106 107 108 109 110 111 112 113 114 115 116 117

            if (type == QLatin1String("7z")) {
                m_archiveType = ArchiveType7z;
            } else if (type == QLatin1String("BZip2")) {
                m_archiveType = ArchiveTypeBZip2;
            } else if (type == QLatin1String("GZip")) {
                m_archiveType = ArchiveTypeGZip;
            } else if (type == QLatin1String("Tar")) {
                m_archiveType = ArchiveTypeTar;
            } else if (type == QLatin1String("Zip")) {
                m_archiveType = ArchiveTypeZip;
            } else {
                // Should not happen
118
                qWarning() << "Unsupported archive type";
119 120
                return false;
            }
121
        }
122

123 124 125
        break;

    case ReadStateEntryInformation:
126
        if (line.startsWith(QLatin1String("Path ="))) {
127 128
            const QString entryFilename =
                QDir::fromNativeSeparators(line.mid(6).trimmed());
129 130 131
            m_currentArchiveEntry.clear();
            m_currentArchiveEntry[FileName] = entryFilename;
            m_currentArchiveEntry[InternalID] = entryFilename;
132
        } else if (line.startsWith(QLatin1String("Size = "))) {
133 134
            m_currentArchiveEntry[ Size ] = line.mid(7).trimmed();
        } else if (line.startsWith(QLatin1String("Packed Size = "))) {
135 136 137 138 139
            // #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();
            }
140
        } else if (line.startsWith(QLatin1String("Modified = "))) {
141 142
            m_currentArchiveEntry[ Timestamp ] =
                QDateTime::fromString(line.mid(11).trimmed(),
143
                                      QLatin1String( "yyyy-MM-dd hh:mm:ss" ));
144
        } else if (line.startsWith(QLatin1String("Attributes = "))) {
145
            const QString attributes = line.mid(13).trimmed();
146

147
            const bool isDirectory = attributes.startsWith(QLatin1Char( 'D' ));
148 149
            m_currentArchiveEntry[ IsDirectory ] = isDirectory;
            if (isDirectory) {
150 151
                const QString directoryName =
                    m_currentArchiveEntry[FileName].toString();
152 153
                if (!directoryName.endsWith(QLatin1Char( '/' ))) {
                    const bool isPasswordProtected = (line.at(12) == QLatin1Char( '+' ));
154
                    m_currentArchiveEntry[FileName] =
155
                        m_currentArchiveEntry[InternalID] = QString(directoryName + QLatin1Char( '/' ));
156 157
                    m_currentArchiveEntry[ IsPasswordProtected ] =
                        isPasswordProtected;
158 159 160 161 162 163 164
                }
            }

            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 = "))) {
165 166 167
            m_currentArchiveEntry[ Method ] = line.mid(9).trimmed();
        } else if (line.startsWith(QLatin1String("Encrypted = ")) &&
                   line.size() >= 13) {
168
            m_currentArchiveEntry[ IsPasswordProtected ] = (line.at(12) == QLatin1Char( '+' ));
169 170
        } else if (line.startsWith(QLatin1String("Block = "))) {
            if (m_currentArchiveEntry.contains(FileName)) {
171
                emit entry(m_currentArchiveEntry);
172 173 174 175 176 177
            }
        }
        break;
    }

    return true;
178
}
179

180
#include "cliplugin.moc"