File: mainwindow.cpp

package info (click to toggle)
libkgapi5 22.12.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,452 kB
  • sloc: cpp: 29,921; ansic: 979; xml: 142; makefile: 18; sh: 1
file content (321 lines) | stat: -rw-r--r-- 11,649 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
    SPDX-FileCopyrightText: 2019 David Barchiesi <david@barchie.si>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "mainwindow.h"

#include "core/account.h"
#include "core/authjob.h"
#include "drive/drives.h"
#include "drive/drivescreatejob.h"
#include "drive/drivesdeletejob.h"
#include "drive/drivesfetchjob.h"
#include "drive/driveshidejob.h"
#include "drive/drivesmodifyjob.h"
#include "drive/drivessearchquery.h"
#include "drive/file.h"
#include "drive/filefetchjob.h"
#include "drive/filesearchquery.h"

#include <QListWidgetItem>
#include <QUuid>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* Initialize GUI */
    ui.setupUi(this);
    ui.errorLabel->setVisible(false);
    connect(ui.authButton, &QAbstractButton::clicked, this, &MainWindow::authenticate);
    connect(ui.newDrivesButton, &QAbstractButton::clicked, this, &MainWindow::createDrives);
    connect(ui.drivesListButton, &QAbstractButton::clicked, this, &MainWindow::fetchDrivesList);
    connect(ui.drivesSelectedDeleteButton, &QAbstractButton::clicked, this, &MainWindow::deleteSelectedDrives);
    connect(ui.renameDrivesButton, &QAbstractButton::clicked, this, &MainWindow::renameSelectedDrives);
    connect(ui.hideDrivesButton, &QAbstractButton::clicked, this, &MainWindow::hideSelectedDrives);
    connect(ui.unhideDrivesButton, &QAbstractButton::clicked, this, &MainWindow::unhideSelectedDrives);
    connect(ui.drivesList, &QListWidget::itemSelectionChanged, this, &MainWindow::drivesSelected);
    connect(ui.drivesPreviewList, &QListWidget::itemSelectionChanged, this, &MainWindow::drivesItemSelected);
}

void MainWindow::authenticate()
{
    auto account = KGAPI2::AccountPtr::create();
    account->setScopes({KGAPI2::Account::driveScopeUrl()});

    /* Create AuthJob to retrieve OAuth tokens for the account */
    auto *authJob = new KGAPI2::AuthJob(account, QStringLiteral("554041944266.apps.googleusercontent.com"), QStringLiteral("mdT1DjzohxN3npUUzkENT0gO"));
    connect(authJob, &KGAPI2::Job::finished, this, [this, authJob]() {
        /* Always remember to delete the jobs, otherwise your application will
         * leak memory. */
        authJob->deleteLater();

        if (authJob->error() != KGAPI2::NoError) {
            ui.errorLabel->setText(QStringLiteral("Error: %1").arg(authJob->errorString()));
            ui.errorLabel->setVisible(true);
            return;
        }

        m_account = authJob->account();

        ui.authStatusLabel->setText(QStringLiteral("Authenticated"));
        ui.drivesListButton->setEnabled(true);
        ui.newDrivesEdit->setEnabled(true);
        ui.newDrivesButton->setEnabled(true);
        ui.authButton->setEnabled(false);
    });
}

void MainWindow::createDrives()
{
    const auto drivesName = ui.newDrivesEdit->text();
    if (drivesName.isEmpty()) {
        return;
    }
    const auto requestId = QUuid::createUuid().toString();

    auto drives = KGAPI2::Drive::DrivesPtr::create();
    drives->setName(drivesName);

    auto *createJob = new KGAPI2::Drive::DrivesCreateJob(requestId, drives, m_account, this);
    connect(createJob, &KGAPI2::Job::finished, this, [this, createJob]() {
        createJob->deleteLater();

        if (createJob->error() != KGAPI2::NoError) {
            ui.errorLabel->setText(QStringLiteral("Error: %1").arg(createJob->errorString()));
            ui.errorLabel->setVisible(true);
            ui.drivesListButton->setEnabled(true);
            return;
        }

        ui.newDrivesEdit->clear();
        fetchDrivesList();
    });
}

