diff options
author | Christian Mollekopf <mollekopf@kolabsys.com> | 2013-11-07 22:08:11 (GMT) |
---|---|---|
committer | Christian Mollekopf <mollekopf@kolabsys.com> | 2013-11-07 22:08:11 (GMT) |
commit | 8418bf5af4e9c26937d1d0af1b76b89ef071566e (patch) | |
tree | e6e0995d89d1be3dc212b8e72ea71320bdd5173f | |
parent | fc4896670ccff5dcbd3dce97cb33e6c172e16ef1 (diff) | |
download | kolab-utils-dev/googlemigration.tar.gz |
Support for tasksdev/googlemigration
-rw-r--r-- | migrationutility/googlesourceaccount.cpp | 147 | ||||
-rw-r--r-- | migrationutility/googlesourceaccount.h | 52 | ||||
-rw-r--r-- | migrationutility/googlesourceserver.cpp | 4 |
3 files changed, 201 insertions, 2 deletions
diff --git a/migrationutility/googlesourceaccount.cpp b/migrationutility/googlesourceaccount.cpp index f3e4158..b99f250 100644 --- a/migrationutility/googlesourceaccount.cpp +++ b/migrationutility/googlesourceaccount.cpp @@ -23,6 +23,10 @@ #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> @@ -188,6 +192,114 @@ void FetchCalendarFoldersJob::slotFetchJobFinished(KGAPI2::Job* job) emitResult(); } +FetchTaskObjectsJob::FetchTaskObjectsJob(const QString &tasklistId, const KGAPI2::AccountPtr& account, QObject* parent) +: FetchObjectsJob(parent), + mAccount(account), + mTasklistId(tasklistId) +{ +} + +void FetchTaskObjectsJob::start() +{ + if (mAccount.isNull()) { + Error() << "Error: Please authenticate first"; + setError(KJob::UserDefinedError); + setErrorText("Error: Please authenticate first"); + emitResult(); + return; + } + Debug() << "fetching tasks"; + + KGAPI2::TaskFetchJob *fetchJob = new KGAPI2::TaskFetchJob(mTasklistId, mAccount, this); + connect(fetchJob, SIGNAL(finished(KGAPI2::Job*)), this, SLOT(slotFetchJobFinished(KGAPI2::Job*))); +} + +void FetchTaskObjectsJob::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 tasks = fetchJob->items(); + + QList<Object> objects; + Debug() << "Fetched " << tasks.size() << " tasks"; + Q_FOREACH (const KGAPI2::ObjectPtr &c, tasks) { + KCalCore::Incidence::Ptr task(c.dynamicCast<KGAPI2::Task>()->clone()); + Debug() << task->uid() << task->summary(); + Object obj; + obj.object = QVariant::fromValue(task); + objects << obj; + } + emit objectsReceived(mTasklistId, objects); + emitResult(); +} + + +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), @@ -245,8 +357,7 @@ void GoogleSourceAccount::setUser(const QString& username) KJob* GoogleSourceAccount::login() { mAccount = KGAPI2::AccountPtr(new KGAPI2::Account); -// account->setScopes( QList<QUrl>() << KGAPI2::Account::contactsScopeUrl() << KGAPI2::Account::tasksScopeUrl() << KGAPI2::Account::calendarScopeUrl()); - mAccount->setScopes( QList<QUrl>() << KGAPI2::Account::contactsScopeUrl() << KGAPI2::Account::calendarScopeUrl()); + mAccount->setScopes(QList<QUrl>() << KGAPI2::Account::contactsScopeUrl() << KGAPI2::Account::calendarScopeUrl() << KGAPI2::Account::tasksScopeUrl()); mAccount->setAccountName(mUser); LoginJob *authJob = new LoginJob(mAccount, this); @@ -305,3 +416,35 @@ QPair< Kolab::FolderType, QString > GoogleCalendarSourceAccount::translateFolder { 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)); +} + diff --git a/migrationutility/googlesourceaccount.h b/migrationutility/googlesourceaccount.h index fc0f2f7..52c76d1 100644 --- a/migrationutility/googlesourceaccount.h +++ b/migrationutility/googlesourceaccount.h @@ -69,6 +69,23 @@ private: QHash<QString, QString> mCalendarNames; }; +class GoogleTasksSourceAccount: public GoogleSourceAccount +{ + Q_OBJECT +public: + explicit GoogleTasksSourceAccount(QObject* parent = 0); + + virtual FetchFoldersJob *fetchFolderList(); + virtual FetchObjectsJob *fetchObjects(const QString &folder); + virtual QPair<Kolab::FolderType, QString> translateFolder(const QString& folder); + +private slots: + void onFetchedTasklists(KJob *job); + +private: + QHash<QString, QString> mTasklists; +}; + class FetchContactObjectsJob : public FetchObjectsJob { Q_OBJECT @@ -97,6 +114,11 @@ private: KGAPI2::AccountPtr mAccount; }; +/* + * TODO + * All fetch jobs could be consolidated into one, but we need multiple inheritance + */ + class FetchCalendarObjectsJob : public FetchObjectsJob { Q_OBJECT @@ -127,4 +149,34 @@ private: KGAPI2::AccountPtr mAccount; }; +class FetchTaskObjectsJob : public FetchObjectsJob +{ + Q_OBJECT +public: + explicit FetchTaskObjectsJob(const QString &mTasklistId, const KGAPI2::AccountPtr &account, QObject* parent = 0); + virtual void start(); + +private slots: + void slotFetchJobFinished(KGAPI2::Job *job); + +private: + KGAPI2::AccountPtr mAccount; + QString mTasklistId; +}; + +class FetchTaskListsJob : public FetchFoldersJob +{ + Q_OBJECT +public: + explicit FetchTaskListsJob(const KGAPI2::AccountPtr &account, QObject* parent = 0); + virtual void start(); + QHash<QString, QString> mNames; + +private slots: + void slotFetchJobFinished(KGAPI2::Job *job); + +private: + KGAPI2::AccountPtr mAccount; +}; + #endif
\ No newline at end of file diff --git a/migrationutility/googlesourceserver.cpp b/migrationutility/googlesourceserver.cpp index 6301b3b..978149c 100644 --- a/migrationutility/googlesourceserver.cpp +++ b/migrationutility/googlesourceserver.cpp @@ -66,6 +66,10 @@ QList<SourceAccount*> GoogleSourceServer::getSourceAccountsImpl(const QString& u GoogleCalendarSourceAccount *calendarAccount = new GoogleCalendarSourceAccount(this); calendarAccount->setUser(user); sourceAccounts << calendarAccount; + + GoogleTasksSourceAccount *tasksAccount = new GoogleTasksSourceAccount(this); + tasksAccount->setUser(user); + sourceAccounts << tasksAccount; IMAPSourceAccount *imapAccount = new IMAPSourceAccount(this); imapAccount->prepareConnection(mHost, mPort, user, user, mPw, mEncryptionMode, mAuthenticationMode); |