exportnotesjob.cpp 6.87 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2013-2018 Montel Laurent <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.
18 19 20 21
*/

#include "exportnotesjob.h"

Laurent Montel's avatar
Port++  
Laurent Montel committed
22
#include <AkonadiCore/AgentManager>
23

24
#include <KLocalizedString>
25
#include <KZip>
Laurent Montel's avatar
Laurent Montel committed
26
#include <QTemporaryFile>
Laurent Montel's avatar
Laurent Montel committed
27
#include <KConfigGroup>
28 29

#include <QDir>
Laurent Montel's avatar
Laurent Montel committed
30
#include <QTimer>
Laurent Montel's avatar
Laurent Montel committed
31
#include <QStandardPaths>
Laurent Montel's avatar
Laurent Montel committed
32
#include <exportresourcearchivejob.h>
33

Laurent Montel's avatar
Laurent Montel committed
34
ExportNotesJob::ExportNotesJob(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep)
Laurent Montel's avatar
Laurent Montel committed
35
    : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep)
36 37 38 39 40 41 42 43 44
{
}

ExportNotesJob::~ExportNotesJob()
{
}

void ExportNotesJob::start()
{
45
    Q_EMIT title(i18n("Start export KNotes settings..."));
Laurent Montel's avatar
Laurent Montel committed
46
    createProgressDialog(i18n("Export KNotes settings"));
Laurent Montel's avatar
Minor  
Laurent Montel committed
47
    if (mTypeSelected & Utils::Data) {
Laurent Montel's avatar
Laurent Montel committed
48
        QTimer::singleShot(0, this, &ExportNotesJob::slotCheckBackupResource);
Laurent Montel's avatar
Laurent Montel committed
49
    } else if (mTypeSelected & Utils::Config) {
Laurent Montel's avatar
Laurent Montel committed
50
        QTimer::singleShot(0, this, &ExportNotesJob::slotCheckBackupConfig);
Laurent Montel's avatar
Laurent Montel committed
51 52 53 54 55 56 57 58 59 60
    } else {
        Q_EMIT jobFinished();
    }
}

void ExportNotesJob::backupTheme()
{
    const QString notesThemeDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/knotes/print/");
    QDir notesThemeDirectory(notesThemeDir);
    if (notesThemeDirectory.exists()) {
Laurent Montel's avatar
Laurent Montel committed
61
        const bool notesDirAdded = archive()->addLocalDirectory(notesThemeDir, Utils::dataPath() +  QLatin1String("knotes/print"));
Laurent Montel's avatar
Laurent Montel committed
62 63
        if (!notesDirAdded) {
            Q_EMIT error(i18n("\"%1\" directory cannot be added to backup file.", notesThemeDir));
Laurent Montel's avatar
Minor  
Laurent Montel committed
64 65
        }
    }
Laurent Montel's avatar
Laurent Montel committed
66 67 68 69
}

void ExportNotesJob::slotCheckBackupResource()
{
Laurent Montel's avatar
Laurent Montel committed
70
    setProgressDialogLabel(i18n("Backing up resources..."));
Laurent Montel's avatar
Laurent Montel committed
71 72 73
    increaseProgressDialog();
    backupTheme();

Laurent Montel's avatar
Laurent Montel committed
74
    QTimer::singleShot(0, this, &ExportNotesJob::slotWriteNextArchiveResource);
Laurent Montel's avatar
Laurent Montel committed
75 76 77 78
}

void ExportNotesJob::slotCheckBackupConfig()
{
79 80 81 82 83 84 85
    if (mTypeSelected & Utils::Config) {
        backupConfig();
        increaseProgressDialog();
        if (wasCanceled()) {
            Q_EMIT jobFinished();
            return;
        }
Laurent Montel's avatar
Laurent Montel committed
86
    }
87
    Q_EMIT jobFinished();
88 89
}

Laurent Montel's avatar
Laurent Montel committed
90
void ExportNotesJob::slotNoteJobTerminated()
Laurent Montel's avatar
Laurent Montel committed
91 92 93 94 95 96
{
    if (wasCanceled()) {
        Q_EMIT jobFinished();
        return;
    }
    mIndexIdentifier++;
Laurent Montel's avatar
Laurent Montel committed
97
    QTimer::singleShot(0, this, &ExportNotesJob::slotWriteNextArchiveResource);
Laurent Montel's avatar
Laurent Montel committed
98 99 100 101 102 103 104 105 106
}

