summaryrefslogtreecommitdiff
path: root/migrationutility/googlesourceaccount.cpp
blob: e4252814a6deb0ce26a20358ac449c4a018dee5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * Copyright (C) 2013  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "googlesourceaccount.h"

#include <libkgapi2/contacts/contact.h>
#include <libkgapi2/contacts/contactfetchjob.h>
#include <libkgapi2/calendar/calendar.h>
#include <libkgapi2/calendar/calendarfetchjob.h>
#include <libkgapi2/calendar/event.h>
#include <libkgapi2/calendar/eventfetchjob.h>
#include <libkgapi2/tasks/tasklistfetchjob.h>
#include <libkgapi2/tasks/task.h>
#include <libkgapi2/tasks/taskfetchjob.h>
#include <libkgapi2/tasks/tasklist.h>
#include <libkgapi2/authjob.h>
#include <libkgapi2/account.h>

#include <errorhandler.h>

GoogleFetchObjectsJob::GoogleFetchObjectsJob(const QString &currentFolder, const KGAPI2::AccountPtr& account, QObject* parent)
:   FetchObjectsJob(parent),
    mAccount(account),
    mCurrentFolder(currentFolder)
{

}

void GoogleFetchObjectsJob::start()
{
    if (mAccount.isNull()) {
        Error() << "Error: Please authenticate first";
        setError(KJob::UserDefinedError);
        setErrorText("Error: Please authenticate first");
        emitResult();
        return;
    }
    Debug() << "fetching contacts";

    KGAPI2::FetchJob *fetchJob = getFetchJob(mAccount, mCurrentFolder);
    connect(fetchJob, SIGNAL(finished(KGAPI2::Job*)), this, SLOT(slotFetchJobFinished(KGAPI2::Job*)));
}


void GoogleFetchObjectsJob::slotFetchJobFinished(KGAPI2::Job* job)
{
    KGAPI2::FetchJob *fetchJob = qobject_cast<KGAPI2::FetchJob*>(job);
    Q_ASSERT(fetchJob);
    fetchJob->deleteLater();

    if (fetchJob->error() != KGAPI2::NoError) {
        Error() << fetchJob->errorString();
        setError(KJob::UserDefinedError);
        setErrorText(fetchJob->errorString());
        emitResult();
        return;
    }

    QList<Object> objects;
    /* Get all items the job has retrieved */
    const KGAPI2::ObjectsList items = fetchJob->items();
    foreach (const KGAPI2::ObjectPtr &i, items) {
        objects << getObject(i);

    }
    emit objectsReceived(mCurrentFolder, objects);
    emitResult();
}



FetchContactObjectsJob::FetchContactObjectsJob(const KGAPI2::AccountPtr &account, QObject* parent)
:   GoogleFetchObjectsJob(QLatin1String("Contacts"), account, parent)
{

}

KGAPI2::FetchJob* FetchContactObjectsJob::getFetchJob(const KGAPI2::AccountPtr& account, const QString& folder)
{
    Q_UNUSED(folder);
    return new KGAPI2::ContactFetchJob(account, this);
}

Object FetchContactObjectsJob::getObject(const KGAPI2::ObjectPtr& object)
{
    const KABC::Addressee contact = *object.dynamicCast<KGAPI2::Contact>();
    Debug() << "contact: " << contact.name();
    qDebug() << contact.emails();
    Object obj;
    obj.object = QVariant::fromValue(contact);
    return obj;
}

FetchCalendarObjectsJob::FetchCalendarObjectsJob(const QString &calendarId, const KGAPI2::AccountPtr& account, QObject* parent)
:   GoogleFetchObjectsJob(calendarId, account, parent)
{
}

KGAPI2::FetchJob* FetchCalendarObjectsJob::getFetchJob(const KGAPI2::AccountPtr &account, const QString& folder)
{
    return new KGAPI2::EventFetchJob(folder, account, this);
}

Object FetchCalendarObjectsJob::getObject(const KGAPI2::ObjectPtr &object)
{
    KCalCore::Incidence::Ptr event(object.dynamicCast<KGAPI2::Event>()->clone());
    Debug() << event->uid() << event->summary();
    Object obj;
    obj.object = QVariant::fromValue(event);
    return obj;
}



FetchCalendarFoldersJob::FetchCalendarFoldersJob(const KGAPI2::AccountPtr& account, QObject* parent)
:   FetchFoldersJob(parent),
    mAccount(account)
{
}

void FetchCalendarFoldersJob::start()
{
    if (mAccount.isNull()) {
        Error() << "Error: Please authenticate first";
        setError(KJob::UserDefinedError);
        setErrorText("Error: Please authenticate first");
        emitResult();
        return;
    }
    Debug() << "fetch calendars";
    KGAPI2::CalendarFetchJob *fetchJob = new KGAPI2::CalendarFetchJob(mAccount, this);
    connect(fetchJob, SIGNAL(finished(KGAPI2::Job*)), this, SLOT(slotFetchJobFinished(KGAPI2::Job*)));
}

