diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 9502dae..2d2831f 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,79 +1,80 @@ include(ECMAddTests) find_package(Qt5Test CONFIG REQUIRED) set(kgapitest_SRCS fakeaccountstorage.cpp fakeauthwidget.cpp fakenetworkreply.cpp fakenetworkaccessmanager.cpp fakenetworkaccessmanagerfactory.cpp testutils.cpp ) add_library(kgapitest STATIC ${kgapitest_SRCS}) target_link_libraries(kgapitest Qt5::Core Qt5::Network Qt5::Test KPimGAPICore) macro(add_libkgapi2_test _module _testname) set(testSources ${_module}/${_testname}.cpp) string(SUBSTRING ${_module} 0 1 moduleFirst) string(SUBSTRING ${_module} 1 -1 moduleLast) string(TOUPPER ${moduleFirst} moduleFirst) string(CONCAT moduleName ${moduleFirst} ${moduleLast}) set(extraLibs "KPimGAPI${moduleName}") set(utilsFile ${CMAKE_CURRENT_SOURCE_DIR}/${_module}/${_module}testutils.cpp) if (EXISTS ${utilsFile}) list(APPEND testSources ${utilsFile}) endif() ecm_add_test(${testSources} LINK_LIBRARIES kgapitest KPimGAPICore ${extraLibs} TEST_NAME ${_testname} NAME_PREFIX ${_module}- ) endmacro(add_libkgapi2_test) ecm_add_test(fakenamtest.cpp LINK_LIBRARIES kgapitest KPimGAPICore TEST_NAME fakenamtest NAME_PREFIX fake-) add_libkgapi2_test(core accountinfofetchjobtest) add_libkgapi2_test(core accountmanagertest) add_libkgapi2_test(core createjobtest) add_libkgapi2_test(core fetchjobtest) add_libkgapi2_test(contacts contactcreatejobtest) add_libkgapi2_test(contacts contactdeletejobtest) add_libkgapi2_test(contacts contactfetchjobtest) add_libkgapi2_test(contacts contactfetchphotojobtest) add_libkgapi2_test(contacts contactmodifyjobtest) add_libkgapi2_test(contacts contactsgroupcreatejobtest) add_libkgapi2_test(contacts contactsgroupdeletejobtest) add_libkgapi2_test(contacts contactsgroupfetchjobtest) add_libkgapi2_test(contacts contactsgroupmodifyjobtest) add_libkgapi2_test(calendar calendarcreatejobtest) add_libkgapi2_test(calendar calendardeletejobtest) add_libkgapi2_test(calendar calendarfetchjobtest) add_libkgapi2_test(calendar calendarmodifyjobtest) add_libkgapi2_test(calendar eventcreatejobtest) add_libkgapi2_test(calendar eventdeletejobtest) add_libkgapi2_test(calendar eventfetchjobtest) add_libkgapi2_test(calendar eventmodifyjobtest) add_libkgapi2_test(calendar freebusyqueryjobtest) add_libkgapi2_test(tasks taskcreatejobtest) add_libkgapi2_test(tasks taskdeletejobtest) add_libkgapi2_test(tasks taskfetchjobtest) add_libkgapi2_test(tasks taskmodifyjobtest) add_libkgapi2_test(tasks taskmovejobtest) add_libkgapi2_test(tasks tasklistcreatejobtest) add_libkgapi2_test(tasks tasklistdeletejobtest) add_libkgapi2_test(tasks tasklistfetchjobtest) add_libkgapi2_test(tasks tasklistmodifyjobtest) add_libkgapi2_test(drive aboutfetchjobtest) add_libkgapi2_test(drive changefetchjobtest) add_libkgapi2_test(drive filecopyjobtest) add_libkgapi2_test(drive filecreatejobtest) add_libkgapi2_test(drive filesearchquerytest) +add_libkgapi2_test(drive teamdrivecreatejobtest) add_libkgapi2_test(drive teamdrivefetchjobtest) add_libkgapi2_test(drive teamdrivesearchquerytest) diff --git a/autotests/drive/data/teamdrive_create_request.txt b/autotests/drive/data/teamdrive_create_request.txt new file mode 100644 index 0000000..d6f1eb5 --- /dev/null +++ b/autotests/drive/data/teamdrive_create_request.txt @@ -0,0 +1,7 @@ +POST https://www.googleapis.com/drive/v2/teamdrives?requestId=MockRequestId + +{ + "kind": "drive#teamDrive", + "id": "0APqEW6eViiMXUk9PVA", + "name": "First Team Drive" +} diff --git a/autotests/drive/data/teamdrive_create_response.txt b/autotests/drive/data/teamdrive_create_response.txt new file mode 100644 index 0000000..1aa9987 --- /dev/null +++ b/autotests/drive/data/teamdrive_create_response.txt @@ -0,0 +1,8 @@ +HTTP/1.1 200 OK +content-type: application/json; charset=UTF-8 + +{ + "kind": "drive#teamDrive", + "id": "0APqEW6eViiMXUk9PVA", + "name": "First Team Drive" +} diff --git a/autotests/drive/teamdrivecreatejobtest.cpp b/autotests/drive/teamdrivecreatejobtest.cpp new file mode 100644 index 0000000..4ce2c7e --- /dev/null +++ b/autotests/drive/teamdrivecreatejobtest.cpp @@ -0,0 +1,92 @@ +/* + * Createright (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 create of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include +#include + +#include "fakenetworkaccessmanagerfactory.h" +#include "testutils.h" +#include "drivetestutils.h" + +#include "types.h" +#include "teamdrivecreatejob.h" +#include "teamdrive.h" +#include "account.h" + +using namespace KGAPI2; + +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(KGAPI2::Drive::TeamdrivePtr) + +class TeamdriveCreateJobTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase() + { + NetworkAccessManagerFactory::setFactory(new FakeNetworkAccessManagerFactory); + } + + void testCreate_data() + { + QTest::addColumn>("scenarios"); + QTest::addColumn("sourceTeamdrive"); + QTest::addColumn("requestId"); + QTest::addColumn("expectedResult"); + + QTest::newRow("metadata only") + << QList{ + scenarioFromFile(QFINDTESTDATA("data/teamdrive_create_request.txt"), + QFINDTESTDATA("data/teamdrive_create_response.txt")) + } + << teamdriveFromFile(QFINDTESTDATA("data/teamdrive.json")) + << QStringLiteral("MockRequestId") + << teamdriveFromFile(QFINDTESTDATA("data/teamdrive.json")); + } + + void testCreate() + { + QFETCH(QList, scenarios); + QFETCH(Drive::TeamdrivePtr, sourceTeamdrive); + QFETCH(QString, requestId); + QFETCH(Drive::TeamdrivePtr, expectedResult); + + FakeNetworkAccessManagerFactory::get()->setScenarios(scenarios); + + auto account = AccountPtr::create(QStringLiteral("MockAccount"), QStringLiteral("MockToken")); + Drive::TeamdriveCreateJob *job = new Drive::TeamdriveCreateJob(requestId, sourceTeamdrive, account); + + QVERIFY(execJob(job)); + const auto items = job->items(); + QCOMPARE(items.count(), 1); + QVERIFY(*items.cbegin()); + QCOMPARE(**items.cbegin(), *expectedResult); + QCOMPARE(requestId, job->requestId()); + } +}; + +QTEST_GUILESS_MAIN(TeamdriveCreateJobTest) + +#include "teamdrivecreatejobtest.moc" + + + + + diff --git a/examples/teamdrive/mainwindow.cpp b/examples/teamdrive/mainwindow.cpp index 3d9a04a..ff1a907 100644 --- a/examples/teamdrive/mainwindow.cpp +++ b/examples/teamdrive/mainwindow.cpp @@ -1,187 +1,220 @@ /* 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 "mainwindow.h" #include "ui_main.h" #include +#include #include #include #include #include #include #include #include #include +#include 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->newTeamdriveButton, &QAbstractButton::clicked, + this, &MainWindow::createTeamdrive); connect(m_ui->teamdriveListButton, &QAbstractButton::clicked, this, &MainWindow::fetchTeamdriveList); connect(m_ui->teamdriveList, &QListWidget::itemSelectionChanged, this, &MainWindow::teamdriveSelected); } MainWindow::~MainWindow() { delete m_ui; } void MainWindow::authenticate() { KGAPI2::AccountPtr account(new KGAPI2::Account); account->setScopes( QList() << KGAPI2::Account::driveScopeUrl() ); /* 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(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->teamdriveListButton->setEnabled(true); m_ui->authButton->setEnabled(false); } +void MainWindow::createTeamdrive() +{ + QString teamdriveName = m_ui->newTeamdriveEdit->text(); + if (teamdriveName.isEmpty()) { + return; + } + QString requestId = QUuid::createUuid().toString(); + + KGAPI2::Drive::TeamdrivePtr teamdrive = KGAPI2::Drive::TeamdrivePtr::create(); + teamdrive->setName(teamdriveName); + KGAPI2::Drive::TeamdriveCreateJob *createJob = new KGAPI2::Drive::TeamdriveCreateJob(requestId, teamdrive, m_account, this); + connect(createJob, &KGAPI2::Job::finished, + this, &MainWindow::slotTeamdriveCreateJobFinished); +} + +void MainWindow::slotTeamdriveCreateJobFinished(KGAPI2::Job *job) +{ + KGAPI2::Drive::TeamdriveCreateJob *createJob = qobject_cast(job); + Q_ASSERT(createJob); + createJob->deleteLater(); + + if (createJob->error() != KGAPI2::NoError) { + m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(createJob->errorString())); + m_ui->errorLabel->setVisible(true); + m_ui->teamdriveListButton->setEnabled(true); + return; + } + + m_ui->newTeamdriveEdit->clear(); + fetchTeamdriveList(); +} + void MainWindow::fetchTeamdriveList() { 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::Drive::TeamdriveSearchQuery query; - query.addQuery(KGAPI2::Drive::TeamdriveSearchQuery::MemberCount, KGAPI2::Drive::TeamdriveSearchQuery::Less, 10); - - KGAPI2::Drive::TeamdriveFetchJob *fetchJob = new KGAPI2::Drive::TeamdriveFetchJob(query, m_account, this); + KGAPI2::Drive::TeamdriveFetchJob *fetchJob = new KGAPI2::Drive::TeamdriveFetchJob(m_account, this); connect(fetchJob, &KGAPI2::Job::finished, this, &MainWindow::slotFetchJobFinished); m_ui->teamdriveListButton->setEnabled(false); } void MainWindow::slotFetchJobFinished(KGAPI2::Job *job) { KGAPI2::Drive::TeamdriveFetchJob *fetchJob = qobject_cast(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->teamdriveListButton->setEnabled(true); return; } /* Get all items the job has retrieved */ const KGAPI2::ObjectsList objects = fetchJob->items(); - + m_ui->teamdriveList->clear(); for (const KGAPI2::ObjectPtr &object : objects) { const KGAPI2::Drive::TeamdrivePtr teamdrive = object.dynamicCast(); /* Convert the teamdrive to QListWidget item */ QListWidgetItem *item = new QListWidgetItem(m_ui->teamdriveList); item->setText(teamdrive->name()); item->setData(Qt::UserRole, teamdrive->id()); m_ui->teamdriveList->addItem(item); } m_ui->teamdriveListButton->setEnabled(true); } void MainWindow::teamdriveSelected() { if (m_ui->teamdriveList->selectedItems().count() == 0) { m_ui->teamdrivePreview->clear(); return; } const QString id = m_ui->teamdriveList->selectedItems().at(0)->data(Qt::UserRole).toString(); KGAPI2::Drive::FileSearchQuery query; query.addQuery(KGAPI2::Drive::FileSearchQuery::Trashed, KGAPI2::Drive::FileSearchQuery::Equals, false); query.addQuery(KGAPI2::Drive::FileSearchQuery::Parents, KGAPI2::Drive::FileSearchQuery::In, id); KGAPI2::Drive::FileFetchJob *fileFetchJob = new KGAPI2::Drive::FileFetchJob(query, m_account, nullptr); fileFetchJob->setIncludeTeamDriveItems(true); fileFetchJob->setFields((KGAPI2::Drive::FileFetchJob::BasicFields & ~KGAPI2::Drive::FileFetchJob::Permissions) | KGAPI2::Drive::FileFetchJob::Labels | KGAPI2::Drive::FileFetchJob::ExportLinks | KGAPI2::Drive::FileFetchJob::LastViewedByMeDate); connect(fileFetchJob, &KGAPI2::Job::finished, this, &MainWindow::slotTeamdriveFetchJobFinished); } void MainWindow::slotTeamdriveFetchJobFinished(KGAPI2::Job *job) { KGAPI2::Drive::FileFetchJob *fetchJob = qobject_cast(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->teamdriveListButton->setEnabled(true); return; } /* Get all items we have received from Google (should be just one) */ KGAPI2::ObjectsList objects = fetchJob->items(); QString text; Q_FOREACH (const KGAPI2::ObjectPtr &object, objects) { const KGAPI2::Drive::FilePtr file = object.dynamicCast(); text += QStringLiteral("%1").arg(file->title()); text += QLatin1Char('\n'); } m_ui->teamdrivePreview->setText(text); } diff --git a/examples/teamdrive/mainwindow.h b/examples/teamdrive/mainwindow.h index d25fdcf..ac3c49d 100644 --- a/examples/teamdrive/mainwindow.h +++ b/examples/teamdrive/mainwindow.h @@ -1,87 +1,97 @@ /* 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 MAINWINDOW_H #define MAINWINDOW_H #include #include 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); + /** + * Creates the Team Drive with name in newTeamdriveEdit + */ + void createTeamdrive(); + + /** + * Team Drive was created. + */ + void slotTeamdriveCreateJobFinished(KGAPI2::Job *job); + /** * All Team Drives were fetched. */ void slotFetchJobFinished(KGAPI2::Job *job); /** * Team Drive listing was fetched. */ void slotTeamdriveFetchJobFinished(KGAPI2::Job *job); /** * Retrieves list of all teamdrive from user's Google teamdrive * addressbook */ void fetchTeamdriveList(); /** * A specific contact in contact list has been selected. Sends a request * to Google to retrieve full details about the specific contact */ void teamdriveSelected(); private: Ui::MainWindow *m_ui; KGAPI2::AccountPtr m_account; }; #endif // MAINWINDOW_H diff --git a/examples/teamdrive/ui/main.ui b/examples/teamdrive/ui/main.ui index 3e9a19f..3f21528 100644 --- a/examples/teamdrive/ui/main.ui +++ b/examples/teamdrive/ui/main.ui @@ -1,168 +1,180 @@ MainWindow 0 0 640 - 413 + 486 MainWindow Authenticate Not authenticated Qt::Horizontal + + + + + + + + + Create + + + + + false Get Team Drive list 12 75 true Qt::RightToLeft Team Drive list Qt::AlignCenter 12 75 true Qt::RightToLeft Team Drive folder preview Qt::AlignCenter true 0 0 640 - 25 + 30 File Quit - - actionQuit activated() MainWindow close() -1 -1 319 206 diff --git a/src/drive/CMakeLists.txt b/src/drive/CMakeLists.txt index 64d46a8..407ded6 100644 --- a/src/drive/CMakeLists.txt +++ b/src/drive/CMakeLists.txt @@ -1,148 +1,150 @@ set(kgapidrive_SRCS about.cpp aboutfetchjob.cpp app.cpp appfetchjob.cpp change.cpp changefetchjob.cpp childreference.cpp childreferencecreatejob.cpp childreferencedeletejob.cpp childreferencefetchjob.cpp driveservice.cpp searchquery.cpp file.cpp fileabstractdatajob.cpp fileabstractmodifyjob.cpp fileabstractuploadjob.cpp filecopyjob.cpp filecreatejob.cpp filedeletejob.cpp filefetchcontentjob.cpp filefetchjob.cpp filemodifyjob.cpp filesearchquery.cpp filetouchjob.cpp filetrashjob.cpp fileuntrashjob.cpp parentreference.cpp parentreferencecreatejob.cpp parentreferencedeletejob.cpp parentreferencefetchjob.cpp permission.cpp permissioncreatejob.cpp permissiondeletejob.cpp permissionfetchjob.cpp permissionmodifyjob.cpp revision.cpp revisiondeletejob.cpp revisionfetchjob.cpp revisionmodifyjob.cpp teamdrive.cpp + teamdrivecreatejob.cpp teamdrivefetchjob.cpp teamdrivesearchquery.cpp user.cpp ../debug.cpp ) ecm_generate_headers(kgapidrive_CamelCase_HEADERS HEADER_NAMES About AboutFetchJob App AppFetchJob Change ChangeFetchJob ChildReference ChildReferenceCreateJob ChildReferenceDeleteJob ChildReferenceFetchJob SearchQuery File FileAbstractDataJob FileAbstractModifyJob FileAbstractUploadJob FileCopyJob FileCreateJob FileDeleteJob FileFetchContentJob FileFetchJob FileModifyJob FileSearchQuery FileTouchJob FileTrashJob FileUntrashJob ParentReference ParentReferenceCreateJob ParentReferenceDeleteJob ParentReferenceFetchJob Permission PermissionCreateJob PermissionDeleteJob PermissionFetchJob PermissionModifyJob Revision RevisionDeleteJob RevisionFetchJob RevisionModifyJob Teamdrive + TeamdriveCreateJob TeamdriveFetchJob TeamdriveSearchQuery User PREFIX KGAPI/Drive REQUIRED_HEADERS kgapidrive_HEADERS ) add_library(KPimGAPIDrive ${kgapidrive_SRCS} ) generate_export_header(KPimGAPIDrive BASE_NAME kgapidrive) add_library(KPim::GAPIDrive ALIAS KPimGAPIDrive) # TODO: Backwards compatibility, remove in next major release add_library(KF5::GAPIDrive ALIAS KPimGAPIDrive) target_include_directories(KPimGAPIDrive INTERFACE "$") target_link_libraries(KPimGAPIDrive PUBLIC KPim::GAPICore PRIVATE Qt5::Network ) set_target_properties(KPimGAPIDrive PROPERTIES VERSION ${KGAPI_VERSION_STRING} SOVERSION ${KGAPI_SOVERSION} EXPORT_NAME GAPIDrive ) install(TARGETS KPimGAPIDrive EXPORT KPimGAPITargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} ) install(FILES ${kgapidrive_CamelCase_HEADERS} DESTINATION "${KDE_INSTALL_INCLUDEDIR}/KPim/KGAPI/KGAPI/Drive" COMPONENT Devel ) install(FILES ${kgapidrive_HEADERS} "${CMAKE_CURRENT_BINARY_DIR}/kgapidrive_export.h" DESTINATION "${KDE_INSTALL_INCLUDEDIR}/KPim/KGAPI/kgapi/drive" COMPONENT Devel ) ecm_generate_pri_file(BASE_NAME KGAPIDrive LIB_NAME KPimGAPIDrive DEPS "GAPICore" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR "${KDE_INSTALL_INCLUDEDIR}/KPim/KGAPI" ) install(FILES "${PRI_FILENAME}" DESTINATION "${ECM_MKSPECS_INSTALL_DIR}" ) diff --git a/src/drive/teamdrive.cpp b/src/drive/teamdrive.cpp index b72a5bc..7a63f6f 100644 --- a/src/drive/teamdrive.cpp +++ b/src/drive/teamdrive.cpp @@ -1,583 +1,627 @@ /* 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 "driveservice.h" #include "teamdrive.h" #include "utils_p.h" #include #include #include namespace { + static const QString ApiKind = QStringLiteral("drive#teamDrive"); + static const QString ApiKindList = QStringLiteral("drive#teamDriveList"); static const QString AdminManagedRestrictionsAttr = QStringLiteral("adminManagedRestrictions"); static const QString BackgroundImageFileAttr = QStringLiteral("backgroundImageFile"); static const QString BackgroundImageLinkAttr = QStringLiteral("backgroundImageLink"); static const QString CanAddChildrenAttr = QStringLiteral("canAddChildren"); static const QString CanChangeCopyRequiresWriterPermissionRestrictionAttr = QStringLiteral("canChangeCopyRequiresWriterPermissionRestriction"); static const QString CanChangeDomainUsersOnlyRestrictionAttr = QStringLiteral("canChangeDomainUsersOnlyRestriction"); static const QString CanChangeTeamDriveBackgroundAttr = QStringLiteral("canChangeTeamDriveBackground"); static const QString CanChangeTeamMembersOnlyRestrictionAttr = QStringLiteral("canChangeTeamMembersOnlyRestriction"); static const QString CanCommentAttr = QStringLiteral("canComment"); static const QString CanCopyAttr = QStringLiteral("canCopy"); static const QString CanDeleteChildrenAttr = QStringLiteral("canDeleteChildren"); static const QString CanDeleteTeamDriveAttr = QStringLiteral("canDeleteTeamDrive"); static const QString CanDownloadAttr = QStringLiteral("canDownload"); static const QString CanEditAttr = QStringLiteral("canEdit"); static const QString CanListChildrenAttr = QStringLiteral("canListChildren"); static const QString CanManageMembersAttr = QStringLiteral("canManageMembers"); static const QString CanReadRevisionsAttr = QStringLiteral("canReadRevisions"); static const QString CanRenameAttr = QStringLiteral("canRename"); static const QString CanRenameTeamDriveAttr = QStringLiteral("canRenameTeamDrive"); static const QString CanShareAttr = QStringLiteral("canShare"); static const QString CanTrashChildrenAttr = QStringLiteral("canTrashChildren"); static const QString CapabilitiesAttr = QStringLiteral("capabilities"); static const QString ColorRgbAttr = QStringLiteral("colorRgb"); static const QString CopyRequiresWriterPermissionAttr = QStringLiteral("copyRequiresWriterPermission"); static const QString CreatedDateAttr = QStringLiteral("createdDate"); static const QString DomainUsersOnlyAttr = QStringLiteral("domainUsersOnly"); static const QString IdAttr = QStringLiteral("id"); static const QString ItemsAttr = QStringLiteral("items"); static const QString KindAttr = QStringLiteral("kind"); static const QString KindDriveAttr = QStringLiteral("kind"); static const QString NameAttr = QStringLiteral("name"); static const QString PageTokenAttr = QStringLiteral("pageToken"); static const QString NextPageTokenAttr = QStringLiteral("nextPageToken"); static const QString RestrictionsAttr = QStringLiteral("restrictions"); static const QString TeamMembersOnlyAttr = QStringLiteral("teamMembersOnly"); static const QString ThemeIdAttr = QStringLiteral("themeId"); static const QString WidthAttr = QStringLiteral("width"); static const QString XCoordinateAttr = QStringLiteral("xCoordinate"); static const QString YCoordinateAttr = QStringLiteral("yCoordinate"); } using namespace KGAPI2; using namespace KGAPI2::Drive; ///// DriveTeamdrive::Restrictions class Q_DECL_HIDDEN Teamdrive::Restrictions::Private { public: Private() = default; Private(const Private &other) = default; bool adminManagedRestrictions = false; bool copyRequiresWriterPermission = false; bool domainUsersOnly = false; bool teamMembersOnly = false; }; Teamdrive::Restrictions::Restrictions(): d(new Private) { } Teamdrive::Restrictions::Restrictions(const Teamdrive::Restrictions &other): d(new Private(*(other.d))) { } Teamdrive::Restrictions::~Restrictions() = default; bool Teamdrive::Restrictions::operator==(const Teamdrive::Restrictions &other) const { GAPI_COMPARE(adminManagedRestrictions); GAPI_COMPARE(copyRequiresWriterPermission); GAPI_COMPARE(domainUsersOnly); GAPI_COMPARE(teamMembersOnly); return true; } bool Teamdrive::Restrictions::adminManagedRestrictions() const { return d->adminManagedRestrictions; } bool Teamdrive::Restrictions::copyRequiresWriterPermission() const { return d->copyRequiresWriterPermission; } bool Teamdrive::Restrictions::domainUsersOnly() const { return d->domainUsersOnly; } bool Teamdrive::Restrictions::teamMembersOnly() const { return d->teamMembersOnly; } ///// DriveTeamdrive::Capabilities class Q_DECL_HIDDEN Teamdrive::Capabilities::Private { public: Private() = default; Private(const Private &other) = default; bool canAddChildren = false; bool canChangeCopyRequiresWriterPermissionRestriction = false; bool canChangeDomainUsersOnlyRestriction = false; bool canChangeTeamDriveBackground = false; bool canChangeTeamMembersOnlyRestriction = false; bool canComment = false; bool canCopy = false; bool canDeleteChildren = false; bool canDeleteTeamDrive = false; bool canDownload = false; bool canEdit = false; bool canListChildren = false; bool canManageMembers = false; bool canReadRevisions = false; bool canRename = false; bool canRenameTeamDrive = false; bool canShare = false; bool canTrashChildren = false; }; Teamdrive::Capabilities::Capabilities(): d(new Private) { } Teamdrive::Capabilities::Capabilities(const Teamdrive::Capabilities &other): d(new Private(*(other.d))) { } Teamdrive::Capabilities::~Capabilities() = default; bool Teamdrive::Capabilities::operator==(const Teamdrive::Capabilities &other) const { GAPI_COMPARE(canAddChildren); GAPI_COMPARE(canChangeCopyRequiresWriterPermissionRestriction); GAPI_COMPARE(canChangeDomainUsersOnlyRestriction); GAPI_COMPARE(canChangeTeamDriveBackground); GAPI_COMPARE(canChangeTeamMembersOnlyRestriction); GAPI_COMPARE(canComment); GAPI_COMPARE(canCopy); GAPI_COMPARE(canDeleteChildren); GAPI_COMPARE(canDeleteTeamDrive); GAPI_COMPARE(canDownload); GAPI_COMPARE(canEdit); GAPI_COMPARE(canListChildren); GAPI_COMPARE(canManageMembers); GAPI_COMPARE(canReadRevisions); GAPI_COMPARE(canRename); GAPI_COMPARE(canRenameTeamDrive); GAPI_COMPARE(canShare); GAPI_COMPARE(canTrashChildren); return true; } bool Teamdrive::Capabilities::canAddChildren() const { return d->canAddChildren; } bool Teamdrive::Capabilities::canChangeCopyRequiresWriterPermissionRestriction() const { return d->canChangeCopyRequiresWriterPermissionRestriction; } bool Teamdrive::Capabilities::canChangeDomainUsersOnlyRestriction() const { return d->canChangeDomainUsersOnlyRestriction; } bool Teamdrive::Capabilities::canChangeTeamDriveBackground() const { return d->canChangeTeamDriveBackground; } bool Teamdrive::Capabilities::canChangeTeamMembersOnlyRestriction() const { return d->canChangeTeamMembersOnlyRestriction; } bool Teamdrive::Capabilities::canComment() const { return d->canComment; } bool Teamdrive::Capabilities::canCopy() const { return d->canCopy; } bool Teamdrive::Capabilities::canDeleteChildren() const { return d->canDeleteChildren; } bool Teamdrive::Capabilities::canDeleteTeamDrive() const { return d->canDeleteTeamDrive; } bool Teamdrive::Capabilities::canDownload() const { return d->canDownload; } bool Teamdrive::Capabilities::canEdit() const { return d->canEdit; } bool Teamdrive::Capabilities::canListChildren() const { return d->canListChildren; } bool Teamdrive::Capabilities::canManageMembers() const { return d->canManageMembers; } bool Teamdrive::Capabilities::canReadRevisions() const { return d->canReadRevisions; } bool Teamdrive::Capabilities::canRename() const { return d->canRename; } bool Teamdrive::Capabilities::canRenameTeamDrive() const { return d->canRenameTeamDrive; } bool Teamdrive::Capabilities::canShare() const { return d->canShare; } bool Teamdrive::Capabilities::canTrashChildren() const { return d->canTrashChildren; } ///// DriveTeamdrive::BackgroundImageFile class Q_DECL_HIDDEN Teamdrive::BackgroundImageFile::Private { public: Private() = default; Private(const Private &other) = default; QString id; float xCoordinate = 0.0f; float yCoordinate = 0.0f; float width = 0.0f; }; Teamdrive::BackgroundImageFile::BackgroundImageFile(): d(new Private) { } Teamdrive::BackgroundImageFile::BackgroundImageFile(const Teamdrive::BackgroundImageFile &other): d(new Private(*(other.d))) { } Teamdrive::BackgroundImageFile::~BackgroundImageFile() = default; bool Teamdrive::BackgroundImageFile::operator==(const Teamdrive::BackgroundImageFile &other) const { GAPI_COMPARE(id); GAPI_COMPARE(xCoordinate); GAPI_COMPARE(yCoordinate); GAPI_COMPARE(width); return true; } QString Teamdrive::BackgroundImageFile::id() const { return d->id; } float Teamdrive::BackgroundImageFile::xCoordinate() const { return d->xCoordinate; } float Teamdrive::BackgroundImageFile::yCoordinate() const { return d->yCoordinate; } float Teamdrive::BackgroundImageFile::width() const { return d->width; } ///// DriveTeamdrive class Q_DECL_HIDDEN Teamdrive::Private { public: Private() = default; Private(const Private& other) = default; QString id; QString name; QString themeId; QString colorRgb; BackgroundImageFilePtr backgroundImageFile; QString backgroundImageLink; CapabilitiesPtr capabilities; QDateTime createdDate; RestrictionsPtr restrictions; static TeamdrivePtr fromJSON(const QVariantMap &map); }; TeamdrivePtr Teamdrive::Private::fromJSON(const QVariantMap &map) { if (!map.contains(KindAttr) || - map[KindAttr].toString() != QLatin1String("drive#teamDrive")) + map[KindAttr].toString() != ApiKind) { return TeamdrivePtr(); } auto teamdrive = TeamdrivePtr::create(); - teamdrive->d->id = map[IdAttr].toString(); - teamdrive->d->name = map[NameAttr].toString(); - teamdrive->d->themeId = map[ThemeIdAttr].toString(); - teamdrive->d->colorRgb = map[ColorRgbAttr].toString(); - teamdrive->d->backgroundImageLink = map[BackgroundImageLinkAttr].toString(); - teamdrive->d->createdDate = QDateTime::fromString(map[CreatedDateAttr].toString(), Qt::ISODate); - - const QVariantMap backgroundImageFileMap = map[BackgroundImageFileAttr].toMap(); - auto backgroundImageFile = BackgroundImageFilePtr::create(); - backgroundImageFile->d->id = backgroundImageFileMap[IdAttr].toString(); - backgroundImageFile->d->xCoordinate = backgroundImageFileMap[XCoordinateAttr].toReal(); - backgroundImageFile->d->yCoordinate = backgroundImageFileMap[YCoordinateAttr].toReal(); - backgroundImageFile->d->width = backgroundImageFileMap[WidthAttr].toReal(); - teamdrive->d->backgroundImageFile = backgroundImageFile; - - const QVariantMap capabilitiesMap = map[CapabilitiesAttr].toMap(); - auto capabilities = CapabilitiesPtr::create(); - capabilities->d->canAddChildren = capabilitiesMap[CanAddChildrenAttr].toBool(); - capabilities->d->canChangeCopyRequiresWriterPermissionRestriction = capabilitiesMap[CanChangeCopyRequiresWriterPermissionRestrictionAttr].toBool(); - capabilities->d->canChangeDomainUsersOnlyRestriction = capabilitiesMap[CanChangeDomainUsersOnlyRestrictionAttr].toBool(); - capabilities->d->canChangeTeamDriveBackground = capabilitiesMap[CanChangeTeamDriveBackgroundAttr].toBool(); - capabilities->d->canChangeTeamMembersOnlyRestriction = capabilitiesMap[CanChangeTeamMembersOnlyRestrictionAttr].toBool(); - capabilities->d->canComment = capabilitiesMap[CanCommentAttr].toBool(); - capabilities->d->canCopy = capabilitiesMap[CanCopyAttr].toBool(); - capabilities->d->canDeleteChildren = capabilitiesMap[CanDeleteChildrenAttr].toBool(); - capabilities->d->canDeleteTeamDrive = capabilitiesMap[CanDeleteTeamDriveAttr].toBool(); - capabilities->d->canDownload = capabilitiesMap[CanDownloadAttr].toBool(); - capabilities->d->canEdit = capabilitiesMap[CanEditAttr].toBool(); - capabilities->d->canListChildren = capabilitiesMap[CanListChildrenAttr].toBool(); - capabilities->d->canManageMembers = capabilitiesMap[CanManageMembersAttr].toBool(); - capabilities->d->canReadRevisions = capabilitiesMap[CanReadRevisionsAttr].toBool(); - capabilities->d->canRename = capabilitiesMap[CanRenameAttr].toBool(); - capabilities->d->canRenameTeamDrive = capabilitiesMap[CanRenameTeamDriveAttr].toBool(); - capabilities->d->canShare = capabilitiesMap[CanShareAttr].toBool(); - capabilities->d->canTrashChildren = capabilitiesMap[CanTrashChildrenAttr].toBool(); - teamdrive->d->capabilities = capabilities; - - const QVariantMap restrictionsMap = map[RestrictionsAttr].toMap(); - auto restrictions = RestrictionsPtr::create(); - restrictions->d->adminManagedRestrictions = restrictionsMap[AdminManagedRestrictionsAttr].toBool(); - restrictions->d->copyRequiresWriterPermission = restrictionsMap[CopyRequiresWriterPermissionAttr].toBool(); - restrictions->d->domainUsersOnly = restrictionsMap[DomainUsersOnlyAttr].toBool(); - restrictions->d->teamMembersOnly = restrictionsMap[TeamMembersOnlyAttr].toBool(); - teamdrive->d->restrictions = restrictions; + if (map.contains(IdAttr)) { + teamdrive->d->id = map[IdAttr].toString(); + } + if (map.contains(NameAttr)) { + teamdrive->d->name = map[NameAttr].toString(); + } + if (map.contains(ThemeIdAttr)) { + teamdrive->d->themeId = map[ThemeIdAttr].toString(); + } + if (map.contains(ColorRgbAttr)) { + teamdrive->d->colorRgb = map[ColorRgbAttr].toString(); + } + if (map.contains(BackgroundImageLinkAttr)) { + teamdrive->d->backgroundImageLink = map[BackgroundImageLinkAttr].toString(); + } + if (map.contains(CreatedDateAttr)) { + teamdrive->d->createdDate = QDateTime::fromString(map[CreatedDateAttr].toString(), Qt::ISODate); + } + + if (map.contains(BackgroundImageFileAttr)) { + const QVariantMap backgroundImageFileMap = map[BackgroundImageFileAttr].toMap(); + auto backgroundImageFile = BackgroundImageFilePtr::create(); + backgroundImageFile->d->id = backgroundImageFileMap[IdAttr].toString(); + backgroundImageFile->d->xCoordinate = backgroundImageFileMap[XCoordinateAttr].toReal(); + backgroundImageFile->d->yCoordinate = backgroundImageFileMap[YCoordinateAttr].toReal(); + backgroundImageFile->d->width = backgroundImageFileMap[WidthAttr].toReal(); + teamdrive->d->backgroundImageFile = backgroundImageFile; + } + + if (map.contains(CapabilitiesAttr)) { + const QVariantMap capabilitiesMap = map[CapabilitiesAttr].toMap(); + auto capabilities = CapabilitiesPtr::create(); + capabilities->d->canAddChildren = capabilitiesMap[CanAddChildrenAttr].toBool(); + capabilities->d->canChangeCopyRequiresWriterPermissionRestriction = capabilitiesMap[CanChangeCopyRequiresWriterPermissionRestrictionAttr].toBool(); + capabilities->d->canChangeDomainUsersOnlyRestriction = capabilitiesMap[CanChangeDomainUsersOnlyRestrictionAttr].toBool(); + capabilities->d->canChangeTeamDriveBackground = capabilitiesMap[CanChangeTeamDriveBackgroundAttr].toBool(); + capabilities->d->canChangeTeamMembersOnlyRestriction = capabilitiesMap[CanChangeTeamMembersOnlyRestrictionAttr].toBool(); + capabilities->d->canComment = capabilitiesMap[CanCommentAttr].toBool(); + capabilities->d->canCopy = capabilitiesMap[CanCopyAttr].toBool(); + capabilities->d->canDeleteChildren = capabilitiesMap[CanDeleteChildrenAttr].toBool(); + capabilities->d->canDeleteTeamDrive = capabilitiesMap[CanDeleteTeamDriveAttr].toBool(); + capabilities->d->canDownload = capabilitiesMap[CanDownloadAttr].toBool(); + capabilities->d->canEdit = capabilitiesMap[CanEditAttr].toBool(); + capabilities->d->canListChildren = capabilitiesMap[CanListChildrenAttr].toBool(); + capabilities->d->canManageMembers = capabilitiesMap[CanManageMembersAttr].toBool(); + capabilities->d->canReadRevisions = capabilitiesMap[CanReadRevisionsAttr].toBool(); + capabilities->d->canRename = capabilitiesMap[CanRenameAttr].toBool(); + capabilities->d->canRenameTeamDrive = capabilitiesMap[CanRenameTeamDriveAttr].toBool(); + capabilities->d->canShare = capabilitiesMap[CanShareAttr].toBool(); + capabilities->d->canTrashChildren = capabilitiesMap[CanTrashChildrenAttr].toBool(); + teamdrive->d->capabilities = capabilities; + } + + if (map.contains(RestrictionsAttr)) { + const QVariantMap restrictionsMap = map[RestrictionsAttr].toMap(); + auto restrictions = RestrictionsPtr::create(); + restrictions->d->adminManagedRestrictions = restrictionsMap[AdminManagedRestrictionsAttr].toBool(); + restrictions->d->copyRequiresWriterPermission = restrictionsMap[CopyRequiresWriterPermissionAttr].toBool(); + restrictions->d->domainUsersOnly = restrictionsMap[DomainUsersOnlyAttr].toBool(); + restrictions->d->teamMembersOnly = restrictionsMap[TeamMembersOnlyAttr].toBool(); + teamdrive->d->restrictions = restrictions; + } return teamdrive; } Teamdrive::Teamdrive(): KGAPI2::Object(), d(new Private) { } Teamdrive::Teamdrive(const Teamdrive& other): KGAPI2::Object(other), d(new Private(*(other.d))) { } Teamdrive::~Teamdrive() = default; bool Teamdrive::operator==(const Teamdrive &other) const { if (!Object::operator==(other)) { return false; } GAPI_COMPARE(id); GAPI_COMPARE(name); GAPI_COMPARE(themeId); GAPI_COMPARE(colorRgb); GAPI_COMPARE_SHAREDPTRS(backgroundImageFile); GAPI_COMPARE(backgroundImageLink); GAPI_COMPARE_SHAREDPTRS(capabilities); GAPI_COMPARE(createdDate); GAPI_COMPARE_SHAREDPTRS(restrictions); return true; } QString Teamdrive::id() const { return d->id; } QString Teamdrive::name() const { return d->name; } +void Teamdrive::setName(const QString &name) const +{ + d->name = name; +} + QString Teamdrive::themeId() const { return d->themeId; } QString Teamdrive::colorRgb() const { return d->colorRgb; } Teamdrive::BackgroundImageFilePtr Teamdrive::backgroundImageFile() const { return d->backgroundImageFile; } QString Teamdrive::backgroundImageLink() const { return d->backgroundImageLink; } Teamdrive::CapabilitiesPtr Teamdrive::capabilities() const { return d->capabilities; } QDateTime Teamdrive::createdDate() const { return d->createdDate; } Teamdrive::RestrictionsPtr Teamdrive::restrictions() const { return d->restrictions; } TeamdrivePtr Teamdrive::fromJSON(const QByteArray &jsonData) { QJsonDocument document = QJsonDocument::fromJson(jsonData); if (document.isNull()) { return TeamdrivePtr(); } const QVariant data = document.toVariant(); return Private::fromJSON(data.toMap()); } TeamdrivesList Teamdrive::fromJSONFeed(const QByteArray &jsonData, FeedData &feedData) { QJsonDocument document = QJsonDocument::fromJson(jsonData); if (document.isNull()) { return TeamdrivesList(); } const QVariant data = document.toVariant(); const QVariantMap map = data.toMap(); if (!map.contains(KindAttr) || - map[KindAttr].toString() != QLatin1String("drive#teamDriveList")) { + map[KindAttr].toString() != ApiKindList) { return TeamdrivesList(); } if (map.contains(NextPageTokenAttr)) { feedData.nextPageUrl = DriveService::fetchTeamdrivesUrl(); QUrlQuery query(feedData.nextPageUrl); query.addQueryItem(PageTokenAttr, map.value(NextPageTokenAttr).toString()); feedData.nextPageUrl.setQuery(query); } TeamdrivesList list; const QVariantList items = map[ItemsAttr].toList(); for (const QVariant & item : items) { const TeamdrivePtr teamdrive = Private::fromJSON(item.toMap()); if (!teamdrive.isNull()) { list << teamdrive; } } return list; } QByteArray Teamdrive::toJSON(const TeamdrivePtr &teamdrive) { QVariantMap teamDriveMap; - teamDriveMap[IdAttr] = teamdrive->id(); - teamDriveMap[NameAttr] = teamdrive->name(); - teamDriveMap[ThemeIdAttr] = teamdrive->themeId(); - teamDriveMap[ColorRgbAttr] = teamdrive->colorRgb(); - teamDriveMap[BackgroundImageLinkAttr] = teamdrive->backgroundImageLink(); - teamDriveMap[CreatedDateAttr] = teamdrive->createdDate(); - - QVariantMap restrictionsMap; - restrictionsMap[AdminManagedRestrictionsAttr] = teamdrive->restrictions()->adminManagedRestrictions(); - restrictionsMap[CopyRequiresWriterPermissionAttr] = teamdrive->restrictions()->copyRequiresWriterPermission(); - restrictionsMap[DomainUsersOnlyAttr] = teamdrive->restrictions()->domainUsersOnly(); - restrictionsMap[TeamMembersOnlyAttr] = teamdrive->restrictions()->teamMembersOnly(); - teamDriveMap[RestrictionsAttr] = restrictionsMap; - - QVariantMap backgroundImageFileMap; - backgroundImageFileMap[IdAttr] = teamdrive->backgroundImageFile()->id(); - backgroundImageFileMap[XCoordinateAttr] = teamdrive->backgroundImageFile()->xCoordinate(); - backgroundImageFileMap[YCoordinateAttr] = teamdrive->backgroundImageFile()->yCoordinate(); - backgroundImageFileMap[WidthAttr] = teamdrive->backgroundImageFile()->width(); - teamDriveMap[BackgroundImageFileAttr] = backgroundImageFileMap; - - QVariantMap capabilitiesMap; - capabilitiesMap[CanAddChildrenAttr] = teamdrive->capabilities()->canAddChildren(); - capabilitiesMap[CanChangeCopyRequiresWriterPermissionRestrictionAttr] = teamdrive->capabilities()->canChangeCopyRequiresWriterPermissionRestriction(); - capabilitiesMap[CanChangeDomainUsersOnlyRestrictionAttr] = teamdrive->capabilities()->canChangeDomainUsersOnlyRestriction(); - capabilitiesMap[CanChangeTeamDriveBackgroundAttr] = teamdrive->capabilities()->canChangeTeamDriveBackground(); - capabilitiesMap[CanChangeTeamMembersOnlyRestrictionAttr] = teamdrive->capabilities()->canChangeTeamMembersOnlyRestriction(); - capabilitiesMap[CanCommentAttr] = teamdrive->capabilities()->canComment(); - capabilitiesMap[CanCopyAttr] = teamdrive->capabilities()->canCopy(); - capabilitiesMap[CanDeleteChildrenAttr] = teamdrive->capabilities()->canDeleteChildren(); - capabilitiesMap[CanDeleteTeamDriveAttr] = teamdrive->capabilities()->canDeleteTeamDrive(); - capabilitiesMap[CanDownloadAttr] = teamdrive->capabilities()->canDownload(); - capabilitiesMap[CanEditAttr] = teamdrive->capabilities()->canEdit(); - capabilitiesMap[CanListChildrenAttr] = teamdrive->capabilities()->canListChildren(); - capabilitiesMap[CanManageMembersAttr] = teamdrive->capabilities()->canManageMembers(); - capabilitiesMap[CanReadRevisionsAttr] = teamdrive->capabilities()->canReadRevisions(); - capabilitiesMap[CanRenameAttr] = teamdrive->capabilities()->canRename(); - capabilitiesMap[CanRenameTeamDriveAttr] = teamdrive->capabilities()->canRenameTeamDrive(); - capabilitiesMap[CanShareAttr] = teamdrive->capabilities()->canShare(); - capabilitiesMap[CanTrashChildrenAttr] = teamdrive->capabilities()->canTrashChildren(); - teamDriveMap[CapabilitiesAttr] = capabilitiesMap; + teamDriveMap[KindAttr] = ApiKind; + if (!teamdrive->id().isEmpty()) { + teamDriveMap[IdAttr] = teamdrive->id(); + } + if (!teamdrive->name().isEmpty()) { + teamDriveMap[NameAttr] = teamdrive->name(); + } + if (!teamdrive->themeId().isEmpty()) { + teamDriveMap[ThemeIdAttr] = teamdrive->themeId(); + } + if (!teamdrive->colorRgb().isEmpty()) { + teamDriveMap[ColorRgbAttr] = teamdrive->colorRgb(); + } + if (!teamdrive->backgroundImageLink().isEmpty()) { + teamDriveMap[BackgroundImageLinkAttr] = teamdrive->backgroundImageLink(); + } + if (teamdrive->createdDate().isValid()) { + teamDriveMap[CreatedDateAttr] = teamdrive->createdDate(); + } + + if (teamdrive->restrictions()) { + QVariantMap restrictionsMap; + restrictionsMap[AdminManagedRestrictionsAttr] = teamdrive->restrictions()->adminManagedRestrictions(); + restrictionsMap[CopyRequiresWriterPermissionAttr] = teamdrive->restrictions()->copyRequiresWriterPermission(); + restrictionsMap[DomainUsersOnlyAttr] = teamdrive->restrictions()->domainUsersOnly(); + restrictionsMap[TeamMembersOnlyAttr] = teamdrive->restrictions()->teamMembersOnly(); + teamDriveMap[RestrictionsAttr] = restrictionsMap; + } + + if (teamdrive->backgroundImageFile()) { + QVariantMap backgroundImageFileMap; + backgroundImageFileMap[IdAttr] = teamdrive->backgroundImageFile()->id(); + backgroundImageFileMap[XCoordinateAttr] = teamdrive->backgroundImageFile()->xCoordinate(); + backgroundImageFileMap[YCoordinateAttr] = teamdrive->backgroundImageFile()->yCoordinate(); + backgroundImageFileMap[WidthAttr] = teamdrive->backgroundImageFile()->width(); + teamDriveMap[BackgroundImageFileAttr] = backgroundImageFileMap; + } + + if (teamdrive->capabilities()) { + QVariantMap capabilitiesMap; + capabilitiesMap[CanAddChildrenAttr] = teamdrive->capabilities()->canAddChildren(); + capabilitiesMap[CanChangeCopyRequiresWriterPermissionRestrictionAttr] = teamdrive->capabilities()->canChangeCopyRequiresWriterPermissionRestriction(); + capabilitiesMap[CanChangeDomainUsersOnlyRestrictionAttr] = teamdrive->capabilities()->canChangeDomainUsersOnlyRestriction(); + capabilitiesMap[CanChangeTeamDriveBackgroundAttr] = teamdrive->capabilities()->canChangeTeamDriveBackground(); + capabilitiesMap[CanChangeTeamMembersOnlyRestrictionAttr] = teamdrive->capabilities()->canChangeTeamMembersOnlyRestriction(); + capabilitiesMap[CanCommentAttr] = teamdrive->capabilities()->canComment(); + capabilitiesMap[CanCopyAttr] = teamdrive->capabilities()->canCopy(); + capabilitiesMap[CanDeleteChildrenAttr] = teamdrive->capabilities()->canDeleteChildren(); + capabilitiesMap[CanDeleteTeamDriveAttr] = teamdrive->capabilities()->canDeleteTeamDrive(); + capabilitiesMap[CanDownloadAttr] = teamdrive->capabilities()->canDownload(); + capabilitiesMap[CanEditAttr] = teamdrive->capabilities()->canEdit(); + capabilitiesMap[CanListChildrenAttr] = teamdrive->capabilities()->canListChildren(); + capabilitiesMap[CanManageMembersAttr] = teamdrive->capabilities()->canManageMembers(); + capabilitiesMap[CanReadRevisionsAttr] = teamdrive->capabilities()->canReadRevisions(); + capabilitiesMap[CanRenameAttr] = teamdrive->capabilities()->canRename(); + capabilitiesMap[CanRenameTeamDriveAttr] = teamdrive->capabilities()->canRenameTeamDrive(); + capabilitiesMap[CanShareAttr] = teamdrive->capabilities()->canShare(); + capabilitiesMap[CanTrashChildrenAttr] = teamdrive->capabilities()->canTrashChildren(); + teamDriveMap[CapabilitiesAttr] = capabilitiesMap; + } QJsonDocument document = QJsonDocument::fromVariant(teamDriveMap); return document.toJson(QJsonDocument::Compact); } diff --git a/src/drive/teamdrive.h b/src/drive/teamdrive.h index 1d49ceb..9e736aa 100644 --- a/src/drive/teamdrive.h +++ b/src/drive/teamdrive.h @@ -1,342 +1,349 @@ /* 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 LIBKGAPI2_DRIVETEAMDRIVE_H #define LIBKGAPI2_DRIVETEAMDRIVE_H #include "types.h" #include "object.h" #include "kgapidrive_export.h" #include #include namespace KGAPI2 { namespace Drive { /** * @brief Teamdrive contains a representation of a Team Drive. * * Getters and setters' documentation is based on Google Drive's API v2 reference * @see Teamdrives * * @since x.0 * @author David Barchiesi */ class KGAPIDRIVE_EXPORT Teamdrive: public KGAPI2::Object { public: /** * @brief DriveTeamdrive::Restrictions holds the structure used for * restrictions property. */ class Restrictions { public: Restrictions(); Restrictions(const Restrictions &other); ~Restrictions(); bool operator==(const Restrictions &other) const; bool operator!=(const Restrictions &other) const { return !operator==(other); } /** * @brief Returns whether administrative privileges on this Team Drive * are required to modify restrictions. */ bool adminManagedRestrictions() const; /** * @brief Returns whether the options to copy, print, or download files * inside this Team Drive, should be disabled for readers and commenters. * When this restriction is set to true, it will override the similarly * named field to true for any file inside this Team Drive. */ bool copyRequiresWriterPermission() const; /** * @brief Returns whether access to this Team Drive and items inside this * Team Drive is restricted to users of the domain to which this Team * Drive belongs. This restriction may be overridden by other sharing * policies controlled outside of this Team Drive. */ bool domainUsersOnly() const; /** * @brief Returns whether access to items inside this Team Drive is * restricted to members of this Team Drive. */ bool teamMembersOnly() const; private: class Private; QScopedPointer const d; friend class Private; friend class Teamdrive; }; typedef QSharedPointer RestrictionsPtr; /** * @brief DriveTeamdrive::Capabilities holds the structure used for capabilities property. */ class Capabilities { public: Capabilities(); Capabilities(const Capabilities &other); ~Capabilities(); bool operator==(const Capabilities &other) const; bool operator!=(const Capabilities &other) const { return !operator==(other); } /** * @brief Returns whether the current user can add children to folders * in this Team Drive. */ bool canAddChildren() const; /** * @brief Returns whether the current user can change the * copyRequiresWriterPermission restriction of this Team Drive. */ bool canChangeCopyRequiresWriterPermissionRestriction() const; /** * @brief Returns whether the current user can change the domainUsersOnly * restriction of this Team Drive. */ bool canChangeDomainUsersOnlyRestriction() const; /** * @brief Returns whether the current user can change the background of * this Team Drive. */ bool canChangeTeamDriveBackground() const; /** * @brief Returns whether the current user can change the teamMembersOnly * restriction of this Team Drive. */ bool canChangeTeamMembersOnlyRestriction() const; /** * @brief Returns Whether the current user can comment on files in * this Team Drive. */ bool canComment() const; /** * @brief Returns Whether the current user can copy files in this Team Drive. */ bool canCopy() const; /** * @brief Returns Whether the current user can delete children from * folders in this Team Drive. */ bool canDeleteChildren() const; /** * @brief Returns Whether the current user can delete this Team Drive. * * Attempting to delete the Team Drive may still fail if there are * untrashed items inside the Team Drive. */ bool canDeleteTeamDrive() const; /** * @brief Returns Whether the current user can download files in this * Team Drive. */ bool canDownload() const; /** * @brief Returns Whether the current user can edit files in this * Team Drive */ bool canEdit() const; /** * @brief Returns Whether the current user can list the children of * folders in this Team Drive. */ bool canListChildren() const; /** * @brief Returns Whether the current user can add members to this Team Drive * or remove them or change their role. */ bool canManageMembers() const; /** * @brief Returns Whether the current user can read the revisions * resource of files in this Team Drive. */ bool canReadRevisions() const; /** * @brief Returns Whether the current user can rename files or folders * in this Team Drive. */ bool canRename() const; /** * @brief Returns Whether the current user can rename this Team Drive. */ bool canRenameTeamDrive() const; /** * @brief Returns Whether the current user can share files or folders * in this Team Drive. */ bool canShare() const; /** * @brief Returns Whether the current user can trash children from * folders in this Team Drive. */ bool canTrashChildren() const; private: class Private; QScopedPointer const d; friend class Private; friend class Teamdrive; }; typedef QSharedPointer CapabilitiesPtr; /** * @brief DriveTeamdrive::BackgroundImageFile holds the structure used * for backgroundImageFile property. */ class BackgroundImageFile { public: BackgroundImageFile(); BackgroundImageFile(const BackgroundImageFile &other); ~BackgroundImageFile(); bool operator==(const BackgroundImageFile &other) const; bool operator!=(const BackgroundImageFile &other) const { return !operator==(other); } /** * @brief Returns the id of the background image file. */ QString id() const; /** * @brief Returns the x coordinate for this background image file. */ float xCoordinate() const; /** * @brief Returns the y coordinate for this background image file. */ float yCoordinate() const; /** * @brief Returns the width for this background image file. */ float width() const; private: class Private; QScopedPointer const d; friend class Private; friend class Teamdrive; }; typedef QSharedPointer BackgroundImageFilePtr; Teamdrive(); Teamdrive(const Teamdrive &other); ~Teamdrive() override; bool operator==(const Teamdrive &other) const; bool operator!=(const Teamdrive &other) const { return !operator==(other); } /** * @brief Returns the id of the teamdrive. */ QString id() const; /** * @brief Returns the name of the teamdrive. */ QString name() const; + /** + * @brief Sets the name of the teamdrive. + * + * @param name + */ + void setName(const QString &name) const; + /** * @brief Returns the themeId of the teamdrive. */ QString themeId() const; /** * @brief Returns the colorRgb of the teamdrive. */ QString colorRgb() const; /** * @brief Returns the image file and cropping parameters from which a background image for this Team Drive is set. */ BackgroundImageFilePtr backgroundImageFile() const; /** * @brief Returns the backgroundImageLink of the teamdrive. */ QString backgroundImageLink() const; /** * @brief Returns the capabilities the current user has on this Team Drive. */ CapabilitiesPtr capabilities() const; /** * @brief Returns the time at which the Team Drive was created. */ QDateTime createdDate() const; /** * @brief Returns the set of restrictions that apply to this Team Drive or * items inside this Team Drive. */ RestrictionsPtr restrictions() const; static TeamdrivePtr fromJSON(const QByteArray &jsonData); static TeamdrivesList fromJSONFeed(const QByteArray &jsonData, FeedData &feedData); static QByteArray toJSON(const TeamdrivePtr &teamdrive); private: class Private; QScopedPointer const d; friend class Private; }; } /* namespace Drive */ } /* namespace KGAPI2 */ #endif // LIBKGAPI2_DRIVETEAMDRIVE_H diff --git a/src/drive/teamdrivecreatejob.cpp b/src/drive/teamdrivecreatejob.cpp new file mode 100644 index 0000000..8199976 --- /dev/null +++ b/src/drive/teamdrivecreatejob.cpp @@ -0,0 +1,136 @@ +/* + * 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 "teamdrivecreatejob.h" +#include "account.h" +#include "driveservice.h" +#include "teamdrive.h" +#include "utils.h" + +#include +#include +#include + + +namespace { + static const QString RequestIdParam = QStringLiteral("requestId"); +} +using namespace KGAPI2; +using namespace KGAPI2::Drive; + +class Q_DECL_HIDDEN TeamdriveCreateJob::Private +{ + public: + Private(TeamdriveCreateJob *parent); + void processNext(); + + TeamdrivesList teamdrives; + QString requestId; + + private: + TeamdriveCreateJob *const q; +}; + +TeamdriveCreateJob::Private::Private(TeamdriveCreateJob *parent): + q(parent) +{ +} + +void TeamdriveCreateJob::Private::processNext() +{ + if (teamdrives.isEmpty()) { + q->emitFinished(); + return; + } + + const TeamdrivePtr teamdrive = teamdrives.takeFirst(); + + QUrl url = DriveService::fetchTeamdrivesUrl(); + + QNetworkRequest request; + request.setRawHeader("Authorization", "Bearer " + q->account()->accessToken().toLatin1()); + QUrlQuery query(url); + if (!requestId.isEmpty()) { + query.addQueryItem(RequestIdParam, requestId); + } + url.setQuery(query); + request.setUrl(url); + + const QByteArray rawData = Teamdrive::toJSON(teamdrive); + q->enqueueRequest(request, rawData, QStringLiteral("application/json")); +} + +TeamdriveCreateJob::TeamdriveCreateJob(const QString &requestId, + const TeamdrivePtr &teamdrive, + const AccountPtr &account, + QObject *parent): + CreateJob(account, parent), + d(new Private(this)) +{ + d->requestId = requestId; + d->teamdrives << teamdrive; +} + +TeamdriveCreateJob::TeamdriveCreateJob(const QString &requestId, + const TeamdrivesList &teamdrives, + const AccountPtr &account, + QObject *parent): + CreateJob(account, parent), + d(new Private(this)) +{ + d->requestId = requestId; + d->teamdrives = teamdrives; +} + +TeamdriveCreateJob::~TeamdriveCreateJob() = default; + +QString TeamdriveCreateJob::requestId() const +{ + return d->requestId; +} + +void TeamdriveCreateJob::start() +{ + d->processNext(); +} + +ObjectsList TeamdriveCreateJob::handleReplyWithItems(const QNetworkReply *reply, + const QByteArray &rawData) +{ + const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); + ContentType ct = Utils::stringToContentType(contentType); + ObjectsList items; + if (ct == KGAPI2::JSON) { + items << Teamdrive::fromJSON(rawData); + } else { + setError(KGAPI2::InvalidResponse); + setErrorString(tr("Invalid response content type")); + emitFinished(); + } + + // Enqueue next item or finish + d->processNext(); + + return items; +} + + diff --git a/src/drive/teamdrivecreatejob.h b/src/drive/teamdrivecreatejob.h new file mode 100644 index 0000000..43ab091 --- /dev/null +++ b/src/drive/teamdrivecreatejob.h @@ -0,0 +1,68 @@ +/* + * 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_DRIVETEAMDRIVECREATEJOB_H +#define KGAPI2_DRIVETEAMDRIVECREATEJOB_H + +#include "createjob.h" +#include "kgapidrive_export.h" + +namespace KGAPI2 +{ + +namespace Drive +{ + +class KGAPIDRIVE_EXPORT TeamdriveCreateJob : public KGAPI2::CreateJob +{ + Q_OBJECT + + public: + TeamdriveCreateJob(const QString &requestId, + const TeamdrivePtr &teamdrive, + const AccountPtr &account, QObject *parent = nullptr); + TeamdriveCreateJob(const QString &requestId, + const TeamdrivesList &teamdrives, + const AccountPtr &account, QObject *parent = nullptr); + ~TeamdriveCreateJob() override; + + /** + * @brief Returns the requestId used in this create request. + */ + QString requestId() const; + + protected: + void start() override; + KGAPI2::ObjectsList handleReplyWithItems(const QNetworkReply *reply, + const QByteArray &rawData) override; + + private: + class Private; + QScopedPointer d; + friend class Private; +}; + +} // namespace Drive + +} // namespace KGAPI2 + +#endif // KGAPI2_DRIVETEAMDRIVECREATEJOB_H