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-or-later 0005 * 0006 */ 0007 0008 #include "contextmenuaction.h" 0009 #include "../../gdrive_udsentry.h" 0010 0011 #include <QAction> 0012 #include <QClipboard> 0013 #include <QDesktopServices> 0014 #include <QGuiApplication> 0015 #include <QMenu> 0016 0017 #include <KFileItemListProperties> 0018 #include <KLocalizedString> 0019 #include <KPluginFactory> 0020 0021 K_PLUGIN_CLASS_WITH_JSON(ContextMenuAction, "contextmenuaction.json") 0022 0023 ContextMenuAction::ContextMenuAction(QObject *parent, const QVariantList &) 0024 : KAbstractFileItemActionPlugin(parent) 0025 { 0026 } 0027 0028 QList<QAction *> ContextMenuAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget) 0029 { 0030 // Ignore if more than one file is selected 0031 if (fileItemInfos.items().size() != 1) { 0032 return {}; 0033 } 0034 0035 const KFileItem item = fileItemInfos.items().at(0); 0036 0037 // Ignore if not a Google Drive url 0038 if (item.url().scheme() != QLatin1String("gdrive")) { 0039 return {}; 0040 } 0041 0042 const KIO::UDSEntry entry = item.entry(); 0043 const QString gdriveLink = entry.stringValue(GDriveUDSEntryExtras::Url); 0044 // Ignore if missing a shareable link 0045 if (gdriveLink.isEmpty()) { 0046 return {}; 0047 } 0048 0049 QMenu *gdriveMenu = new QMenu(parentWidget); 0050 gdriveMenu->addAction(createOpenUrlAction(parentWidget, gdriveLink)); 0051 gdriveMenu->addAction(createCopyUrlAction(parentWidget, gdriveLink)); 0052 0053 QAction *gdriveMenuAction = new QAction(i18n("Google Drive"), parentWidget); 0054 gdriveMenuAction->setMenu(gdriveMenu); 0055 gdriveMenuAction->setIcon(QIcon::fromTheme(QStringLiteral("folder-gdrive"))); 0056 0057 return {gdriveMenuAction}; 0058 } 0059 0060 QAction *ContextMenuAction::createCopyUrlAction(QWidget *parent, const QString &gdriveLink) 0061 { 0062 const QString name = i18n("Copy URL to clipboard"); 0063 const QIcon icon = QIcon::fromTheme(QStringLiteral("edit-copy")); 0064 QAction *action = new QAction(icon, name, parent); 0065 0066 connect(action, &QAction::triggered, this, [gdriveLink]() { 0067 QGuiApplication::clipboard()->setText(gdriveLink); 0068 }); 0069 0070 return action; 0071 } 0072 0073 QAction *ContextMenuAction::createOpenUrlAction(QWidget *parent, const QString &gdriveLink) 0074 { 0075 const QString name = i18n("Open in browser"); 0076 const QIcon icon = QIcon::fromTheme(QStringLiteral("internet-services")); 0077 QAction *action = new QAction(icon, name, parent); 0078 0079 connect(action, &QAction::triggered, this, [gdriveLink]() { 0080 QDesktopServices::openUrl(QUrl(gdriveLink)); 0081 }); 0082 0083 return action; 0084 } 0085 0086 #include "contextmenuaction.moc"