eventedit.cpp 12 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
2
3
4
   SPDX-FileCopyrightText: 2014-2020 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: GPL-2.0-or-later
Laurent Montel's avatar
Laurent Montel committed
5
6
7
8
9
10
*/

#include "eventedit.h"
#include "createeventplugin_debug.h"
#include "globalsettings_messageviewer.h"
#include "eventdatetimewidget.h"
11
#include <CalendarSupport/KCalPrefs>
Laurent Montel's avatar
Laurent Montel committed
12
#include <CalendarSupport/Utils>
Laurent Montel's avatar
Laurent Montel committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <KLocalizedString>
#include <QLineEdit>
#include <QIcon>
#include <QDateTime>

#include <QHBoxLayout>
#include <QEvent>
#include <QKeyEvent>
#include <QLabel>

#include <AkonadiWidgets/CollectionComboBox>
#include <QPushButton>

Laurent Montel's avatar
Laurent Montel committed
27
28
#include <IncidenceEditor/IncidenceDialogFactory>
#include <IncidenceEditor/IncidenceDialog>
Laurent Montel's avatar
Laurent Montel committed
29
30
31
#include <KGuiItem>
#include <KStandardGuiItem>

Laurent Montel's avatar
Laurent Montel committed
32
namespace MessageViewer {
Laurent Montel's avatar
Laurent Montel committed
33
QAbstractItemModel *_k_eventEditStubModel = nullptr;
Laurent Montel's avatar
Laurent Montel committed
34
35
36
37
38
39
40
}

using namespace MessageViewer;