void MainWindow::renameSelectedDrives()
{
    const auto drivesName = ui.renameDrivesEdit->text();
    if (drivesName.isEmpty()) {
        return;
    }
    const auto drivesId = ui.drivesList->selectedItems().at(0)->data(Qt::UserRole).toString();

    auto drives = KGAPI2::Drive::DrivesPtr::create();
    drives->setId(drivesId);
    drives->setName(drivesName);

    auto *modifyJob = new KGAPI2::Drive::DrivesModifyJob(drives, m_account, this);
    connect(modifyJob, &KGAPI2::Job::finished, this, [this, modifyJob]() {
        modifyJob->deleteLater();

        if (modifyJob->error() != KGAPI2::NoError) {
            ui.errorLabel->setText(QStringLiteral("Error: %1").arg(modifyJob->errorString()));
            ui.errorLabel->setVisible(true);
            ui.drivesListButton->setEnabled(true);
            return;
        }

        fetchDrivesList();
    });
}

void MainWindow::hideSelectedDrives()
{
    const auto drivesId = ui.drivesList->selectedItems().at(0)->data(Qt::UserRole).toString();

    auto drive = KGAPI2::Drive::DrivesPtr::create();
    drive->setId(drivesId);

    auto *hideJob = new KGAPI2::Drive::DrivesHideJob(drive, true, m_account, this);
    connect(hideJob, &KGAPI2::Job::finished, this, &MainWindow::slotDrivesHideJobFinished);
}

void MainWindow::unhideSelectedDrives()
{
    const auto drivesId = ui.drivesList->selectedItems().at(0)->data(Qt::UserRole).toString();

    auto drive = KGAPI2::Drive::DrivesPtr::create();
    drive->setId(drivesId);

    auto *hideJob = new KGAPI2::Drive::DrivesHideJob(drive, false, m_account, this);
    connect(hideJob, &KGAPI2::Job::finished, this, &MainWindow::slotDrivesHideJobFinished);
}

void MainWindow::slotDrivesHideJobFinished(KGAPI2::Job *job)
{
    auto *hideJob = qobject_cast<KGAPI2::Drive::DrivesHideJob *>(job);
    Q_ASSERT(hideJob);
    hideJob->deleteLater();

    if (hideJob->error() != KGAPI2::NoError) {
        ui.errorLabel->setText(QStringLiteral("Error: %1").arg(hideJob->errorString()));
        ui.errorLabel->setVisible(true);
        ui.drivesListButton->setEnabled(true);
        return;
    }

    fetchDrivesList();
}

void MainWindow::fetchDrivesList()
{
    if (m_account.isNull()) {
        ui.errorLabel->setText(QStringLiteral("Error: Please authenticate first"));
        ui.errorLabel->setVisible(true);
        ui.authButton->setVisible(true);
        return;
    }

    const bool showHidden = ui.showHiddenCheckBox->isChecked();

    KGAPI2::Drive::DrivesSearchQuery query;
    query.addQuery(KGAPI2::Drive::DrivesSearchQuery::Hidden, KGAPI2::Drive::DrivesSearchQuery::Equals, showHidden);

    auto *fetchJob = new KGAPI2::Drive::DrivesFetchJob(query, m_account, this);
    fetchJob->setUseDomainAdminAccess(false);
    fetchJob->setFields({KGAPI2::Drive::Drives::Fields::Id, KGAPI2::Drive::Drives::Fields::Name, KGAPI2::Drive::Drives::Fields::Hidden});
    connect(fetchJob, &KGAPI2::Job::finished, this, [this, fetchJob]() {
        fetchJob->deleteLater();

        if (fetchJob->error() != KGAPI2::NoError) {
            ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString()));
            ui.errorLabel->setVisible(true);
            ui.drivesListButton->setEnabled(true);
            return;
        }

        /* Get all items the job has retrieved */
        const auto objects = fetchJob->items();
        ui.drivesList->clear();
        for (const auto &object : objects) {
            const auto drives = object.dynamicCast<KGAPI2::Drive::Drives>();

            /* Convert the drives to QListWidget item */
            auto *item = new QListWidgetItem(ui.drivesList);
            item->setText(drives->name() + (drives->hidden() ? QStringLiteral(" (hidden)") : QStringLiteral("")));
            item->setData(Qt::UserRole, drives->id());

            ui.drivesList->addItem(item);
        }

        ui.drivesListButton->setEnabled(true);
    });

    ui.drivesListButton->setEnabled(false);
}

