1 /*
2 SPDX-FileCopyrightText: 2015-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "viewerplugincreatenoteinterface.h"
8 #include "createnotejob.h"
9 #include "createnoteplugin_debug.h"
10 #include "noteedit.h"
11 #include <KActionCollection>
12 #include <KLocalizedString>
13
14 #include <Akonadi/ItemFetchJob>
15 #include <Akonadi/ItemFetchScope>
16 #include <QAction>
17 #include <QIcon>
18 #include <QLayout>
19
20 using namespace MessageViewer;
21
ViewerPluginCreatenoteInterface(KActionCollection * ac,QWidget * parent)22 ViewerPluginCreatenoteInterface::ViewerPluginCreatenoteInterface(KActionCollection *ac, QWidget *parent)
23 : ViewerPluginInterface(parent)
24 {
25 createAction(ac);
26 }
27
~ViewerPluginCreatenoteInterface()28 ViewerPluginCreatenoteInterface::~ViewerPluginCreatenoteInterface()
29 {
30 }
31
setText(const QString & text)32 void ViewerPluginCreatenoteInterface::setText(const QString &text)
33 {
34 Q_UNUSED(text)
35 // Nothing
36 }
37
actions() const38 QList<QAction *> ViewerPluginCreatenoteInterface::actions() const
39 {
40 return mAction;
41 }
42
setMessage(const KMime::Message::Ptr & value)43 void ViewerPluginCreatenoteInterface::setMessage(const KMime::Message::Ptr &value)
44 {
45 widget()->setMessage(value);
46 }
47
closePlugin()48 void ViewerPluginCreatenoteInterface::closePlugin()
49 {
50 if (mNoteEdit) {
51 mNoteEdit->slotCloseWidget();
52 }
53 }
54
relatedNoteRelation() const55 Akonadi::Relation ViewerPluginCreatenoteInterface::relatedNoteRelation() const
56 {
57 Akonadi::Relation relation;
58 const auto relations = mMessageItem.relations();
59 for (const Akonadi::Relation &r : relations) {
60 // assuming that GENERIC relations to emails are notes is a pretty horrific hack imo - aseigo
61 if (r.type() == Akonadi::Relation::GENERIC && r.right().mimeType() == Akonadi::NoteUtils::noteMimeType()) {
62 relation = r;
63 break;
64 }
65 }
66 return relation;
67 }
68
showWidget()69 void ViewerPluginCreatenoteInterface::showWidget()
70 {
71 if (!mMessageItem.relations().isEmpty()) {
72 Akonadi::Relation relation = relatedNoteRelation();
73 if (relation.isValid()) {
74 auto job = new Akonadi::ItemFetchJob(relation.right());
75 job->fetchScope().fetchFullPayload(true);
76 connect(job, &Akonadi::ItemFetchJob::result, this, &ViewerPluginCreatenoteInterface::slotNoteItemFetched);
77 return;
78 }
79 }
80 showCreateNewNoteWidget();
81 }
82
showCreateNewNoteWidget()83 void ViewerPluginCreatenoteInterface::showCreateNewNoteWidget()
84 {
85 widget()->showNoteEdit();
86 }
87
slotNoteItemFetched(KJob * job)88 void ViewerPluginCreatenoteInterface::slotNoteItemFetched(KJob *job)
89 {
90 if (job->error()) {
91 qCDebug(CREATENOTEPLUGIN_LOG) << "There is not valid note:" << job->errorString();
92 showCreateNewNoteWidget();
93 } else {
94 auto fetch = qobject_cast<Akonadi::ItemFetchJob *>(job);
95 Q_ASSERT(fetch);
96 if (fetch->items().isEmpty() || !fetch->items().constFirst().hasPayload<KMime::Message::Ptr>()) {
97 showCreateNewNoteWidget();
98 } else {
99 Akonadi::NoteUtils::NoteMessageWrapper note(fetch->items().constFirst().payload<KMime::Message::Ptr>());
100 widget()->setMessage(note.message());
101 showCreateNewNoteWidget();
102 }
103 }
104 }
105
setMessageItem(const Akonadi::Item & item)106 void ViewerPluginCreatenoteInterface::setMessageItem(const Akonadi::Item &item)
107 {
108 mMessageItem = item;
109 }
110
featureTypes() const111 ViewerPluginInterface::SpecificFeatureTypes ViewerPluginCreatenoteInterface::featureTypes() const
112 {
113 return ViewerPluginInterface::NeedMessage;
114 }
115
updateAction(const Akonadi::Item & item)116 void ViewerPluginCreatenoteInterface::updateAction(const Akonadi::Item &item)
117 {
118 mMessageItem = item;
119 if (!mAction.isEmpty()) {
120 QString createNoteText;
121 if (relatedNoteRelation().isValid()) {
122 createNoteText = i18nc("edit a note on this message", "Edit Note");
123 } else {
124 createNoteText = i18nc("create a new note out of this message", "Create Note");
125 }
126
127 mAction.at(0)->setText(createNoteText);
128 mAction.at(0)->setIconText(createNoteText);
129 }
130 }
131
createAction(KActionCollection * ac)132 void ViewerPluginCreatenoteInterface::createAction(KActionCollection *ac)
133 {
134 if (ac) {
135 auto act = new QAction(QIcon::fromTheme(QStringLiteral("view-pim-notes")), i18nc("create a new note out of this message", "Create Note"), this);
136 act->setIconText(i18nc("create a new note out of this message", "Create Note"));
137 addHelpTextAction(act, i18n("Allows you to create a note from this message"));
138 act->setWhatsThis(i18n("This option starts an editor to create a note. Then you can edit the note to your liking before saving it."));
139 ac->addAction(QStringLiteral("create_note"), act);
140 connect(act, &QAction::triggered, this, &ViewerPluginCreatenoteInterface::slotActivatePlugin);
141 mAction.append(act);
142 }
143 }
144
slotCreateNote(const KMime::Message::Ptr & notePtr,const Akonadi::Collection & collection)145 void ViewerPluginCreatenoteInterface::slotCreateNote(const KMime::Message::Ptr ¬ePtr, const Akonadi::Collection &collection)
146 {
147 auto createJob = new CreateNoteJob(notePtr, collection, mMessageItem, this);
148 createJob->start();
149 }
150
widget()151 NoteEdit *ViewerPluginCreatenoteInterface::widget()
152 {
153 if (!mNoteEdit) {
154 auto parentWidget = static_cast<QWidget *>(parent());
155 mNoteEdit = new NoteEdit(parentWidget);
156 connect(mNoteEdit, &NoteEdit::createNote, this, &ViewerPluginCreatenoteInterface::slotCreateNote);
157 mNoteEdit->setObjectName(QStringLiteral("noteedit"));
158 parentWidget->layout()->addWidget(mNoteEdit);
159 mNoteEdit->hide();
160 }
161 return mNoteEdit;
162 }
163