Verified Commit 6b5e362e authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

Modernize the examples

parent 93fac16d
Pipeline #93384 passed with stage
in 4 minutes and 17 seconds
......@@ -4,4 +4,6 @@ add_subdirectory(teamdrive)
add_subdirectory(permissions)
add_subdirectory(tasks)
add_subdirectory(files)
# Deprecated
#add_subdirectory(staticmaps)
......@@ -17,153 +17,129 @@
#include <QListWidgetItem>
MainWindow::MainWindow(QWidget * parent):
QMainWindow(parent),
m_ui(new Ui::MainWindow)
QMainWindow(parent)
{
/* Initialize GUI */
m_ui->setupUi(this);
m_ui->errorLabel->setVisible(false);
connect(m_ui->authButton, &QAbstractButton::clicked,
ui.setupUi(this);
ui.errorLabel->setVisible(false);
connect(ui.authButton, &QAbstractButton::clicked,
this, &MainWindow::authenticate);
connect(m_ui->contactListButton, &QAbstractButton::clicked,
connect(ui.contactListButton, &QAbstractButton::clicked,
this, &MainWindow::fetchContactList);
connect(m_ui->contactList, &QListWidget::itemSelectionChanged,
connect(ui.contactList, &QListWidget::itemSelectionChanged,
this, &MainWindow::contactSelected);
}
MainWindow::~MainWindow()
{
delete m_ui;
}
void MainWindow::authenticate()
{
KGAPI2::AccountPtr account(new KGAPI2::Account);
account->setScopes( QList<QUrl>() << KGAPI2::Account::contactsScopeUrl() );
auto account = KGAPI2::AccountPtr::create();
account->setScopes({KGAPI2::Account::contactsScopeUrl()});
/* Create AuthJob to retrieve OAuth tokens for the account */
KGAPI2::AuthJob *authJob = new KGAPI2::AuthJob(
auto *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();
m_ui->authStatusLabel->setText(QStringLiteral("Authenticated"));
m_ui->contactListButton->setEnabled(true);
m_ui->authButton->setEnabled(false);
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"));
ui.contactListButton->setEnabled(true);
ui.authButton->setEnabled(false);
});
}
void MainWindow::fetchContactList()
{
if (m_account.isNull()) {
m_ui->errorLabel->setText(QStringLiteral("Error: Please authenticate first"));
m_ui->errorLabel->setVisible(true);
m_ui->authButton->setVisible(true);
ui.errorLabel->setText(QStringLiteral("Error: Please authenticate first"));
ui.errorLabel->setVisible(true);
ui.authButton->setVisible(true);
return;
}
KGAPI2::ContactFetchJob *fetchJob = new KGAPI2::ContactFetchJob(m_account, this);
connect(fetchJob, &KGAPI2::Job::finished,
this, &MainWindow::slotFetchJobFinished);
m_ui->contactListButton->setEnabled(false);
}
auto *fetchJob = new KGAPI2::ContactFetchJob(m_account, this);
connect(fetchJob, &KGAPI2::Job::finished, this,
[this, fetchJob]() {
fetchJob->deleteLater();
void MainWindow::slotFetchJobFinished(KGAPI2::Job *job)
{
KGAPI2::ContactFetchJob *fetchJob = qobject_cast<KGAPI2::ContactFetchJob*>(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);
m_ui->contactListButton->setEnabled(true);
return;
}
if (fetchJob->error() != KGAPI2::NoError) {
ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
ui.errorLabel->setVisible(true);
ui.contactListButton->setEnabled(true);
return;
}
/* Get all items the job has retrieved */
const KGAPI2::ObjectsList objects = fetchJob->items();
/* Get all items the job has retrieved */
const auto objects = fetchJob->items();
for (const auto &object : objects) {
const auto contact = object.dynamicCast<KGAPI2::Contact>();
for (const KGAPI2::ObjectPtr &object : objects) {
const KGAPI2::ContactPtr contact = object.dynamicCast<KGAPI2::Contact>();
/* Convert the contact to QListWidget item */
auto *item = new QListWidgetItem(ui.contactList);
item->setText(contact->name());
item->setData(Qt::UserRole, contact->uid());
ui.contactList->addItem(item);
}
/* Convert the contact to QListWidget item */
QListWidgetItem *item = new QListWidgetItem(m_ui->contactList);
item->setText(contact->name());
item->setData(Qt::UserRole, contact->uid());
ui.contactListButton->setEnabled(true);
});
m_ui->contactList->addItem(item);
}
m_ui->contactListButton->setEnabled(true);
ui.contactListButton->setEnabled(false);
}
void MainWindow::contactSelected()
{
if (m_ui->contactList->selectedItems().count() == 0) {
m_ui->contactInfo->clear();
if (ui.contactList->selectedItems().count() == 0) {
ui.contactInfo->clear();
return;
}
const QString id = m_ui->contactList->selectedItems().at(0)->data(Qt::UserRole).toString();
KGAPI2::ContactFetchJob *fetchJob = new KGAPI2::ContactFetchJob(id, m_account);
connect(fetchJob, &KGAPI2::Job::finished,
this, &MainWindow::slotContactFetchJobFinished);
const auto id = ui.contactList->selectedItems().at(0)->data(Qt::UserRole).toString();
auto *fetchJob = new KGAPI2::ContactFetchJob(id, m_account);
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);
ui.contactListButton->setEnabled(true);
return;
}
/* Get all items we have received from Google (should be just one) */
const auto objects = fetchJob->items();
if (objects.count() != 1) {
ui.errorLabel->setText(QStringLiteral("Error: Server sent unexpected amount of contacts"));
ui.errorLabel->setVisible(true);
return;
}
const auto contact = objects.first().dynamicCast<KGAPI2::Contact>();
QString text = QStringLiteral("Name: %1").arg(contact->name());
if (!contact->phoneNumbers().isEmpty()) {
text += QLatin1Char('\n') % QStringLiteral("Phone: %1").arg(contact->phoneNumbers().at(0).number());
}
if (!contact->emails().isEmpty()) {
text += QLatin1Char('\n') % QStringLiteral("Email: %1").arg(contact->emails().at(0));
}
ui.contactInfo->setText(text);
});
}
void MainWindow::slotContactFetchJobFinished(KGAPI2::Job *job)
{
KGAPI2::ContactFetchJob *fetchJob = qobject_cast<KGAPI2::ContactFetchJob*>(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);
m_ui->contactListButton->setEnabled(true);
return;
}
/* Get all items we have received from Google (should be just one) */
KGAPI2::ObjectsList objects = fetchJob->items();
if (objects.count() != 1) {
m_ui->errorLabel->setText(QStringLiteral("Error: Server sent unexpected amount of contacts"));
m_ui->errorLabel->setVisible(true);
return;
}
KGAPI2::ContactPtr contact = objects.first().dynamicCast<KGAPI2::Contact>();
QString text = QStringLiteral("Name: %1").arg(contact->name());
if (!contact->phoneNumbers().isEmpty()) {
text += QLatin1Char('\n') % QStringLiteral("Phone: %1").arg(contact->phoneNumbers().at(0).number());
}
if (!contact->emails().isEmpty()) {
text += QLatin1Char('\n') % QStringLiteral("Email: %1").arg(contact->emails().at(0));
}
m_ui->contactInfo->setText(text);
}
......@@ -9,25 +9,20 @@
#pragma once
#include <QMainWindow>
#include "ui_mainwindow.h"
#include <types.h>
namespace Ui {
class MainWindow;
}
#include "core/types.h"
namespace KGAPI2 {
class Job;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
private Q_SLOTS:
/**
......@@ -36,21 +31,6 @@ class MainWindow : public QMainWindow
*/
void authenticate();
/**
* Authentication has finished
*/
void slotAuthJobFinished(KGAPI2::Job *job);
/**
* All contacts were fetched.
*/
void slotFetchJobFinished(KGAPI2::Job *job);
/**
* Contact details were fetched.
*/
void slotContactFetchJobFinished(KGAPI2::Job *job);
/**
* Retrieves list of all contacts from user's Google Contacts
* addressbook
......@@ -64,7 +44,7 @@ class MainWindow : public QMainWindow
void contactSelected();
private:
Ui::MainWindow *m_ui;
Ui::MainWindow ui;
KGAPI2::AccountPtr m_account;
......
......@@ -150,7 +150,7 @@
<connections>
<connection>
<sender>actionQuit</sender>
<signal>activated()</signal>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>close()</slot>
<hints>
......
This diff is collapsed.
......@@ -8,58 +8,38 @@
#pragma once
#include <QMainWindow>
#include "ui_mainwindow.h"
#include <types.h>
namespace Ui {
class MainWindow;
}
#include "core/types.h"
namespace KGAPI2 {
class Job;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
private Q_SLOTS:
private Q_SLOTS:
/**
* Retrieves tokens from Google that we will use to authenticate
* further requests
*/
void authenticate();
/**
* Authentication has finished
*/
void slotAuthJobFinished(KGAPI2::Job *job);
/**
* Creates the Drive with name in newDrivesEdit
*/
void createDrives();
/**
* Drive was created.
*/
void slotDrivesCreateJobFinished(KGAPI2::Job *job);
/**
* Rename the selected Drive with name in renameDrivesEdit
*/
void renameSelectedDrives();
/**
* Drive was modified.
*/
void slotDrivesModifyJobFinished(KGAPI2::Job *job);
/**
* Hide the selected Drive
*/
......@@ -75,16 +55,6 @@ class MainWindow : public QMainWindow
*/
void slotDrivesHideJobFinished(KGAPI2::Job *job);
/**
* All Drives were fetched.
*/
void slotFetchJobFinished(KGAPI2::Job *job);
/**
* Drive listing was fetched.
*/
void slotDrivesFetchJobFinished(KGAPI2::Job *job);
/**
* Retrieves list of all drives from user's Google drives
*/
......@@ -95,11 +65,6 @@ class MainWindow : public QMainWindow
*/
void deleteSelectedDrives();
/**
* Drive was deleted.
*/
void slotDrivesDeleteJobFinished(KGAPI2::Job *job);
/**
* A specific team drive in drives list has been selected. Sends a request
* to Google to retrieve the team drive file list.
......@@ -112,13 +77,8 @@ class MainWindow : public QMainWindow
*/
void drivesItemSelected();
/**
* Drive item detail was fetched.
*/
void slotDrivesItemFetchJobFinished(KGAPI2::Job *job);
private:
Ui::MainWindow *m_ui;
private:
Ui::MainWindow ui;
KGAPI2::AccountPtr m_account;
......
......@@ -237,7 +237,7 @@
<connections>
<connection>
<sender>actionQuit</sender>
<signal>activated()</signal>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>close()</slot>
<hints>
......
......@@ -16,108 +16,94 @@
#include <QDebug>
MainWindow::MainWindow(QWidget * parent):
QMainWindow(parent),
m_ui(new Ui::MainWindow)
QMainWindow(parent)
{
/* Initialize GUI */
m_ui->setupUi(this);
connect(m_ui->authButton, &QAbstractButton::clicked,
this, &MainWindow::authenticate);
connect(m_ui->browseButton, &QAbstractButton::clicked,
this, &MainWindow::browseFiles);
connect(m_ui->uploadButton, &QAbstractButton::clicked,
this, &MainWindow::uploadFile);
ui.setupUi(this);
connect(ui.authButton, &QAbstractButton::clicked, this, &MainWindow::authenticate);
connect(ui.browseButton, &QAbstractButton::clicked, this, &MainWindow::browseFiles);
connect(ui.uploadButton, &QAbstractButton::clicked, this, &MainWindow::uploadFile);
setInputsEnabled(false);
}
MainWindow::~MainWindow()
{
delete m_ui;
}
void MainWindow::authenticate()
{
KGAPI2::AccountPtr account(new KGAPI2::Account);
account->setScopes( QList<QUrl>() << KGAPI2::Account::driveScopeUrl() );
auto account = KGAPI2::AccountPtr::create();
account->setScopes({KGAPI2::Account::driveScopeUrl()});
/* Create AuthJob to retrieve OAuth tokens for the account */
KGAPI2::AuthJob *authJob = new KGAPI2::AuthJob(
auto *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->statusbar->showMessage(QStringLiteral("Error: %1").arg(authJob->errorString()));
return;
}
m_account = authJob->account();
m_ui->authStatusLabel->setText(QStringLiteral("Authenticated"));
m_ui->authButton->setEnabled(false);
setInputsEnabled(true);
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.statusbar->showMessage(QStringLiteral("Error: %1").arg(authJob->errorString()));
return;
}
m_account = authJob->account();
ui.authStatusLabel->setText(QStringLiteral("Authenticated"));
ui.authButton->setEnabled(false);
setInputsEnabled(true);
});
}
void MainWindow::browseFiles()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Select file"));
m_ui->sourceLineEdit->setText(fileName);
const auto fileName = QFileDialog::getOpenFileName(this, tr("Select file"));
ui.sourceLineEdit->setText(fileName);
}
void MainWindow::uploadFile()
{
if (m_ui->sourceLineEdit->text().isEmpty()) {
m_ui->statusbar->showMessage(QStringLiteral("Error: must specify source file."));
if (ui.sourceLineEdit->text().isEmpty()) {
ui.statusbar->showMessage(QStringLiteral("Error: must specify source file."));
return;
}
uploadingFile = new QFile(m_ui->sourceLineEdit->text());
if (!uploadingFile->open(QIODevice::ReadOnly)) {
m_ui->statusbar->showMessage(QStringLiteral("Error: source file not valid."));
m_uploadingFile = std::make_unique<QFile>(ui.sourceLineEdit->text());
if (!m_uploadingFile->open(QIODevice::ReadOnly)) {
ui.statusbar->showMessage(QStringLiteral("Error: source file not valid."));
return;
}
KGAPI2::Drive::FilePtr uploadFile = KGAPI2::Drive::FilePtr::create();
QFileInfo fileInfo(uploadingFile->fileName());
auto uploadFile = KGAPI2::Drive::FilePtr::create();
QFileInfo fileInfo(m_uploadingFile->fileName());
uploadFile->setTitle(fileInfo.fileName());
KGAPI2::Drive::FileResumableCreateJob *fileCreateJob;
if (m_ui->writeOnSignalRadioButton->isChecked()) {
KGAPI2::Drive::FileResumableCreateJob *fileCreateJob = nullptr;
if (ui.writeOnSignalRadioButton->isChecked()) {
fileCreateJob = new KGAPI2::Drive::FileResumableCreateJob(uploadFile, m_account, this);
connect(fileCreateJob, &KGAPI2::Drive::FileResumableCreateJob::readyWrite,
this, &MainWindow::slotFileCreateJobReadyWrite);
} else {
fileCreateJob = new KGAPI2::Drive::FileResumableCreateJob(uploadingFile, uploadFile, m_account, this);
fileCreateJob = new KGAPI2::Drive::FileResumableCreateJob(m_uploadingFile.get(), uploadFile, m_account, this);
}
fileCreateJob->setUploadSize(uploadingFile->size());
fileCreateJob->setUploadSize(m_uploadingFile->size());
connect(fileCreateJob, &KGAPI2::Drive::FileResumableCreateJob::finished,
this, &MainWindow::slotFileCreateJobFinished);
connect(fileCreateJob, &KGAPI2::Drive::FileResumableCreateJob::progress,
this, &MainWindow::slotFileCreateJobProgress);
m_ui->statusbar->clearMessage();
bytesUploaded = 0;
if (m_ui->writeOnSignalRadioButton->isChecked()) {
fileUploadProgressBar = new QProgressBar(m_ui->statusbar);
fileUploadProgressBar->setFormat(QStringLiteral("Sent to Job: %v bytes [%p%]"));
fileUploadProgressBar->setMaximum(uploadingFile->size());
m_ui->statusbar->addWidget(fileUploadProgressBar);
ui.statusbar->clearMessage();
m_bytesUploaded = 0;
if (ui.writeOnSignalRadioButton->isChecked()) {
m_fileUploadProgressBar = std::make_unique<QProgressBar>(ui.statusbar);
m_fileUploadProgressBar->setFormat(QStringLiteral("Sent to Job: %v bytes [%p%]"));
m_fileUploadProgressBar->setMaximum(m_uploadingFile->size());
ui.statusbar->addWidget(m_fileUploadProgressBar.get());
}
jobUploadProgressBar = new QProgressBar(m_ui->statusbar);
jobUploadProgressBar->setFormat(QStringLiteral("Job progress: %v bytes [%p%]"));
m_ui->statusbar->addWidget(jobUploadProgressBar);
m_jobUploadProgressBar = std::make_unique<QProgressBar>(ui.statusbar);
m_jobUploadProgressBar->setFormat(QStringLiteral("Job progress: %v bytes [%p%]"));
ui.statusbar->addWidget(m_jobUploadProgressBar.get());
setInputsEnabled(false);
}
......@@ -126,24 +112,19 @@ void MainWindow::slotFileCreateJobFinished(KGAPI2::Job *job)
{
qDebug() << "Completed job" << job << "error code:" << job->error() << "- message:" << job->errorString();