1 /*
2 SPDX-FileCopyrightText: 2020-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "generatelistfilefromarchive.h"
8 #include "core/utils.h"
9 #include <KZip>
10 #include <QTest>
11
GenerateListFileFromArchive(const QString & fileName)12 GenerateListFileFromArchive::GenerateListFileFromArchive(const QString &fileName)
13 : mFileName(fileName)
14 {
15 generateList();
16 }
17
~GenerateListFileFromArchive()18 GenerateListFileFromArchive::~GenerateListFileFromArchive()
19 {
20 delete mZip;
21 mZip = nullptr;
22 }
23
generateList()24 void GenerateListFileFromArchive::generateList()
25 {
26 mZip = new KZip(mFileName);
27 // qDebug() << " mFileName" << mFileName;
28 bool result = mZip->open(QIODevice::ReadOnly);
29 QVERIFY(result);
30 const KArchiveDirectory *topDirectory = mZip->directory();
31 const bool isAValidArchive = searchArchiveElement(Utils::infoPath(), topDirectory);
32 QVERIFY(isAValidArchive);
33 (void)searchArchiveElement(Utils::mailsPath(), topDirectory);
34 (void)searchArchiveElement(Utils::alarmPath(), topDirectory);
35 (void)searchArchiveElement(Utils::calendarPath(), topDirectory);
36 (void)searchArchiveElement(Utils::addressbookPath(), topDirectory);
37 (void)searchArchiveElement(Utils::identitiesPath(), topDirectory);
38 (void)searchArchiveElement(Utils::resourcesPath(), topDirectory);
39 (void)searchArchiveElement(Utils::configsPath(), topDirectory);
40 (void)searchArchiveElement(Utils::transportsPath(), topDirectory);
41 (void)searchArchiveElement(Utils::dataPath(), topDirectory);
42 (void)searchArchiveElement(Utils::notePath(), topDirectory);
43 mListFile.sort();
44 }
45
listFile() const46 QStringList GenerateListFileFromArchive::listFile() const
47 {
48 return mListFile;
49 }
50
searchArchiveElement(const QString & path,const KArchiveDirectory * topDirectory)51 bool GenerateListFileFromArchive::searchArchiveElement(const QString &path, const KArchiveDirectory *topDirectory)
52 {
53 const KArchiveEntry *topEntry = topDirectory->entry(path);
54 bool result = true;
55 if (topEntry) {
56 addSubItems(path, topEntry, 0);
57 } else {
58 result = false;
59 }
60 return result;
61 }
62
addSubItems(const QString & topLevelPath,const KArchiveEntry * entry,int indent,const QString & fullpath)63 void GenerateListFileFromArchive::addSubItems(const QString &topLevelPath, const KArchiveEntry *entry, int indent, const QString &fullpath)
64 {
65 const auto dir = static_cast<const KArchiveDirectory *>(entry);
66 const QString space = QString(indent * 2, QLatin1Char(' '));
67 const QStringList lst = dir->entries();
68 for (const QString &entryName : lst) {
69 const KArchiveEntry *archiveEntry = dir->entry(entryName);
70 if (archiveEntry) {
71 if (archiveEntry->isDirectory()) {
72 const auto dirEntry = static_cast<const KArchiveDirectory *>(archiveEntry);
73 // mListFile += space + dirEntry->name();
74 addSubItems(topLevelPath, archiveEntry, indent, (fullpath.isEmpty() ? QString() : fullpath + QLatin1Char('/')) + dirEntry->name());
75 } else if (archiveEntry->isFile()) {
76 const auto file = static_cast<const KArchiveFile *>(archiveEntry);
77 const QString fileFullPath = topLevelPath + (fullpath.isEmpty() ? QString() : fullpath + QLatin1Char('/')) + file->name();
78 mListFile += space + fileFullPath;
79 }
80 }
81 }
82 }
83