exportcalendarjob.cpp 5.75 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  Copyright (c) 2012-2013 Montel Laurent <montel@kde.org>
3

4 5 6
  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License, version 2, as
  published by the Free Software Foundation.
7

8 9 10 11
  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.
12

13 14 15 16 17 18
  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "exportcalendarjob.h"
19

Laurent Montel's avatar
Laurent Montel committed
20
#include "messageviewer/utils/kcursorsaver.h"
21

Laurent Montel's avatar
Laurent Montel committed
22
#include <Akonadi/AgentManager>
23

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

30
#include <QFile>
Laurent Montel's avatar
Laurent Montel committed
31
#include <QDir>
Laurent Montel's avatar
Laurent Montel committed
32
#include <QWidget>
33

34

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

ExportCalendarJob::~ExportCalendarJob()
{

}
Laurent Montel's avatar
Minor  
Laurent Montel committed
44

Laurent Montel's avatar
Minor  
Laurent Montel committed
45
void ExportCalendarJob::start()
Laurent Montel's avatar
Minor  
Laurent Montel committed
46
{
47
    Q_EMIT title(i18n("Start export KOrganizer settings..."));
Laurent Montel's avatar
Laurent Montel committed
48
    mArchiveDirectory = archive()->directory();
49 50
    createProgressDialog();

Laurent Montel's avatar
Laurent Montel committed
51
    if (mTypeSelected & Utils::Resources) {
52 53 54
        backupResources();
        increaseProgressDialog();
        if (wasCanceled()) {
55
            Q_EMIT jobFinished();
56 57 58
            return;
        }
    }
Laurent Montel's avatar
Laurent Montel committed
59
    if (mTypeSelected & Utils::Config) {
60 61 62
        backupConfig();
        increaseProgressDialog();
        if (wasCanceled()) {
63
            Q_EMIT jobFinished();
64 65 66
            return;
        }
    }
67
    Q_EMIT jobFinished();
Laurent Montel's avatar
Minor  
Laurent Montel committed
68
}
69 70 71 72


void ExportCalendarJob::backupResources()
{
Laurent Montel's avatar
Laurent Montel committed
73 74
    showInfo(i18n("Backing up resources..."));
    MessageViewer::KCursorSaver busy( MessageViewer::KBusyPtr::busy() );
Laurent Montel's avatar
Laurent Montel committed
75 76 77 78

    Akonadi::AgentManager *manager = Akonadi::AgentManager::self();
    const Akonadi::AgentInstance::List list = manager->instances();
    foreach( const Akonadi::AgentInstance &agent, list ) {
Laurent Montel's avatar
Laurent Montel committed
79
        const QString identifier = agent.identifier();
Laurent Montel's avatar
Laurent Montel committed
80 81 82 83
        if (identifier.contains(QLatin1String("akonadi_icaldir_resource_"))) {
            const QString archivePath = Utils::calendarPath() + identifier + QDir::separator();

            KUrl url = Utils::resourcePath(agent);
84 85 86 87 88 89 90 91 92 93 94 95 96
            if (!mAgentPaths.contains(url.path())) {
                if (!url.isEmpty()) {
                    mAgentPaths << url.path();
                    const bool fileAdded = backupFullDirectory(url, archivePath, QLatin1String("calendar.zip"));
                    if (fileAdded) {
                        const QString errorStr = Utils::storeResources(archive(), identifier, archivePath);
                        if (!errorStr.isEmpty())
                            Q_EMIT error(errorStr);
                        url = Utils::akonadiAgentConfigPath(identifier);
                        if (!url.isEmpty()) {
                            const QString filename = url.fileName();
                            const bool fileAdded  = archive()->addLocalFile(url.path(), archivePath + filename);
                            if (fileAdded)
David Jarvie's avatar
David Jarvie committed
97
                                Q_EMIT info(i18n("\"%1\" was backed up.",filename));
98 99 100
                            else
                                Q_EMIT error(i18n("\"%1\" file cannot be added to backup file.",filename));
                        }
Laurent Montel's avatar
Laurent Montel committed
101 102 103 104
                    }
                }
            }
        } else if (identifier.contains(QLatin1String("akonadi_ical_resource_"))) {
Laurent Montel's avatar
Laurent Montel committed
105
            backupResourceFile(agent, Utils::calendarPath());
Laurent Montel's avatar
Laurent Montel committed
106 107 108
        }
    }

Laurent Montel's avatar
Laurent Montel committed
109
    Q_EMIT info(i18n("Resources backup done."));
110
}
Laurent Montel's avatar
Laurent Montel committed
111

