cliplugin.cpp 5.67 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) 2010 Raphael Kubo da Costa <kubito@gmail.com>
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"
26

27
#include <QDateTime>
28
#include <QDir>
29 30
#include <QLatin1String>
#include <QString>
31

32 33
#include <KDebug>

34 35
using namespace Kerfuffle;

36 37
CliPlugin::CliPlugin(QObject *parent, const QVariantList & args)
        : CliInterface(parent, args)
38
        , m_state(ReadStateHeader)
39
{
40
}
41

42 43
CliPlugin::~CliPlugin()
{
44

45
}
46

47 48
ParameterList CliPlugin::parameterList() const
{
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    static ParameterList p;

    if (p.isEmpty()) {
        //p[CaptureProgress] = true;
        p[ListProgram] = p[ExtractProgram] = p[DeleteProgram] = p[AddProgram] = "7z";

        p[ListArgs] = QStringList() << "l" << "-slt" << "$Archive";
        p[ExtractArgs] = QStringList() << "$PreservePathSwitch" << "$PasswordSwitch" << "$RootNodeSwitch" << "$Archive" << "$Files";
        p[PreservePathSwitch] = QStringList() << "x" << "e";
        p[RootNodeSwitch] = QStringList() << "-w$Path";
        p[PasswordSwitch] = QStringList() << "-p$Password";
        p[FileExistsExpression] = "already exists. Overwrite with";
        p[WrongPasswordPatterns] = QStringList() << "Wrong password";
        p[AddArgs] = QStringList() << "a" << "$Archive" << "$Files";
        p[DeleteArgs] = QStringList() << "d" << "$Archive" << "$Files";

        p[FileExistsInput] = QStringList()
                             << "Y" //overwrite
                             << "N" //skip
                             << "A" //overwrite all
                             << "S" //autoskip
                             << "Q" //cancel
                             ;

    }

    return p;
76
}
77

78
bool CliPlugin::readListLine(const QString &line)
79
{
80 81 82
    static const QLatin1String archiveInfoDelimiter("----");
    static const QLatin1String entryInfoDelimiter("----------");

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

    case ReadStateArchiveInformation:
        if (line == entryInfoDelimiter) {
            m_state = ReadStateEntryInformation;
        }
        break;

    case ReadStateEntryInformation:
102
        if (line.startsWith(QLatin1String("Path ="))) {
103 104
            const QString entryFilename =
                QDir::fromNativeSeparators(line.mid(6).trimmed());
105 106 107
            m_currentArchiveEntry.clear();
            m_currentArchiveEntry[FileName] = entryFilename;
            m_currentArchiveEntry[InternalID] = entryFilename;
108
        } else if (line.startsWith(QLatin1String("Size = "))) {
109 110 111 112
            m_currentArchiveEntry[ Size ] = line.mid(7).trimmed();
        } else if (line.startsWith(QLatin1String("Packed Size = "))) {
            m_currentArchiveEntry[ CompressedSize ] = line.mid(14).trimmed();
        } else if (line.startsWith(QLatin1String("Modified = "))) {
113 114 115
            m_currentArchiveEntry[ Timestamp ] =
                QDateTime::fromString(line.mid(11).trimmed(),
                                      "yyyy-MM-dd hh:mm:ss");
116
        } else if (line.startsWith(QLatin1String("Attributes = "))) {
117
            const QString attributes = line.mid(13).trimmed();
118

119
            const bool isDirectory = attributes.startsWith('D');
120 121
            m_currentArchiveEntry[ IsDirectory ] = isDirectory;
            if (isDirectory) {
122 123
                const QString directoryName =
                    m_currentArchiveEntry[FileName].toString();
124
                if (!directoryName.endsWith('/')) {
125 126 127 128 129
                    const bool isPasswordProtected = (line.at(12) == '+');
                    m_currentArchiveEntry[FileName] =
                        m_currentArchiveEntry[InternalID] = directoryName + '/';
                    m_currentArchiveEntry[ IsPasswordProtected ] =
                        isPasswordProtected;
130 131 132 133 134 135 136
                }
            }

            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 = "))) {
137 138 139 140
            m_currentArchiveEntry[ Method ] = line.mid(9).trimmed();
        } else if (line.startsWith(QLatin1String("Encrypted = ")) &&
                   line.size() >= 13) {
            m_currentArchiveEntry[ IsPasswordProtected ] = (line.at(12) == '+');
141 142 143 144 145 146 147 148 149
        } else if (line.startsWith(QLatin1String("Block = "))) {
            if (m_currentArchiveEntry.contains(FileName)) {
                entry(m_currentArchiveEntry);
            }
        }
        break;
    }

    return true;
150
}
151

152
KERFUFFLE_EXPORT_PLUGIN(CliPlugin)
153

154
#include "cliplugin.moc"