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

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "exportcalendarjobinterface.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include <KConfigGroup>
0012 #include <QTemporaryFile>
0013 
0014 #include "resourceconverterimpl.h"
0015 #include <QColor>
0016 #include <QFile>
0017 #include <QTimer>
0018 
0019 #include <QFileInfo>
0020 #include <QStandardPaths>
0021 
0022 ExportCalendarJobInterface::ExportCalendarJobInterface(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep)
0023     : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep)
0024 {
0025 }
0026 
0027 ExportCalendarJobInterface::~ExportCalendarJobInterface() = default;
0028 
0029 void ExportCalendarJobInterface::slotCalendarJobTerminated()
0030 {
0031     if (wasCanceled()) {
0032         Q_EMIT jobFinished();
0033         return;
0034     }
0035     mIndexIdentifier++;
0036     QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotWriteNextArchiveResource);
0037 }
0038 
0039 void ExportCalendarJobInterface::slotWriteNextArchiveResource()
0040 {
0041     if (mIndexIdentifier < mAkonadiInstanceInfo.count()) {
0042         const Utils::AkonadiInstanceInfo agent = mAkonadiInstanceInfo.at(mIndexIdentifier);
0043         const QString identifier = agent.identifier;
0044         if (identifier.contains(QLatin1StringView("akonadi_icaldir_resource_"))) {
0045             const QString archivePath = Utils::calendarPath() + identifier + QLatin1Char('/');
0046 
0047             const QString url = resourcePath(identifier);
0048             if (!mAgentPaths.contains(url)) {
0049                 if (!url.isEmpty()) {
0050                     mAgentPaths << url;
0051                     exportResourceToArchive(archivePath, url, identifier);
0052                 } else {
0053                     qCDebug(PIMDATAEXPORTERCORE_LOG) << "Url is empty for " << identifier;
0054                     QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotCalendarJobTerminated);
0055                 }
0056             } else {
0057                 QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotCalendarJobTerminated);
0058             }
0059         } else if (identifier.contains(QLatin1StringView("akonadi_ical_resource_"))) {
0060             backupCalendarResourceFile(identifier, Utils::calendarPath());
0061             QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotCalendarJobTerminated);
0062         } else if (identifier.contains(QLatin1StringView("akonadi_davgroupware_resource_"))) {
0063             backupCalendarResourceFile(identifier, Utils::calendarPath());
0064             QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotCalendarJobTerminated);
0065         } else if (identifier.contains(QLatin1StringView("akonadi_openxchange_resource_"))) {
0066             backupCalendarResourceFile(identifier, Utils::calendarPath());
0067             QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotCalendarJobTerminated);
0068         } else if (identifier.contains(QLatin1StringView("akonadi_google_resource_"))) {
0069             backupCalendarResourceFile(identifier, Utils::calendarPath());
0070             QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotCalendarJobTerminated);
0071         } else {
0072             QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotCalendarJobTerminated);
0073         }
0074     } else {
0075         emitInfo(i18n("Resources backup done."));
0076         QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotCheckBackupConfig);
0077     }
0078 }
0079 
0080 void ExportCalendarJobInterface::start()
0081 {
0082     Q_EMIT title(i18n("Start export KOrganizer settings..."));
0083     createProgressDialog(i18n("Export KOrganizer settings"));
0084     if (mTypeSelected & Utils::Resources) {
0085         QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotCheckBackupResource);
0086     } else if (mTypeSelected & Utils::Config) {
0087         QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotCheckBackupConfig);
0088     } else {
0089         Q_EMIT jobFinished();
0090     }
0091 }
0092 
0093 void ExportCalendarJobInterface::slotCheckBackupResource()
0094 {
0095     mAkonadiInstanceInfo = listOfResource();
0096     setProgressDialogLabel(i18n("Backing up resources..."));
0097     increaseProgressDialog();
0098     QTimer::singleShot(0, this, &ExportCalendarJobInterface::slotWriteNextArchiveResource);
0099 }
0100 
0101 void ExportCalendarJobInterface::slotCheckBackupConfig()
0102 {
0103     if (mTypeSelected & Utils::Config) {
0104         backupConfig();
0105         increaseProgressDialog();
0106         if (wasCanceled()) {
0107             Q_EMIT jobFinished();
0108             return;
0109         }
0110     }
0111     Q_EMIT jobFinished();
0112 }
0113 
0114 QString ExportCalendarJobInterface::applicationName() const
0115 {
0116     return QStringLiteral("[Calendar]");
0117 }
0118 
0119 void ExportCalendarJobInterface::exportEventViewConfig()
0120 {
0121     const QString eventviewsrcStr(QStringLiteral("eventviewsrc"));
0122     const QString eventviewsrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + eventviewsrcStr;
0123     if (QFileInfo::exists(eventviewsrc)) {
0124         KSharedConfigPtr eventviews = KSharedConfig::openConfig(eventviewsrc);
0125 
0126         QTemporaryFile tmp;
0127         tmp.open();
0128 
0129         KConfig *eventviewConfig = eventviews->copyTo(tmp.fileName());
0130         exportResourceColors(eventviewConfig);
0131 
0132         eventviewConfig->sync();
0133         backupFile(tmp.fileName(), Utils::configsPath(), eventviewsrcStr);
0134         delete eventviewConfig;
0135     }
0136 }
0137 
0138 void ExportCalendarJobInterface::exportResourceColors(KConfig *config)
0139 {
0140     const QString resourceColorStr(QStringLiteral("Resources Colors"));
0141     if (config->hasGroup(resourceColorStr)) {
0142         KConfigGroup group = config->group(resourceColorStr);
0143 
0144         const QStringList keyList = group.keyList();
0145         bool found = false;
0146         for (const QString &key : keyList) {
0147             const int collectionValue = key.toInt(&found);
0148             if (found && collectionValue != -1) {
0149                 const QString realPath = convertToFullCollectionPath(collectionValue);
0150                 const QColor color = group.readEntry(key, QColor());
0151                 if (color.isValid()) {
0152                     group.writeEntry(realPath, color);
0153                 }
0154                 group.deleteEntry(key);
0155             }
0156         }
0157     }
0158 }
0159 
0160 void ExportCalendarJobInterface::exportKalendarConfig()
0161 {
0162     const QString kalendarStr(QStringLiteral("kalendarrc"));
0163     const QString kalendarrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + kalendarStr;
0164     if (QFileInfo::exists(kalendarrc)) {
0165         KSharedConfigPtr kalendar = KSharedConfig::openConfig(kalendarrc);
0166 
0167         QTemporaryFile tmp;
0168         tmp.open();
0169 
0170         KConfig *kalendarConfig = kalendar->copyTo(tmp.fileName());
0171 
0172         const QString globalCollectionsStr(QStringLiteral("GlobalCollectionSelection"));
0173         if (kalendarConfig->hasGroup(globalCollectionsStr)) {
0174             KConfigGroup group = kalendarConfig->group(globalCollectionsStr);
0175             const QString selectionKey(QStringLiteral("Selection"));
0176             convertCollectionListToRealPath(group, selectionKey);
0177             // Add Current
0178         }
0179 
0180         exportResourceColors(kalendarConfig);
0181         kalendarConfig->sync();
0182         backupFile(tmp.fileName(), Utils::configsPath(), kalendarStr);
0183         delete kalendarConfig;
0184     }
0185 }
0186 
0187 void ExportCalendarJobInterface::exportKorganizerConfig()
0188 {
0189     const QString korganizerStr(QStringLiteral("korganizerrc"));
0190     const QString korganizerrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + korganizerStr;
0191     if (QFileInfo::exists(korganizerrc)) {
0192         KSharedConfigPtr korganizer = KSharedConfig::openConfig(korganizerrc);
0193 
0194         QTemporaryFile tmp;
0195         tmp.open();
0196 
0197         KConfig *korganizerConfig = korganizer->copyTo(tmp.fileName());
0198 
0199         const QString globalCollectionsStr(QStringLiteral("GlobalCollectionSelection"));
0200         if (korganizerConfig->hasGroup(globalCollectionsStr)) {
0201             KConfigGroup group = korganizerConfig->group(globalCollectionsStr);
0202             const QString selectionKey(QStringLiteral("Selection"));
0203             convertCollectionListToRealPath(group, selectionKey);
0204         }
0205 
0206         const QString collectionTreeViewStr(QStringLiteral("CollectionTreeView"));
0207         if (korganizerConfig->hasGroup(collectionTreeViewStr)) {
0208             KConfigGroup group = korganizerConfig->group(collectionTreeViewStr);
0209             const QString selectionKey(QStringLiteral("Expansion"));
0210             convertCollectionListToRealPath(group, selectionKey);
0211         }
0212 
0213         const QString globalCollectionViewStr(QStringLiteral("GlobalCollectionView"));
0214         if (korganizerConfig->hasGroup(globalCollectionViewStr)) {
0215             KConfigGroup group = korganizerConfig->group(globalCollectionViewStr);
0216             const QString selectionKey(QStringLiteral("Expansion"));
0217             convertCollectionListToRealPath(group, selectionKey);
0218         }
0219 
0220         korganizerConfig->sync();
0221         backupFile(tmp.fileName(), Utils::configsPath(), korganizerStr);
0222         delete korganizerConfig;
0223     }
0224 }
0225 
0226 void ExportCalendarJobInterface::backupConfig()
0227 {
0228     setProgressDialogLabel(i18n("Backing up config..."));
0229 
0230     exportKorganizerConfig();
0231     exportEventViewConfig();
0232     backupConfigFile(QStringLiteral("kalendaracrc"));
0233     exportKalendarConfig();
0234 
0235     backupConfigFile(QStringLiteral("calendar_printing.rc"));
0236 
0237     const QString freebusyurlsStr(QStringLiteral("korganizer/freebusyurls"));
0238     const QString freebusyurls = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + freebusyurlsStr;
0239     if (QFileInfo::exists(freebusyurls)) {
0240         backupFile(freebusyurls, Utils::dataPath(), freebusyurlsStr);
0241     }
0242 
0243     storeDirectory(QStringLiteral("/korganizer/templates/"));
0244     storeDirectory(QStringLiteral("/korganizer/designer/"));
0245 
0246     backupUiRcFile(QStringLiteral("korganizerui.rc"), QStringLiteral("korganizer"));
0247     backupUiRcFile(QStringLiteral("korganizer_part.rc"), QStringLiteral("korganizer"));
0248     emitInfo(i18n("Config backup done."));
0249 }
0250 
0251 #include "moc_exportcalendarjobinterface.cpp"