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 "viewerplugincreateeventinterface.h"
8 #include "createeventjob.h"
9 #include "eventedit.h"
10 #include <KLocalizedString>
11 
12 #include <KActionCollection>
13 #include <QAction>
14 #include <QIcon>
15 #include <QLayout>
16 
17 using namespace MessageViewer;
18 
ViewerPluginCreateEventInterface(KActionCollection * ac,QWidget * parent)19 ViewerPluginCreateEventInterface::ViewerPluginCreateEventInterface(KActionCollection *ac, QWidget *parent)
20     : ViewerPluginInterface(parent)
21 {
22     createAction(ac);
23 }
24 
~ViewerPluginCreateEventInterface()25 ViewerPluginCreateEventInterface::~ViewerPluginCreateEventInterface()
26 {
27 }
28 
featureTypes() const29 ViewerPluginInterface::SpecificFeatureTypes ViewerPluginCreateEventInterface::featureTypes() const
30 {
31     return ViewerPluginInterface::NeedMessage;
32 }
33 
setText(const QString & text)34 void ViewerPluginCreateEventInterface::setText(const QString &text)
35 {
36     Q_UNUSED(text)
37     // Nothing
38 }
39 
actions() const40 QList<QAction *> ViewerPluginCreateEventInterface::actions() const
41 {
42     return mAction;
43 }
44 
setMessage(const KMime::Message::Ptr & value)45 void ViewerPluginCreateEventInterface::setMessage(const KMime::Message::Ptr &value)
46 {
47     widget()->setMessage(value);
48 }
49 
closePlugin()50 void ViewerPluginCreateEventInterface::closePlugin()
51 {
52     if (mEventEdit) {
53         mEventEdit->slotCloseWidget();
54     }
55 }
56 
showWidget()57 void ViewerPluginCreateEventInterface::showWidget()
58 {
59     widget()->showEventEdit();
60 }
61 
setMessageItem(const Akonadi::Item & item)62 void ViewerPluginCreateEventInterface::setMessageItem(const Akonadi::Item &item)
63 {
64     mMessageItem = item;
65 }
66 
createAction(KActionCollection * ac)67 void ViewerPluginCreateEventInterface::createAction(KActionCollection *ac)
68 {
69     if (ac) {
70         auto act = new QAction(QIcon::fromTheme(QStringLiteral("appointment-new")), i18n("Create Event..."), this);
71         act->setIconText(i18n("Create Event"));
72         addHelpTextAction(act, i18n("Allows you to create a calendar Event"));
73         ac->addAction(QStringLiteral("create_event"), act);
74         ac->setDefaultShortcut(act, QKeySequence(Qt::CTRL | Qt::Key_E));
75         connect(act, &QAction::triggered, this, &ViewerPluginCreateEventInterface::slotActivatePlugin);
76         mAction.append(act);
77     }
78 }
79 
widget()80 EventEdit *ViewerPluginCreateEventInterface::widget()
81 {
82     if (!mEventEdit) {
83         auto parentWidget = static_cast<QWidget *>(parent());
84         mEventEdit = new EventEdit(parentWidget);
85         connect(mEventEdit, &EventEdit::createEvent, this, &ViewerPluginCreateEventInterface::slotCreateEvent);
86         mEventEdit->setObjectName(QStringLiteral("eventedit"));
87         parentWidget->layout()->addWidget(mEventEdit);
88         mEventEdit->hide();
89     }
90     return mEventEdit;
91 }
92 
slotCreateEvent(const KCalendarCore::Event::Ptr & eventPtr,const Akonadi::Collection & collection)93 void ViewerPluginCreateEventInterface::slotCreateEvent(const KCalendarCore::Event::Ptr &eventPtr, const Akonadi::Collection &collection)
94 {
95     auto createJob = new CreateEventJob(eventPtr, collection, mMessageItem, this);
96     createJob->start();
97 }
98