Changeset View
Changeset View
Standalone View
Standalone View
examples/drives/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/drives.h> | ||||
26 | #include <drive/drivescreatejob.h> | ||||
27 | #include <drive/drivesdeletejob.h> | ||||
28 | #include <drive/drivesfetchjob.h> | ||||
29 | #include <drive/drivesmodifyjob.h> | ||||
30 | #include <drive/driveshidejob.h> | ||||
31 | #include <drive/drivessearchquery.h> | ||||
32 | #include <drive/file.h> | ||||
33 | #include <drive/filesearchquery.h> | ||||
34 | #include <drive/filefetchjob.h> | ||||
35 | #include <authjob.h> | ||||
36 | #include <account.h> | ||||
37 | | ||||
38 | #include <QListWidgetItem> | ||||
39 | #include <QUuid> | ||||
40 | | ||||
41 | MainWindow::MainWindow(QWidget * parent): | ||||
42 | QMainWindow(parent), | ||||
43 | m_ui(new Ui::MainWindow) | ||||
44 | { | ||||
45 | /* Initialize GUI */ | ||||
46 | m_ui->setupUi(this); | ||||
47 | m_ui->errorLabel->setVisible(false); | ||||
48 | connect(m_ui->authButton, &QAbstractButton::clicked, | ||||
49 | this, &MainWindow::authenticate); | ||||
50 | connect(m_ui->newDrivesButton, &QAbstractButton::clicked, | ||||
51 | this, &MainWindow::createDrives); | ||||
52 | connect(m_ui->drivesListButton, &QAbstractButton::clicked, | ||||
53 | this, &MainWindow::fetchDrivesList); | ||||
54 | connect(m_ui->drivesSelectedDeleteButton, &QAbstractButton::clicked, | ||||
55 | this, &MainWindow::deleteSelectedDrives); | ||||
56 | connect(m_ui->renameDrivesButton, &QAbstractButton::clicked, | ||||
57 | this, &MainWindow::renameSelectedDrives); | ||||
58 | connect(m_ui->hideDrivesButton, &QAbstractButton::clicked, | ||||
59 | this, &MainWindow::hideSelectedDrives); | ||||
60 | connect(m_ui->unhideDrivesButton, &QAbstractButton::clicked, | ||||
61 | this, &MainWindow::unhideSelectedDrives); | ||||
62 | connect(m_ui->drivesList, &QListWidget::itemSelectionChanged, | ||||
63 | this, &MainWindow::drivesSelected); | ||||
64 | connect(m_ui->drivesPreviewList, &QListWidget::itemSelectionChanged, | ||||
65 | this, &MainWindow::drivesItemSelected); | ||||
66 | } | ||||
67 | | ||||
68 | MainWindow::~MainWindow() | ||||
69 | { | ||||
70 | delete m_ui; | ||||
71 | } | ||||
72 | | ||||
73 | void MainWindow::authenticate() | ||||
74 | { | ||||
75 | KGAPI2::AccountPtr account(new KGAPI2::Account); | ||||
76 | account->setScopes( QList<QUrl>() << KGAPI2::Account::driveScopeUrl() ); | ||||
77 | | ||||
78 | /* Create AuthJob to retrieve OAuth tokens for the account */ | ||||
79 | KGAPI2::AuthJob *authJob = new KGAPI2::AuthJob( | ||||
80 | account, | ||||
81 | QStringLiteral("554041944266.apps.googleusercontent.com"), | ||||
82 | QStringLiteral("mdT1DjzohxN3npUUzkENT0gO")); | ||||
83 | connect(authJob, &KGAPI2::Job::finished, | ||||
84 | this, &MainWindow::slotAuthJobFinished); | ||||
85 | } | ||||
86 | | ||||
87 | void MainWindow::slotAuthJobFinished(KGAPI2::Job *job) | ||||
88 | { | ||||
89 | KGAPI2::AuthJob *authJob = qobject_cast<KGAPI2::AuthJob*>(job); | ||||
90 | Q_ASSERT(authJob); | ||||
91 | /* Always remember to delete the jobs, otherwise your application will | ||||
92 | * leak memory. */ | ||||
93 | authJob->deleteLater(); | ||||
94 | | ||||
95 | if (authJob->error() != KGAPI2::NoError) { | ||||
96 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(authJob->errorString())); | ||||
97 | m_ui->errorLabel->setVisible(true); | ||||
98 | return; | ||||
99 | } | ||||
100 | | ||||
101 | m_account = authJob->account(); | ||||
102 | | ||||
103 | m_ui->authStatusLabel->setText(QStringLiteral("Authenticated")); | ||||
104 | m_ui->drivesListButton->setEnabled(true); | ||||
105 | m_ui->newDrivesEdit->setEnabled(true); | ||||
106 | m_ui->newDrivesButton->setEnabled(true); | ||||
107 | m_ui->authButton->setEnabled(false); | ||||
108 | } | ||||
109 | | ||||
110 | void MainWindow::createDrives() | ||||
111 | { | ||||
112 | QString drivesName = m_ui->newDrivesEdit->text(); | ||||
113 | if (drivesName.isEmpty()) { | ||||
114 | return; | ||||
115 | } | ||||
116 | QString requestId = QUuid::createUuid().toString(); | ||||
117 | | ||||
118 | KGAPI2::Drive::DrivesPtr drives = KGAPI2::Drive::DrivesPtr::create(); | ||||
119 | drives->setName(drivesName); | ||||
120 | | ||||
121 | KGAPI2::Drive::DrivesCreateJob *createJob = new KGAPI2::Drive::DrivesCreateJob(requestId, drives, m_account, this); | ||||
122 | connect(createJob, &KGAPI2::Job::finished, | ||||
123 | this, &MainWindow::slotDrivesCreateJobFinished); | ||||
124 | } | ||||
125 | | ||||
126 | void MainWindow::slotDrivesCreateJobFinished(KGAPI2::Job *job) | ||||
127 | { | ||||
128 | KGAPI2::Drive::DrivesCreateJob *createJob = qobject_cast<KGAPI2::Drive::DrivesCreateJob*>(job); | ||||
129 | Q_ASSERT(createJob); | ||||
130 | createJob->deleteLater(); | ||||
131 | | ||||
132 | if (createJob->error() != KGAPI2::NoError) { | ||||
133 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(createJob->errorString())); | ||||
134 | m_ui->errorLabel->setVisible(true); | ||||
135 | m_ui->drivesListButton->setEnabled(true); | ||||
136 | return; | ||||
137 | } | ||||
138 | | ||||
139 | m_ui->newDrivesEdit->clear(); | ||||
140 | fetchDrivesList(); | ||||
141 | } | ||||
142 | | ||||
143 | void MainWindow::renameSelectedDrives() | ||||
144 | { | ||||
145 | QString drivesName = m_ui->renameDrivesEdit->text(); | ||||
146 | if (drivesName.isEmpty()) { | ||||
147 | return; | ||||
148 | } | ||||
149 | QString drivesId = m_ui->drivesList->selectedItems().at(0)->data(Qt::UserRole).toString(); | ||||
150 | | ||||
151 | KGAPI2::Drive::DrivesPtr drives = KGAPI2::Drive::DrivesPtr::create(); | ||||
152 | drives->setId(drivesId); | ||||
153 | drives->setName(drivesName); | ||||
154 | | ||||
155 | KGAPI2::Drive::DrivesModifyJob *modifyJob = new KGAPI2::Drive::DrivesModifyJob(drives, m_account, this); | ||||
156 | connect(modifyJob, &KGAPI2::Job::finished, | ||||
157 | this, &MainWindow::slotDrivesModifyJobFinished); | ||||
158 | } | ||||
159 | | ||||
160 | void MainWindow::slotDrivesModifyJobFinished(KGAPI2::Job *job) | ||||
161 | { | ||||
162 | KGAPI2::Drive::DrivesModifyJob *modifyJob = qobject_cast<KGAPI2::Drive::DrivesModifyJob*>(job); | ||||
163 | Q_ASSERT(modifyJob); | ||||
164 | modifyJob->deleteLater(); | ||||
165 | | ||||
166 | if (modifyJob->error() != KGAPI2::NoError) { | ||||
167 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(modifyJob->errorString())); | ||||
168 | m_ui->errorLabel->setVisible(true); | ||||
169 | m_ui->drivesListButton->setEnabled(true); | ||||
170 | return; | ||||
171 | } | ||||
172 | | ||||
173 | fetchDrivesList(); | ||||
174 | } | ||||
175 | | ||||
176 | void MainWindow::hideSelectedDrives() | ||||
177 | { | ||||
178 | QString drivesId = m_ui->drivesList->selectedItems().at(0)->data(Qt::UserRole).toString(); | ||||
179 | | ||||
180 | KGAPI2::Drive::DrivesPtr drive = KGAPI2::Drive::DrivesPtr::create(); | ||||
181 | drive->setId(drivesId); | ||||
182 | | ||||
183 | KGAPI2::Drive::DrivesHideJob *hideJob = new KGAPI2::Drive::DrivesHideJob(drive, true, m_account, this); | ||||
184 | connect(hideJob, &KGAPI2::Job::finished, | ||||
185 | this, &MainWindow::slotDrivesHideJobFinished); | ||||
186 | } | ||||
187 | | ||||
188 | void MainWindow::unhideSelectedDrives() | ||||
189 | { | ||||
190 | QString drivesId = m_ui->drivesList->selectedItems().at(0)->data(Qt::UserRole).toString(); | ||||
191 | | ||||
192 | KGAPI2::Drive::DrivesPtr drive = KGAPI2::Drive::DrivesPtr::create(); | ||||
193 | drive->setId(drivesId); | ||||
194 | | ||||
195 | KGAPI2::Drive::DrivesHideJob *hideJob = new KGAPI2::Drive::DrivesHideJob(drive, false, m_account, this); | ||||
196 | connect(hideJob, &KGAPI2::Job::finished, | ||||
197 | this, &MainWindow::slotDrivesHideJobFinished); | ||||
198 | } | ||||
199 | | ||||
200 | void MainWindow::slotDrivesHideJobFinished(KGAPI2::Job *job) | ||||
201 | { | ||||
202 | KGAPI2::Drive::DrivesHideJob *hideJob = qobject_cast<KGAPI2::Drive::DrivesHideJob*>(job); | ||||
203 | Q_ASSERT(hideJob); | ||||
204 | hideJob->deleteLater(); | ||||
205 | | ||||
206 | if (hideJob->error() != KGAPI2::NoError) { | ||||
207 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(hideJob->errorString())); | ||||
208 | m_ui->errorLabel->setVisible(true); | ||||
209 | m_ui->drivesListButton->setEnabled(true); | ||||
210 | return; | ||||
211 | } | ||||
212 | | ||||
213 | fetchDrivesList(); | ||||
214 | } | ||||
215 | | ||||
216 | void MainWindow::fetchDrivesList() | ||||
217 | { | ||||
218 | if (m_account.isNull()) { | ||||
219 | m_ui->errorLabel->setText(QStringLiteral("Error: Please authenticate first")); | ||||
220 | m_ui->errorLabel->setVisible(true); | ||||
221 | m_ui->authButton->setVisible(true); | ||||
222 | return; | ||||
223 | } | ||||
224 | | ||||
225 | bool showHidden = m_ui->showHiddenCheckBox->isChecked(); | ||||
226 | | ||||
227 | KGAPI2::Drive::DrivesSearchQuery query; | ||||
228 | query.addQuery(KGAPI2::Drive::DrivesSearchQuery::Hidden, KGAPI2::Drive::DrivesSearchQuery::Equals, showHidden); | ||||
229 | | ||||
230 | KGAPI2::Drive::DrivesFetchJob *fetchJob = new KGAPI2::Drive::DrivesFetchJob(query, m_account, this); | ||||
231 | fetchJob->setUseDomainAdminAccess(false); | ||||
232 | fetchJob->setFields({ | ||||
233 | KGAPI2::Drive::Drives::Fields::Id, | ||||
234 | KGAPI2::Drive::Drives::Fields::Name, | ||||
235 | KGAPI2::Drive::Drives::Fields::Hidden}); | ||||
236 | connect(fetchJob, &KGAPI2::Job::finished, | ||||
237 | this, &MainWindow::slotFetchJobFinished); | ||||
238 | | ||||
239 | m_ui->drivesListButton->setEnabled(false); | ||||
240 | } | ||||
241 | | ||||
242 | void MainWindow::slotFetchJobFinished(KGAPI2::Job *job) | ||||
243 | { | ||||
244 | KGAPI2::Drive::DrivesFetchJob *fetchJob = qobject_cast<KGAPI2::Drive::DrivesFetchJob*>(job); | ||||
245 | Q_ASSERT(fetchJob); | ||||
246 | fetchJob->deleteLater(); | ||||
247 | | ||||
248 | if (fetchJob->error() != KGAPI2::NoError) { | ||||
249 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString())); | ||||
250 | m_ui->errorLabel->setVisible(true); | ||||
251 | m_ui->drivesListButton->setEnabled(true); | ||||
252 | return; | ||||
253 | } | ||||
254 | | ||||
255 | /* Get all items the job has retrieved */ | ||||
256 | const KGAPI2::ObjectsList objects = fetchJob->items(); | ||||
257 | m_ui->drivesList->clear(); | ||||
258 | for (const KGAPI2::ObjectPtr &object : objects) { | ||||
259 | const KGAPI2::Drive::DrivesPtr drives = object.dynamicCast<KGAPI2::Drive::Drives>(); | ||||
260 | | ||||
261 | /* Convert the drives to QListWidget item */ | ||||
262 | QListWidgetItem *item = new QListWidgetItem(m_ui->drivesList); | ||||
263 | item->setText(drives->name() + (drives->hidden() ? QStringLiteral(" (hidden)") : QStringLiteral(""))); | ||||
264 | item->setData(Qt::UserRole, drives->id()); | ||||
265 | | ||||
266 | m_ui->drivesList->addItem(item); | ||||
267 | } | ||||
268 | | ||||
269 | m_ui->drivesListButton->setEnabled(true); | ||||
270 | } | ||||
271 | | ||||
272 | void MainWindow::deleteSelectedDrives() { | ||||
273 | const QString drives_id = m_ui->drivesList->selectedItems().at(0)->data(Qt::UserRole).toString(); | ||||
274 | | ||||
275 | KGAPI2::Drive::DrivesDeleteJob *deleteJob = new KGAPI2::Drive::DrivesDeleteJob(drives_id, m_account, this); | ||||
276 | connect(deleteJob, &KGAPI2::Job::finished, | ||||
277 | this, &MainWindow::slotDrivesDeleteJobFinished); | ||||
278 | } | ||||
279 | | ||||
280 | void MainWindow::slotDrivesDeleteJobFinished(KGAPI2::Job *job) | ||||
281 | { | ||||
282 | KGAPI2::Drive::DrivesDeleteJob *deleteJob = qobject_cast<KGAPI2::Drive::DrivesDeleteJob*>(job); | ||||
283 | Q_ASSERT(deleteJob); | ||||
284 | deleteJob->deleteLater(); | ||||
285 | | ||||
286 | if (deleteJob->error() != KGAPI2::NoError) { | ||||
287 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(deleteJob->errorString())); | ||||
288 | m_ui->errorLabel->setVisible(true); | ||||
289 | m_ui->drivesListButton->setEnabled(true); | ||||
290 | return; | ||||
291 | } | ||||
292 | | ||||
293 | fetchDrivesList(); | ||||
294 | } | ||||
295 | | ||||
296 | void MainWindow::drivesSelected() | ||||
297 | { | ||||
298 | bool hasSelection = (m_ui->drivesList->selectedItems().count() != 0); | ||||
299 | | ||||
300 | m_ui->drivesSelectedDeleteButton->setEnabled(hasSelection); | ||||
301 | m_ui->renameDrivesButton->setEnabled(hasSelection); | ||||
302 | m_ui->hideDrivesButton->setEnabled(hasSelection); | ||||
303 | m_ui->unhideDrivesButton->setEnabled(hasSelection); | ||||
304 | m_ui->renameDrivesEdit->setEnabled(hasSelection); | ||||
305 | | ||||
306 | m_ui->drivesPreviewList->clear(); | ||||
307 | | ||||
308 | if (!hasSelection) { | ||||
309 | m_ui->renameDrivesEdit->clear(); | ||||
310 | return; | ||||
311 | } | ||||
312 | | ||||
313 | const QString id = m_ui->drivesList->selectedItems().at(0)->data(Qt::UserRole).toString(); | ||||
314 | const QString name = m_ui->drivesList->selectedItems().at(0)->data(Qt::DisplayRole).toString(); | ||||
315 | | ||||
316 | m_ui->renameDrivesEdit->setText(name); | ||||
317 | | ||||
318 | KGAPI2::Drive::FileSearchQuery query; | ||||
319 | query.addQuery(KGAPI2::Drive::FileSearchQuery::Trashed, KGAPI2::Drive::FileSearchQuery::Equals, false); | ||||
320 | query.addQuery(KGAPI2::Drive::FileSearchQuery::Parents, KGAPI2::Drive::FileSearchQuery::In, id); | ||||
321 | | ||||
322 | KGAPI2::Drive::FileFetchJob *fileFetchJob = new KGAPI2::Drive::FileFetchJob(query, m_account, nullptr); | ||||
323 | fileFetchJob->setFields({ | ||||
324 | KGAPI2::Drive::File::Fields::Id, | ||||
325 | KGAPI2::Drive::File::Fields::Title, | ||||
326 | }); | ||||
327 | connect(fileFetchJob, &KGAPI2::Job::finished, | ||||
328 | this, &MainWindow::slotDrivesFetchJobFinished); | ||||
329 | } | ||||
330 | | ||||
331 | void MainWindow::slotDrivesFetchJobFinished(KGAPI2::Job *job) | ||||
332 | { | ||||
333 | KGAPI2::Drive::FileFetchJob *fetchJob = qobject_cast<KGAPI2::Drive::FileFetchJob*>(job); | ||||
334 | Q_ASSERT(fetchJob); | ||||
335 | fetchJob->deleteLater(); | ||||
336 | | ||||
337 | if (fetchJob->error() != KGAPI2::NoError) { | ||||
338 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString())); | ||||
339 | m_ui->errorLabel->setVisible(true); | ||||
340 | m_ui->drivesListButton->setEnabled(true); | ||||
341 | return; | ||||
342 | } | ||||
343 | | ||||
344 | /* Get all items we have received from Google */ | ||||
345 | KGAPI2::ObjectsList objects = fetchJob->items(); | ||||
346 | | ||||
347 | Q_FOREACH (const KGAPI2::ObjectPtr &object, objects) { | ||||
348 | const KGAPI2::Drive::FilePtr file = object.dynamicCast<KGAPI2::Drive::File>(); | ||||
349 | | ||||
350 | /* Convert the drives to QListWidget item */ | ||||
351 | QListWidgetItem *item = new QListWidgetItem(m_ui->drivesPreviewList); | ||||
352 | item->setText(file->title()); | ||||
353 | item->setData(Qt::UserRole, file->id()); | ||||
354 | | ||||
355 | m_ui->drivesPreviewList->addItem(item); | ||||
356 | } | ||||
357 | } | ||||
358 | | ||||
359 | void MainWindow::drivesItemSelected() | ||||
360 | { | ||||
361 | bool hasSelection = (m_ui->drivesPreviewList->selectedItems().count() != 0); | ||||
362 | if (!hasSelection) { | ||||
363 | return; | ||||
364 | } | ||||
365 | | ||||
366 | const QString id = m_ui->drivesPreviewList->selectedItems().at(0)->data(Qt::UserRole).toString(); | ||||
367 | | ||||
368 | KGAPI2::Drive::FileFetchJob *fileFetchJob = new KGAPI2::Drive::FileFetchJob(id, m_account, nullptr); | ||||
369 | fileFetchJob->setFields({ | ||||
370 | KGAPI2::Drive::File::Fields::Title, | ||||
371 | KGAPI2::Drive::File::Fields::FileSize, | ||||
372 | }); | ||||
373 | connect(fileFetchJob, &KGAPI2::Job::finished, | ||||
374 | this, &MainWindow::slotDrivesItemFetchJobFinished); | ||||
375 | } | ||||
376 | | ||||
377 | void MainWindow::slotDrivesItemFetchJobFinished(KGAPI2::Job *job) | ||||
378 | { | ||||
379 | KGAPI2::Drive::FileFetchJob *fetchJob = qobject_cast<KGAPI2::Drive::FileFetchJob*>(job); | ||||
380 | Q_ASSERT(fetchJob); | ||||
381 | fetchJob->deleteLater(); | ||||
382 | | ||||
383 | if (fetchJob->error() != KGAPI2::NoError) { | ||||
384 | m_ui->errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString())); | ||||
385 | m_ui->errorLabel->setVisible(true); | ||||
386 | return; | ||||
387 | } | ||||
388 | | ||||
389 | KGAPI2::ObjectsList objects = fetchJob->items(); | ||||
390 | if (objects.size() != 1) { | ||||
391 | return; | ||||
392 | } | ||||
393 | | ||||
394 | KGAPI2::ObjectPtr object = objects.at(0); | ||||
395 | const KGAPI2::Drive::FilePtr file = object.dynamicCast<KGAPI2::Drive::File>(); | ||||
396 | QStringList msgBuilder; | ||||
397 | msgBuilder << file->title(); | ||||
398 | msgBuilder << QString::number(file->fileSize()) + QStringLiteral(" bytes"); | ||||
399 | QString msg = msgBuilder.join(QStringLiteral(", ")); | ||||
400 | m_ui->statusbar->showMessage(msg); | ||||
401 | } |