/src/integration/propertiesplugin/gdrivepropertiesplugin.cpp

https://github.com/KDE/kio-gdrive · C++ · 98 lines · 67 code · 23 blank · 8 comment · 6 complexity · 03ea813a1c13dc06bb60af9115f5cfb2 MD5 · raw file

  1. /*
  2. * SPDX-FileCopyrightText: 2020 David Barchiesi <david@barchie.si>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
  5. */
  6. #include "gdrivepropertiesplugin.h"
  7. #include "../../gdrivedebug.h"
  8. #include "../../gdrive_udsentry.h"
  9. #include <KPluginFactory>
  10. #include <QClipboard>
  11. #include <QDesktopServices>
  12. #include <KIO/StatJob>
  13. K_PLUGIN_CLASS_WITH_JSON(GDrivePropertiesPlugin, "gdrivepropertiesplugin.json")
  14. GDrivePropertiesPlugin::GDrivePropertiesPlugin(QObject *parent, const QList<QVariant> &args)
  15. : KPropertiesDialogPlugin(qobject_cast<KPropertiesDialog *>(parent))
  16. {
  17. Q_UNUSED(args)
  18. qCDebug(GDRIVE) << "Starting Google Drive properties tab";
  19. // Ignore if more than one file is selected
  20. if (properties->items().size() != 1) {
  21. qCDebug(GDRIVE) << "Can't show Google Drive properties tab for more than one item";
  22. return;
  23. }
  24. m_item = properties->items().at(0);
  25. // Ignore if not a Google Drive url
  26. if (m_item.url().scheme() != QLatin1String("gdrive")) {
  27. qCDebug(GDRIVE) << "Can't show Google Drive properties for non Google Drive entries";
  28. return;
  29. }
  30. m_ui.setupUi(&m_widget);
  31. // Re stat() the item because the entry is probably lacking required information.
  32. const KIO::StatJob* job = KIO::stat(m_item.url(), KIO::HideProgressInfo);
  33. connect(job, &KJob::finished, this, &GDrivePropertiesPlugin::statJobFinished);
  34. }
  35. void GDrivePropertiesPlugin::showEntryDetails(const KIO::UDSEntry &entry)
  36. {
  37. const QString id = entry.stringValue(GDriveUDSEntryExtras::Id);
  38. m_ui.idValue->setText(id);
  39. const QString created = m_item.timeString(KFileItem::CreationTime);
  40. m_ui.createdValue->setText(created);
  41. const QString modified = m_item.timeString(KFileItem::ModificationTime);
  42. m_ui.modifiedValue->setText(modified);
  43. const QString lastViewedByMe = m_item.timeString(KFileItem::AccessTime);
  44. m_ui.lastViewedByMeValue->setText(lastViewedByMe);
  45. const QString version = entry.stringValue(GDriveUDSEntryExtras::Version);
  46. m_ui.versionValue->setText(version);
  47. const QString md5 = entry.stringValue(GDriveUDSEntryExtras::Md5);
  48. m_ui.md5Value->setText(md5);
  49. const QString lastModifyingUserName = entry.stringValue(GDriveUDSEntryExtras::LastModifyingUser);
  50. m_ui.lastModifiedByValue->setText(lastModifyingUserName);
  51. const QString owners = entry.stringValue(GDriveUDSEntryExtras::Owners);
  52. m_ui.ownersValue->setText(owners);
  53. const QString description = entry.stringValue(KIO::UDSEntry::UDS_COMMENT);
  54. m_ui.descriptionValue->setText(description);
  55. const QString gdriveLink = entry.stringValue(GDriveUDSEntryExtras::Url);
  56. connect(m_ui.urlOpenButton, &QPushButton::clicked, this, [gdriveLink]() {
  57. QDesktopServices::openUrl(QUrl(gdriveLink));
  58. });
  59. connect(m_ui.urlCopyButton, &QPushButton::clicked, this, [gdriveLink]() {
  60. QGuiApplication::clipboard()->setText(gdriveLink);
  61. });
  62. }
  63. void GDrivePropertiesPlugin::statJobFinished(KJob *job)
  64. {
  65. KIO::StatJob *statJob = qobject_cast<KIO::StatJob*>(job);
  66. if (!statJob || statJob->error()) {
  67. qCDebug(GDRIVE) << "Failed stat()ing" << statJob->url() << statJob->errorString();
  68. qCDebug(GDRIVE) << "Not showing Google Drive properties tab";
  69. return;
  70. }
  71. const KIO::UDSEntry entry = statJob->statResult();
  72. showEntryDetails(entry);
  73. properties->addPage(&m_widget, "G&oogle Drive");
  74. }
  75. #include "gdrivepropertiesplugin.moc"