Changeset View
Changeset View
Standalone View
Standalone View
src/copyurlitemaction.cpp
- This file was added.
| 1 | /* | ||||
|---|---|---|---|---|---|
| 2 | * Copyright (C) 2019 David Barchiesi <david@barchie.si> | ||||
| 3 | * | ||||
| 4 | * This program is free software; you can redistribute it and/or modify | ||||
| 5 | * it under the terms of the GNU General Public License as published by | ||||
| 6 | * the Free Software Foundation; either version 2 of the License, or | ||||
| 7 | * (at your option) any later version. | ||||
| 8 | * | ||||
| 9 | * This program is distributed in the hope that it will be useful, | ||||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
| 12 | * GNU General Public License for more details. | ||||
| 13 | * | ||||
| 14 | * You should have received a copy of the GNU General Public License along | ||||
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., | ||||
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||||
| 17 | * | ||||
| 18 | */ | ||||
| 19 | | ||||
| 20 | #include "copyurlitemaction.h" | ||||
| 21 | | ||||
| 22 | #include <QGuiApplication> | ||||
| 23 | #include <QClipboard> | ||||
elvisangelaccio: Not needed | |||||
| 24 | #include <QAction> | ||||
elvisangelaccio: QGuiApplication | |||||
| 25 | | ||||
| 26 | #include <KFileItemListProperties> | ||||
| 27 | #include <KLocalizedString> | ||||
| 28 | #include <KPluginFactory> | ||||
| 29 | #include <KFileItem> | ||||
| 30 | | ||||
elvisangelaccio: Not needed | |||||
| 31 | | ||||
| 32 | K_PLUGIN_CLASS_WITH_JSON(CopyUrlItemAction, "copyurlitemaction.json") | ||||
| 33 | | ||||
| 34 | CopyUrlItemAction::CopyUrlItemAction(QObject* parent, const QVariantList&) | ||||
| 35 | : KAbstractFileItemActionPlugin(parent) | ||||
| 36 | {} | ||||
| 37 | | ||||
| 38 | QList<QAction*> CopyUrlItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) | ||||
| 39 | { | ||||
| 40 | // Ignore if more than one file is selected | ||||
| 41 | if (fileItemInfos.items().size() != 1) { | ||||
| 42 | return {}; | ||||
| 43 | } | ||||
| 44 | | ||||
| 45 | const KFileItem item = fileItemInfos.items().at(0); | ||||
| 46 | | ||||
| 47 | // Ignore if not a Google Drive url | ||||
| 48 | if (item.url().scheme() != QLatin1String("gdrive")) { | ||||
elvisangelaccio: Missing `const`; prefer `at(0)`. | |||||
| 49 | return {}; | ||||
elvisangelaccio: QLatin1String for comparisons | |||||
| 50 | } | ||||
| 51 | | ||||
| 52 | const KIO::UDSEntry entry = item.entry(); | ||||
| 53 | const QString gdriveLink = entry.stringValue(KIO::UDSEntry::UDS_EXTRA); | ||||
| 54 | // Ignore if missing a shareable link | ||||
| 55 | if (gdriveLink.isEmpty()) { | ||||
| 56 | return {}; | ||||
| 57 | } | ||||
| 58 | | ||||
| 59 | QAction *copyUrlAction = createCopyUrlAction(parentWidget, gdriveLink); | ||||
| 60 | return { copyUrlAction }; | ||||
| 61 | } | ||||
| 62 | | ||||
| 63 | | ||||
I'd call it "Copy Google URL to clipboard", since it can be a "drive" url (drive.google.com) but also a "document" url (docs.google.com). elvisangelaccio: I'd call it "Copy Google URL to clipboard", since it can be a "drive" url (`drive.google.com`)… | |||||
| 64 | QAction *CopyUrlItemAction::createCopyUrlAction(QWidget *parent, const QString& gdriveLink) | ||||
| 65 | { | ||||
| 66 | const QString name = i18n("Copy Google URL to clipboard"); | ||||
| 67 | const QIcon icon = QIcon::fromTheme(QStringLiteral("folder-gdrive")); | ||||
| 68 | QAction *action = new QAction(icon, name, parent); | ||||
| 69 | | ||||
Can the link be empty? In this case we might not show the action at all. elvisangelaccio: Can the link be empty? In this case we might not show the action at all. | |||||
| 70 | connect(action, &QAction::triggered, this, [gdriveLink]() { | ||||
Why are we stating the URL again? We already have the KFileItem for it which can give us access to its UDSEntry object. elvisangelaccio: Why are we `stat`ing the URL again? We already have the `KFileItem` for it which can give us… | |||||
| 71 | QGuiApplication::clipboard()->setText(gdriveLink); | ||||
| 72 | }); | ||||
elvisangelaccio: `QGuiApplication` should be enough | |||||
| 73 | | ||||
| 74 | return action; | ||||
| 75 | } | ||||
elvisangelaccio: This doesn't seem to work on Wayland. Haven't tested on X11 yet. | |||||
I just tried it out simply selecting "Plasma (Wayland)" in SDDM and it seems to work, I think the bug is elsewhere. What were you right clicking on? barchiesi: I just tried it out simply selecting "Plasma (Wayland)" in SDDM and it seems to work, I think… | |||||
Nevermind, it works. It just doesn't show up in the clipboard plasmoid, but that's surely not a bug introduced by this patch. elvisangelaccio: Nevermind, it works. It just doesn't show up in the clipboard plasmoid, but that's surely not a… | |||||
Ah, then in that case it probably has something to do with T4449: [kwayland] Clipboard Manager protocol barchiesi: Ah, then in that case it probably has something to do with {T4449} | |||||
| 76 | | ||||
| 77 | | ||||
| 78 | #include "copyurlitemaction.moc" | ||||
barchiesi: How should this error be handled? A notification perhaps? | |||||
Not needed