File indexing completed on 2024-05-12 05:22:09
0001 /* 0002 SPDX-FileCopyrightText: 2019 David Barchiesi <david@barchie.si> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "mainwindow.h" 0008 0009 #include "core/account.h" 0010 #include "core/authjob.h" 0011 #include "drive/drives.h" 0012 #include "drive/drivescreatejob.h" 0013 #include "drive/drivesdeletejob.h" 0014 #include "drive/drivesfetchjob.h" 0015 #include "drive/driveshidejob.h" 0016 #include "drive/drivesmodifyjob.h" 0017 #include "drive/drivessearchquery.h" 0018 #include "drive/file.h" 0019 #include "drive/filefetchjob.h" 0020 #include "drive/filesearchquery.h" 0021 0022 #include <QListWidgetItem> 0023 #include <QUuid> 0024 0025 MainWindow::MainWindow(QWidget *parent) 0026 : QMainWindow(parent) 0027 { 0028 /* Initialize GUI */ 0029 ui.setupUi(this); 0030 ui.errorLabel->setVisible(false); 0031 connect(ui.authButton, &QAbstractButton::clicked, this, &MainWindow::authenticate); 0032 connect(ui.newDrivesButton, &QAbstractButton::clicked, this, &MainWindow::createDrives); 0033 connect(ui.drivesListButton, &QAbstractButton::clicked, this, &MainWindow::fetchDrivesList); 0034 connect(ui.drivesSelectedDeleteButton, &QAbstractButton::clicked, this, &MainWindow::deleteSelectedDrives); 0035 connect(ui.renameDrivesButton, &QAbstractButton::clicked, this, &MainWindow::renameSelectedDrives); 0036 connect(ui.hideDrivesButton, &QAbstractButton::clicked, this, &MainWindow::hideSelectedDrives); 0037 connect(ui.unhideDrivesButton, &QAbstractButton::clicked, this, &MainWindow::unhideSelectedDrives); 0038 connect(ui.drivesList, &QListWidget::itemSelectionChanged, this, &MainWindow::drivesSelected); 0039 connect(ui.drivesPreviewList, &QListWidget::itemSelectionChanged, this, &MainWindow::drivesItemSelected); 0040 } 0041 0042 void MainWindow::authenticate() 0043 { 0044 auto account = KGAPI2::AccountPtr::create(); 0045 account->setScopes({KGAPI2::Account::driveScopeUrl()}); 0046 0047 /* Create AuthJob to retrieve OAuth tokens for the account */ 0048 auto *authJob = new KGAPI2::AuthJob(account, QStringLiteral("554041944266.apps.googleusercontent.com"), QStringLiteral("mdT1DjzohxN3npUUzkENT0gO")); 0049 connect(authJob, &KGAPI2::Job::finished, this, [this, authJob]() { 0050 /* Always remember to delete the jobs, otherwise your application will 0051 * leak memory. */ 0052 authJob->deleteLater(); 0053 0054 if (authJob->error() != KGAPI2::NoError) { 0055 ui.errorLabel->setText(QStringLiteral("Error: %1").arg(authJob->errorString())); 0056 ui.errorLabel->setVisible(true); 0057 return; 0058 } 0059 0060 m_account = authJob->account(); 0061 0062 ui.authStatusLabel->setText(QStringLiteral("Authenticated")); 0063 ui.drivesListButton->setEnabled(true); 0064 ui.newDrivesEdit->setEnabled(true); 0065 ui.newDrivesButton->setEnabled(true); 0066 ui.authButton->setEnabled(false); 0067 }); 0068 } 0069 0070 void MainWindow::createDrives() 0071 { 0072 const auto drivesName = ui.newDrivesEdit->text(); 0073 if (drivesName.isEmpty()) { 0074 return; 0075 } 0076 const auto requestId = QUuid::createUuid().toString(); 0077 0078 auto drives = KGAPI2::Drive::DrivesPtr::create(); 0079 drives->setName(drivesName); 0080 0081 auto *createJob = new KGAPI2::Drive::DrivesCreateJob(requestId, drives, m_account, this); 0082 connect(createJob, &KGAPI2::Job::finished, this, [this, createJob]() { 0083 createJob->deleteLater(); 0084 0085 if (createJob->error() != KGAPI2::NoError) { 0086 ui.errorLabel->setText(QStringLiteral("Error: %1").arg(createJob->errorString())); 0087 ui.errorLabel->setVisible(true); 0088 ui.drivesListButton->setEnabled(true); 0089 return; 0090 } 0091 0092 ui.newDrivesEdit->clear(); 0093 fetchDrivesList(); 0094 }); 0095 } 0096 0097 void MainWindow::renameSelectedDrives() 0098 { 0099 const auto drivesName = ui.renameDrivesEdit->text(); 0100 if (drivesName.isEmpty()) { 0101 return; 0102 } 0103 const auto drivesId = ui.drivesList->selectedItems().at(0)->data(Qt::UserRole).toString(); 0104 0105 auto drives = KGAPI2::Drive::DrivesPtr::create(); 0106 drives->setId(drivesId); 0107 drives->setName(drivesName); 0108 0109 auto *modifyJob = new KGAPI2::Drive::DrivesModifyJob(drives, m_account, this); 0110 connect(modifyJob, &KGAPI2::Job::finished, this, [this, modifyJob]() { 0111 modifyJob->deleteLater(); 0112 0113 if (modifyJob->error() != KGAPI2::NoError) { 0114 ui.errorLabel->setText(QStringLiteral("Error: %1").arg(modifyJob->errorString())); 0115 ui.errorLabel->setVisible(true); 0116 ui.drivesListButton->setEnabled(true); 0117 return; 0118 } 0119 0120 fetchDrivesList(); 0121 }); 0122 } 0123 0124 void MainWindow::hideSelectedDrives() 0125 { 0126 const auto drivesId = ui.drivesList->selectedItems().at(0)->data(Qt::UserRole).toString(); 0127 0128 auto drive = KGAPI2::Drive::DrivesPtr::create(); 0129 drive->setId(drivesId); 0130 0131 auto *hideJob = new KGAPI2::Drive::DrivesHideJob(drive, true, m_account, this); 0132 connect(hideJob, &KGAPI2::Job::finished, this, &MainWindow::slotDrivesHideJobFinished); 0133 } 0134 0135 void MainWindow::unhideSelectedDrives() 0136 { 0137 const auto drivesId = ui.drivesList->selectedItems().at(0)->data(Qt::UserRole).toString(); 0138 0139 auto drive = KGAPI2::Drive::DrivesPtr::create(); 0140 drive->setId(drivesId); 0141 0142 auto *hideJob = new KGAPI2::Drive::DrivesHideJob(drive, false, m_account, this); 0143 connect(hideJob, &KGAPI2::Job::finished, this, &MainWindow::slotDrivesHideJobFinished); 0144 } 0145 0146 void MainWindow::slotDrivesHideJobFinished(KGAPI2::Job *job) 0147 { 0148 auto *hideJob = qobject_cast<KGAPI2::Drive::DrivesHideJob *>(job); 0149 Q_ASSERT(hideJob); 0150 hideJob->deleteLater(); 0151 0152 if (hideJob->error() != KGAPI2::NoError) { 0153 ui.errorLabel->setText(QStringLiteral("Error: %1").arg(hideJob->errorString())); 0154 ui.errorLabel->setVisible(true); 0155 ui.drivesListButton->setEnabled(true); 0156 return; 0157 } 0158 0159 fetchDrivesList(); 0160 } 0161 0162 void MainWindow::fetchDrivesList() 0163 { 0164 if (m_account.isNull()) { 0165 ui.errorLabel->setText(QStringLiteral("Error: Please authenticate first")); 0166 ui.errorLabel->setVisible(true); 0167 ui.authButton->setVisible(true); 0168 return; 0169 } 0170 0171 const bool showHidden = ui.showHiddenCheckBox->isChecked(); 0172 0173 KGAPI2::Drive::DrivesSearchQuery query; 0174 query.addQuery(KGAPI2::Drive::DrivesSearchQuery::Hidden, KGAPI2::Drive::DrivesSearchQuery::Equals, showHidden); 0175 0176 auto *fetchJob = new KGAPI2::Drive::DrivesFetchJob(query, m_account, this); 0177 fetchJob->setUseDomainAdminAccess(false); 0178 fetchJob->setFields({KGAPI2::Drive::Drives::Fields::Id, KGAPI2::Drive::Drives::Fields::Name, KGAPI2::Drive::Drives::Fields::Hidden}); 0179 connect(fetchJob, &KGAPI2::Job::finished, this, [this, fetchJob]() { 0180 fetchJob->deleteLater(); 0181 0182 if (fetchJob->error() != KGAPI2::NoError) { 0183 ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fetchJob->errorString())); 0184 ui.errorLabel->setVisible(true); 0185 ui.drivesListButton->setEnabled(true); 0186 return; 0187 } 0188 0189 /* Get all items the job has retrieved */ 0190 const auto objects = fetchJob->items(); 0191 ui.drivesList->clear(); 0192 for (const auto &object : objects) { 0193 const auto drives = object.dynamicCast<KGAPI2::Drive::Drives>(); 0194 0195 /* Convert the drives to QListWidget item */ 0196 auto *item = new QListWidgetItem(ui.drivesList); 0197 item->setText(drives->name() + (drives->hidden() ? QStringLiteral(" (hidden)") : QStringLiteral(""))); 0198 item->setData(Qt::UserRole, drives->id()); 0199 0200 ui.drivesList->addItem(item); 0201 } 0202 0203 ui.drivesListButton->setEnabled(true); 0204 }); 0205 0206 ui.drivesListButton->setEnabled(false); 0207 } 0208 0209 void MainWindow::deleteSelectedDrives() 0210 { 0211 const auto drives_id = ui.drivesList->selectedItems().at(0)->data(Qt::UserRole).toString(); 0212 0213 auto *deleteJob = new KGAPI2::Drive::DrivesDeleteJob(drives_id, m_account, this); 0214 connect(deleteJob, &KGAPI2::Job::finished, this, [this, deleteJob]() { 0215 deleteJob->deleteLater(); 0216 0217 if (deleteJob->error() != KGAPI2::NoError) { 0218 ui.errorLabel->setText(QStringLiteral("Error: %1").arg(deleteJob->errorString())); 0219 ui.errorLabel->setVisible(true); 0220 ui.drivesListButton->setEnabled(true); 0221 return; 0222 } 0223 0224 fetchDrivesList(); 0225 }); 0226 } 0227 0228 void MainWindow::drivesSelected() 0229 { 0230 const bool hasSelection = (ui.drivesList->selectedItems().count() != 0); 0231 0232 ui.drivesSelectedDeleteButton->setEnabled(hasSelection); 0233 ui.renameDrivesButton->setEnabled(hasSelection); 0234 ui.hideDrivesButton->setEnabled(hasSelection); 0235 ui.unhideDrivesButton->setEnabled(hasSelection); 0236 ui.renameDrivesEdit->setEnabled(hasSelection); 0237 0238 ui.drivesPreviewList->clear(); 0239 0240 if (!hasSelection) { 0241 ui.renameDrivesEdit->clear(); 0242 return; 0243 } 0244 0245 const auto id = ui.drivesList->selectedItems().at(0)->data(Qt::UserRole).toString(); 0246 const auto name = ui.drivesList->selectedItems().at(0)->data(Qt::DisplayRole).toString(); 0247 0248 ui.renameDrivesEdit->setText(name); 0249 0250 KGAPI2::Drive::FileSearchQuery query; 0251 query.addQuery(KGAPI2::Drive::FileSearchQuery::Trashed, KGAPI2::Drive::FileSearchQuery::Equals, false); 0252 query.addQuery(KGAPI2::Drive::FileSearchQuery::Parents, KGAPI2::Drive::FileSearchQuery::In, id); 0253 0254 auto *fileFetchJob = new KGAPI2::Drive::FileFetchJob(query, m_account, nullptr); 0255 fileFetchJob->setFields({ 0256 KGAPI2::Drive::File::Fields::Id, 0257 KGAPI2::Drive::File::Fields::Title, 0258 }); 0259 connect(fileFetchJob, &KGAPI2::Job::finished, this, [this, fileFetchJob]() { 0260 fileFetchJob->deleteLater(); 0261 0262 if (fileFetchJob->error() != KGAPI2::NoError) { 0263 ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fileFetchJob->errorString())); 0264 ui.errorLabel->setVisible(true); 0265 ui.drivesListButton->setEnabled(true); 0266 return; 0267 } 0268 0269 /* Get all items we have received from Google */ 0270 const auto objects = fileFetchJob->items(); 0271 0272 for (const auto &object : objects) { 0273 const auto file = object.dynamicCast<KGAPI2::Drive::File>(); 0274 0275 /* Convert the drives to QListWidget item */ 0276 auto *item = new QListWidgetItem(ui.drivesPreviewList); 0277 item->setText(file->title()); 0278 item->setData(Qt::UserRole, file->id()); 0279 0280 ui.drivesPreviewList->addItem(item); 0281 } 0282 }); 0283 } 0284 0285 void MainWindow::drivesItemSelected() 0286 { 0287 const bool hasSelection = (ui.drivesPreviewList->selectedItems().count() != 0); 0288 if (!hasSelection) { 0289 return; 0290 } 0291 0292 const auto id = ui.drivesPreviewList->selectedItems().at(0)->data(Qt::UserRole).toString(); 0293 0294 auto *fileFetchJob = new KGAPI2::Drive::FileFetchJob(id, m_account, nullptr); 0295 fileFetchJob->setFields({ 0296 KGAPI2::Drive::File::Fields::Title, 0297 KGAPI2::Drive::File::Fields::FileSize, 0298 }); 0299 connect(fileFetchJob, &KGAPI2::Job::finished, this, [this, fileFetchJob]() { 0300 fileFetchJob->deleteLater(); 0301 0302 if (fileFetchJob->error() != KGAPI2::NoError) { 0303 ui.errorLabel->setText(QStringLiteral("Error: %1").arg(fileFetchJob->errorString())); 0304 ui.errorLabel->setVisible(true); 0305 return; 0306 } 0307 0308 const auto objects = fileFetchJob->items(); 0309 if (objects.size() != 1) { 0310 return; 0311 } 0312 0313 const auto object = objects.at(0); 0314 const auto file = object.dynamicCast<KGAPI2::Drive::File>(); 0315 QStringList msgBuilder; 0316 msgBuilder << file->title(); 0317 msgBuilder << QString::number(file->fileSize()) + QStringLiteral(" bytes"); 0318 QString msg = msgBuilder.join(QLatin1StringView(", ")); 0319 ui.statusbar->showMessage(msg); 0320 }); 0321 } 0322 0323 #include "moc_mainwindow.cpp"