Purpose

sharefileitemaction.cpp
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)
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(m_menu, &Purpose::Menu::finished, this, [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(QLatin1String("url"))) {
45 }
46 } else {
47 Q_EMIT error(errorMessage);
48 qWarning() << "job failed with error" << errorCode << errorMessage << output;
49 }
50 });
51}
52
53ShareFileItemAction::~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(m_menu, &Purpose::Menu::finished, [](const QJsonObject &output, int errorCode, const QString &errorMessage) {
59 if (errorCode == 0 || errorCode == KIO::ERR_USER_CANCELED) {
60 if (output.contains(QLatin1String("url"))) {
62 }
63 } else {
64 KNotification::event(KNotification::Error, i18n("Error sharing"), errorMessage);
65 qWarning() << "job failed with error" << errorCode << errorMessage << output;
66 }
67 });
68 }
69}
70
71QList<QAction *> ShareFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
72{
73 QJsonArray urlsJson;
74
75 for (const QUrl &url : fileItemInfos.urlList()) {
76 urlsJson.append(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(parentWidget, Qt::Popup);
84
85 return {m_menu->menuAction()};
86}
87
88#include "moc_sharefileitemaction.cpp"
89#include "sharefileitemaction.moc"
QString mimeType() const
QList< QUrl > urlList() const
static KNotification * event(const QString &eventId, const QString &text=QString(), const QPixmap &pixmap=QPixmap(), const NotificationFlags &flags=CloseOnTimeout, const QString &componentName=QString())
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
bool openUrl(const QUrl &url)
QIcon fromTheme(const QString &name)
void append(const QJsonValue &value)
bool contains(QLatin1StringView key) const const
QJsonValue value(QLatin1StringView key) const const
QAction * menuAction() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setParent(QWidget *parent)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.