void ExportNotesJob::slotWriteNextArchiveResource()
{
    Akonadi::AgentManager *manager = Akonadi::AgentManager::self();
    const Akonadi::AgentInstance::List list = manager->instances();
    if (mIndexIdentifier < list.count()) {
        Akonadi::AgentInstance agent = list.at(mIndexIdentifier);
        const QString identifier = agent.identifier();
Laurent Montel's avatar
Laurent Montel committed
107
        if (identifier.contains(QLatin1String("akonadi_akonotes_resource_"))) {
Laurent Montel's avatar
Laurent Montel committed
108 109 110
            const QString archivePath = Utils::notePath() + identifier + QDir::separator();

            QString url = Utils::resourcePath(agent);
111
            if (!mAgentPaths.contains(url) && QDir(url).exists()) {
Laurent Montel's avatar
Laurent Montel committed
112 113 114 115 116 117 118 119 120 121
                mAgentPaths << url;
                if (!url.isEmpty()) {
                    ExportResourceArchiveJob *resourceJob = new ExportResourceArchiveJob(this);
                    resourceJob->setArchivePath(archivePath);
                    resourceJob->setUrl(url);
                    resourceJob->setIdentifier(identifier);
                    resourceJob->setArchive(archive());
                    resourceJob->setArchiveName(QStringLiteral("notes.zip"));
                    connect(resourceJob, &ExportResourceArchiveJob::error, this, &ExportNotesJob::error);
                    connect(resourceJob, &ExportResourceArchiveJob::info, this, &ExportNotesJob::info);
Laurent Montel's avatar
Laurent Montel committed
122
                    connect(resourceJob, &ExportResourceArchiveJob::terminated, this, &ExportNotesJob::slotNoteJobTerminated);
Laurent Montel's avatar
Laurent Montel committed
123
                    resourceJob->start();
124
                } else {
125
                    qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "Url is empty for " << identifier;
Laurent Montel's avatar
Laurent Montel committed
126
                    QTimer::singleShot(0, this, &ExportNotesJob::slotNoteJobTerminated);
Laurent Montel's avatar
Laurent Montel committed
127 128
                }
            } else {
Laurent Montel's avatar
Laurent Montel committed
129
                QTimer::singleShot(0, this, &ExportNotesJob::slotNoteJobTerminated);
Laurent Montel's avatar
Laurent Montel committed
130 131
            }
        } else {
Laurent Montel's avatar
Laurent Montel committed
132
            QTimer::singleShot(0, this, &ExportNotesJob::slotNoteJobTerminated);
Laurent Montel's avatar
Laurent Montel committed
133 134 135
        }
    } else {
        Q_EMIT info(i18n("Resources backup done."));
Laurent Montel's avatar
Laurent Montel committed
136
        QTimer::singleShot(0, this, &ExportNotesJob::slotCheckBackupConfig);
Laurent Montel's avatar
Laurent Montel committed
137 138 139
    }
}

140 141
void ExportNotesJob::backupConfig()
{
Laurent Montel's avatar
Laurent Montel committed
142
    setProgressDialogLabel(i18n("Backing up config..."));
Laurent Montel's avatar
Laurent Montel committed
143

144
    const QString knotesStr(QStringLiteral("knotesrc"));
Laurent Montel's avatar
Laurent Montel committed
145
    const QString knotesrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + knotesStr;
146 147 148
    if (QFileInfo::exists(knotesrc)) {
        backupFile(knotesrc, Utils::configsPath(), knotesStr);
    }
149
    const QString globalNoteSettingsStr(QStringLiteral("globalnotesettings"));
Laurent Montel's avatar
Laurent Montel committed
150
    const QString globalNoteSettingsrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + globalNoteSettingsStr;
151

152
    if (QFileInfo::exists(globalNoteSettingsrc)) {
Laurent Montel's avatar
Laurent Montel committed
153 154
        KSharedConfigPtr globalnotesettingsrc = KSharedConfig::openConfig(globalNoteSettingsrc);

Laurent Montel's avatar
Laurent Montel committed
155
        QTemporaryFile tmp;
Laurent Montel's avatar
Laurent Montel committed
156 157
        tmp.open();

Laurent Montel's avatar
Laurent Montel committed
158
        KConfig *knoteConfig = globalnotesettingsrc->copyTo(tmp.fileName());
159
        const QString selectFolderNoteStr(QStringLiteral("SelectNoteFolder"));
Laurent Montel's avatar
Laurent Montel committed
160 161 162
        if (knoteConfig->hasGroup(selectFolderNoteStr)) {
            KConfigGroup selectFolderNoteGroup = knoteConfig->group(selectFolderNoteStr);

163
            const QString selectFolderNoteGroupStr(QStringLiteral("DefaultFolder"));
Laurent Montel's avatar
Laurent Montel committed
164 165 166 167 168 169
            Utils::convertCollectionIdsToRealPath(selectFolderNoteGroup, selectFolderNoteGroupStr);
        }
        knoteConfig->sync();
        backupFile(tmp.fileName(), Utils::configsPath(), globalNoteSettingsStr);
        delete knoteConfig;
    }
Laurent Montel's avatar
Laurent Montel committed
170 171 172
    backupUiRcFile(QStringLiteral("knotesappui.rc"), QStringLiteral("knotes"));
    backupUiRcFile(QStringLiteral("knotesui.rc"), QStringLiteral("knotes"));
    backupUiRcFile(QStringLiteral("knotes_part.rc"), QStringLiteral("knotes"));
Laurent Montel's avatar
Laurent Montel committed
173
    backupConfigFile(QStringLiteral("akonadi_notes_agent.notifyrc"));
Laurent Montel's avatar
Laurent Montel committed
174
    storeDirectory(QStringLiteral("/knotes/print/theme/"));
175 176
    Q_EMIT info(i18n("Config backup done."));
}