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"
8#include "pimcommon_debug.h"
9using namespace Qt::Literals::StringLiterals;
10
11#include <KLocalizedString>
12#include <Purpose/AlternativesModel>
13#include <Purpose/Menu>
14#include <QJsonArray>
15#include <QTemporaryFile>
16#include <QUrl>
17
18using namespace PimCommon;
19PurposeMenuWidget::PurposeMenuWidget(QWidget *parentWidget, QObject *parent)
20 : QObject(parent)
21 , mParentWidget(parentWidget)
22 , mShareMenu(new Purpose::Menu(mParentWidget))
23{
24 mShareMenu->setObjectName("purposesharemenu"_L1);
25 connect(mShareMenu, &Purpose::Menu::aboutToShow, this, &PurposeMenuWidget::slotInitializeShareMenu);
26 connect(mShareMenu, &Purpose::Menu::finished, this, &PurposeMenuWidget::slotShareActionFinished);
27}
28
29PurposeMenuWidget::~PurposeMenuWidget()
30{
31 delete mTemporaryShareFile;
32}
33
34QMenu *PurposeMenuWidget::menu() const
35{
36 return mShareMenu;
37}
38
39void PurposeMenuWidget::slotInitializeShareMenu()
40{
41 delete mTemporaryShareFile;
42 mTemporaryShareFile = new QTemporaryFile();
43 if (!mTemporaryShareFile->open()) {
44 qCWarning(PIMCOMMON_LOG) << "Impossible to open temporary file";
45 }
46 mTemporaryShareFile->setPermissions(QFile::ReadUser);
47 mTemporaryShareFile->write(text());
48 mTemporaryShareFile->close();
49 mShareMenu->model()->setInputData(QJsonObject{{QStringLiteral("urls"), QJsonArray{{QUrl::fromLocalFile(mTemporaryShareFile->fileName()).toString()}}},
50 {QStringLiteral("mimeType"), {QStringLiteral("text/plain")}}});
51 mShareMenu->model()->setPluginType(QStringLiteral("Export"));
52 mShareMenu->reload();
53}
54
55void PurposeMenuWidget::slotShareActionFinished(const QJsonObject &output, int error, const QString &message)
56{
57 if (error) {
58 Q_EMIT shareError(i18n("There was a problem sharing the document: %1", message));
59 } else {
60 const QString url = output["url"_L1].toString();
61 if (url.isEmpty()) {
62 Q_EMIT shareSuccess(i18n("File was shared."));
63 } else {
64 Q_EMIT shareSuccess(i18n("<qt>You can find the new request at:<br /><a href='%1'>%1</a> </qt>", url));
65 }
66 }
67}
68
69#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 Mon Nov 18 2024 12:14:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.