importaddressbookjobinterface.cpp 7.26 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2013-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
Laurent Montel's avatar
Laurent Montel committed
18
19
*/

Laurent Montel's avatar
Laurent Montel committed
20
#include "importaddressbookjobinterface.h"
Laurent Montel's avatar
Laurent Montel committed
21
22
#include "archivestorage.h"

23
#include <KLocalizedString>
24
#include <KConfigGroup>
25
#include <KZip>
Laurent Montel's avatar
Laurent Montel committed
26
#include <KArchiveEntry>
27

Laurent Montel's avatar
Laurent Montel committed
28
#include <QTimer>
29
#include <QFile>
30
#include <QDir>
Laurent Montel's avatar
Laurent Montel committed
31
#include <QStandardPaths>
32

Laurent Montel's avatar
Laurent Montel committed
33
ImportAddressbookJobInterface::ImportAddressbookJobInterface(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep)
Laurent Montel's avatar
Laurent Montel committed
34
    : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep)
Laurent Montel's avatar
Laurent Montel committed
35
{
Laurent Montel's avatar
Laurent Montel committed
36
    initializeImportJob();
Laurent Montel's avatar
Laurent Montel committed
37
38
}

Laurent Montel's avatar
Laurent Montel committed
39
ImportAddressbookJobInterface::~ImportAddressbookJobInterface()
Laurent Montel's avatar
Laurent Montel committed
40
41
42
{
}

Laurent Montel's avatar
Laurent Montel committed
43
void ImportAddressbookJobInterface::start()
Laurent Montel's avatar
Laurent Montel committed
44
{
45
    Q_EMIT title(i18n("Starting to import KAddressBook settings..."));
Laurent Montel's avatar
Laurent Montel committed
46
    mArchiveDirectory = archive()->directory();
Laurent Montel's avatar
Laurent Montel committed
47
    createProgressDialog(i18n("Import KAddressBook settings"));
Laurent Montel's avatar
Laurent Montel committed
48
    searchAllFiles(mArchiveDirectory, QString(), QStringLiteral("addressbook"));
Laurent Montel's avatar
Laurent Montel committed
49
    initializeListStep();
Laurent Montel's avatar
Laurent Montel committed
50
    QTimer::singleShot(0, this, &ImportAddressbookJobInterface::slotNextStep);
Laurent Montel's avatar
Laurent Montel committed
51
52
}

Laurent Montel's avatar
Laurent Montel committed
53
void ImportAddressbookJobInterface::slotNextStep()
Laurent Montel's avatar
Laurent Montel committed
54
55
56
{
    ++mIndex;
    if (mIndex < mListStep.count()) {
Laurent Montel's avatar
Laurent Montel committed
57
        const Utils::StoredType type = mListStep.at(mIndex);
Laurent Montel's avatar
Laurent Montel committed
58
        if (type == Utils::Resources) {
Laurent Montel's avatar
Laurent Montel committed
59
            restoreResources();
Laurent Montel's avatar
Laurent Montel committed
60
        } else if (type == Utils::Config) {
Laurent Montel's avatar
Laurent Montel committed
61
            restoreConfig();
Laurent Montel's avatar
Laurent Montel committed
62
        }
Laurent Montel's avatar
Laurent Montel committed
63
64
65
    } else {
        Q_EMIT jobFinished();
    }
Laurent Montel's avatar
Laurent Montel committed
66
67
}

