Commit 2825dd5d authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Allow to return list of files from archive. So we can compare

that all files are exported
parent 83562e94
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
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; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "generatelistfilefromarchive.h"
#include "core/utils.h"
#include <KZip>
#include <QTest>
GenerateListFileFromArchive::GenerateListFileFromArchive(const QString &fileName)
: mFileName(fileName)
{
generateList();
}
GenerateListFileFromArchive::~GenerateListFileFromArchive()
{
delete mZip;
mZip = nullptr;
}
void GenerateListFileFromArchive::generateList()
{
mZip = new KZip(mFileName);
bool result = mZip->open(QIODevice::ReadOnly);
QVERIFY(result);
const KArchiveDirectory *topDirectory = mZip->directory();
const bool isAValidArchive = searchArchiveElement(Utils::infoPath(), topDirectory);
QVERIFY(isAValidArchive);
(void)searchArchiveElement(Utils::mailsPath(), topDirectory);
(void)searchArchiveElement(Utils::alarmPath(), topDirectory);
(void)searchArchiveElement(Utils::calendarPath(), topDirectory);
(void)searchArchiveElement(Utils::addressbookPath(), topDirectory);
(void)searchArchiveElement(Utils::identitiesPath(), topDirectory);
(void)searchArchiveElement(Utils::resourcesPath(), topDirectory);
(void)searchArchiveElement(Utils::configsPath(), topDirectory);
(void)searchArchiveElement(Utils::transportsPath(), topDirectory);
(void)searchArchiveElement(Utils::dataPath(), topDirectory);
mListFile.sort();
}
QStringList GenerateListFileFromArchive::listFile() const
{
return mListFile;
}
bool GenerateListFileFromArchive::searchArchiveElement(const QString &path, const KArchiveDirectory *topDirectory)
{
const KArchiveEntry *topEntry = topDirectory->entry(path);
bool result = true;
if (topEntry) {
addSubItems(path, topEntry, 0);
} else {
result = false;
}
return result;
}
void GenerateListFileFromArchive::addSubItems(const QString &topLevelPath, const KArchiveEntry *entry, int indent, const QString &fullpath)
{
const KArchiveDirectory *dir = static_cast<const KArchiveDirectory *>(entry);
//++indent;
const QString space = QString(indent * 2, QLatin1Char(' '));
const QStringList lst = dir->entries();
for (const QString &entryName : lst) {
const KArchiveEntry *entry = dir->entry(entryName);
if (entry) {
if (entry->isDirectory()) {
const KArchiveDirectory *dirEntry = static_cast<const KArchiveDirectory *>(entry);
mListFile += space + dirEntry->name();
addSubItems(topLevelPath, entry, indent, (fullpath.isEmpty() ? QString() : fullpath + QLatin1Char('/')) + dirEntry->name());
} else if (entry->isFile()) {
const KArchiveFile *file = static_cast<const KArchiveFile *>(entry);
const QString fileFullPath = topLevelPath + (fullpath.isEmpty() ? QString() : fullpath + QLatin1Char('/')) + file->name();
mListFile += space + fileFullPath;
}
}
}
}
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
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; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef GENERATELISTFILEFROMARCHIVE_H
#define GENERATELISTFILEFROMARCHIVE_H
#include <QString>
#include <QStringList>
class KZip;
class KArchiveDirectory;
class KArchiveEntry;
class GenerateListFileFromArchive
{
public:
explicit GenerateListFileFromArchive(const QString &fileName);
~GenerateListFileFromArchive();
Q_REQUIRED_RESULT QStringList listFile() const;
private:
Q_REQUIRED_RESULT bool searchArchiveElement(const QString &path, const KArchiveDirectory *topDirectory);
void addSubItems(const QString &topLevelPath, const KArchiveEntry *entry, int indent, const QString &fullpath = QString());
void generateList();
QString mFileName;
QStringList mListFile;
KZip *mZip = nullptr;
};
#endif // GENERATELISTFILEFROMARCHIVE_H
add_definitions( -DPIMDATAEXPORTER_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data" )
set( exportnotesjobinterfacetest_SRCS exportnotesjobinterfacetest.cpp ../resourceconvertertest.cpp )
set( exportnotesjobinterfacetest_SRCS exportnotesjobinterfacetest.cpp ../resourceconvertertest.cpp ../generatelistfilefromarchive.cpp)
add_executable( exportnotesjobinterfacetest ${exportnotesjobinterfacetest_SRCS})
add_test(NAME exportnotesjobinterfacetest COMMAND exportnotesjobinterfacetest)
ecm_mark_as_test(exportnotesjobinterfacetest)
target_link_libraries( exportnotesjobinterfacetest Qt5::Test pimdataexporterprivate KF5::ConfigCore)
target_link_libraries( exportnotesjobinterfacetest Qt5::Test pimdataexporterprivate KF5::ConfigCore KF5::Archive)
......@@ -21,6 +21,7 @@
#include "notes/exportnotesjobinterface.h"
#include "archivestorage.h"
#include "../resourceconvertertest.h"
#include "../generatelistfilefromarchive.h"
#include <QDebug>
#include <QTest>
#include <QSignalSpy>
......@@ -75,6 +76,9 @@ void ExportNotesJobInterfaceTest::exportNoteConfigTest1()
QCOMPARE(error.count(), 0);
delete exportNote;
delete archiveStorage;
GenerateListFileFromArchive archive(QStringLiteral("/tmp/foo.zip"));
qDebug() << " archive " << archive.listFile();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment