1 | /* |
2 | SPDX-FileCopyrightText: 2011 Alejandro Fiestas Olivares <afiestas@kde.org> |
3 | SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@kde.org> |
4 | SPDX-FileCopyrightText: 2018 Nicolas Fella <nicolas.fella@gmx.de> |
5 | |
6 | SPDX-License-Identifier: GPL-2.0-or-later |
7 | */ |
8 | |
9 | #include "sharefileitemaction.h" |
10 | |
11 | #include <QAction> |
12 | #include <QDesktopServices> |
13 | #include <QIcon> |
14 | #include <QJsonArray> |
15 | #include <QList> |
16 | #include <QUrl> |
17 | #include <QVariantList> |
18 | #include <QWidget> |
19 | |
20 | #include <KLocalizedString> |
21 | #include <KNotification> |
22 | #include <KPluginFactory> |
23 | #include <kio/global.h> |
24 | |
25 | #include "alternativesmodel.h" |
26 | #include "menu.h" |
27 | |
28 | K_PLUGIN_CLASS_WITH_JSON(ShareFileItemAction, "sharefileitemaction.json" ) |
29 | |
30 | Q_LOGGING_CATEGORY(PURPOSE_FILEITEMACTION, "kf.kio.widgets.fileitemactions.purpose" ) |
31 | |
32 | ShareFileItemAction::ShareFileItemAction(QObject *parent) |
33 | : KAbstractFileItemActionPlugin(parent) |
34 | , m_menu(new Purpose::Menu()) |
35 | { |
36 | m_menu->setTitle(i18n("Share" )); |
37 | m_menu->setIcon(QIcon::fromTheme(QStringLiteral("document-share" ))); |
38 | m_menu->model()->setPluginType(QStringLiteral("Export" )); |
39 | |
40 | connect(sender: m_menu, signal: &Purpose::Menu::finished, context: this, slot: [this](const QJsonObject &output, int errorCode, const QString &errorMessage) { |
41 | m_isFinished = true; |
42 | if (errorCode == 0 || errorCode == KIO::ERR_USER_CANCELED) { |
43 | if (output.contains(key: QLatin1String("url" ))) { |
44 | QDesktopServices::openUrl(url: QUrl(output.value(key: QLatin1String("url" )).toString())); |
45 | } |
46 | } else { |
47 | Q_EMIT error(errorMessage); |
48 | qWarning() << "job failed with error" << errorCode << errorMessage << output; |
49 | } |
50 | }); |
51 | } |
52 | |
53 | ShareFileItemAction::~ShareFileItemAction() |
54 | { |
55 | // TODO KF6 Remove this compatibility block |
56 | // In case our instance is deleted, but the job isn't finished we fall back to a notification |
57 | if (!m_isFinished) { |
58 | QObject::connect(sender: m_menu, signal: &Purpose::Menu::finished, slot: [](const QJsonObject &output, int errorCode, const QString &errorMessage) { |
59 | if (errorCode == 0 || errorCode == KIO::ERR_USER_CANCELED) { |
60 | if (output.contains(key: QLatin1String("url" ))) { |
61 | QDesktopServices::openUrl(url: QUrl(output.value(key: QLatin1String("url" )).toString())); |
62 | } |
63 | } else { |
64 | KNotification::event(eventId: KNotification::Error, i18n("Error sharing" ), text: errorMessage); |
65 | qWarning() << "job failed with error" << errorCode << errorMessage << output; |
66 | } |
67 | }); |
68 | } |
69 | } |
70 | |
71 | QList<QAction *> ShareFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget) |
72 | { |
73 | QJsonArray urlsJson; |
74 | |
75 | for (const QUrl &url : fileItemInfos.urlList()) { |
76 | urlsJson.append(value: url.toString()); |
77 | } |
78 | |
79 | m_menu->model()->setInputData( |
80 | QJsonObject{{QStringLiteral("mimeType" ), QJsonValue{!fileItemInfos.mimeType().isEmpty() ? fileItemInfos.mimeType() : QStringLiteral("*/*" )}}, |
81 | {QStringLiteral("urls" ), urlsJson}}); |
82 | m_menu->reload(); |
83 | m_menu->setParent(parent: parentWidget, f: Qt::Popup); |
84 | |
85 | return {m_menu->menuAction()}; |
86 | } |
87 | |
88 | #include "moc_sharefileitemaction.cpp" |
89 | #include "sharefileitemaction.moc" |
90 | |