62
64
if (!archive()->isOpen() && !archive()->open(QIODevice::ReadOnly)) {
63
error(i18nc("@info", "Could not open the archive <filename>%1</filename> for reading", filename()));
65
emit error(i18nc("@info", "Could not open the archive <filename>%1</filename> for reading", filename()));
66
68
return browseArchive(archive());
72
void KArchiveInterface::getAllEntries(const KArchiveDirectory *dir, const QString &prefix, QList< QVariant > &list)
74
foreach(const QString &entryName, dir->entries()) {
75
const KArchiveEntry *entry = dir->entry(entryName);
76
if (entry->isDirectory()) {
77
QString newPrefix = (prefix.isEmpty() ? prefix : prefix + QLatin1Char('/')) + entryName;
78
getAllEntries(static_cast<const KArchiveDirectory*>(entry), newPrefix, list);
81
list.append(prefix + QLatin1Char('/') + entryName);
70
86
bool KArchiveInterface::copyFiles(const QList<QVariant> &files, const QString &destinationDirectory, ExtractionOptions options)
72
88
const bool preservePaths = options.value(QLatin1String("PreservePaths")).toBool();
89
const KArchiveDirectory *dir = archive()->directory();
74
91
if (!archive()->isOpen() && !archive()->open(QIODevice::ReadOnly)) {
75
error(i18nc("@info", "Could not open the archive <filename>%1</filename> for reading", filename()));
92
emit error(i18nc("@info", "Could not open the archive <filename>%1</filename> for reading", filename()));
79
foreach(const QVariant &file, files) {
96
QList<QVariant> extrFiles = files;
97
if (extrFiles.isEmpty()) { // All files should be extracted
98
getAllEntries(dir, QString(), extrFiles);
101
bool overwriteAllSelected = false;
102
bool autoSkipSelected = false;
103
QSet<QString> dirCache;
104
foreach(const QVariant &file, extrFiles) {
80
105
QString realDestination = destinationDirectory;
81
const KArchiveEntry *archiveEntry = archive()->directory()->entry(file.toString());
106
const KArchiveEntry *archiveEntry = dir->entry(file.toString());
82
107
if (!archiveEntry) {
83
error(i18nc("@info", "File <filename>%1</filename> not found in the archive" , file.toString()));
108
emit error(i18nc("@info", "File <filename>%1</filename> not found in the archive" , file.toString()));
87
// TODO: handle errors, copyTo fails silently
88
112
if (preservePaths) {
89
113
QFileInfo fi(file.toString());
90
114
QDir dest(destinationDirectory);
91
115
QString filepath = archiveEntry->isDirectory() ? fi.filePath() : fi.path();
92
dest.mkpath(filepath);
93
realDestination = dest.absolutePath() + '/' + filepath;
116
if (!dirCache.contains(filepath)) {
117
if (!dest.mkpath(filepath)) {
118
emit error(i18nc("@info", "Error creating directory <filename>%1</filename>", filepath));
121
dirCache << filepath;
123
realDestination = dest.absolutePath() + QLatin1Char('/') + filepath;
95
if (archiveEntry->isDirectory()) {
96
kDebug() << "Calling copyTo(" << realDestination << ") for " << archiveEntry->name();
97
static_cast<const KArchiveDirectory*>(archiveEntry)->copyTo(realDestination);
99
static_cast<const KArchiveFile*>(archiveEntry)->copyTo(realDestination);
126
// TODO: handle errors, copyTo fails silently
127
if (!archiveEntry->isDirectory()) { // We don't need to do anything about directories
128
if (QFile::exists(realDestination + QLatin1Char('/') + archiveEntry->name()) && !overwriteAllSelected) {
129
if (autoSkipSelected) {
133
int response = handleFileExistsMessage(realDestination, archiveEntry->name());
135
if (response == OverwriteCancel) {
138
if (response == OverwriteYes || response == OverwriteAll) {
139
static_cast<const KArchiveFile*>(archiveEntry)->copyTo(realDestination);
140
if (response == OverwriteAll) {
141
overwriteAllSelected = true;
144
if (response == OverwriteAutoSkip) {
145
autoSkipSelected = true;
149
static_cast<const KArchiveFile*>(archiveEntry)->copyTo(realDestination);
157
int KArchiveInterface::handleFileExistsMessage(const QString &dir, const QString &fileName)
159
Kerfuffle::OverwriteQuery query(dir + QLatin1Char('/') + fileName);
160
query.setNoRenameMode(true);
161
emit userQuery(&query);
162
query.waitForResponse();
164
if (query.responseOverwrite()) {
166
} else if (query.responseSkip()) {
167
return OverwriteSkip;
168
} else if (query.responseOverwriteAll()) {
170
} else if (query.responseAutoSkip()) {
171
return OverwriteAutoSkip;
174
return OverwriteCancel;
106
177
bool KArchiveInterface::browseArchive(KArchive *archive)
108
179
return processDir(archive->directory());
167
243
if (fi.isDir()) {
168
244
if (archive()->addLocalDirectory(path, fi.fileName())) {
169
245
const KArchiveEntry *entry = archive()->directory()->entry(fi.fileName());
170
createEntryFor(entry, "");
246
createEntryFor(entry, QString());
171
247
processDir((KArchiveDirectory*) archive()->directory()->entry(fi.fileName()), fi.fileName());
173
error(i18nc("@info", "Could not add the directory <filename>%1</filename> to the archive", path));
249
emit error(i18nc("@info", "Could not add the directory <filename>%1</filename> to the archive", path));
177
253
if (archive()->addLocalFile(path, fi.fileName())) {
178
254
const KArchiveEntry *entry = archive()->directory()->entry(fi.fileName());
179
createEntryFor(entry, "");
255
createEntryFor(entry, QString());
181
error(i18nc("@info", "Could not add the file <filename>%1</filename> to the archive.", path));
257
emit error(i18nc("@info", "Could not add the file <filename>%1</filename> to the archive.", path));