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

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

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

30 31
#include <KDebug>

32 33
using namespace Kerfuffle;

34 35 36
CliPlugin::CliPlugin( const QString & filename, QObject *parent )
	: CliInterface( filename, parent )
	, m_state(0)
37
{
38
}
39

40 41
CliPlugin::~CliPlugin()
{
42

43
}
44

45 46 47
ParameterList CliPlugin::parameterList() const
{
	static ParameterList p;
48

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

53
		p[ListArgs] = QStringList() << "l" << "-slt" << "$Archive";
Harald Hvaal's avatar
Harald Hvaal committed
54
		p[ExtractArgs] = QStringList() << "$PreservePathSwitch" << "$PasswordSwitch" << "$RootNodeSwitch" << "$Archive" << "$Files";
55 56
		p[PreservePathSwitch] = QStringList() << "x" << "e";
		p[RootNodeSwitch] = QStringList() << "-w$Path";
Harald Hvaal's avatar
Harald Hvaal committed
57 58 59 60 61 62 63 64 65 66 67 68 69
		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
			;
70

71
	}
72

73 74
	return p;
}
75

76 77 78 79 80
bool CliPlugin::readListLine(QString line)
{
	switch (m_state)
	{
		case 0: // header
81
			if (line.startsWith(QLatin1String("Listing archive:")))
82
			{
83 84
				kDebug( 1601 ) << "Archive name: " << line.right(line.size() - 16).trimmed();
			}
85
			else if (line.startsWith(QLatin1String("----------")))
86 87 88 89 90 91 92 93 94 95
			{
				m_state = 1;
			}
			else if (line.contains("Error:"))
			{
				kDebug( 1601 ) << line.mid(6);
				//m_errorMessages << line.mid(6);
			}
			break;
		case 1: // beginning of a file detail
96
			if (line.startsWith(QLatin1String("Path =")))
97 98 99 100 101 102 103 104
			{
				m_currentArchiveEntry.clear();
				QString entryFilename = QDir::fromNativeSeparators(line.mid( 6).trimmed());
				m_currentArchiveEntry[FileName] = entryFilename;
				m_currentArchiveEntry[InternalID] = entryFilename;
				m_state = 2;
			}
			break;
105

106
		case 2: // file details
107
			if (line.startsWith(QLatin1String("Size = ")))
108 109 110
			{
				m_currentArchiveEntry[ Size ] = line.mid( 7).trimmed();
			}
111
			else if (line.startsWith(QLatin1String("Packed Size = ")))
112 113 114
			{
				m_currentArchiveEntry[ CompressedSize ] = line.mid( 14).trimmed();
			}
115
			else if (line.startsWith(QLatin1String("Modified = ")))
116 117 118 119
			{
				QDateTime ts = QDateTime::fromString(line.mid(11).trimmed(), "yyyy-MM-dd hh:mm:ss");
				m_currentArchiveEntry[ Timestamp ] = ts;
			}
120
			else if (line.startsWith(QLatin1String("Attributes = ")))
121 122 123 124 125 126 127 128 129
			{
				QString attributes = line.mid(13).trimmed();

				bool isDirectory = attributes.startsWith('D');
				m_currentArchiveEntry[ IsDirectory ] = isDirectory;
				if (isDirectory)
				{
					QString directoryName = m_currentArchiveEntry[FileName].toString();
					if (!directoryName.endsWith('/'))
130
					{
131
						m_currentArchiveEntry[FileName] = m_currentArchiveEntry[InternalID] = directoryName + '/';
132 133
						bool isPasswordProtected = (line.at(12) == '+');
						m_currentArchiveEntry[ IsPasswordProtected ] = isPasswordProtected;
134
					}
135
				}
136

137 138
				m_currentArchiveEntry[ Permissions ] = attributes.mid(1);
			}
139
			else if (line.startsWith(QLatin1String("CRC = ")))
140 141 142
			{
				m_currentArchiveEntry[ CRC ] = line.mid(6).trimmed();
			}
143
			else if (line.startsWith(QLatin1String("Method = ")))
144 145 146 147
			{
				QString method = line.mid(9).trimmed();
				m_currentArchiveEntry[ Method ] = method;
			}
148
			else if (line.startsWith(QLatin1String("Encrypted = ")) && line.size() >= 13)
149
			{
150 151
				bool isPasswordProtected = (line.at(12) == '+');
				m_currentArchiveEntry[ IsPasswordProtected ] = isPasswordProtected;
152
			}
153
			else if (line.startsWith(QLatin1String("Block = ")))
154 155 156 157 158
			{
				if (m_currentArchiveEntry.contains(FileName))
				{
					entry(m_currentArchiveEntry);
				}
159

160
				m_state = 1;
161
			}
162 163 164 165 166 167 168 169
			break;

		default:
			break;
	}

	return true;
}
170 171 172

KERFUFFLE_PLUGIN_FACTORY(CliPlugin)

173
#include "cliplugin.moc"