EventEdit::EventEdit(QWidget *parent)
    : QWidget(parent)
{
Laurent Montel's avatar
Laurent Montel committed
41
    auto *vbox = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
42
    vbox->setContentsMargins(5, 5, 5, 5);
Laurent Montel's avatar
Laurent Montel committed
43
44
    vbox->setSpacing(2);

Laurent Montel's avatar
Laurent Montel committed
45
    auto *hbox = new QHBoxLayout;
Laurent Montel's avatar
Use {}    
Laurent Montel committed
46
    hbox->setContentsMargins({});
Laurent Montel's avatar
Laurent Montel committed
47
48
49
    hbox->setSpacing(2);
    vbox->addLayout(hbox);

Laurent Montel's avatar
Laurent Montel committed
50
    QLabel *lab = new QLabel(i18n("Event:"), this);
Laurent Montel's avatar
Laurent Montel committed
51
52
    hbox->addWidget(lab);

Laurent Montel's avatar
Laurent Montel committed
53
    mEventEdit = new QLineEdit(this);
Laurent Montel's avatar
Laurent Montel committed
54
55
56
57
58
59
60
61
62
    mEventEdit->setClearButtonEnabled(true);
    mEventEdit->setObjectName(QStringLiteral("eventedit"));
    mEventEdit->setFocus();
    connect(mEventEdit, &QLineEdit::returnPressed, this, &EventEdit::slotReturnPressed);
    connect(mEventEdit, &QLineEdit::textChanged, this, &EventEdit::slotUpdateButtons);
    hbox->addWidget(mEventEdit);

    hbox->addSpacing(5);

Laurent Montel's avatar
Laurent Montel committed
63
    mCollectionCombobox = new Akonadi::CollectionComboBox(_k_eventEditStubModel, this);
Laurent Montel's avatar
Laurent Montel committed
64
65
    mCollectionCombobox->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
    mCollectionCombobox->setMinimumWidth(250);
66
    mCollectionCombobox->setMimeTypeFilter(QStringList() << KCalendarCore::Event::eventMimeType());
Laurent Montel's avatar
Laurent Montel committed
67
68
69
70
71
72
    mCollectionCombobox->setObjectName(QStringLiteral("akonadicombobox"));
#ifndef QT_NO_ACCESSIBILITY
    mCollectionCombobox->setAccessibleDescription(i18n("Calendar where the new event will be stored."));
#endif
    mCollectionCombobox->setToolTip(i18n("Calendar where the new event will be stored"));

Laurent Montel's avatar
Laurent Montel committed
73
74
    connect(mCollectionCombobox, qOverload<int>(&Akonadi::CollectionComboBox::currentIndexChanged), this, &EventEdit::slotCollectionChanged);
    connect(mCollectionCombobox, qOverload<int>(&Akonadi::CollectionComboBox::activated), this, &EventEdit::slotCollectionChanged);
Laurent Montel's avatar
Laurent Montel committed
75

76
    connect(mCollectionCombobox->model(), &QAbstractItemModel::rowsInserted, this, &EventEdit::comboboxRowInserted);
Laurent Montel's avatar
Laurent Montel committed
77
78
79
    hbox->addWidget(mCollectionCombobox);

    hbox = new QHBoxLayout;
Laurent Montel's avatar
Use {}    
Laurent Montel committed
80
    hbox->setContentsMargins({});
Laurent Montel's avatar
Laurent Montel committed
81
82
83
    hbox->setSpacing(2);
    vbox->addLayout(hbox);

Laurent Montel's avatar
Laurent Montel committed
84
    lab = new QLabel(i18n("Start:"), this);
Laurent Montel's avatar
Laurent Montel committed
85
86
    hbox->addWidget(lab);
    QDateTime currentDateTime = QDateTime::currentDateTime();
Laurent Montel's avatar
Laurent Montel committed
87
    mStartDateTimeEdit = new EventDateTimeWidget(this);
Laurent Montel's avatar
Laurent Montel committed
88
89
90
91
92
93
94
95
96
97
    mStartDateTimeEdit->setObjectName(QStringLiteral("startdatetimeedit"));
    mStartDateTimeEdit->setDateTime(currentDateTime);
#ifndef QT_NO_ACCESSIBILITY
    mStartDateTimeEdit->setAccessibleDescription(i18n("Select start time for event."));
#endif
    connect(mStartDateTimeEdit, &EventDateTimeWidget::dateTimeChanged, this, &EventEdit::slotStartDateTimeChanged);
    hbox->addWidget(mStartDateTimeEdit);

    hbox->addSpacing(5);

Laurent Montel's avatar
Laurent Montel committed
98
    lab = new QLabel(i18n("End:"), this);
Laurent Montel's avatar
Laurent Montel committed
99
    hbox->addWidget(lab);
Laurent Montel's avatar
Laurent Montel committed
100
    mEndDateTimeEdit = new EventDateTimeWidget(this);
Laurent Montel's avatar
Laurent Montel committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    mEndDateTimeEdit->setObjectName(QStringLiteral("enddatetimeedit"));
    mEndDateTimeEdit->setDateTime(currentDateTime.addSecs(3600));
#ifndef QT_NO_ACCESSIBILITY
    mEndDateTimeEdit->setAccessibleDescription(i18n("Select end time for event."));
#endif
    connect(mEndDateTimeEdit, &EventDateTimeWidget::dateTimeChanged,
            this, &EventEdit::slotEndDateTimeChanged);

    hbox->addWidget(mEndDateTimeEdit);

    hbox->addStretch(1);

    hbox = new QHBoxLayout;
    hbox->setSpacing(2);
Laurent Montel's avatar
Use {}    
Laurent Montel committed
115
    hbox->setContentsMargins({});
Laurent Montel's avatar
Laurent Montel committed
116
117
118
119
    vbox->addLayout(hbox);

    hbox->addStretch(1);

Laurent Montel's avatar
Laurent Montel committed
120
    mSaveButton = new QPushButton(QIcon::fromTheme(QStringLiteral("appointment-new")), i18n("&Save"), this);
Laurent Montel's avatar
Laurent Montel committed
121
122
123
124
125
126
127
128
    mSaveButton->setObjectName(QStringLiteral("save-button"));
    mSaveButton->setEnabled(false);
#ifndef QT_NO_ACCESSIBILITY
    mSaveButton->setAccessibleDescription(i18n("Create new event and close this widget."));
#endif
    connect(mSaveButton, &QPushButton::clicked, this, &EventEdit::slotReturnPressed);
    hbox->addWidget(mSaveButton);

129
    mOpenEditorButton = new QPushButton(i18n("Open &Editor..."), this);
Laurent Montel's avatar
Laurent Montel committed
130
131
132
133
134
135
136
137
#ifndef QT_NO_ACCESSIBILITY
    mOpenEditorButton->setAccessibleDescription(i18n("Open event editor, where more details can be changed."));
#endif
    mOpenEditorButton->setObjectName(QStringLiteral("open-editor-button"));
    mOpenEditorButton->setEnabled(false);
    connect(mOpenEditorButton, &QPushButton::clicked, this, &EventEdit::slotOpenEditor);
    hbox->addWidget(mOpenEditorButton);

Laurent Montel's avatar
Laurent Montel committed
138
    auto *btn = new QPushButton(this);
Laurent Montel's avatar
Laurent Montel committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    KGuiItem::assign(btn, KStandardGuiItem::cancel());
    btn->setObjectName(QStringLiteral("close-button"));
#ifndef QT_NO_ACCESSIBILITY
    btn->setAccessibleDescription(i18n("Close the widget for creating new events."));
#endif
    connect(btn, &QPushButton::clicked, this, &EventEdit::slotCloseWidget);
    hbox->addWidget(btn);

    readConfig();
    setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
    installEventFilter(this);
    mCollectionCombobox->installEventFilter(this);
}

