File indexing completed on 2025-01-05 04:49:46

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "viewerplugincreatenoteinterface.h"
0008 #include "createnotejob.h"
0009 #include "createnoteplugin_debug.h"
0010 #include "noteedit.h"
0011 #include <KActionCollection>
0012 #include <KLocalizedString>
0013 
0014 #include <Akonadi/ItemFetchJob>
0015 #include <Akonadi/ItemFetchScope>
0016 #include <QAction>
0017 #include <QIcon>
0018 #include <QLayout>
0019 
0020 using namespace MessageViewer;
0021 
0022 ViewerPluginCreatenoteInterface::ViewerPluginCreatenoteInterface(KActionCollection *ac, QWidget *parent)
0023     : ViewerPluginInterface(parent)
0024 {
0025     createAction(ac);
0026 }
0027 
0028 ViewerPluginCreatenoteInterface::~ViewerPluginCreatenoteInterface() = default;
0029 
0030 void ViewerPluginCreatenoteInterface::setText(const QString &text)
0031 {
0032     Q_UNUSED(text)
0033     // Nothing
0034 }
0035 
0036 QList<QAction *> ViewerPluginCreatenoteInterface::actions() const
0037 {
0038     return mAction;
0039 }
0040 
0041 void ViewerPluginCreatenoteInterface::setMessage(const KMime::Message::Ptr &value)
0042 {
0043     widget()->setMessage(value);
0044 }
0045 
0046 void ViewerPluginCreatenoteInterface::closePlugin()
0047 {
0048     if (mNoteEdit) {
0049         mNoteEdit->slotCloseWidget();
0050     }
0051 }
0052 
0053 Akonadi::Relation ViewerPluginCreatenoteInterface::relatedNoteRelation() const
0054 {
0055     Akonadi::Relation relation;
0056     const auto relations = mMessageItem.relations();
0057     for (const Akonadi::Relation &r : relations) {
0058         // assuming that GENERIC relations to emails are notes is a pretty horrific hack imo - aseigo
0059         if (r.type() == Akonadi::Relation::GENERIC && r.right().mimeType() == Akonadi::NoteUtils::noteMimeType()) {
0060             relation = r;
0061             break;
0062         }
0063     }
0064     return relation;
0065 }
0066 
0067 void ViewerPluginCreatenoteInterface::showWidget()
0068 {
0069     if (!mMessageItem.relations().isEmpty()) {
0070         Akonadi::Relation relation = relatedNoteRelation();
0071         if (relation.isValid()) {
0072             auto job = new Akonadi::ItemFetchJob(relation.right());
0073             job->fetchScope().fetchFullPayload(true);
0074             connect(job, &Akonadi::ItemFetchJob::result, this, &ViewerPluginCreatenoteInterface::slotNoteItemFetched);
0075             return;
0076         }
0077     }
0078     showCreateNewNoteWidget();
0079 }
0080 
0081 void ViewerPluginCreatenoteInterface::showCreateNewNoteWidget()
0082 {
0083     widget()->showNoteEdit();
0084 }
0085 
0086 void ViewerPluginCreatenoteInterface::slotNoteItemFetched(KJob *job)
0087 {
0088     if (job->error()) {
0089         qCDebug(CREATENOTEPLUGIN_LOG) << "There is not valid note:" << job->errorString();
0090         showCreateNewNoteWidget();
0091     } else {
0092         auto fetch = qobject_cast<Akonadi::ItemFetchJob *>(job);
0093         Q_ASSERT(fetch);
0094         if (fetch->items().isEmpty() || !fetch->items().constFirst().hasPayload<KMime::Message::Ptr>()) {
0095             showCreateNewNoteWidget();
0096         } else {
0097             Akonadi::NoteUtils::NoteMessageWrapper note(fetch->items().constFirst().payload<KMime::Message::Ptr>());
0098             widget()->setMessage(note.message());
0099             showCreateNewNoteWidget();
0100         }
0101     }
0102 }
0103 
0104 void ViewerPluginCreatenoteInterface::setMessageItem(const Akonadi::Item &item)
0105 {
0106     mMessageItem = item;
0107 }
0108 
0109 ViewerPluginInterface::SpecificFeatureTypes ViewerPluginCreatenoteInterface::featureTypes() const
0110 {
0111     return ViewerPluginInterface::NeedMessage;
0112 }
0113 
0114 void ViewerPluginCreatenoteInterface::updateAction(const Akonadi::Item &item)
0115 {
0116     mMessageItem = item;
0117     if (!mAction.isEmpty()) {
0118         QString createNoteText;
0119         if (relatedNoteRelation().isValid()) {
0120             createNoteText = i18nc("edit a note on this message", "Edit Note");
0121         } else {
0122             createNoteText = i18nc("create a new note out of this message", "Create Note");
0123         }
0124 
0125         mAction.at(0)->setText(createNoteText);
0126         mAction.at(0)->setIconText(createNoteText);
0127     }
0128 }
0129 
0130 void ViewerPluginCreatenoteInterface::createAction(KActionCollection *ac)
0131 {
0132     if (ac) {
0133         auto act = new QAction(QIcon::fromTheme(QStringLiteral("view-pim-notes")), i18nc("create a new note out of this message", "Create Note"), this);
0134         act->setIconText(i18nc("create a new note out of this message", "Create Note"));
0135         addHelpTextAction(act, i18n("Allows you to create a note from this message"));
0136         act->setWhatsThis(i18n("This option starts an editor to create a note. Then you can edit the note to your liking before saving it."));
0137         ac->addAction(QStringLiteral("create_note"), act);
0138         connect(act, &QAction::triggered, this, &ViewerPluginCreatenoteInterface::slotActivatePlugin);
0139         mAction.append(act);
0140     }
0141 }
0142 
0143 void ViewerPluginCreatenoteInterface::slotCreateNote(const KMime::Message::Ptr &notePtr, const Akonadi::Collection &collection)
0144 {
0145     auto createJob = new CreateNoteJob(notePtr, collection, mMessageItem, this);
0146     createJob->start();
0147 }
0148 
0149 NoteEdit *ViewerPluginCreatenoteInterface::widget()
0150 {
0151     if (!mNoteEdit) {
0152         auto parentWidget = static_cast<QWidget *>(parent());
0153         mNoteEdit = new NoteEdit(parentWidget);
0154         connect(mNoteEdit, &NoteEdit::createNote, this, &ViewerPluginCreatenoteInterface::slotCreateNote);
0155         mNoteEdit->setObjectName(QLatin1StringView("noteedit"));
0156         parentWidget->layout()->addWidget(mNoteEdit);
0157         mNoteEdit->hide();
0158     }
0159     return mNoteEdit;
0160 }
0161 
0162 #include "moc_viewerplugincreatenoteinterface.cpp"