Laurent Montel's avatar
Laurent Montel committed
68
void ImportAddressbookJobInterface::addSpecificResourceSettings(const KSharedConfig::Ptr &resourceConfig, const QString &resourceName, QMap<QString, QVariant> &settings)
Laurent Montel's avatar
Laurent Montel committed
69
70
{
    if (resourceName == QLatin1String("akonadi_vcard_resource")) {
71
72
73
        KConfigGroup general = resourceConfig->group(QStringLiteral("General"));
        if (general.hasKey(QStringLiteral("DisplayName"))) {
            settings.insert(QStringLiteral("DisplayName"), general.readEntry(QStringLiteral("DisplayName")));
Laurent Montel's avatar
Laurent Montel committed
74
        }
75
76
        if (general.hasKey(QStringLiteral("ReadOnly"))) {
            settings.insert(QStringLiteral("ReadOnly"), general.readEntry(QStringLiteral("ReadOnly"), false));
Laurent Montel's avatar
Laurent Montel committed
77
        }
78
79
        if (general.hasKey(QStringLiteral("MonitorFile"))) {
            settings.insert(QStringLiteral("MonitorFile"), general.readEntry(QStringLiteral("MonitorFile"), true));
Laurent Montel's avatar
Laurent Montel committed
80
81
82
83
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
84
bool ImportAddressbookJobInterface::isAConfigFile(const QString &name) const
Laurent Montel's avatar
Laurent Montel committed
85
{
Laurent Montel's avatar
Laurent Montel committed
86
87
88
    return name.endsWith(QLatin1String("rc")) && (name.startsWith(QLatin1String("akonadi_vcarddir_resource_"))
                                                  || name.startsWith(QLatin1String("akonadi_vcard_resource_"))
                                                  || name.startsWith(QLatin1String("akonadi_contacts_resource_")));
Laurent Montel's avatar
Laurent Montel committed
89
90
}

Laurent Montel's avatar
Laurent Montel committed
91
void ImportAddressbookJobInterface::restoreConfig()
Laurent Montel's avatar
Laurent Montel committed
92
{
Laurent Montel's avatar
Laurent Montel committed
93
    increaseProgressDialog();
Laurent Montel's avatar
Laurent Montel committed
94
    setProgressDialogLabel(i18n("Restore configs..."));
95
    const QString kaddressbookStr(QStringLiteral("kaddressbookrc"));
Laurent Montel's avatar
Laurent Montel committed
96
    const KArchiveEntry *kaddressbookrcentry = mArchiveDirectory->entry(Utils::configsPath() + kaddressbookStr);
Laurent Montel's avatar
Laurent Montel committed
97
    if (kaddressbookrcentry && kaddressbookrcentry->isFile()) {
Laurent Montel's avatar
Laurent Montel committed
98
        const KArchiveFile *kaddressbookrcFile = static_cast<const KArchiveFile *>(kaddressbookrcentry);
Laurent Montel's avatar
Laurent Montel committed
99
        const QString kaddressbookrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + kaddressbookStr;
100
        if (QFileInfo::exists(kaddressbookrc)) {
Laurent Montel's avatar
Laurent Montel committed
101
            if (overwriteConfigMessageBox(kaddressbookStr)) {
Laurent Montel's avatar
Laurent Montel committed
102
                importkaddressBookConfig(kaddressbookrcFile, kaddressbookrc, kaddressbookStr, Utils::configsPath());
Laurent Montel's avatar
Laurent Montel committed
103
104
            }
        } else {
Laurent Montel's avatar
Laurent Montel committed
105
            importkaddressBookConfig(kaddressbookrcFile, kaddressbookrc, kaddressbookStr, Utils::configsPath());
Laurent Montel's avatar
Laurent Montel committed
106
107
        }
    }
108
    restoreUiRcFile(QStringLiteral("kaddressbookui.rc"), QStringLiteral("kaddressbook"));
Laurent Montel's avatar
Laurent Montel committed
109
    Q_EMIT info(i18n("Config restored."));
Laurent Montel's avatar
Laurent Montel committed
110
    QTimer::singleShot(0, this, &ImportAddressbookJobInterface::slotNextStep);
Laurent Montel's avatar
Laurent Montel committed
111
112
}

Laurent Montel's avatar
Laurent Montel committed
113
void ImportAddressbookJobInterface::importkaddressBookConfig(const KArchiveFile *file, const QString &config, const QString &filename, const QString &prefix)
Laurent Montel's avatar
Laurent Montel committed
114
115
{
    copyToFile(file, config, filename, prefix);
116
    KSharedConfig::Ptr kaddressBookConfig = KSharedConfig::openConfig(config);
117

Laurent Montel's avatar
Laurent Montel committed
118
119
    convertCollectionListStrToAkonadiId(kaddressBookConfig, QStringLiteral("CollectionViewCheckState"), QStringLiteral("Selection"), true);

120
    const QString collectionViewStateStr(QStringLiteral("CollectionViewState"));
121
122
    if (kaddressBookConfig->hasGroup(collectionViewStateStr)) {
        KConfigGroup group = kaddressBookConfig->group(collectionViewStateStr);
123
        QString currentKey(QStringLiteral("Current"));
124
125
        convertRealPathToCollection(group, currentKey, true);

126
        currentKey = QStringLiteral("Expansion");
Laurent Montel's avatar
Laurent Montel committed
127
        convertRealPathToCollectionList(group, currentKey, true);
128

129
        currentKey = QStringLiteral("Selection");
130
131
        convertRealPathToCollection(group, currentKey, true);
    }
132
    const QString cvsTemplateDirName = QStringLiteral("/kaddressbook/csv-templates/");
Laurent Montel's avatar
Laurent Montel committed
133
    const KArchiveEntry *csvtemplateEntry = mArchiveDirectory->entry(Utils::dataPath() + cvsTemplateDirName);
134
135
    if (csvtemplateEntry && csvtemplateEntry->isDirectory()) {
        const KArchiveDirectory *csvTemplateDir = static_cast<const KArchiveDirectory *>(csvtemplateEntry);
Laurent Montel's avatar
Laurent Montel committed
136
137
        const QStringList lst = csvTemplateDir->entries();
        for (const QString &entryName : lst) {
138
139
140
141
142
            const KArchiveEntry *entry = csvTemplateDir->entry(entryName);
            if (entry && entry->isFile()) {
                const KArchiveFile *csvTemplateFile = static_cast<const KArchiveFile *>(entry);
                const QString name = csvTemplateFile->name();
                QString autocorrectionPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + cvsTemplateDirName;
143
                if (QFileInfo::exists(autocorrectionPath)) {
144
145
146
147
148
149
150
151
152
153
154
                    if (overwriteConfigMessageBox(name)) {
                        copyToFile(csvTemplateFile, autocorrectionPath + QLatin1Char('/') + name, name, Utils::dataPath() + cvsTemplateDirName);
                    }
                } else {
                    autocorrectionPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + cvsTemplateDirName;
                    copyToFile(csvTemplateFile, autocorrectionPath + QLatin1Char('/') + name, name, Utils::dataPath() + cvsTemplateDirName);
                }
            }
        }
    }

Laurent Montel's avatar
Laurent Montel committed
155
156
    importDataSubdirectory(QStringLiteral("/kaddressbook/viewertemplates/"));
    importDataSubdirectory(QStringLiteral("/kaddressbook/printing/"));
157
158
    kaddressBookConfig->sync();
}