File indexing completed on 2024-12-22 05:05:25

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "generatelistfilefromarchive.h"
0008 #include "core/utils.h"
0009 #include <KZip>
0010 #include <QTest>
0011 
0012 GenerateListFileFromArchive::GenerateListFileFromArchive(const QString &fileName)
0013     : mFileName(fileName)
0014 {
0015     generateList();
0016 }
0017 
0018 GenerateListFileFromArchive::~GenerateListFileFromArchive()
0019 {
0020     delete mZip;
0021     mZip = nullptr;
0022 }
0023 
0024 void GenerateListFileFromArchive::generateList()
0025 {
0026     mZip = new KZip(mFileName);
0027     // qDebug() << " mFileName" << mFileName;
0028     bool result = mZip->open(QIODevice::ReadOnly);
0029     QVERIFY(result);
0030     const KArchiveDirectory *topDirectory = mZip->directory();
0031     const bool isAValidArchive = searchArchiveElement(Utils::infoPath(), topDirectory);
0032     QVERIFY(isAValidArchive);
0033     (void)searchArchiveElement(Utils::mailsPath(), topDirectory);
0034     (void)searchArchiveElement(Utils::alarmPath(), topDirectory);
0035     (void)searchArchiveElement(Utils::calendarPath(), topDirectory);
0036     (void)searchArchiveElement(Utils::addressbookPath(), topDirectory);
0037     (void)searchArchiveElement(Utils::identitiesPath(), topDirectory);
0038     (void)searchArchiveElement(Utils::resourcesPath(), topDirectory);
0039     (void)searchArchiveElement(Utils::configsPath(), topDirectory);
0040     (void)searchArchiveElement(Utils::transportsPath(), topDirectory);
0041     (void)searchArchiveElement(Utils::dataPath(), topDirectory);
0042     (void)searchArchiveElement(Utils::notePath(), topDirectory);
0043     mListFile.sort();
0044 }
0045 
0046 QStringList GenerateListFileFromArchive::listFile() const
0047 {
0048     return mListFile;
0049 }
0050 
0051 bool GenerateListFileFromArchive::searchArchiveElement(const QString &path, const KArchiveDirectory *topDirectory)
0052 {
0053     const KArchiveEntry *topEntry = topDirectory->entry(path);
0054     bool result = true;
0055     if (topEntry) {
0056         addSubItems(path, topEntry, 0);
0057     } else {
0058         result = false;
0059     }
0060     return result;
0061 }
0062 
0063 void GenerateListFileFromArchive::addSubItems(const QString &topLevelPath, const KArchiveEntry *entry, int indent, const QString &fullpath)
0064 {
0065     const auto dir = static_cast<const KArchiveDirectory *>(entry);
0066     const QString space = QString(indent * 2, QLatin1Char(' '));
0067     const QStringList lst = dir->entries();
0068     for (const QString &entryName : lst) {
0069         const KArchiveEntry *archiveEntry = dir->entry(entryName);
0070         if (archiveEntry) {
0071             if (archiveEntry->isDirectory()) {
0072                 const auto dirEntry = static_cast<const KArchiveDirectory *>(archiveEntry);
0073                 // mListFile += space + dirEntry->name();
0074                 addSubItems(topLevelPath, archiveEntry, indent, (fullpath.isEmpty() ? QString() : fullpath + QLatin1Char('/')) + dirEntry->name());
0075             } else if (archiveEntry->isFile()) {
0076                 const auto file = static_cast<const KArchiveFile *>(archiveEntry);
0077                 const QString fileFullPath = topLevelPath + (fullpath.isEmpty() ? QString() : fullpath + QLatin1Char('/')) + file->name();
0078                 mListFile += space + fileFullPath;
0079             }
0080         }
0081     }
0082 }