File indexing completed on 2025-01-05 04:38:27
0001 /* 0002 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "purposemenuwidget.h" 0008 #include <KLocalizedString> 0009 #include <KMessageBox> 0010 #include <Purpose/AlternativesModel> 0011 #include <Purpose/Menu> 0012 #include <QJsonArray> 0013 #include <QTemporaryFile> 0014 #include <QUrl> 0015 0016 PurposeMenuWidget::PurposeMenuWidget(QObject *parent) 0017 : QObject(parent) 0018 , mShareMenu(new Purpose::Menu) 0019 { 0020 mShareMenu->setTitle(i18n("Share Text...")); 0021 mShareMenu->setIcon(QIcon::fromTheme(QStringLiteral("document-share"))); 0022 mShareMenu->setObjectName(QStringLiteral("purposesharemenu")); 0023 connect(mShareMenu, &Purpose::Menu::aboutToShow, this, &PurposeMenuWidget::slotInitializeShareMenu); 0024 connect(mShareMenu, &Purpose::Menu::finished, this, &PurposeMenuWidget::slotShareActionFinished); 0025 } 0026 0027 PurposeMenuWidget::~PurposeMenuWidget() 0028 { 0029 delete mTemporaryShareFile; 0030 delete mShareMenu; 0031 } 0032 0033 QByteArray PurposeMenuWidget::text() 0034 { 0035 return mSelectedText.toUtf8(); 0036 } 0037 0038 QMenu *PurposeMenuWidget::menu() const 0039 { 0040 return mShareMenu; 0041 } 0042 0043 void PurposeMenuWidget::setSelectedText(const QString &str) 0044 { 0045 mSelectedText = str; 0046 } 0047 0048 void PurposeMenuWidget::slotInitializeShareMenu() 0049 { 0050 delete mTemporaryShareFile; 0051 mTemporaryShareFile = new QTemporaryFile(); 0052 mTemporaryShareFile->open(); 0053 mTemporaryShareFile->setPermissions(QFile::ReadUser); 0054 mTemporaryShareFile->write(text()); 0055 mTemporaryShareFile->close(); 0056 mShareMenu->model()->setInputData(QJsonObject{{QStringLiteral("urls"), QJsonArray{{QUrl::fromLocalFile(mTemporaryShareFile->fileName()).toString()}}}, 0057 {QStringLiteral("mimeType"), {QStringLiteral("text/plain")}}}); 0058 mShareMenu->model()->setPluginType(QStringLiteral("Export")); 0059 mShareMenu->reload(); 0060 } 0061 0062 void PurposeMenuWidget::slotShareActionFinished(const QJsonObject &output, int error, const QString &message) 0063 { 0064 if (error) { 0065 Q_EMIT errorMessage(i18n("There was a problem sharing the document: %1", message)); 0066 } else { 0067 const QString url = output[QLatin1String("url")].toString(); 0068 if (url.isEmpty()) { 0069 Q_EMIT successMessage(i18n("File was shared.")); 0070 } else { 0071 Q_EMIT successMessage(i18n("<qt>You can find the new request at:<br /><a href='%1'>%1</a> </qt>", url)); 0072 } 0073 } 0074 } 0075 0076 #include "moc_purposemenuwidget.cpp"