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 | #include "gdriveurl.h" | ||||
22 | #include "gdrivedebug.h" | ||||
23 | | ||||
elvisangelaccio: Not needed | |||||
24 | #include <QStringLiteral> | ||||
elvisangelaccio: QGuiApplication | |||||
25 | #include <QApplication> | ||||
26 | #include <QClipboard> | ||||
27 | | ||||
28 | #include <KLocalizedString> | ||||
29 | #include <KPluginFactory> | ||||
30 | #include <KIO/UDSEntry> | ||||
elvisangelaccio: Not needed | |||||
31 | | ||||
32 | | ||||
33 | K_PLUGIN_CLASS_WITH_JSON(CopyUrlItemAction, "copyurlitemaction.json") | ||||
34 | | ||||
35 | CopyUrlItemAction::CopyUrlItemAction(QObject* parent, const QVariantList&) | ||||
36 | : KAbstractFileItemActionPlugin(parent) | ||||
37 | {} | ||||
38 | | ||||
39 | QList<QAction*> CopyUrlItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) | ||||
40 | { | ||||
41 | // Ignore if more than one file is selected | ||||
42 | if (fileItemInfos.items().size() != 1) { | ||||
43 | qCDebug(GDRIVE) << "Not showing, too many files selected"; | ||||
44 | return {}; | ||||
45 | } | ||||
46 | | ||||
47 | KFileItem item = fileItemInfos.items()[0]; | ||||
elvisangelaccio: Missing `const`; prefer `at(0)`. | |||||
48 | | ||||
49 | // Ignore if not a Google Drive url | ||||
elvisangelaccio: QLatin1String for comparisons | |||||
50 | if (item.url().scheme() != GDriveUrl::Scheme) { | ||||
51 | qCDebug(GDRIVE) << "Not showing, not GDriveUrl"; | ||||
52 | return {}; | ||||
53 | } | ||||
54 | | ||||
55 | QAction *copyUrlAction = createCopyUrlAction(parentWidget, item); | ||||
56 | return { copyUrlAction }; | ||||
57 | } | ||||
58 | | ||||
59 | | ||||
60 | QAction *CopyUrlItemAction::createCopyUrlAction(QWidget *parent, const KFileItem& item) | ||||
61 | { | ||||
62 | const QString name = i18n("Copy Drive url to clipboard"); | ||||
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`)… | |||||
63 | const QIcon icon = QIcon::fromTheme(QStringLiteral("folder-gdrive")); | ||||
64 | QAction *action = new QAction(icon, name, parent); | ||||
65 | | ||||
66 | connect(action, &QAction::triggered, this, [item]() { | ||||
67 | const KIO::UDSEntry entry = item.entry(); | ||||
68 | const QString gdriveLink = entry.stringValue(KIO::UDSEntry::UDS_EXTRA); | ||||
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. | |||||
69 | QApplication::clipboard()->setText(gdriveLink); | ||||
70 | qCDebug(GDRIVE) << "Copied" << gdriveLink << "to clipboard for" << item.url(); | ||||
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 | }); | ||||
72 | | ||||
elvisangelaccio: `QGuiApplication` should be enough | |||||
73 | return action; | ||||
74 | } | ||||
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 | #include "copyurlitemaction.moc" | ||||
barchiesi: How should this error be handled? A notification perhaps? |
Not needed