2 Copyright (c) 2013 Sérgio Martins <iamsergio@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 As a special exception, permission is given to link this program
19 with any edition of Qt, and distribute the resulting executable,
20 without including the source code for Qt in the source distribution.
25 #include <calendarsupport/utils.h>
27 #include <KCalCore/Incidence>
28 #include <KCalCore/FileStorage>
30 #include <Akonadi/CollectionFetchJob>
31 #include <Akonadi/CollectionFetchScope>
32 #include <Akonadi/ItemFetchJob>
33 #include <Akonadi/ItemFetchScope>
38 #include <QCoreApplication>
40 static void print(const QString
&message
)
42 QTextStream
out(stdout
);
43 out
<< message
<< "\n";
46 void Backuper::emitFinished(bool success
, const QString
&message
)
49 print(QLatin1Char('\n') + i18np("Backup was successful. %1 incidence was saved.", "Backup was successful. %1 incidences were saved.", m_calendar
->incidences().count()));
56 emit
finished(success
, message
);
57 qApp
->exit(success
? 0 : -1); // TODO: If we move this class to kdepimlibs, remove this
60 Backuper::Backuper(QObject
*parent
) : QObject(parent
), m_backupInProgress(false)
64 void Backuper::backup(const QString
&filename
, const QList
<Akonadi::Entity::Id
> &collectionIds
)
66 if (filename
.isEmpty()) {
67 emitFinished(false, i18n("File is empty."));
71 if (m_backupInProgress
) {
72 emitFinished(false, i18n("A backup is already in progress."));
75 print(i18n("Backing up your calendar data..."));
76 m_calendar
= KCalCore::MemoryCalendar::Ptr(new KCalCore::MemoryCalendar(KDateTime::LocalZone
));
77 m_requestedCollectionIds
= collectionIds
;
78 m_backupInProgress
= true;
79 m_filename
= filename
;
81 Akonadi::CollectionFetchJob
*job
= new Akonadi::CollectionFetchJob(Akonadi::Collection::root(),
82 Akonadi::CollectionFetchJob::Recursive
);
84 job
->fetchScope().setContentMimeTypes(KCalCore::Incidence::mimeTypes());
85 connect(job
, SIGNAL(result(KJob
*)), SLOT(onCollectionsFetched(KJob
*)));
89 void Backuper::onCollectionsFetched(KJob
*job
)
91 if (job
->error() == 0) {
92 QSet
<QString
> mimeTypeSet
= KCalCore::Incidence::mimeTypes().toSet();
93 Akonadi::CollectionFetchJob
*cfj
= qobject_cast
<Akonadi::CollectionFetchJob
*>(job
);
94 foreach(const Akonadi::Collection
&collection
, cfj
->collections()) {
95 if (!m_requestedCollectionIds
.isEmpty() && !m_requestedCollectionIds
.contains(collection
.id()))
97 if (!mimeTypeSet
.intersect(collection
.contentMimeTypes().toSet()).isEmpty()) {
98 m_collections
<< collection
;
99 loadCollection(collection
);
103 if (m_collections
.isEmpty()) {
104 emitFinished(false, i18n("No data to backup."));
107 kError() << job
->errorString();
108 m_backupInProgress
= false;
109 emitFinished(false, job
->errorString());
113 void Backuper::loadCollection(const Akonadi::Collection
&collection
)
115 print(i18n("Processing collection %1 (id=%2)...", collection
.displayName(), collection
.id()));
116 Akonadi::ItemFetchJob
*ifj
= new Akonadi::ItemFetchJob(collection
, this);
117 ifj
->setProperty("collectionId", collection
.id());
118 ifj
->fetchScope().fetchFullPayload(true);
119 connect(ifj
, SIGNAL(result(KJob
*)), SLOT(onCollectionLoaded(KJob
*)));
120 m_pendingCollections
<< collection
.id();
123 void Backuper::onCollectionLoaded(KJob
*job
)
126 m_backupInProgress
= false;
128 emitFinished(false, job
->errorString());
130 Akonadi::ItemFetchJob
*ifj
= qobject_cast
<Akonadi::ItemFetchJob
*>(job
);
131 Akonadi::Collection::Id id
= ifj
->property("collectionId").toInt();
133 Akonadi::Item::List items
= ifj
->items();
134 m_pendingCollections
.removeAll(id
);
136 foreach (const Akonadi::Item
&item
, items
) {
137 KCalCore::Incidence::Ptr incidence
= CalendarSupport::incidence(item
);
139 m_calendar
->addIncidence(incidence
);
142 if (m_pendingCollections
.isEmpty()) { // We're done
143 KCalCore::FileStorage
storage(m_calendar
, m_filename
);
144 bool success
= storage
.save();
145 QString message
= success
? QString() : i18n("An error occurred");
146 emitFinished(success
, message
);