Laurent Montel's avatar
Laurent Montel committed
112 113
void ExportCalendarJob::backupConfig()
{
Laurent Montel's avatar
Laurent Montel committed
114 115
    showInfo(i18n("Backing up config..."));
    MessageViewer::KCursorSaver busy( MessageViewer::KBusyPtr::busy() );
116 117 118 119 120 121 122 123 124 125 126

    const QString korganizerStr(QLatin1String("korganizerrc"));
    const QString korganizerrc = KStandardDirs::locateLocal( "config", korganizerStr);
    if (QFile(korganizerrc).exists()) {
        KSharedConfigPtr korganizer = KSharedConfig::openConfig(korganizerrc);

        KTemporaryFile tmp;
        tmp.open();

        KConfig *korganizerConfig = korganizer->copyTo( tmp.fileName() );

Laurent Montel's avatar
Laurent Montel committed
127 128 129 130 131 132 133 134

        const QString globalCollectionsStr(QLatin1String("GlobalCollectionSelection"));
        if (korganizerConfig->hasGroup(globalCollectionsStr)) {
            KConfigGroup group = korganizerConfig->group(globalCollectionsStr);
            const QString selectionKey(QLatin1String("Selection"));
            Utils::convertCollectionListToRealPath(group, selectionKey);
        }

135
        korganizerConfig->sync();
Laurent Montel's avatar
Laurent Montel committed
136
        backupFile(tmp.fileName(), Utils::configsPath(), korganizerStr);
Laurent Montel's avatar
Laurent Montel committed
137
        delete korganizerConfig;
138 139
    }

140
    backupConfigFile(QLatin1String("calendar_printing.rc"));
141
    backupConfigFile(QLatin1String("korgacrc"));
142

Laurent Montel's avatar
Laurent Montel committed
143 144 145 146 147 148
    const QString freebusyurlsStr(QLatin1String("korganizer/freebusyurls"));
    const QString freebusyurls = KStandardDirs::locateLocal( "data", freebusyurlsStr );
    if (QFile(freebusyurls).exists()) {
        backupFile(freebusyurls, Utils::dataPath(), freebusyurlsStr);
    }

Laurent Montel's avatar
Laurent Montel committed
149 150 151 152
    const QString templateDir = KStandardDirs::locateLocal( "data", QLatin1String( "korganizer/templates/" ) );
    QDir templateDirectory( templateDir );
    if (templateDirectory.exists()) {
        const bool templateDirAdded = archive()->addLocalDirectory(templateDir, Utils::dataPath() +  QLatin1String( "/korganizer/templates/" ));
Laurent Montel's avatar
Laurent Montel committed
153
        if (!templateDirAdded) {
Laurent Montel's avatar
Laurent Montel committed
154
            Q_EMIT error(i18n("\"%1\" directory cannot be added to backup file.", templateDir));
Laurent Montel's avatar
Laurent Montel committed
155
        }
Laurent Montel's avatar
Laurent Montel committed
156 157
    }

Laurent Montel's avatar
Laurent Montel committed
158

Laurent Montel's avatar
Laurent Montel committed
159
    Q_EMIT info(i18n("Config backup done."));
Laurent Montel's avatar
Laurent Montel committed
160 161
}