void MainWindow::deleteSelectedDrives()
{
    const auto drives_id = ui.drivesList->selectedItems().at(0)->data(Qt::UserRole).toString();

    auto *deleteJob = new KGAPI2::Drive::DrivesDeleteJob(drives_id, m_account, this);
    connect(deleteJob, &KGAPI2::Job::finished, this, [this, deleteJob]() {
        deleteJob->deleteLater();

        if (deleteJob->error() != KGAPI2::NoError) {
            ui.errorLabel->setText(QStringLiteral("Error: %1").arg(deleteJob->errorString()));
            ui.errorLabel->setVisible(true);
            ui.drivesListButton->setEnabled(true);
            return;
        }

        fetchDrivesList();
    });
}

void MainWindow::drivesSelected()
{
    const bool hasSelection = (ui.drivesList->selectedItems().count() != 0);

    ui.drivesSelectedDeleteButton->setEnabled(hasSelection);
    ui.renameDrivesButton->setEnabled(hasSelection);
    ui.hideDrivesButton->setEnabled(hasSelection);
    ui.unhideDrivesButton->setEnabled(hasSelection);
    ui.renameDrivesEdit->setEnabled(hasSelection);

    ui.drivesPreviewList->clear();

    if (!hasSelection) {
        ui.renameDrivesEdit->clear();
        return;
    }

    const auto id = ui.drivesList->selectedItems().at(0)->data(Qt::UserRole).toString();
    const auto name = ui.drivesList->selectedItems().at(0)->data(Qt::DisplayRole).toString();

    ui.renameDrivesEdit->setText(name);

    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);

    auto *fileFetchJob = new KGAPI2::Drive::FileFetchJob(query, m_account, nullptr);
    fileFetchJob->setFields({
        KGAPI2::Drive::File::Fields::Id,
        KGAPI2::Drive::File::Fields::Title,
    });
    connect(fileFetchJob, &KGAPI2::Job::finished, this, [this, fileFetchJob]() {
        fileFetchJob->deleteLater();

        if (fileFetchJob->error() != KGAPI2::NoError) {
            ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fileFetchJob->errorString()));
            ui.errorLabel->setVisible(true);
            ui.drivesListButton->setEnabled(true);
            return;
        }

        /* Get all items we have received from Google */
        const auto objects = fileFetchJob->items();

        for (const auto &object : objects) {
            const auto file = object.dynamicCast<KGAPI2::Drive::File>();

            /* Convert the drives to QListWidget item */
            auto *item = new QListWidgetItem(ui.drivesPreviewList);
            item->setText(file->title());
            item->setData(Qt::UserRole, file->id());

            ui.drivesPreviewList->addItem(item);
        }
    });
}

void MainWindow::drivesItemSelected()
{
    const bool hasSelection = (ui.drivesPreviewList->selectedItems().count() != 0);
    if (!hasSelection) {
        return;
    }

    const auto id = ui.drivesPreviewList->selectedItems().at(0)->data(Qt::UserRole).toString();

    auto *fileFetchJob = new KGAPI2::Drive::FileFetchJob(id, m_account, nullptr);
    fileFetchJob->setFields({
        KGAPI2::Drive::File::Fields::Title,
        KGAPI2::Drive::File::Fields::FileSize,
    });
    connect(fileFetchJob, &KGAPI2::Job::finished, this, [this, fileFetchJob]() {
        fileFetchJob->deleteLater();

        if (fileFetchJob->error() != KGAPI2::NoError) {
            ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fileFetchJob->errorString()));
            ui.errorLabel->setVisible(true);
            return;
        }

        const auto objects = fileFetchJob->items();
        if (objects.size() != 1) {
            return;
        }

        const auto object = objects.at(0);
        const auto file = object.dynamicCast<KGAPI2::Drive::File>();
        QStringList msgBuilder;
        msgBuilder << file->title();
        msgBuilder << QString::number(file->fileSize()) + QStringLiteral(" bytes");
        QString msg = msgBuilder.join(QLatin1String(", "));
        ui.statusbar->showMessage(msg);
    });
}