diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -76,5 +76,6 @@ add_libkgapi2_test(drive filecreatejobtest) add_libkgapi2_test(drive filesearchquerytest) add_libkgapi2_test(drive teamdrivecreatejobtest) +add_libkgapi2_test(drive teamdrivedeletejobtest) add_libkgapi2_test(drive teamdrivefetchjobtest) add_libkgapi2_test(drive teamdrivesearchquerytest) diff --git a/autotests/drive/data/generic_no_content_response.txt b/autotests/drive/data/generic_no_content_response.txt new file mode 100644 --- /dev/null +++ b/autotests/drive/data/generic_no_content_response.txt @@ -0,0 +1,2 @@ +HTTP/1.1 204 No Content + diff --git a/autotests/drive/data/teamdrive_delete_request.txt b/autotests/drive/data/teamdrive_delete_request.txt new file mode 100644 --- /dev/null +++ b/autotests/drive/data/teamdrive_delete_request.txt @@ -0,0 +1 @@ +DELETE https://www.googleapis.com/drive/v2/teamdrives/somelongid diff --git a/autotests/drive/teamdrivedeletejobtest.cpp b/autotests/drive/teamdrivedeletejobtest.cpp new file mode 100644 --- /dev/null +++ b/autotests/drive/teamdrivedeletejobtest.cpp @@ -0,0 +1,80 @@ +/* + * Deleteright (C) 2019 David Barchiesi + * + * 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 delete of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include +#include + +#include "fakenetworkaccessmanagerfactory.h" +#include "testutils.h" + +#include "types.h" +#include "teamdrivedeletejob.h" +#include "teamdrive.h" +#include "account.h" + +using namespace KGAPI2; + +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(KGAPI2::Drive::TeamdrivePtr) + +class TeamdriveDeleteJobTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase() + { + NetworkAccessManagerFactory::setFactory(new FakeNetworkAccessManagerFactory); + } + + void testDelete_data() + { + QTest::addColumn>("scenarios"); + QTest::addColumn("teamdriveId"); + + QTest::newRow("metadata only") + << QList{ + scenarioFromFile(QFINDTESTDATA("data/teamdrive_delete_request.txt"), + QFINDTESTDATA("data/generic_no_content_response.txt")) + } + << QStringLiteral("somelongid"); + } + + void testDelete() + { + QFETCH(QList, scenarios); + QFETCH(QString, teamdriveId); + + FakeNetworkAccessManagerFactory::get()->setScenarios(scenarios); + + auto account = AccountPtr::create(QStringLiteral("MockAccount"), QStringLiteral("MockToken")); + Drive::TeamdriveDeleteJob *job = new Drive::TeamdriveDeleteJob(teamdriveId, account); + + QVERIFY(execJob(job)); + } +}; + +QTEST_GUILESS_MAIN(TeamdriveDeleteJobTest) + +#include "teamdrivedeletejobtest.moc" + + + + + diff --git a/examples/teamdrive/mainwindow.h b/examples/teamdrive/mainwindow.h --- a/examples/teamdrive/mainwindow.h +++ b/examples/teamdrive/mainwindow.h @@ -81,6 +81,16 @@ */ void fetchTeamdriveList(); + /** + * Deletes the selected Team Drive + */ + void deleteSelectedTeamdrive(); + + /** + * Team Drive was deleted. + */ + void slotTeamdriveDeleteJobFinished(KGAPI2::Job *job); + /** * A specific contact in contact list has been selected. Sends a request * to Google to retrieve full details about the specific contact diff --git a/examples/teamdrive/mainwindow.cpp b/examples/teamdrive/mainwindow.cpp --- a/examples/teamdrive/mainwindow.cpp +++ b/examples/teamdrive/mainwindow.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -48,6 +49,8 @@ this, &MainWindow::createTeamdrive); connect(m_ui->teamdriveListButton, &QAbstractButton::clicked, this, &MainWindow::fetchTeamdriveList); + connect(m_ui->teamdriveSelectedDeleteButton, &QAbstractButton::clicked, + this, &MainWindow::deleteSelectedTeamdrive); connect(m_ui->teamdriveList, &QListWidget::itemSelectionChanged, this, &MainWindow::teamdriveSelected); } @@ -170,9 +173,37 @@ m_ui->teamdriveListButton->setEnabled(true); } +void MainWindow::deleteSelectedTeamdrive() { + const QString teamdrive_id = m_ui->teamdriveList->selectedItems().at(0)->data(Qt::UserRole).toString(); + + KGAPI2::Drive::TeamdriveDeleteJob *deleteJob = new KGAPI2::Drive::TeamdriveDeleteJob(teamdrive_id, m_account, this); + connect(deleteJob, &KGAPI2::Job::finished, + this, &MainWindow::slotTeamdriveDeleteJobFinished); +} + +void MainWindow::slotTeamdriveDeleteJobFinished(KGAPI2::Job *job) +{ + KGAPI2::Drive::TeamdriveDeleteJob *deleteJob = qobject_cast(job); + Q_ASSERT(deleteJob); + deleteJob->deleteLater(); + + if (deleteJob->error() != KGAPI2::NoError) { + m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(deleteJob->errorString())); + m_ui->errorLabel->setVisible(true); + m_ui->teamdriveListButton->setEnabled(true); + return; + } + + fetchTeamdriveList(); +} + void MainWindow::teamdriveSelected() { - if (m_ui->teamdriveList->selectedItems().count() == 0) { + bool hasSelection = (m_ui->teamdriveList->selectedItems().count() != 0); + + m_ui->teamdriveSelectedDeleteButton->setEnabled(hasSelection); + + if (!hasSelection) { m_ui->teamdrivePreview->clear(); return; } diff --git a/examples/teamdrive/ui/main.ui b/examples/teamdrive/ui/main.ui --- a/examples/teamdrive/ui/main.ui +++ b/examples/teamdrive/ui/main.ui @@ -55,14 +55,28 @@ - - - false - - - Get Team Drive list - - + + + + + false + + + Get Team Drive list + + + + + + + false + + + Delete selected Team Drive + + + + diff --git a/src/drive/CMakeLists.txt b/src/drive/CMakeLists.txt --- a/src/drive/CMakeLists.txt +++ b/src/drive/CMakeLists.txt @@ -40,6 +40,7 @@ revisionmodifyjob.cpp teamdrive.cpp teamdrivecreatejob.cpp + teamdrivedeletejob.cpp teamdrivefetchjob.cpp teamdrivesearchquery.cpp user.cpp @@ -89,6 +90,7 @@ RevisionModifyJob Teamdrive TeamdriveCreateJob + TeamdriveDeleteJob TeamdriveFetchJob TeamdriveSearchQuery User diff --git a/src/drive/teamdrivedeletejob.h b/src/drive/teamdrivedeletejob.h new file mode 100644 --- /dev/null +++ b/src/drive/teamdrivedeletejob.h @@ -0,0 +1,64 @@ +/* + * This file is part of LibKGAPI library + * + * Copyright (C) 2019 David Barchiesi + * + * 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 . + */ + + +#ifndef KGAPI2_DRIVETEAMDRIVEDELETEJOB_H +#define KGAPI2_DRIVETEAMDRIVEDELETEJOB_H + +#include "deletejob.h" +#include "kgapidrive_export.h" + +namespace KGAPI2 +{ + +namespace Drive +{ + +class KGAPIDRIVE_EXPORT TeamdriveDeleteJob : public KGAPI2::DeleteJob +{ + Q_OBJECT + + public: + TeamdriveDeleteJob(const QString &teamdriveId, + const AccountPtr &account, QObject *parent = nullptr); + TeamdriveDeleteJob(const QStringList &teamdrivesIds, + const AccountPtr &account, QObject *parent = nullptr); + TeamdriveDeleteJob(const TeamdrivePtr &teamdrive, + const AccountPtr &account, QObject *parent = nullptr); + TeamdriveDeleteJob(const TeamdrivesList &teamdrives, + const AccountPtr &account, QObject *parent = nullptr); + ~TeamdriveDeleteJob() override; + + protected: + void start() override; + + private: + class Private; + QScopedPointer d; + friend class Private; +}; + +} // namespace Drive + +} // namespace KGAPI2 + +#endif // KGAPI2_DRIVETEAMDRIVEDELETEJOB_H diff --git a/src/drive/teamdrivedeletejob.cpp b/src/drive/teamdrivedeletejob.cpp new file mode 100644 --- /dev/null +++ b/src/drive/teamdrivedeletejob.cpp @@ -0,0 +1,91 @@ +/* + * This file is part of LibKGAPI library + * + * Copyright (C) 2019 David Barchiesi + * + * 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 . + */ + + +#include "teamdrivedeletejob.h" +#include "teamdrive.h" +#include "account.h" +#include "driveservice.h" + +#include + +using namespace KGAPI2; +using namespace KGAPI2::Drive; + +class Q_DECL_HIDDEN TeamdriveDeleteJob::Private +{ + public: + QStringList teamdrivesIds; +}; + +TeamdriveDeleteJob::TeamdriveDeleteJob(const QString &teamdriveId, + const AccountPtr &account, QObject *parent): + DeleteJob(account, parent), + d(new Private) +{ + d->teamdrivesIds << teamdriveId; +} + +TeamdriveDeleteJob::TeamdriveDeleteJob(const QStringList &teamdrivesIds, + const AccountPtr &account, QObject *parent): + DeleteJob(account, parent), + d(new Private) +{ + d->teamdrivesIds << teamdrivesIds; +} + +TeamdriveDeleteJob::TeamdriveDeleteJob(const TeamdrivePtr &teamdrive, + const AccountPtr &account, QObject *parent): + DeleteJob(account, parent), + d(new Private) +{ + d->teamdrivesIds << teamdrive->id(); +} + +TeamdriveDeleteJob::TeamdriveDeleteJob(const TeamdrivesList &teamdrives, + const AccountPtr &account, QObject *parent): + DeleteJob(account, parent), + d(new Private) +{ + for (const TeamdrivePtr & teamdrive : qAsConst(teamdrives)) { + d->teamdrivesIds << teamdrive->id(); + } +} + +TeamdriveDeleteJob::~TeamdriveDeleteJob() = default; + +void TeamdriveDeleteJob::start() +{ + if (d->teamdrivesIds.isEmpty()) { + emitFinished(); + return; + } + + const QString teamdriveId = d->teamdrivesIds.takeFirst(); + const QUrl url = DriveService::fetchTeamdriveUrl(teamdriveId); + QNetworkRequest request(url); + request.setRawHeader("Authorization", "Bearer " + account()->accessToken().toLatin1()); + + enqueueRequest(request); +} + +