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

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 "exportnotesjobinterface.h"
0008 
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 #include <KZip>
0012 #include <QTemporaryFile>
0013 
0014 #include "resourceconverterimpl.h"
0015 #include <QDir>
0016 #include <QStandardPaths>
0017 #include <QTimer>
0018 
0019 ExportNotesJobInterface::ExportNotesJobInterface(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep)
0020     : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep)
0021 {
0022 }
0023 
0024 ExportNotesJobInterface::~ExportNotesJobInterface() = default;
0025 
0026 void ExportNotesJobInterface::start()
0027 {
0028     Q_EMIT title(i18n("Start export KNotes settings..."));
0029     createProgressDialog(i18n("Export KNotes settings"));
0030     if (mTypeSelected & Utils::Resources) {
0031         QTimer::singleShot(0, this, &ExportNotesJobInterface::slotCheckBackupResource);
0032     } else if (mTypeSelected & Utils::Config) {
0033         QTimer::singleShot(0, this, &ExportNotesJobInterface::slotCheckBackupConfig);
0034     } else {
0035         Q_EMIT jobFinished();
0036     }
0037 }
0038 
0039 void ExportNotesJobInterface::backupTheme()
0040 {
0041     const QString notesThemeDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/knotes/print/");
0042     QDir notesThemeDirectory(notesThemeDir);
0043     if (notesThemeDirectory.exists()) {
0044         const bool notesDirAdded = archive()->addLocalDirectory(notesThemeDir, Utils::dataPath() + QStringLiteral("knotes/print"));
0045         if (!notesDirAdded) {
0046             emitError(i18n("\"%1\" directory cannot be added to backup file.", notesThemeDir));
0047         }
0048     }
0049 }
0050 
0051 void ExportNotesJobInterface::slotCheckBackupResource()
0052 {
0053     setProgressDialogLabel(i18n("Backing up resources..."));
0054     increaseProgressDialog();
0055     backupTheme();
0056     mAkonadiInstanceInfo = listOfResource();
0057     QTimer::singleShot(0, this, &ExportNotesJobInterface::slotWriteNextArchiveResource);
0058 }
0059 
0060 void ExportNotesJobInterface::slotCheckBackupConfig()
0061 {
0062     if (mTypeSelected & Utils::Config) {
0063         backupConfig();
0064         increaseProgressDialog();
0065         if (wasCanceled()) {
0066             Q_EMIT jobFinished();
0067             return;
0068         }
0069     }
0070     Q_EMIT jobFinished();
0071 }
0072 
0073 void ExportNotesJobInterface::backupConfig()
0074 {
0075     setProgressDialogLabel(i18n("Backing up config..."));
0076 
0077     const QString knotesStr(QStringLiteral("knotesrc"));
0078     const QString knotesrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + knotesStr;
0079     if (QFileInfo::exists(knotesrc)) {
0080         backupFile(knotesrc, Utils::configsPath(), knotesStr);
0081     }
0082     const QString globalNoteSettingsStr(QStringLiteral("globalnotesettings"));
0083     const QString globalNoteSettingsrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + globalNoteSettingsStr;
0084 
0085     if (QFileInfo::exists(globalNoteSettingsrc)) {
0086         KSharedConfigPtr globalnotesettingsrc = KSharedConfig::openConfig(globalNoteSettingsrc);
0087 
0088         QTemporaryFile tmp;
0089         tmp.open();
0090 
0091         KConfig *knoteConfig = globalnotesettingsrc->copyTo(tmp.fileName());
0092         const QString selectFolderNoteStr(QStringLiteral("SelectNoteFolder"));
0093         if (knoteConfig->hasGroup(selectFolderNoteStr)) {
0094             KConfigGroup selectFolderNoteGroup = knoteConfig->group(selectFolderNoteStr);
0095 
0096             const QString selectFolderNoteGroupStr(QStringLiteral("DefaultFolder"));
0097             convertCollectionIdsToRealPath(selectFolderNoteGroup, selectFolderNoteGroupStr);
0098         }
0099         knoteConfig->sync();
0100         backupFile(tmp.fileName(), Utils::configsPath(), globalNoteSettingsStr);
0101         delete knoteConfig;
0102     }
0103     backupUiRcFile(QStringLiteral("knotesappui.rc"), QStringLiteral("knotes"));
0104     backupUiRcFile(QStringLiteral("knotesui.rc"), QStringLiteral("knotes"));
0105     backupUiRcFile(QStringLiteral("knotes_part.rc"), QStringLiteral("knotes"));
0106     backupConfigFile(QStringLiteral("akonadi_notes_agent.notifyrc"));
0107     storeDirectory(QStringLiteral("/knotes/print/theme/"));
0108     emitInfo(i18n("Config backup done."));
0109 }
0110 
0111 void ExportNotesJobInterface::slotNoteJobTerminated()
0112 {
0113     if (wasCanceled()) {
0114         Q_EMIT jobFinished();
0115         return;
0116     }
0117     mIndexIdentifier++;
0118     QTimer::singleShot(0, this, &ExportNotesJobInterface::slotWriteNextArchiveResource);
0119 }
0120 
0121 QString ExportNotesJobInterface::applicationName() const
0122 {
0123     return QStringLiteral("[KNotes]");
0124 }
0125 
0126 void ExportNotesJobInterface::slotWriteNextArchiveResource()
0127 {
0128     if (mIndexIdentifier < mAkonadiInstanceInfo.count()) {
0129         const Utils::AkonadiInstanceInfo agent = mAkonadiInstanceInfo.at(mIndexIdentifier);
0130         const QString identifier = agent.identifier;
0131         if (identifier.contains(QLatin1StringView("akonadi_akonotes_resource_"))) {
0132             const QString archivePath = Utils::notePath() + identifier + QLatin1Char('/');
0133 
0134             const QString url = resourcePath(identifier);
0135             if (!mAgentPaths.contains(url) && QDir(url).exists()) {
0136                 if (!url.isEmpty()) {
0137                     mAgentPaths << url;
0138                     exportResourceToArchive(archivePath, url, identifier);
0139                 } else {
0140                     qCDebug(PIMDATAEXPORTERCORE_LOG) << "Url is empty for " << identifier;
0141                     QTimer::singleShot(0, this, &ExportNotesJobInterface::slotNoteJobTerminated);
0142                 }
0143             } else {
0144                 QTimer::singleShot(0, this, &ExportNotesJobInterface::slotNoteJobTerminated);
0145             }
0146         } else {
0147             QTimer::singleShot(0, this, &ExportNotesJobInterface::slotNoteJobTerminated);
0148         }
0149     } else {
0150         emitInfo(i18n("Resources backup done."));
0151         QTimer::singleShot(0, this, &ExportNotesJobInterface::slotCheckBackupConfig);
0152     }
0153 }
0154 
0155 #include "moc_exportnotesjobinterface.cpp"