Pimcommon

purposemenuwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "purposemenuwidget.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include <KLocalizedString>
11#include <Purpose/AlternativesModel>
12#include <Purpose/Menu>
13#include <QJsonArray>
14#include <QTemporaryFile>
15#include <QUrl>
16
17using namespace PimCommon;
18PurposeMenuWidget::PurposeMenuWidget(QWidget *parentWidget, QObject *parent)
19 : QObject(parent)
20 , mParentWidget(parentWidget)
21 , mShareMenu(new Purpose::Menu(mParentWidget))
22{
23 mShareMenu->setObjectName("purposesharemenu"_L1);
24 connect(mShareMenu, &Purpose::Menu::aboutToShow, this, &PurposeMenuWidget::slotInitializeShareMenu);
25 connect(mShareMenu, &Purpose::Menu::finished, this, &PurposeMenuWidget::slotShareActionFinished);
26}
27
28PurposeMenuWidget::~PurposeMenuWidget()
29{
30 delete mTemporaryShareFile;
31}
32
33QMenu *PurposeMenuWidget::menu() const
34{
35 return mShareMenu;
36}
37
38void PurposeMenuWidget::slotInitializeShareMenu()
39{
40 delete mTemporaryShareFile;
41 mTemporaryShareFile = new QTemporaryFile();
42 mTemporaryShareFile->open();
43 mTemporaryShareFile->setPermissions(QFile::ReadUser);
44 mTemporaryShareFile->write(text());
45 mTemporaryShareFile->close();
46 mShareMenu->model()->setInputData(QJsonObject{{QStringLiteral("urls"), QJsonArray{{QUrl::fromLocalFile(mTemporaryShareFile->fileName()).toString()}}},
47 {QStringLiteral("mimeType"), {QStringLiteral("text/plain")}}});
48 mShareMenu->model()->setPluginType(QStringLiteral("Export"));
49 mShareMenu->reload();
50}
51
52void PurposeMenuWidget::slotShareActionFinished(const QJsonObject &output, int error, const QString &message)
53{
54 if (error) {
55 Q_EMIT shareError(i18n("There was a problem sharing the document: %1", message));
56 } else {
57 const QString url = output["url"_L1].toString();
58 if (url.isEmpty()) {
59 Q_EMIT shareSuccess(i18n("File was shared."));
60 } else {
61 Q_EMIT shareSuccess(i18n("<qt>You can find the new request at:<br /><a href='%1'>%1</a> </qt>", url));
62 }
63 }
64}
65
66#include "moc_purposemenuwidget.cpp"
QString i18n(const char *text, const TYPE &arg...)
folderdialogacltab.h
virtual bool setPermissions(Permissions permissions) override
virtual void close() override
qint64 write(const QByteArray &data)
Q_EMITQ_EMIT
void setObjectName(QAnyStringView name)
bool isEmpty() const const
virtual QString fileName() const const override
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl fromLocalFile(const QString &localFile)
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 7 2024 12:07:03 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.