File indexing completed on 2025-01-05 04:36:20

0001 /*
0002  * SPDX-FileCopyrightText: 2020 David Barchiesi <david@barchie.si>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "gdrivepropertiesplugin.h"
0008 #include "../../gdrive_udsentry.h"
0009 #include "../../gdrivedebug.h"
0010 
0011 #include <KIO/StatJob>
0012 #include <KLocalizedString>
0013 #include <KPluginFactory>
0014 #include <QClipboard>
0015 #include <QDesktopServices>
0016 #include <QtGlobal>
0017 
0018 K_PLUGIN_CLASS_WITH_JSON(GDrivePropertiesPlugin, "gdrivepropertiesplugin.json")
0019 
0020 GDrivePropertiesPlugin::GDrivePropertiesPlugin(QObject *parent, const QList<QVariant> &args)
0021 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0022     : KPropertiesDialogPlugin(qobject_cast<KPropertiesDialog *>(parent))
0023 #else
0024     : KPropertiesDialogPlugin(parent)
0025 #endif
0026 
0027 {
0028     Q_UNUSED(args)
0029 
0030     qCDebug(GDRIVE) << "Starting Google Drive properties tab";
0031 
0032     // Ignore if more than one file is selected
0033     if (properties->items().size() != 1) {
0034         qCDebug(GDRIVE) << "Can't show Google Drive properties tab for more than one item";
0035         return;
0036     }
0037 
0038     m_item = properties->items().at(0);
0039 
0040     // Ignore if not a Google Drive url
0041     if (m_item.url().scheme() != QLatin1String("gdrive")) {
0042         qCDebug(GDRIVE) << "Can't show Google Drive properties for non Google Drive entries";
0043         return;
0044     }
0045 
0046     m_ui.setupUi(&m_widget);
0047 
0048     // Re stat() the item because the entry is probably lacking required information.
0049     const KIO::StatJob *job = KIO::stat(m_item.url(), KIO::HideProgressInfo);
0050     connect(job, &KJob::finished, this, &GDrivePropertiesPlugin::statJobFinished);
0051 }
0052 
0053 void GDrivePropertiesPlugin::showEntryDetails(const KIO::UDSEntry &entry)
0054 {
0055     const QString id = entry.stringValue(GDriveUDSEntryExtras::Id);
0056     m_ui.idValue->setText(id);
0057 
0058     const QString created = m_item.timeString(KFileItem::CreationTime);
0059     m_ui.createdValue->setText(created);
0060 
0061     const QString modified = m_item.timeString(KFileItem::ModificationTime);
0062     m_ui.modifiedValue->setText(modified);
0063 
0064     const QString lastViewedByMe = m_item.timeString(KFileItem::AccessTime);
0065     m_ui.lastViewedByMeValue->setText(lastViewedByMe);
0066 
0067     if (entry.contains(GDriveUDSEntryExtras::SharedWithMeDate)) {
0068         m_ui.sharedWithMeValue->setText(entry.stringValue(GDriveUDSEntryExtras::SharedWithMeDate));
0069         m_ui.sharedWithMeLabel->show();
0070         m_ui.sharedWithMeValue->show();
0071     } else {
0072         m_ui.sharedWithMeLabel->hide();
0073         m_ui.sharedWithMeValue->hide();
0074     }
0075 
0076     const QString version = entry.stringValue(GDriveUDSEntryExtras::Version);
0077     m_ui.versionValue->setText(version);
0078 
0079     const QString md5 = entry.stringValue(GDriveUDSEntryExtras::Md5);
0080     m_ui.md5Value->setText(md5);
0081 
0082     const QString lastModifyingUserName = entry.stringValue(GDriveUDSEntryExtras::LastModifyingUser);
0083     m_ui.lastModifiedByValue->setText(lastModifyingUserName);
0084 
0085     const QString owners = entry.stringValue(GDriveUDSEntryExtras::Owners);
0086     m_ui.ownersValue->setText(owners);
0087 
0088     const QString description = entry.stringValue(KIO::UDSEntry::UDS_COMMENT);
0089     m_ui.descriptionValue->setText(description);
0090 
0091     const QString gdriveLink = entry.stringValue(GDriveUDSEntryExtras::Url);
0092     connect(m_ui.urlOpenButton, &QPushButton::clicked, this, [gdriveLink]() {
0093         QDesktopServices::openUrl(QUrl(gdriveLink));
0094     });
0095 
0096     connect(m_ui.urlCopyButton, &QPushButton::clicked, this, [gdriveLink]() {
0097         QGuiApplication::clipboard()->setText(gdriveLink);
0098     });
0099 }
0100 
0101 void GDrivePropertiesPlugin::statJobFinished(KJob *job)
0102 {
0103     KIO::StatJob *statJob = qobject_cast<KIO::StatJob *>(job);
0104     if (!statJob || statJob->error()) {
0105         qCDebug(GDRIVE) << "Failed stat()ing" << statJob->url() << statJob->errorString();
0106         qCDebug(GDRIVE) << "Not showing Google Drive properties tab";
0107         return;
0108     }
0109     const KIO::UDSEntry entry = statJob->statResult();
0110     showEntryDetails(entry);
0111     properties->addPage(&m_widget, i18n("G&oogle Drive"));
0112 }
0113 
0114 #include "gdrivepropertiesplugin.moc"