File indexing completed on 2024-05-12 05:22:10

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Barchiesi <david@barchie.si>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "mainwindow.h"
0008 
0009 #include "core/account.h"
0010 #include "core/authjob.h"
0011 #include "tasks/taskcreatejob.h"
0012 #include "tasks/taskfetchjob.h"
0013 #include "tasks/tasklist.h"
0014 #include "tasks/tasklistcreatejob.h"
0015 #include "tasks/tasklistfetchjob.h"
0016 
0017 #include <QDebug>
0018 #include <QListWidgetItem>
0019 
0020 MainWindow::MainWindow(QWidget *parent)
0021     : QMainWindow(parent)
0022 {
0023     /* Initialize GUI */
0024     ui.setupUi(this);
0025     ui.errorLabel->setVisible(false);
0026     connect(ui.authButton, &QAbstractButton::clicked, this, &MainWindow::authenticate);
0027     connect(ui.createTaskListButton, &QAbstractButton::clicked, this, &MainWindow::slotCreateTaskList);
0028     connect(ui.taskListsList, &QListWidget::itemSelectionChanged, this, &MainWindow::taskListSelected);
0029 }
0030 
0031 void MainWindow::authenticate()
0032 {
0033     auto account = KGAPI2::AccountPtr::create();
0034     account->setScopes({KGAPI2::Account::tasksScopeUrl()});
0035 
0036     /* Create AuthJob to retrieve OAuth tokens for the account */
0037     auto *authJob = new KGAPI2::AuthJob(account, QStringLiteral("554041944266.apps.googleusercontent.com"), QStringLiteral("mdT1DjzohxN3npUUzkENT0gO"));
0038     connect(authJob, &KGAPI2::Job::finished, this, [this, authJob]() {
0039         /* Always remember to delete the jobs, otherwise your application will
0040          * leak memory. */
0041         authJob->deleteLater();
0042 
0043         if (authJob->error() != KGAPI2::NoError) {
0044             ui.errorLabel->setText(QStringLiteral("Error: %1").arg(authJob->errorString()));
0045             ui.errorLabel->setVisible(true);
0046             return;
0047         }
0048 
0049         m_account = authJob->account();
0050 
0051         ui.authStatusLabel->setText(QStringLiteral("Authenticated"));
0052         enableCreateTaskList(true);
0053 
0054         QMetaObject::invokeMethod(this, &MainWindow::slotFetchTaskLists, Qt::QueuedConnection);
0055     });
0056 }
0057 
0058 void MainWindow::slotFetchTaskLists()
0059 {
0060     if (m_account.isNull()) {
0061         ui.errorLabel->setText(QStringLiteral("Error: Please authenticate first"));
0062         ui.errorLabel->setVisible(true);
0063         ui.authButton->setVisible(true);
0064         return;
0065     }
0066 
0067     auto *fetchJob = new KGAPI2::TaskListFetchJob(m_account, this);
0068     connect(fetchJob, &KGAPI2::Job::finished, this, [this, fetchJob]() {
0069         fetchJob->deleteLater();
0070 
0071         if (fetchJob->error() != KGAPI2::NoError) {
0072             ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
0073             ui.errorLabel->setVisible(true);
0074             return;
0075         }
0076 
0077         /* Get all items the job has retrieved */
0078         const auto objects = fetchJob->items();
0079         ui.taskListsList->clear();
0080         for (const auto &object : objects) {
0081             const auto taskList = object.dynamicCast<KGAPI2::TaskList>();
0082 
0083             /* Convert the taskList to QListWidget item */
0084             auto *item = new QListWidgetItem(ui.taskListsList);
0085             item->setText(taskList->title());
0086             item->setData(Qt::UserRole, taskList->uid());
0087 
0088             ui.taskListsList->addItem(item);
0089         }
0090     });
0091 }
0092 
0093 void MainWindow::taskListSelected()
0094 {
0095     const bool hasSelection = (ui.taskListsList->selectedItems().count() != 0);
0096 
0097     ui.taskListTasksList->clear();
0098     enableCreateTask(hasSelection);
0099 
0100     if (!hasSelection) {
0101         return;
0102     }
0103 
0104     const auto taskListId = ui.taskListsList->selectedItems().at(0)->data(Qt::UserRole).toString();
0105     auto *fetchJob = new KGAPI2::TaskFetchJob(taskListId, m_account, this);
0106     connect(fetchJob, &KGAPI2::Job::finished, this, [this, fetchJob]() {
0107         fetchJob->deleteLater();
0108 
0109         if (fetchJob->error() != KGAPI2::NoError) {
0110             ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
0111             ui.errorLabel->setVisible(true);
0112             return;
0113         }
0114 
0115         /* Get all items the job has retrieved */
0116         const auto objects = fetchJob->items();
0117         for (const auto &object : objects) {
0118             const auto task = object.dynamicCast<KGAPI2::Task>();
0119 
0120             ui.taskListTasksList->addItem(task->summary());
0121         }
0122     });
0123 }
0124 
0125 void MainWindow::slotCreateTaskList()
0126 {
0127     const auto title = ui.taskListNameEdit->text();
0128 
0129     auto taskList = KGAPI2::TaskListPtr::create();
0130     taskList->setTitle(title);
0131 
0132     auto *createJob = new KGAPI2::TaskListCreateJob(taskList, m_account, this);
0133     connect(createJob, &KGAPI2::Job::finished, this, [this, createJob]() {
0134         createJob->deleteLater();
0135 
0136         if (createJob->error() != KGAPI2::NoError) {
0137             ui.errorLabel->setText(QStringLiteral("Error: %1").arg(createJob->errorString()));
0138             ui.errorLabel->setVisible(true);
0139             return;
0140         }
0141 
0142         QMetaObject::invokeMethod(this, &MainWindow::slotFetchTaskLists, Qt::QueuedConnection);
0143     });
0144 }
0145 
0146 void MainWindow::enableCreateTaskList(bool enabled)
0147 {
0148     ui.taskListNameEdit->setEnabled(enabled);
0149     ui.createTaskListButton->setEnabled(enabled);
0150 }
0151 
0152 void MainWindow::enableCreateTask(bool enabled)
0153 {
0154     ui.taskNameEdit->setEnabled(enabled);
0155     ui.createTaskButton->setEnabled(enabled);
0156 }
0157 
0158 #include "moc_mainwindow.cpp"