Changeset View
Changeset View
Standalone View
Standalone View
examples/teamdrive/mainwindow.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2019 David Barchiesi <david@barchie.si> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <https://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | | ||||
22 | #include "mainwindow.h" | ||||
23 | #include "ui_main.h" | ||||
24 | | ||||
25 | #include <drive/teamdrive.h> | ||||
26 | #include <drive/teamdrivefetchjob.h> | ||||
27 | #include <drive/file.h> | ||||
28 | #include <drive/filesearchquery.h> | ||||
29 | #include <drive/filefetchjob.h> | ||||
30 | #include <authjob.h> | ||||
31 | #include <account.h> | ||||
32 | | ||||
33 | #include <QListWidgetItem> | ||||
34 | | ||||
35 | MainWindow::MainWindow(QWidget * parent): | ||||
36 | QMainWindow(parent), | ||||
37 | m_ui(new Ui::MainWindow) | ||||
38 | { | ||||
39 | /* Initialize GUI */ | ||||
40 | m_ui->setupUi(this); | ||||
41 | m_ui->errorLabel->setVisible(false); | ||||
42 | connect(m_ui->authButton, &QAbstractButton::clicked, | ||||
43 | this, &MainWindow::authenticate); | ||||
44 | connect(m_ui->teamdriveListButton, &QAbstractButton::clicked, | ||||
45 | this, &MainWindow::fetchTeamdriveList); | ||||
46 | connect(m_ui->teamdriveList, &QListWidget::itemSelectionChanged, | ||||
47 | this, &MainWindow::teamdriveSelected); | ||||
48 | } | ||||
49 | | ||||
50 | MainWindow::~MainWindow() | ||||
51 | { | ||||
52 | delete m_ui; | ||||
53 | } | ||||
54 | | ||||
55 | void MainWindow::authenticate() | ||||
56 | { | ||||
57 | KGAPI2::AccountPtr account(new KGAPI2::Account); | ||||
58 | account->setScopes( QList<QUrl>() << KGAPI2::Account::driveScopeUrl() ); | ||||
59 | | ||||
60 | /* Create AuthJob to retrieve OAuth tokens for the account */ | ||||
61 | KGAPI2::AuthJob *authJob = new KGAPI2::AuthJob( | ||||
62 | account, | ||||
63 | QStringLiteral("554041944266.apps.googleusercontent.com"), | ||||
64 | QStringLiteral("mdT1DjzohxN3npUUzkENT0gO")); | ||||
65 | connect(authJob, &KGAPI2::Job::finished, | ||||
66 | this, &MainWindow::slotAuthJobFinished); | ||||
67 | } | ||||
68 | | ||||
69 | void MainWindow::slotAuthJobFinished(KGAPI2::Job *job) | ||||
70 | { | ||||
71 | KGAPI2::AuthJob *authJob = qobject_cast<KGAPI2::AuthJob*>(job); | ||||
72 | Q_ASSERT(authJob); | ||||
73 | /* Always remember to delete the jobs, otherwise your application will | ||||
74 | * leak memory. */ | ||||
75 | authJob->deleteLater(); | ||||
76 | | ||||
77 | if (authJob->error() != KGAPI2::NoError) { | ||||
78 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(authJob->errorString())); | ||||
79 | m_ui->errorLabel->setVisible(true); | ||||
80 | return; | ||||
81 | } | ||||
82 | | ||||
83 | m_account = authJob->account(); | ||||
84 | | ||||
85 | m_ui->authStatusLabel->setText(QStringLiteral("Authenticated")); | ||||
86 | m_ui->teamdriveListButton->setEnabled(true); | ||||
87 | m_ui->authButton->setEnabled(false); | ||||
88 | } | ||||
89 | | ||||
90 | void MainWindow::fetchTeamdriveList() | ||||
91 | { | ||||
92 | if (m_account.isNull()) { | ||||
93 | m_ui->errorLabel->setText(QStringLiteral("Error: Please authenticate first")); | ||||
94 | m_ui->errorLabel->setVisible(true); | ||||
95 | m_ui->authButton->setVisible(true); | ||||
96 | return; | ||||
97 | } | ||||
98 | | ||||
99 | KGAPI2::Drive::TeamdriveFetchJob *fetchJob = new KGAPI2::Drive::TeamdriveFetchJob(m_account, this); | ||||
100 | connect(fetchJob, &KGAPI2::Job::finished, | ||||
101 | this, &MainWindow::slotFetchJobFinished); | ||||
102 | | ||||
103 | m_ui->teamdriveListButton->setEnabled(false); | ||||
104 | } | ||||
105 | | ||||
106 | void MainWindow::slotFetchJobFinished(KGAPI2::Job *job) | ||||
107 | { | ||||
108 | KGAPI2::Drive::TeamdriveFetchJob *fetchJob = qobject_cast<KGAPI2::Drive::TeamdriveFetchJob*>(job); | ||||
109 | Q_ASSERT(fetchJob); | ||||
110 | fetchJob->deleteLater(); | ||||
111 | | ||||
112 | if (fetchJob->error() != KGAPI2::NoError) { | ||||
113 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString())); | ||||
114 | m_ui->errorLabel->setVisible(true); | ||||
115 | m_ui->teamdriveListButton->setEnabled(true); | ||||
116 | return; | ||||
117 | } | ||||
118 | | ||||
119 | /* Get all items the job has retrieved */ | ||||
120 | const KGAPI2::ObjectsList objects = fetchJob->items(); | ||||
121 | | ||||
122 | for (const KGAPI2::ObjectPtr &object : objects) { | ||||
123 | const KGAPI2::Drive::TeamdrivePtr teamdrive = object.dynamicCast<KGAPI2::Drive::Teamdrive>(); | ||||
124 | | ||||
125 | /* Convert the teamdrive to QListWidget item */ | ||||
126 | QListWidgetItem *item = new QListWidgetItem(m_ui->teamdriveList); | ||||
127 | item->setText(teamdrive->name()); | ||||
128 | item->setData(Qt::UserRole, teamdrive->id()); | ||||
129 | | ||||
130 | m_ui->teamdriveList->addItem(item); | ||||
131 | } | ||||
132 | | ||||
133 | m_ui->teamdriveListButton->setEnabled(true); | ||||
134 | } | ||||
135 | | ||||
136 | void MainWindow::teamdriveSelected() | ||||
137 | { | ||||
138 | if (m_ui->teamdriveList->selectedItems().count() == 0) { | ||||
139 | m_ui->teamdrivePreview->clear(); | ||||
140 | return; | ||||
141 | } | ||||
142 | | ||||
143 | const QString id = m_ui->teamdriveList->selectedItems().at(0)->data(Qt::UserRole).toString(); | ||||
144 | | ||||
145 | KGAPI2::Drive::FileSearchQuery query; | ||||
146 | query.addQuery(KGAPI2::Drive::FileSearchQuery::Trashed, KGAPI2::Drive::FileSearchQuery::Equals, false); | ||||
147 | query.addQuery(KGAPI2::Drive::FileSearchQuery::Parents, KGAPI2::Drive::FileSearchQuery::In, id); | ||||
148 | | ||||
149 | KGAPI2::Drive::FileFetchJob *fileFetchJob = new KGAPI2::Drive::FileFetchJob(query, m_account, nullptr); | ||||
150 | fileFetchJob->setIncludeTeamDriveItems(true); | ||||
151 | fileFetchJob->setFields((KGAPI2::Drive::FileFetchJob::BasicFields & ~KGAPI2::Drive::FileFetchJob::Permissions) | ||||
152 | | KGAPI2::Drive::FileFetchJob::Labels | ||||
153 | | KGAPI2::Drive::FileFetchJob::ExportLinks | ||||
154 | | KGAPI2::Drive::FileFetchJob::LastViewedByMeDate); | ||||
155 | connect(fileFetchJob, &KGAPI2::Job::finished, | ||||
156 | this, &MainWindow::slotTeamdriveFetchJobFinished); | ||||
157 | } | ||||
158 | | ||||
159 | void MainWindow::slotTeamdriveFetchJobFinished(KGAPI2::Job *job) | ||||
160 | { | ||||
161 | KGAPI2::Drive::FileFetchJob *fetchJob = qobject_cast<KGAPI2::Drive::FileFetchJob*>(job); | ||||
162 | Q_ASSERT(fetchJob); | ||||
163 | fetchJob->deleteLater(); | ||||
164 | | ||||
165 | if (fetchJob->error() != KGAPI2::NoError) { | ||||
166 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString())); | ||||
167 | m_ui->errorLabel->setVisible(true); | ||||
168 | m_ui->teamdriveListButton->setEnabled(true); | ||||
169 | return; | ||||
170 | } | ||||
171 | | ||||
172 | /* Get all items we have received from Google (should be just one) */ | ||||
173 | KGAPI2::ObjectsList objects = fetchJob->items(); | ||||
174 | | ||||
175 | QString text; | ||||
176 | Q_FOREACH (const KGAPI2::ObjectPtr &object, objects) { | ||||
177 | const KGAPI2::Drive::FilePtr file = object.dynamicCast<KGAPI2::Drive::File>(); | ||||
178 | text += QStringLiteral("%1").arg(file->title()); | ||||
179 | text += QLatin1Char('\n'); | ||||
180 | } | ||||
181 | | ||||
182 | m_ui->teamdrivePreview->setText(text); | ||||
183 | } |