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