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 <QUrlQuery> | ||||
26 | #include <QGuiApplication> | ||||
27 | #include <QClipboard> | ||||
28 | | ||||
29 | #include <KLocalizedString> | ||||
30 | #include <KPluginFactory> | ||||
elvisangelaccio: Not needed | |||||
31 | #include <KIO/UDSEntry> | ||||
32 | #include <KIO/StatJob> | ||||
33 | | ||||
34 | | ||||
35 | K_PLUGIN_CLASS_WITH_JSON(CopyUrlItemAction, "copyurlitemaction.json") | ||||
36 | | ||||
37 | CopyUrlItemAction::CopyUrlItemAction(QObject* parent, const QVariantList&) | ||||
38 | : KAbstractFileItemActionPlugin(parent) | ||||
39 | {} | ||||
40 | | ||||
41 | QList<QAction*> CopyUrlItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) | ||||
42 | { | ||||
43 | // Ignore if more than one file is selected | ||||
44 | if (fileItemInfos.urlList().size() != 1) { | ||||
45 | qCDebug(GDRIVE) << "Not showing, too many files selected"; | ||||
46 | return {}; | ||||
47 | } | ||||
48 | | ||||
elvisangelaccio: Missing `const`; prefer `at(0)`. | |||||
49 | // Ignore if not a Google Drive url | ||||
elvisangelaccio: QLatin1String for comparisons | |||||
50 | QUrl url = fileItemInfos.urlList()[0]; | ||||
51 | if (url.scheme() != GDriveUrl::Scheme) { | ||||
52 | qCDebug(GDRIVE) << "Not showing, not GDriveUrl"; | ||||
53 | return {}; | ||||
54 | } | ||||
55 | | ||||
56 | QAction *copyUrlAction = createCopyUrlAction(parentWidget, fileItemInfos.urlList()[0]); | ||||
57 | return { copyUrlAction }; | ||||
58 | } | ||||
59 | | ||||
60 | | ||||
61 | QAction *CopyUrlItemAction::createCopyUrlAction(QWidget *parent, const QUrl& url) | ||||
62 | { | ||||
63 | 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`)… | |||||
64 | const QIcon icon = QIcon::fromTheme(QStringLiteral("folder-gdrive")); | ||||
65 | QAction *action = new QAction(icon, name, parent); | ||||
66 | | ||||
67 | connect(action, &QAction::triggered, this, [url]() { | ||||
68 | qCDebug(GDRIVE) << "Getting gdrive web url for" << url; | ||||
69 | KIO::StatJob *statJob = KIO::stat(url, KIO::HideProgressInfo); | ||||
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… | |||||
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 | if (statJob->exec()) { | ||||
71 | const KIO::UDSEntry entry = statJob->statResult(); | ||||
72 | const QString gdriveLink = entry.stringValue(GDriveUrl::UDS_ALTERNATE_LINK_FIELD); | ||||
elvisangelaccio: `QGuiApplication` should be enough | |||||
73 | qCDebug(GDRIVE) << "Copying" << gdriveLink << "to clipboard"; | ||||
74 | QGuiApplication::clipboard()->setText(gdriveLink); | ||||
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} | |||||
75 | } | ||||
76 | else { | ||||
77 | qCDebug(GDRIVE) << "Failed getting Google Drive url, error:" << statJob->errorString(); | ||||
barchiesi: How should this error be handled? A notification perhaps? | |||||
78 | } | ||||
79 | }); | ||||
80 | | ||||
81 | return action; | ||||
82 | } | ||||
83 | | ||||
84 | | ||||
85 | #include "copyurlitemaction.moc" |
Not needed