void FetchCalendarFoldersJob::slotFetchJobFinished(KGAPI2::Job* job)
{
    KGAPI2::CalendarFetchJob *fetchJob = qobject_cast<KGAPI2::CalendarFetchJob*>(job);
    Q_ASSERT(fetchJob);
    fetchJob->deleteLater();

    if (fetchJob->error() != KGAPI2::NoError) {
        Error() << fetchJob->errorString();
        setError(KJob::UserDefinedError);
        setErrorText(fetchJob->errorString());
        emitResult();
        return;
    }

    /* Get all items the job has retrieved */
    const KGAPI2::ObjectsList objects = fetchJob->items();

    QStringList folders;
    Debug() << "Fetched " << objects.size() << " calendars";
    Q_FOREACH (const KGAPI2::ObjectPtr &c, objects) {
        const KGAPI2::Calendar &calendar = *c.dynamicCast<KGAPI2::Calendar>();
        //TODO prefix with Calendar/
        QString name = calendar.title();
        //normalize so it works as imap mailbox name
        name.replace(QLatin1String("@"), QLatin1String("at"));
        Debug() << "calendar: " << name;
        mNames.insert(calendar.uid(), name);
        folders << calendar.uid();
    }
    emit foldersReceived(folders);
    emitResult();
}

FetchTaskObjectsJob::FetchTaskObjectsJob(const QString &tasklistId, const KGAPI2::AccountPtr& account, QObject* parent)
:   GoogleFetchObjectsJob(tasklistId, account, parent)
{
}

KGAPI2::FetchJob* FetchTaskObjectsJob::getFetchJob(const KGAPI2::AccountPtr &account, const QString& folder)
{
    return new KGAPI2::TaskFetchJob(folder, account, this);
}

Object FetchTaskObjectsJob::getObject(const KGAPI2::ObjectPtr &object)
{
    KCalCore::Incidence::Ptr task(object.dynamicCast<KGAPI2::Task>()->clone());
    Debug() << task->uid() << task->summary();
    Object obj;
    obj.object = QVariant::fromValue(task);
    return obj;
}

FetchTaskListsJob::FetchTaskListsJob(const KGAPI2::AccountPtr& account, QObject* parent)
:   FetchFoldersJob(parent),
    mAccount(account)
{
}

void FetchTaskListsJob::start()
{
    if (mAccount.isNull()) {
        Error() << "Error: Please authenticate first";
        setError(KJob::UserDefinedError);
        setErrorText("Error: Please authenticate first");
        emitResult();
        return;
    }
    Debug() << "fetch tasks";
    KGAPI2::TaskListFetchJob *fetchJob = new KGAPI2::TaskListFetchJob(mAccount, this);
    connect(fetchJob, SIGNAL(finished(KGAPI2::Job*)), this, SLOT(slotFetchJobFinished(KGAPI2::Job*)));
}

//TODO somehow share this with the other fetchjobs
//multiple inheritance? need to figure out how to use virtual inheritance for this
void FetchTaskListsJob::slotFetchJobFinished(KGAPI2::Job* job)
{
    KGAPI2::FetchJob *fetchJob = qobject_cast<KGAPI2::FetchJob*>(job);
    Q_ASSERT(fetchJob);
    fetchJob->deleteLater();

    if (fetchJob->error() != KGAPI2::NoError) {
        Error() << fetchJob->errorString();
        setError(KJob::UserDefinedError);
        setErrorText(fetchJob->errorString());
        emitResult();
        return;
    }

    /* Get all items the job has retrieved */
    const KGAPI2::ObjectsList objects = fetchJob->items();

    QStringList folders;
    Debug() << "Fetched " << objects.size() << " tasklists";
    Q_FOREACH (const KGAPI2::ObjectPtr &c, objects) {
        const KGAPI2::TaskList &list = *c.dynamicCast<KGAPI2::TaskList>();
        //TODO prefix with Task/
        QString name = list.title();
        //normalize so it works as imap mailbox name
        name.replace(QLatin1String("@"), QLatin1String("at"));
        Debug() << "calendar: " << name;
        mNames.insert(list.uid(), name);
        folders << list.uid();
    }
    emit foldersReceived(folders);
    emitResult();
}


LoginJob::LoginJob(const KGAPI2::AccountPtr &account, QObject* parent)
:   KJob(parent),
    mAccount(account)
{

}

