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
28K_PLUGIN_CLASS_WITH_JSON(ShareFileItemAction, "sharefileitemaction.json")
29
30Q_LOGGING_CATEGORY(PURPOSE_FILEITEMACTION, "kf.kio.widgets.fileitemactions.purpose")
31
32ShareFileItemAction::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 } else {
46 Q_EMIT error(errorMessage);
47 qWarning() << "job failed with error" << errorCode << errorMessage << output;
48 }
49 });
50}
51
52ShareFileItemAction::~ShareFileItemAction()
53{
54 // TODO KF6 Remove this compatibility block
55 // In case our instance is deleted, but the job isn't finished we fall back to a notification
56 if (!m_isFinished) {
57 QObject::connect(sender: m_menu, signal: &Purpose::Menu::finished, slot: [](const QJsonObject &output, int errorCode, const QString &errorMessage) {
58 if (errorCode == 0 || errorCode == KIO::ERR_USER_CANCELED) {
59 if (output.contains(key: QLatin1String("url")))
60 QDesktopServices::openUrl(url: QUrl(output.value(key: QLatin1String("url")).toString()));
61 } else {
62 KNotification::event(eventId: KNotification::Error, i18n("Error sharing"), text: errorMessage);
63 qWarning() << "job failed with error" << errorCode << errorMessage << output;
64 }
65 });
66 }
67}
68
69QList<QAction *> ShareFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
70{
71 QJsonArray urlsJson;
72
73 for (const QUrl &url : fileItemInfos.urlList()) {
74 urlsJson.append(value: url.toString());
75 }
76
77 m_menu->model()->setInputData(
78 QJsonObject{{QStringLiteral("mimeType"), QJsonValue{!fileItemInfos.mimeType().isEmpty() ? fileItemInfos.mimeType() : QStringLiteral("*/*")}},
79 {QStringLiteral("urls"), urlsJson}});
80 m_menu->reload();
81 m_menu->setParent(parent: parentWidget, f: Qt::Popup);
82
83 return {m_menu->menuAction()};
84}
85
86#include "moc_sharefileitemaction.cpp"
87#include "sharefileitemaction.moc"
88

source code of purpose/src/fileitemactionplugin/sharefileitemaction.cpp