22 #include "icalimporter.h"
23 #include "icalimporter_p.h"
26 #include <akonadi/agentmanager.h>
27 #include <akonadi/agentinstancecreatejob.h>
29 #include <kcalcore/filestorage.h>
30 #include <kcalcore/memorycalendar.h>
31 #include <kcalcore/event.h>
32 #include <kcalcore/todo.h>
33 #include <kcalcore/journal.h>
40 #include <KIO/JobClasses>
41 #include <KIO/Scheduler>
42 #include <KTemporaryFile>
46 #include <QMetaObject>
47 #include <QDBusInterface>
49 using namespace KCalCore;
50 using namespace Akonadi;
52 ICalImporter::Private::Private(IncidenceChanger *changer,
53 ICalImporter *qq) : QObject(), q(qq)
60 m_changer =
new IncidenceChanger(q);
62 connect(m_changer, SIGNAL(createFinished(
int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,QString)),
63 SLOT(onIncidenceCreated(
int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,QString)));
67 ICalImporter::Private::~Private()
69 delete m_temporaryFile;
72 void ICalImporter::Private::onIncidenceCreated(
int changeId,
73 const Akonadi::Item &,
74 Akonadi::IncidenceChanger::ResultCode resultCode,
75 const QString &errorString)
77 if (!m_pendingRequests.contains(changeId))
80 m_pendingRequests.removeAll(changeId);
82 if (resultCode != IncidenceChanger::ResultCodeSuccess) {
84 setErrorMessage(errorString);
85 m_pendingRequests.clear();
86 emit q->importIntoExistingFinished(
false, m_numIncidences);
87 }
else if (m_pendingRequests.isEmpty()) {
89 emit q->importIntoExistingFinished(
true, m_numIncidences);
93 void ICalImporter::Private::setErrorMessage(
const QString &message)
95 m_lastErrorMessage = message;
99 void ICalImporter::Private::resourceCreated(KJob *job)
104 if (createjob->error()) {
105 setErrorMessage(i18n(
"Error creating ical resource: %1", createjob->errorString()));
106 emit q->importIntoNewFinished(
false);
111 QDBusInterface iface(QString::fromLatin1(
"org.freedesktop.Akonadi.Resource.%1").arg(instance.
identifier()), QLatin1String(
"/Settings"));
113 if (!iface.isValid()) {
114 setErrorMessage(i18n(
"Failed to obtain D-Bus interface for remote configuration."));
115 emit q->importIntoNewFinished(
false);
119 const QString path = createjob->property(
"path").toString();
120 Q_ASSERT(!path.isEmpty());
122 iface.call(QLatin1String(
"setPath"), path);
125 emit q->importIntoNewFinished(
true);
128 void ICalImporter::Private::remoteDownloadFinished(KIO::Job *job,
const QByteArray &data)
130 const bool success = job->error() == 0;
133 delete m_temporaryFile;
134 m_temporaryFile =
new KTemporaryFile();
135 m_temporaryFile->write(data.constData(), data.count());
136 q->importIntoExistingResource(QUrl(m_temporaryFile->fileName()), m_collection);
138 setErrorMessage(i18n(
"Could not download remote file."));
139 emit q->importIntoExistingFinished(
false, 0);
143 ICalImporter::ICalImporter(Akonadi::IncidenceChanger *changer,
144 QObject *parent) : QObject(parent)
145 , d(new Private(changer, this))
149 QString ICalImporter::errorMessage()
const
151 return d->m_lastErrorMessage;
154 bool ICalImporter::importIntoNewResource(
const QString &filename)
156 d->m_lastErrorMessage.clear();
159 d->setErrorMessage(i18n(
"An import task is already in progress."));
167 job->setProperty(
"path", filename);
168 connect(job, SIGNAL(result(KJob*)), d, SLOT(resourceCreated(KJob*)));
174 bool ICalImporter::importIntoExistingResource(
const QUrl &url,
Akonadi::Collection collection)
176 d->m_lastErrorMessage.clear();
179 d->setErrorMessage(i18n(
"An import task is already in progress."));
184 d->setErrorMessage(i18n(
"Empty filename. Will not import ical file."));
188 if (!url.isValid()) {
189 d->setErrorMessage(i18n(
"Url to import is malformed."));
193 if (url.isLocalFile()) {
194 if (!QFile::exists(url.path())) {
195 d->setErrorMessage(i18n(
"The specified file doesn't exist, aborting import."));
198 MemoryCalendar::Ptr temporaryCalendar(
new MemoryCalendar(KDateTime::LocalZone));
199 FileStorage storage(temporaryCalendar);
200 storage.setFileName(url.path());
201 bool success = storage.load();
203 d->setErrorMessage(i18n(
"Failed to load ical file, check permissions."));
207 d->m_pendingRequests.clear();
208 Incidence::List incidences = temporaryCalendar->incidences();
210 if (incidences.isEmpty()) {
211 d->setErrorMessage(i18n(
"The ical file to merge is empty."));
217 QStringList mimeTypes = QStringList() << KCalCore::Event::eventMimeType() << KCalCore::Todo::todoMimeType()
218 << KCalCore::Journal::journalMimeType();
219 collection = CalendarUtils::selectCollection(0, dialogCode , mimeTypes);
224 d->setErrorMessage(QString());
228 const IncidenceChanger::DestinationPolicy policySaved = d->m_changer->destinationPolicy();
229 d->m_changer->startAtomicOperation(i18n(
"Merge ical file into existing calendar."));
230 d->m_changer->setDestinationPolicy(IncidenceChanger::DestinationPolicyNeverAsk);
231 foreach(
const Incidence::Ptr &incidence, incidences) {
235 const int requestId = d->m_changer->createIncidence(incidence, collection);
236 Q_ASSERT(requestId != -1);
238 d->m_pendingRequests << requestId;
240 d->m_changer->endAtomicOperation();
242 d->m_changer->setDestinationPolicy(policySaved);
243 d->m_numIncidences = incidences.count();
245 d->m_collection = collection;
246 KIO::StoredTransferJob *job = KIO::storedGet(KUrl(url));
247 connect(job, SIGNAL(data(KIO::Job*,QByteArray)), d, SLOT(remoteDownloadFinished(KIO::Job*,QByteArray)));
Represents a collection of PIM items.
QString identifier() const
Returns the unique identifier of the agent instance.
A representation of an agent type.
AgentType type(const QString &identifier) const
Returns the agent type with the given identifier or an invalid agent type if the identifier does not ...
Job for creating new agent instances.
static AgentManager * self()
Returns the global instance of the agent manager.
A representation of an agent instance.
bool isValid() const
Returns whether the entity is valid.
void start()
Starts the instance creation.
void reconfigure() const
Tell the agent that its configuration has been changed remotely via D-Bus.
AgentInstance instance() const
Returns the AgentInstance object of the newly created agent instance.