1 /*
2    SPDX-FileCopyrightText: 2013-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "exportaddressbookjobinterface.h"
8 
9 #include <KLocalizedString>
10 
11 #include <KConfigGroup>
12 #include <QFileInfo>
13 #include <QTemporaryFile>
14 
15 #include <QStandardPaths>
16 #include <QTimer>
17 #include <resourceconverterimpl.h>
18 
ExportAddressbookJobInterface(QObject * parent,Utils::StoredTypes typeSelected,ArchiveStorage * archiveStorage,int numberOfStep)19 ExportAddressbookJobInterface::ExportAddressbookJobInterface(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep)
20     : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep)
21 {
22 }
23 
~ExportAddressbookJobInterface()24 ExportAddressbookJobInterface::~ExportAddressbookJobInterface()
25 {
26 }
27 
start()28 void ExportAddressbookJobInterface::start()
29 {
30     Q_EMIT title(i18n("Start export KAddressBook settings..."));
31     createProgressDialog(i18n("Export KAddressBook settings"));
32     if (mTypeSelected & Utils::Resources) {
33         QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotCheckBackupResource);
34     } else if (mTypeSelected & Utils::Config) {
35         QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotCheckBackupConfig);
36     } else {
37         Q_EMIT jobFinished();
38     }
39 }
40 
slotCheckBackupResource()41 void ExportAddressbookJobInterface::slotCheckBackupResource()
42 {
43     mAkonadiInstanceInfo = listOfResource();
44     setProgressDialogLabel(i18n("Backing up resources..."));
45     increaseProgressDialog();
46     QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotWriteNextArchiveResource);
47 }
48 
slotCheckBackupConfig()49 void ExportAddressbookJobInterface::slotCheckBackupConfig()
50 {
51     if (mTypeSelected & Utils::Config) {
52         backupConfig();
53         increaseProgressDialog();
54         if (wasCanceled()) {
55             Q_EMIT jobFinished();
56             return;
57         }
58     }
59     Q_EMIT jobFinished();
60 }
61 
slotAddressbookJobTerminated()62 void ExportAddressbookJobInterface::slotAddressbookJobTerminated()
63 {
64     if (wasCanceled()) {
65         Q_EMIT jobFinished();
66         return;
67     }
68     mIndexIdentifier++;
69     QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotWriteNextArchiveResource);
70 }
71 
slotWriteNextArchiveResource()72 void ExportAddressbookJobInterface::slotWriteNextArchiveResource()
73 {
74     if (mIndexIdentifier < mAkonadiInstanceInfo.count()) {
75         const Utils::AkonadiInstanceInfo agent = mAkonadiInstanceInfo.at(mIndexIdentifier);
76         const QString identifier = agent.identifier;
77         if (identifier.contains(QLatin1String("akonadi_vcarddir_resource_")) || identifier.contains(QLatin1String("akonadi_contacts_resource_"))) {
78             const QString archivePath = Utils::addressbookPath() + identifier + QLatin1Char('/');
79 
80             const QString url = resourcePath(identifier, QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/contacts/"));
81             if (!mAgentPaths.contains(url)) {
82                 if (!url.isEmpty()) {
83                     mAgentPaths << url;
84                     exportResourceToArchive(archivePath, url, identifier);
85                 } else {
86                     qCDebug(PIMDATAEXPORTERCORE_LOG) << "Url is empty for " << identifier;
87                     QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotAddressbookJobTerminated);
88                 }
89             } else {
90                 QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotAddressbookJobTerminated);
91             }
92         } else if (identifier.contains(QLatin1String("akonadi_vcard_resource_"))) {
93             backupAddressBookResourceFile(identifier, Utils::addressbookPath());
94             QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotAddressbookJobTerminated);
95         } else {
96             QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotAddressbookJobTerminated);
97         }
98     } else {
99         Q_EMIT info(i18n("Resources backup done."));
100         QTimer::singleShot(0, this, &ExportAddressbookJobInterface::slotCheckBackupConfig);
101     }
102 }
103 
backupConfig()104 void ExportAddressbookJobInterface::backupConfig()
105 {
106     setProgressDialogLabel(i18n("Backing up config..."));
107 
108     const QString kaddressbookStr(QStringLiteral("kaddressbookrc"));
109     const QString kaddressbookrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + kaddressbookStr;
110     if (QFileInfo::exists(kaddressbookrc)) {
111         KSharedConfigPtr kaddressbook = KSharedConfig::openConfig(kaddressbookrc);
112 
113         QTemporaryFile tmp;
114         tmp.open();
115 
116         KConfig *kaddressBookConfig = kaddressbook->copyTo(tmp.fileName());
117 
118         const QString collectionViewCheckStateStr(QStringLiteral("CollectionViewCheckState"));
119         if (kaddressBookConfig->hasGroup(collectionViewCheckStateStr)) {
120             KConfigGroup group = kaddressBookConfig->group(collectionViewCheckStateStr);
121             const QString selectionKey(QStringLiteral("Selection"));
122             convertCollectionListToRealPath(group, selectionKey);
123         }
124 
125         const QString collectionViewStateStr(QStringLiteral("CollectionViewState"));
126         if (kaddressBookConfig->hasGroup(collectionViewStateStr)) {
127             KConfigGroup group = kaddressBookConfig->group(collectionViewStateStr);
128             QString currentKey(QStringLiteral("Current"));
129             convertCollectionToRealPath(group, currentKey);
130 
131             currentKey = QStringLiteral("Expansion");
132             convertCollectionListToRealPath(group, currentKey);
133 
134             currentKey = QStringLiteral("Selection");
135             convertCollectionToRealPath(group, currentKey);
136         }
137         kaddressBookConfig->sync();
138         backupFile(tmp.fileName(), Utils::configsPath(), kaddressbookStr);
139         delete kaddressBookConfig;
140     }
141     backupUiRcFile(QStringLiteral("kaddressbookui.rc"), QStringLiteral("kaddressbook"));
142 
143     storeDirectory(QStringLiteral("/kaddressbook/csv-templates/"));
144     storeDirectory(QStringLiteral("/kaddressbook/viewertemplates/"));
145     storeDirectory(QStringLiteral("/kaddressbook/printing/"));
146 
147     Q_EMIT info(i18n("Config backup done."));
148 }
149