EventEdit::~EventEdit()
{
    writeConfig();
}

158
159
160
161
162
void EventEdit::comboboxRowInserted()
{
    slotUpdateButtons(mEventEdit->text());
}

Laurent Montel's avatar
Laurent Montel committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
void EventEdit::writeConfig()
{
    const Akonadi::Collection col = mCollectionCombobox->currentCollection();
    // col might not be valid if the collection wasn't found yet (the combo is async), skip saving in that case.
    if (col.isValid() && col.id() != MessageViewer::MessageViewerSettingsBase::self()->lastEventSelectedFolder()) {
        MessageViewer::MessageViewerSettingsBase::self()->setLastEventSelectedFolder(col.id());
        MessageViewer::MessageViewerSettingsBase::self()->save();
    }
}

void EventEdit::slotUpdateButtons(const QString &subject)
{
    const bool subjectIsNotEmpty = !subject.trimmed().isEmpty();
    const bool collectionComboboxEmpty = (mCollectionCombobox->count() < 1);
    mSaveButton->setEnabled(subjectIsNotEmpty && !collectionComboboxEmpty);
    mOpenEditorButton->setEnabled(subjectIsNotEmpty && !collectionComboboxEmpty);
}

void EventEdit::showEventEdit()
{
    mEventEdit->setFocus();
    QDateTime currentDateTime = QDateTime::currentDateTime();
    mStartDateTimeEdit->setDateTime(currentDateTime);
    mEndDateTimeEdit->setDateTime(currentDateTime.addSecs(3600));
    show();
}

void EventEdit::readConfig()
{
    const qint64 id = MessageViewer::MessageViewerSettingsBase::self()->lastEventSelectedFolder();
    if (id >= 0) {
        mCollectionCombobox->setDefaultCollection(Akonadi::Collection(id));
    }
}

Akonadi::Collection EventEdit::collection() const
{
    return mCollection;
}

void EventEdit::slotCollectionChanged(int /*index*/)
{
    setCollection(mCollectionCombobox->currentCollection());
}

void EventEdit::setCollection(const Akonadi::Collection &value)
{
    if (mCollection != value) {
        mCollection = value;
        Q_EMIT collectionChanged(mCollection);
    }
}

KMime::Message::Ptr EventEdit::message() const
{
    return mMessage;
}

void EventEdit::setMessage(const KMime::Message::Ptr &value)
{
    if (mMessage != value) {
        mMessage = value;
Laurent Montel's avatar
Laurent Montel committed
225
        const KMime::Headers::Subject *const subject = mMessage ? mMessage->subject(false) : nullptr;
Laurent Montel's avatar
Laurent Montel committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
        if (subject) {
            mEventEdit->setText(subject->asUnicodeString());
            mEventEdit->selectAll();
            mEventEdit->setFocus();
        } else {
            mEventEdit->clear();
        }
        Q_EMIT messageChanged(mMessage);
    }
}

void EventEdit::slotCloseWidget()
{
    if (isVisible()) {
        writeConfig();
        mEventEdit->clear();
        mMessage = KMime::Message::Ptr();
        hide();
    }
}

void EventEdit::slotReturnPressed()
{
    if (!mMessage) {
        qCDebug(CREATEEVENTPLUGIN_LOG) << " Message is null";
        return;
    }
    const Akonadi::Collection collection = mCollectionCombobox->currentCollection();
    if (!collection.isValid()) {
        qCDebug(CREATEEVENTPLUGIN_LOG) << " Collection is not valid";
        return;
    }

    const QDateTime dtstart = mStartDateTimeEdit->dateTime();
    const QDateTime dtend = mEndDateTimeEdit->dateTime();
    if (!dtstart.isValid() || !dtend.isValid()) {
        qCDebug(CREATEEVENTPLUGIN_LOG) << " date is not valid !";
        return;
    }

    if (!mEventEdit->text().trimmed().isEmpty()) {
267
        KCalendarCore::Event::Ptr event = createEventItem();
Laurent Montel's avatar
Laurent Montel committed
268
269
270
271
272
273
274
275
276
277
278
279
280
        Q_EMIT createEvent(event, collection);
        hide();
    }
}

