File indexing completed on 2024-06-16 05:00:33

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "exportmailfolderattributejob.h"
0008 #include "pimdataexportcore_debug.h"
0009 #include "utils.h"
0010 #include <KConfigGroup>
0011 #include <KZip>
0012 #include <QTemporaryFile>
0013 
0014 ExportMailFolderAttributeJob::ExportMailFolderAttributeJob(QObject *parent)
0015     : QObject{parent}
0016 {
0017 }
0018 
0019 ExportMailFolderAttributeJob::~ExportMailFolderAttributeJob()
0020 {
0021 }
0022 
0023 bool ExportMailFolderAttributeJob::canStart() const
0024 {
0025     return (mArchive != nullptr);
0026 }
0027 
0028 void ExportMailFolderAttributeJob::start()
0029 {
0030     if (!canStart()) {
0031         Q_EMIT failed();
0032         qCWarning(PIMDATAEXPORTERCORE_LOG) << " Impossible to start job";
0033         deleteLater();
0034         return;
0035     }
0036     fetchAttributes();
0037 }
0038 
0039 void ExportMailFolderAttributeJob::setArchive(KZip *zip)
0040 {
0041     mArchive = zip;
0042 }
0043 
0044 void ExportMailFolderAttributeJob::setExportInterface(ExportMailJobInterface *interface)
0045 {
0046     mInterface = interface;
0047 }
0048 
0049 void ExportMailFolderAttributeJob::storeFileFolderAttribute(const QMap<QString, ImportExportMailUtil::AttributeInfo> &lstAttributeInfo)
0050 {
0051     QTemporaryFile tmp;
0052     tmp.open();
0053 
0054     KConfig conf(tmp.fileName());
0055     QMapIterator<QString, ImportExportMailUtil::AttributeInfo> i(lstAttributeInfo);
0056     while (i.hasNext()) {
0057         i.next();
0058         KConfigGroup attributeGroup = conf.group(i.key());
0059 
0060         auto attr = i.value();
0061         if (!attr.displayAttribute.isEmpty()) {
0062             attributeGroup.writeEntry(QStringLiteral("Display"), attr.displayAttribute);
0063         }
0064         if (!attr.expireAttribute.isEmpty()) {
0065             attributeGroup.writeEntry(QStringLiteral("Expire"), attr.expireAttribute);
0066         }
0067         if (!attr.favoriteAttribute.isEmpty()) {
0068             attributeGroup.writeEntry(QStringLiteral("Favorite"), attr.favoriteAttribute);
0069         }
0070         if (!attr.folderAttribute.isEmpty()) {
0071             attributeGroup.writeEntry(QStringLiteral("Folder"), attr.folderAttribute);
0072         }
0073     }
0074     conf.sync();
0075     tmp.close();
0076 
0077     const bool fileAdded = mArchive->addLocalFile(tmp.fileName(), Utils::configsPath() + QStringLiteral("mailfolderattributes"));
0078     if (fileAdded) {
0079         Q_EMIT successed();
0080     } else {
0081         Q_EMIT failed();
0082     }
0083     deleteLater();
0084 }
0085 
0086 #include "moc_exportmailfolderattributejob.cpp"