cliplugin.cpp 3.75 KB
Newer Older
1
2
3
/*
 * ark -- archiver for the KDE project
 *
4
 * Copyright (C) 2009 Harald Hvaal <haraldhv@stud.ntnu.no>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * 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.
 *
 */
21

22
23
24
25
#include "kerfuffle/cliinterface.h"
#include "kerfuffle/archivefactory.h"

#include <QDir>
26
#include <QLatin1String>
27
28
#include <QString>
#include <QStringList>
29

30
#include <KDebug>
31
32
33
34
35

using namespace Kerfuffle;

class CliPlugin: public CliInterface
{
36
37
38
39
40
41
42
43
44
45
46
47
48
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
public:
    explicit CliPlugin(const QString & filename, QObject *parent = 0)
            : CliInterface(filename, parent),
            m_status(Header) {

    }

    virtual ~CliPlugin() {

    }

    virtual ParameterList parameterList() const {
        static ParameterList p;
        if (p.isEmpty()) {

            p[CaptureProgress] = false;
            p[ListProgram] = "zipinfo";
            p[ExtractProgram] = "unzip";
            p[DeleteProgram] = p[AddProgram] = "zip";

            p[ListArgs] = QStringList() << "-l" << "$Archive";
            p[ExtractArgs] = QStringList() << "$PreservePathSwitch" << "$PasswordSwitch" << "$Archive" << "$Files";
            p[PreservePathSwitch] = QStringList() << "" << "-j";
            p[PasswordSwitch] = QStringList() << "-P$Password";

            p[DeleteArgs] = QStringList() << "-d" << "$Archive" << "$Files";

            p[FileExistsExpression] = "^replace (.+)\\?";
            p[FileExistsInput] = QStringList()
                                 << "y" //overwrite
                                 << "n" //skip
                                 << "A" //overwrite all
                                 << "N" //autoskip
                                 << "N" //cancel
                                 ;

            p[AddArgs] = QStringList() << "-r" << "$Archive" << "$Files";

            p[WrongPasswordPatterns] = QStringList() << "incorrect password";
            //p[ExtractionFailedPatterns] = QStringList() << "CRC failed";
        }
        return p;
    }

    QString m_entryFilename, m_internalId;
    ArchiveEntry m_currentEntry;

    enum ReadStatus {
        Header = 0,
        Entry
    };

    ReadStatus m_status;

    bool readListLine(QString line) {
        static QRegExp entryPattern(
            "^(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(.+)$");

        int i;
        switch (m_status) {
        case Header:
            m_status = Entry;
            break;
        case Entry:
            i = entryPattern.indexIn(line);
            if (i != -1) {
                ArchiveEntry e;
                e[Permissions] = entryPattern.cap(1);
104
                e[IsDirectory] = (entryPattern.cap(1).at(0) == 'd');
105
106
107
108
109
110
111
112
113
114
115
116
117
                e[Owner] = entryPattern.cap(3);
                e[Size] = entryPattern.cap(4).toInt();
                QString status = entryPattern.cap(5);
                if (status[0].isUpper())
                    e[IsPasswordProtected] = true;
                e[CompressedSize] = entryPattern.cap(6).toInt();
                e[FileName] = e[InternalID] = entryPattern.cap(10);
                entry(e);
            }
            break;
        }
        return true;
    }
118
119
120
121
};

KERFUFFLE_PLUGIN_FACTORY(CliPlugin)