void LoginJob::start()
{
    Debug() << "login";
    /* Create AuthJob to retrieve OAuth tokens for the account */
    KGAPI2::AuthJob *authJob = new KGAPI2::AuthJob(
        mAccount,
        QLatin1String("583467588400.apps.googleusercontent.com"),
        QLatin1String("_AwtLuOqS5JcCHWGpq8fxKdd"), this);
//     GoogleAuthJob *authJob = new GoogleAuthJob(
//         account,
//         QLatin1String("583467588400.apps.googleusercontent.com"),
//         QLatin1String("_AwtLuOqS5JcCHWGpq8fxKdd"));
    connect(authJob, SIGNAL(finished(KGAPI2::Job*)), this, SLOT(slotAuthJobFinished(KGAPI2::Job*)));
}

void LoginJob::slotAuthJobFinished(KGAPI2::Job *job)
{
    Debug() << "login done";
    KGAPI2::AuthJob *authJob = qobject_cast<KGAPI2::AuthJob*>(job);
    Q_ASSERT(authJob);
    authJob->deleteLater();

    if (authJob->error() != KGAPI2::NoError) {
        setError(KJob::UserDefinedError);
        setErrorText(authJob->errorString());
    }
    emitResult();
}


GoogleSourceAccount::GoogleSourceAccount(QObject* parent)
:   SourceAccount(parent)
{

}

QPair<Kolab::FolderType, QString> GoogleSourceAccount::translateFolder(const QString& folder)
{
    return QPair<Kolab::FolderType, QString>(Kolab::ContactType, folder);
}

void GoogleSourceAccount::setUser(const QString& username)
{
    mUser = username;
}

KJob* GoogleSourceAccount::login()
{
    mAccount = KGAPI2::AccountPtr(new KGAPI2::Account);
    mAccount->setScopes(QList<QUrl>() << KGAPI2::Account::contactsScopeUrl() << KGAPI2::Account::calendarScopeUrl() << KGAPI2::Account::tasksScopeUrl());
    mAccount->setAccountName(mUser);

    LoginJob *authJob = new LoginJob(mAccount, this);
    return authJob;
}

KJob *GoogleSourceAccount::logout()
{
    mAccount.clear();
    return 0;
}


GoogleContactsSourceAccount::GoogleContactsSourceAccount(QObject* parent)
: GoogleSourceAccount(parent)
{
}

QStringList GoogleContactsSourceAccount::lookupFolderList()
{
    return QStringList() << "Contacts";
}

FetchObjectsJob* GoogleContactsSourceAccount::fetchObjects(const QString& folder)
{
    return new FetchContactObjectsJob(mAccount, this);
}


GoogleCalendarSourceAccount::GoogleCalendarSourceAccount(QObject* parent)
: GoogleSourceAccount(parent)
{
}

FetchFoldersJob* GoogleCalendarSourceAccount::fetchFolderList()
{
    FetchCalendarFoldersJob *job = new FetchCalendarFoldersJob(mAccount, this);
    connect(job, SIGNAL(result(KJob*)), this, SLOT(onFetchedCalendars(KJob*)));
    return job;
}

void GoogleCalendarSourceAccount::onFetchedCalendars(KJob *job)
{
    if (!job->error()) {
        FetchCalendarFoldersJob *calendarFolderJob = static_cast<FetchCalendarFoldersJob*>(job);
        mCalendarNames = calendarFolderJob->mNames;
    }
}

FetchObjectsJob* GoogleCalendarSourceAccount::fetchObjects(const QString& folder)
{
    return new FetchCalendarObjectsJob(folder, mAccount, this);
}

QPair< Kolab::FolderType, QString > GoogleCalendarSourceAccount::translateFolder(const QString& folder)
{
    return QPair<Kolab::FolderType, QString>(Kolab::EventType, mCalendarNames.value(folder));
}


GoogleTasksSourceAccount::GoogleTasksSourceAccount(QObject* parent)
: GoogleSourceAccount(parent)
{
}

FetchFoldersJob* GoogleTasksSourceAccount::fetchFolderList()
{
    FetchTaskListsJob *job = new FetchTaskListsJob(mAccount, this);
    connect(job, SIGNAL(result(KJob*)), this, SLOT(onFetchedTasklists(KJob*)));
    return job;
}

void GoogleTasksSourceAccount::onFetchedTasklists(KJob* job)
{
    if (!job->error()) {
        FetchTaskListsJob *fetchJob = static_cast<FetchTaskListsJob*>(job);
        mTasklists = fetchJob->mNames;
    }
}

FetchObjectsJob* GoogleTasksSourceAccount::fetchObjects(const QString& folder)
{
    return new FetchTaskObjectsJob(folder, mAccount, this);
}

QPair< Kolab::FolderType, QString > GoogleTasksSourceAccount::translateFolder(const QString& folder)
{
    return QPair<Kolab::FolderType, QString>(Kolab::EventType, mTasklists.value(folder));
}