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
|
/*
SPDX-FileCopyrightText: 2019 David Barchiesi <david@barchie.si>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "mainwindow.h"
#include "core/account.h"
#include "core/authjob.h"
#include "tasks/taskcreatejob.h"
#include "tasks/taskfetchjob.h"
#include "tasks/tasklist.h"
#include "tasks/tasklistcreatejob.h"
#include "tasks/tasklistfetchjob.h"
#include <QDebug>
#include <QListWidgetItem>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
/* Initialize GUI */
ui.setupUi(this);
ui.errorLabel->setVisible(false);
connect(ui.authButton, &QAbstractButton::clicked, this, &MainWindow::authenticate);
connect(ui.createTaskListButton, &QAbstractButton::clicked, this, &MainWindow::slotCreateTaskList);
connect(ui.taskListsList, &QListWidget::itemSelectionChanged, this, &MainWindow::taskListSelected);
}
void MainWindow::authenticate()
{
auto account = KGAPI2::AccountPtr::create();
account->setScopes({KGAPI2::Account::tasksScopeUrl()});
/* Create AuthJob to retrieve OAuth tokens for the account */
auto *authJob = new KGAPI2::AuthJob(account, QStringLiteral("554041944266.apps.googleusercontent.com"), QStringLiteral("mdT1DjzohxN3npUUzkENT0gO"));
connect(authJob, &KGAPI2::Job::finished, this, [this, authJob]() {
/* Always remember to delete the jobs, otherwise your application will
* leak memory. */
authJob->deleteLater();
if (authJob->error() != KGAPI2::NoError) {
ui.errorLabel->setText(QStringLiteral("Error: %1").arg(authJob->errorString()));
ui.errorLabel->setVisible(true);
return;
}
m_account = authJob->account();
ui.authStatusLabel->setText(QStringLiteral("Authenticated"));
enableCreateTaskList(true);
QMetaObject::invokeMethod(this, &MainWindow::slotFetchTaskLists, Qt::QueuedConnection);
});
}
void MainWindow::slotFetchTaskLists()
{
if (m_account.isNull()) {
ui.errorLabel->setText(QStringLiteral("Error: Please authenticate first"));
ui.errorLabel->setVisible(true);
ui.authButton->setVisible(true);
return;
}
auto *fetchJob = new KGAPI2::TaskListFetchJob(m_account, this);
connect(fetchJob, &KGAPI2::Job::finished, this, [this, fetchJob]() {
fetchJob->deleteLater();
if (fetchJob->error() != KGAPI2::NoError) {
ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
ui.errorLabel->setVisible(true);
return;
}
/* Get all items the job has retrieved */
const auto objects = fetchJob->items();
ui.taskListsList->clear();
for (const auto &object : objects) {
const auto taskList = object.dynamicCast<KGAPI2::TaskList>();
/* Convert the taskList to QListWidget item */
auto *item = new QListWidgetItem(ui.taskListsList);
item->setText(taskList->title());
item->setData(Qt::UserRole, taskList->uid());
ui.taskListsList->addItem(item);
}
});
}
void MainWindow::taskListSelected()
{
const bool hasSelection = (ui.taskListsList->selectedItems().count() != 0);
ui.taskListTasksList->clear();
enableCreateTask(hasSelection);
if (!hasSelection) {
return;
}
const auto taskListId = ui.taskListsList->selectedItems().at(0)->data(Qt::UserRole).toString();
auto *fetchJob = new KGAPI2::TaskFetchJob(taskListId, m_account, this);
connect(fetchJob, &KGAPI2::Job::finished, this, [this, fetchJob]() {
fetchJob->deleteLater();
if (fetchJob->error() != KGAPI2::NoError) {
ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
ui.errorLabel->setVisible(true);
return;
}
/* Get all items the job has retrieved */
const auto objects = fetchJob->items();
for (const auto &object : objects) {
const auto task = object.dynamicCast<KGAPI2::Task>();
ui.taskListTasksList->addItem(task->summary());
}
});
}
void MainWindow::slotCreateTaskList()
{
const auto title = ui.taskListNameEdit->text();
auto taskList = KGAPI2::TaskListPtr::create();
taskList->setTitle(title);
auto *createJob = new KGAPI2::TaskListCreateJob(taskList, m_account, this);
connect(createJob, &KGAPI2::Job::finished, this, [this, createJob]() {
createJob->deleteLater();
if (createJob->error() != KGAPI2::NoError) {
ui.errorLabel->setText(QStringLiteral("Error: %1").arg(createJob->errorString()));
ui.errorLabel->setVisible(true);
return;
}
QMetaObject::invokeMethod(this, &MainWindow::slotFetchTaskLists, Qt::QueuedConnection);
});
}
void MainWindow::enableCreateTaskList(bool enabled)
{
ui.taskListNameEdit->setEnabled(enabled);
ui.createTaskListButton->setEnabled(enabled);
}
void MainWindow::enableCreateTask(bool enabled)
{
ui.taskNameEdit->setEnabled(enabled);
ui.createTaskButton->setEnabled(enabled);
}
|