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

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "exportaddressbookjobinterface.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include <KConfigGroup>
0012 #include <QFileInfo>
0013 #include <QTemporaryFile>
0014 
0015 #include "resourceconverterimpl.h"
0016 #include <QStandardPaths>
0017 #include <QTimer>
0018 
0019 ExportAddressbookJobInterface::ExportAddressbookJobInterface(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep)
0020     : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep)
0021 {
0022 }
0023 
0024 ExportAddressbookJobInterface::~ExportAddressbookJobInterface() = default;
0025 
0026 void ExportAddressbookJobInterface::start()
0027 {
0028     Q_EMIT title(i18n("Start export KAddressBook settings..."));
0029     createProgressDialog(i18n("Export KAddressBook settings"));
0030     if (mTypeSelected & Utils::Resources) {
0031         QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotCheckBackupResource);
0032     } else if (mTypeSelected & Utils::Config) {
0033         QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotCheckBackupConfig);
0034     } else {
0035         Q_EMIT jobFinished();
0036     }
0037 }
0038 
0039 void ExportAddressbookJobInterface::slotCheckBackupResource()
0040 {
0041     mAkonadiInstanceInfo = listOfResource();
0042     setProgressDialogLabel(i18n("Backing up resources..."));
0043     increaseProgressDialog();
0044     QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotWriteNextArchiveResource);
0045 }
0046 
0047 void ExportAddressbookJobInterface::slotCheckBackupConfig()
0048 {
0049     if (mTypeSelected & Utils::Config) {
0050         backupConfig();
0051         increaseProgressDialog();
0052         if (wasCanceled()) {
0053             Q_EMIT jobFinished();
0054             return;
0055         }
0056     }
0057     Q_EMIT jobFinished();
0058 }
0059 
0060 QString ExportAddressbookJobInterface::applicationName() const
0061 {
0062     return QStringLiteral("[KAddressBook]");
0063 }
0064 
0065 void ExportAddressbookJobInterface::slotAddressbookJobTerminated()
0066 {
0067     if (wasCanceled()) {
0068         Q_EMIT jobFinished();
0069         return;
0070     }
0071     mIndexIdentifier++;
0072     QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotWriteNextArchiveResource);
0073 }
0074 
0075 void ExportAddressbookJobInterface::slotWriteNextArchiveResource()
0076 {
0077     if (mIndexIdentifier < mAkonadiInstanceInfo.count()) {
0078         const Utils::AkonadiInstanceInfo agent = mAkonadiInstanceInfo.at(mIndexIdentifier);
0079         const QString identifier = agent.identifier;
0080         if (identifier.contains(QLatin1StringView("akonadi_vcarddir_resource_")) || identifier.contains(QLatin1StringView("akonadi_contacts_resource_"))) {
0081             const QString archivePath = Utils::addressbookPath() + identifier + QLatin1Char('/');
0082 
0083             const QString url = resourcePath(identifier, QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/contacts/"));
0084             if (!mAgentPaths.contains(url)) {
0085                 if (!url.isEmpty()) {
0086                     mAgentPaths << url;
0087                     exportResourceToArchive(archivePath, url, identifier);
0088                 } else {
0089                     qCDebug(PIMDATAEXPORTERCORE_LOG) << "Url is empty for " << identifier;
0090                     QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotAddressbookJobTerminated);
0091                 }
0092             } else {
0093                 QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotAddressbookJobTerminated);
0094             }
0095         } else if (identifier.contains(QLatin1StringView("akonadi_vcard_resource_"))) {
0096             backupAddressBookResourceFile(identifier, Utils::addressbookPath());
0097             QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotAddressbookJobTerminated);
0098         } else {
0099             QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotAddressbookJobTerminated);
0100         }
0101     } else {
0102         emitInfo(i18n("Resources backup done."));
0103         QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotCheckBackupConfig);
0104     }
0105 }
0106 
0107 void ExportAddressbookJobInterface::backupConfig()
0108 {
0109     setProgressDialogLabel(i18n("Backing up config..."));
0110 
0111     const QString kaddressbookStr(QStringLiteral("kaddressbookrc"));
0112     const QString kaddressbookrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + kaddressbookStr;
0113     if (QFileInfo::exists(kaddressbookrc)) {
0114         KSharedConfigPtr kaddressbook = KSharedConfig::openConfig(kaddressbookrc);
0115 
0116         QTemporaryFile tmp;
0117         tmp.open();
0118 
0119         KConfig *kaddressBookConfig = kaddressbook->copyTo(tmp.fileName());
0120 
0121         const QString collectionViewCheckStateStr(QStringLiteral("CollectionViewCheckState"));
0122         if (kaddressBookConfig->hasGroup(collectionViewCheckStateStr)) {
0123             KConfigGroup group = kaddressBookConfig->group(collectionViewCheckStateStr);
0124             const QString selectionKey(QStringLiteral("Selection"));
0125             convertCollectionListToRealPath(group, selectionKey);
0126         }
0127 
0128         const QString collectionViewStateStr(QStringLiteral("CollectionViewState"));
0129         if (kaddressBookConfig->hasGroup(collectionViewStateStr)) {
0130             KConfigGroup group = kaddressBookConfig->group(collectionViewStateStr);
0131             QString currentKey(QStringLiteral("Current"));
0132             convertCollectionToRealPath(group, currentKey);
0133 
0134             currentKey = QStringLiteral("Expansion");
0135             convertCollectionListToRealPath(group, currentKey);
0136 
0137             currentKey = QStringLiteral("Selection");
0138             convertCollectionToRealPath(group, currentKey);
0139         }
0140         kaddressBookConfig->sync();
0141         backupFile(tmp.fileName(), Utils::configsPath(), kaddressbookStr);
0142         delete kaddressBookConfig;
0143     }
0144     backupUiRcFile(QStringLiteral("kaddressbookui.rc"), QStringLiteral("kaddressbook"));
0145 
0146     storeDirectory(QStringLiteral("/kaddressbook/csv-templates/"));
0147     storeDirectory(QStringLiteral("/kaddressbook/viewertemplates/"));
0148     storeDirectory(QStringLiteral("/kaddressbook/printing/"));
0149 
0150     emitInfo(i18n("Config backup done."));
0151 }
0152 
0153 #include "moc_exportaddressbookjobinterface.cpp"