Commit fbe8ef0e authored by David Barchiesi's avatar David Barchiesi Committed by Daniel Vrátil
Browse files

Cleanup Tasks code.

parent 0befb478
Pipeline #21749 passed with stage
in 6 minutes and 6 seconds
......@@ -2,4 +2,5 @@ add_subdirectory(contacts)
add_subdirectory(drives)
add_subdirectory(teamdrive)
add_subdirectory(permissions)
add_subdirectory(tasks)
#add_subdirectory(staticmaps)
kde_enable_exceptions()
include_directories(
${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src
${CMAKE_BINARY_DIR}
)
set(tasks_example_SRCS main.cpp mainwindow.cpp)
set(tasks_example_HDRS mainwindow.h)
qt5_wrap_ui(tasks_example_SRCS ui/main.ui)
add_executable(tasks-example
${tasks_example_SRCS}
${tasks_example_HDRS_MOC}
)
target_link_libraries(tasks-example
Qt5::Widgets
Qt5::Core
KF5::GAPICore
KF5::GAPITasks
)
/*
Copyright (C) 2019 David Barchiesi <david@barchie.si>
This library 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 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library 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 library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow ex;
ex.show();
return app.exec();
}
/*
Copyright (C) 2019 David Barchiesi <david@barchie.si>
This library 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 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library 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 library. If not, see <https://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_main.h"
#include <tasks/tasklist.h>
#include <tasks/tasklistcreatejob.h>
#include <tasks/tasklistfetchjob.h>
#include <tasks/taskfetchjob.h>
#include <tasks/taskcreatejob.h>
#include <authjob.h>
#include <account.h>
#include <QListWidgetItem>
#include <QUuid>
#include <QDebug>
MainWindow::MainWindow(QWidget * parent):
QMainWindow(parent),
m_ui(new Ui::MainWindow)
{
/* Initialize GUI */
m_ui->setupUi(this);
m_ui->errorLabel->setVisible(false);
connect(m_ui->authButton, &QAbstractButton::clicked,
this, &MainWindow::authenticate);
connect(m_ui->createTaskListButton, &QAbstractButton::clicked,
this, &MainWindow::slotCreateTaskList);
connect(m_ui->taskListsList, &QListWidget::itemSelectionChanged,
this, &MainWindow::taskListSelected);
}
MainWindow::~MainWindow()
{
delete m_ui;
}
void MainWindow::authenticate()
{
KGAPI2::AccountPtr account(new KGAPI2::Account);
account->setScopes( QList<QUrl>() << KGAPI2::Account::tasksScopeUrl() );
/* Create AuthJob to retrieve OAuth tokens for the account */
KGAPI2::AuthJob *authJob = new KGAPI2::AuthJob(
account,
QStringLiteral("554041944266.apps.googleusercontent.com"),
QStringLiteral("mdT1DjzohxN3npUUzkENT0gO"));
connect(authJob, &KGAPI2::Job::finished,
this, &MainWindow::slotAuthJobFinished);
}
void MainWindow::slotAuthJobFinished(KGAPI2::Job *job)
{
KGAPI2::AuthJob *authJob = qobject_cast<KGAPI2::AuthJob*>(job);
Q_ASSERT(authJob);
/* Always remember to delete the jobs, otherwise your application will
* leak memory. */
authJob->deleteLater();
if (authJob->error() != KGAPI2::NoError) {
m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(authJob->errorString()));
m_ui->errorLabel->setVisible(true);
return;
}
m_account = authJob->account();
qDebug() << "Got access token" << m_account->accessToken();
m_ui->authStatusLabel->setText(QStringLiteral("Authenticated"));
enableCreateTaskList(true);
QMetaObject::invokeMethod(this, &MainWindow::slotFetchTaskLists, Qt::QueuedConnection);
}
void MainWindow::slotFetchTaskLists()
{
qDebug() << Q_FUNC_INFO;
if (m_account.isNull()) {
m_ui->errorLabel->setText(QStringLiteral("Error: Please authenticate first"));
m_ui->errorLabel->setVisible(true);
m_ui->authButton->setVisible(true);
return;
}
KGAPI2::TaskListFetchJob *fetchJob = new KGAPI2::TaskListFetchJob(m_account, this);
connect(fetchJob, &KGAPI2::Job::finished,
this, &MainWindow::slotTaskListsFetchJobFinished);
}
void MainWindow::slotTaskListsFetchJobFinished(KGAPI2::Job *job)
{
qDebug() << Q_FUNC_INFO << job;
KGAPI2::TaskListFetchJob *fetchJob = qobject_cast<KGAPI2::TaskListFetchJob*>(job);
Q_ASSERT(fetchJob);
fetchJob->deleteLater();
if (fetchJob->error() != KGAPI2::NoError) {
m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
m_ui->errorLabel->setVisible(true);
return;
}
/* Get all items the job has retrieved */
const KGAPI2::ObjectsList objects = fetchJob->items();
m_ui->taskListsList->clear();
for (const KGAPI2::ObjectPtr &object : objects) {
const KGAPI2::TaskListPtr taskList = object.dynamicCast<KGAPI2::TaskList>();
/* Convert the taskList to QListWidget item */
QListWidgetItem *item = new QListWidgetItem(m_ui->taskListsList);
item->setText(taskList->title());
item->setData(Qt::UserRole, taskList->uid());
m_ui->taskListsList->addItem(item);
}
}
void MainWindow::taskListSelected()
{
qDebug() << Q_FUNC_INFO;
bool hasSelection = (m_ui->taskListsList->selectedItems().count() != 0);
m_ui->taskListTasksList->clear();
enableCreateTask(hasSelection);
if (!hasSelection) {
return;
}
const QString taskListId = m_ui->taskListsList->selectedItems().at(0)->data(Qt::UserRole).toString();
KGAPI2::TaskFetchJob *fetchJob = new KGAPI2::TaskFetchJob(taskListId, m_account, this);
connect(fetchJob, &KGAPI2::Job::finished,
this, &MainWindow::slotTasksFetchJobFinished);
}
void MainWindow::slotTasksFetchJobFinished(KGAPI2::Job *job)
{
qDebug() << Q_FUNC_INFO << job;
KGAPI2::TaskFetchJob *fetchJob = qobject_cast<KGAPI2::TaskFetchJob*>(job);
Q_ASSERT(fetchJob);
fetchJob->deleteLater();
if (fetchJob->error() != KGAPI2::NoError) {
m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
m_ui->errorLabel->setVisible(true);
return;
}
/* Get all items the job has retrieved */
const KGAPI2::ObjectsList objects = fetchJob->items();
for (const KGAPI2::ObjectPtr &object : objects) {
const KGAPI2::TaskPtr task = object.dynamicCast<KGAPI2::Task>();
m_ui->taskListTasksList->addItem(task->summary());
}
}
void MainWindow::slotCreateTaskList()
{
qDebug() << Q_FUNC_INFO;
const QString title = m_ui->taskListNameEdit->text();
KGAPI2::TaskListPtr taskList = KGAPI2::TaskListPtr::create();
taskList->setTitle(title);
KGAPI2::TaskListCreateJob *fetchJob = new KGAPI2::TaskListCreateJob(taskList, m_account, this);
connect(fetchJob, &KGAPI2::Job::finished,
this, &MainWindow::slotCreateTaskListFinished);
}
void MainWindow::slotCreateTaskListFinished(KGAPI2::Job *job)
{
qDebug() << Q_FUNC_INFO << job;
KGAPI2::TaskListCreateJob *fetchJob = qobject_cast<KGAPI2::TaskListCreateJob*>(job);
Q_ASSERT(fetchJob);
fetchJob->deleteLater();
if (fetchJob->error() != KGAPI2::NoError) {
m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
m_ui->errorLabel->setVisible(true);
return;
}
// QMetaObject::invokeMethod(this, &MainWindow::slotFetchTaskLists, Qt::QueuedConnection);
}
void MainWindow::slotCreateTask()
{
qDebug() << Q_FUNC_INFO;
// const QString title = m_ui->taskListNameEdit->text();
// KGAPI2::TaskListPtr taskList = KGAPI2::TaskListPtr::create();
// taskList->setTitle(title);
// KGAPI2::TaskCreateJob *fetchJob = new KGAPI2::TaskCreateJob(taskList, m_account, this);
// connect(fetchJob, &KGAPI2::Job::finished,
// this, &MainWindow::slotCreateTaskListFinished);
}
void MainWindow::slotCreateTaskFinished(KGAPI2::Job *job)
{
qDebug() << Q_FUNC_INFO << job;
// KGAPI2::TaskFetchJob *fetchJob = qobject_cast<KGAPI2::TaskFetchJob*>(job);
// Q_ASSERT(fetchJob);
// fetchJob->deleteLater();
// if (fetchJob->error() != KGAPI2::NoError) {
// m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
// m_ui->errorLabel->setVisible(true);
// return;
// }
// QMetaObject::invokeMethod(this, &MainWindow::slotFetchTaskLists, Qt::QueuedConnection);
}
void MainWindow::enableCreateTaskList(bool enabled)
{
m_ui->taskListNameEdit->setEnabled(enabled);
m_ui->createTaskListButton->setEnabled(enabled);
}
void MainWindow::enableCreateTask(bool enabled)
{
m_ui->taskNameEdit->setEnabled(enabled);
m_ui->createTaskButton->setEnabled(enabled);
}
/*
Copyright (C) 2019 David Barchiesi <david@barchie.si>
This library 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 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library 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 library. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <types.h>
#include <tasks/task.h>
namespace Ui {
class MainWindow;
}
namespace KGAPI2 {
class Job;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
private Q_SLOTS:
/**
* Retrieves tokens from Google that we will use to authenticate
* fursther requests
*/
void authenticate();
/**
* Authentication has finished
*/
void slotAuthJobFinished(KGAPI2::Job *job);
/**
* Retrieves list of all task lists
*/
void slotFetchTaskLists();
/**
* Task lists listing was fetched.
*/
void slotTaskListsFetchJobFinished(KGAPI2::Job *job);
/**
* A specific task list has been selected. Sends a request
* to Google to retrieve the associated tasks
*/
void taskListSelected();
/**
* Tasks listing was fetched.
*/
void slotTasksFetchJobFinished(KGAPI2::Job *job);
/**
* Slot to create a new task list
*/
void slotCreateTaskList();
/**
* Tasks List creation is finished
*/
void slotCreateTaskListFinished(KGAPI2::Job *job);
/**
* Slot to create a new task
*/
void slotCreateTask();
/**
* Tasks creation is finished
*/
void slotCreateTaskFinished(KGAPI2::Job *job);
private:
Ui::MainWindow *m_ui;
KGAPI2::AccountPtr m_account;
void enableCreateTaskList(bool enabled);
void enableCreateTask(bool enabled);
};
#endif // MAINWINDOW_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>486</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="authLayout">
<item>
<widget class="QPushButton" name="authButton">
<property name="text">
<string>Authenticate</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="authStatusLabel">
<property name="text">
<string>Not authenticated</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0" colspan="2">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="createTaskListLayout">
<item>
<widget class="QLineEdit" name="taskListNameEdit">
<property name="enabled">
<bool>false</bool>
</property>
<property name="placeholderText">
<string>Task List name</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="createTaskListButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Create Task List</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="0" colspan="2">
<layout class="QHBoxLayout" name="taskListLayout">
<item>
<layout class="QVBoxLayout" name="taskListsLayout">
<item>
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string>Task Lists</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="taskListsList"/>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="taskListLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string>Task List tasks</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="taskListTasksList"/>
</item>
</layout>
</item>
</layout>
</item>
<item row="3" column="0" colspan="2">
<layout class="QHBoxLayout" name="createTaskLayout">
<item>
<widget class="QLineEdit" name="taskNameEdit">
<property name="enabled">
<bool>false</bool>
</property>
<property name="placeholderText">
<string>Task name</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="createTaskButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Create Task</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
<widget class="QLabel" name="errorLabel">
<property name="geometry">
<rect>
<x>6</x>
<y>409</y>
<width>16</width>
<height>18</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>30</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionQuit"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionQuit">
<property name="text">