bool EventEdit::eventFilter(QObject *object, QEvent *e)
{
    // Close the bar when pressing Escape.
    // Not using a QShortcut for this because it could conflict with
    // window-global actions (e.g. Emil Sedgh binds Esc to "close tab").
    // With a shortcut override we can catch this before it gets to kactions.
    const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride);
    if (shortCutOverride) {
Laurent Montel's avatar
Laurent Montel committed
281
        auto *kev = static_cast<QKeyEvent * >(e);
Laurent Montel's avatar
Laurent Montel committed
282
283
284
285
        if (kev->key() == Qt::Key_Escape) {
            e->accept();
            slotCloseWidget();
            return true;
Laurent Montel's avatar
Laurent Montel committed
286
287
288
        } else if (kev->key() == Qt::Key_Enter
                   || kev->key() == Qt::Key_Return
                   || kev->key() == Qt::Key_Space) {
Laurent Montel's avatar
Laurent Montel committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
            e->accept();
            if (object == mCollectionCombobox) {
                mCollectionCombobox->showPopup();
            }
            return true;
        }
    }
    return QWidget::eventFilter(object, e);
}

void EventEdit::slotEndDateTimeChanged(const QDateTime &newDateTime)
{
    if (!newDateTime.isValid()) {
        return;
    }
    QDateTime currentDateTime = QDateTime::currentDateTime();
    if (newDateTime.date() > currentDateTime.date()) {
        QDateTime newDateDate = newDateTime;
        newDateDate.setTime(QTime(0, 0, 0));
        mEndDateTimeEdit->setMinimumDateTime(newDateDate);
    }
}

void EventEdit::slotStartDateTimeChanged(const QDateTime &newDateTime)
{
    if (!newDateTime.isValid()) {
        return;
    }

    if (mEndDateTimeEdit->date() == newDateTime.date() && mEndDateTimeEdit->time() < newDateTime.time()) {
        mEndDateTimeEdit->setTime(newDateTime.time());
    }
    if (mEndDateTimeEdit->date() < newDateTime.date()) {
        mEndDateTimeEdit->setDate(newDateTime.date());
    }

    mEndDateTimeEdit->setMinimumDateTime(newDateTime);
}

328
KCalendarCore::Event::Ptr EventEdit::createEventItem()
Laurent Montel's avatar
Laurent Montel committed
329
{
330
    KCalendarCore::Attachment attachment(mMessage->encodedContent().toBase64(), KMime::Message::mimeType());
Laurent Montel's avatar
Laurent Montel committed
331
332
    const KMime::Headers::Subject *const subject = mMessage->subject(false);
    if (subject) {
333
        attachment.setLabel(subject->asUnicodeString());
Laurent Montel's avatar
Laurent Montel committed
334
    }
335
    KCalendarCore::Event::Ptr event(new KCalendarCore::Event);
Laurent Montel's avatar
Laurent Montel committed
336
    event->setSummary(mEventEdit->text());
337
338
    event->setDtStart(mStartDateTimeEdit->dateTime());
    event->setDtEnd(mEndDateTimeEdit->dateTime());
Laurent Montel's avatar
Laurent Montel committed
339
    event->addAttachment(attachment);
340
341
342

    if (CalendarSupport::KCalPrefs::instance()->defaultEventReminders()) {
        KCalendarCore::Alarm::Ptr alm = event->newAlarm();
Laurent Montel's avatar
Laurent Montel committed
343
        CalendarSupport::createAlarmReminder(alm, event->type());
344
345
    }

Laurent Montel's avatar
Laurent Montel committed
346
347
348
349
350
    return event;
}

void EventEdit::slotOpenEditor()
{
351
    KCalendarCore::Event::Ptr event = createEventItem();
Laurent Montel's avatar
Laurent Montel committed
352
353

    Akonadi::Item item;
354
355
    item.setPayload<KCalendarCore::Event::Ptr>(event);
    item.setMimeType(KCalendarCore::Event::eventMimeType());
Laurent Montel's avatar
Laurent Montel committed
356

357
    IncidenceEditorNG::IncidenceDialog *dlg = IncidenceEditorNG::IncidenceDialogFactory::create(true, KCalendarCore::IncidenceBase::TypeEvent, nullptr, this);
358
    dlg->selectCollection(mCollectionCombobox->currentCollection());
Laurent Montel's avatar
Laurent Montel committed
359
360
361
362
    connect(dlg, &IncidenceEditorNG::IncidenceDialog::finished, this, &EventEdit::slotCloseWidget);
    dlg->load(item);
    dlg->open();
}