koeventviewerdialog.cpp 4.32 KB
Newer Older
1
2
3
/*
  This file is part of KOrganizer.

4
5
  SPDX-FileCopyrightText: 2000, 2001 Cornelius Schumacher <schumacher@kde.org>
  SPDX-FileCopyrightText: 2005 Rafal Rzepecki <divide@users.sourceforge.net>
6

7
  SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
8
9
10
*/

#include "koeventviewerdialog.h"
11
#include "korganizer_debug.h"
Laurent Montel's avatar
Laurent Montel committed
12
#include "korganizerinterface.h"
13

Sergio Martins's avatar
Sergio Martins committed
14
#include <Akonadi/Calendar/ETMCalendar>
Laurent Montel's avatar
Laurent Montel committed
15
16
#include <CalendarSupport/IncidenceViewer>
#include <CalendarSupport/Utils>
17

Laurent Montel's avatar
Laurent Montel committed
18
#include <AkonadiCore/Item>
19

Laurent Montel's avatar
Laurent Montel committed
20
#include <KGuiItem>
Laurent Montel's avatar
Laurent Montel committed
21
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
22
#include <QDialogButtonBox>
Laurent Montel's avatar
Laurent Montel committed
23
#include <QIcon>
Laurent Montel's avatar
Laurent Montel committed
24
#include <QPushButton>
25
#include <QStandardPaths>
Laurent Montel's avatar
Laurent Montel committed
26
#include <QVBoxLayout>
27

Laurent Montel's avatar
Laurent Montel committed
28
29
KOEventViewerDialog::KOEventViewerDialog(Akonadi::ETMCalendar *calendar, QWidget *parent)
    : QDialog(parent)
30
{
31
    setWindowTitle(i18nc("@title:window", "Event Viewer"));
32
33
    auto mainLayout = new QVBoxLayout(this);
    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
Laurent Montel's avatar
Laurent Montel committed
34
    mUser1Button = new QPushButton(this);
Laurent Montel's avatar
Laurent Montel committed
35
    buttonBox->addButton(mUser1Button, QDialogButtonBox::ActionRole);
36
    auto user2Button = new QPushButton(this);
Laurent Montel's avatar
Laurent Montel committed
37
38
39
    buttonBox->addButton(user2Button, QDialogButtonBox::ActionRole);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &KOEventViewerDialog::reject);
    setModal(false);
Laurent Montel's avatar
Laurent Montel committed
40
    KGuiItem::assign(mUser1Button, KGuiItem(i18n("Edit..."), QIcon::fromTheme(QStringLiteral("document-edit"))));
Laurent Montel's avatar
Laurent Montel committed
41
42
43
44
45
46
47
48
49
50
    KGuiItem::assign(user2Button, KGuiItem(i18n("Show in Context")));
    mEventViewer = new CalendarSupport::IncidenceViewer(calendar, this);
    mainLayout->addWidget(mEventViewer);
    mainLayout->addWidget(buttonBox);

    resize(QSize(500, 520).expandedTo(minimumSizeHint()));

    connect(this, &KOEventViewerDialog::finished, this, &KOEventViewerDialog::delayedDestruct);
    connect(mUser1Button, &QPushButton::clicked, this, &KOEventViewerDialog::editIncidence);
    connect(user2Button, &QPushButton::clicked, this, &KOEventViewerDialog::showIncidenceContext);
51
52
}

Glen Ditchfield's avatar
Glen Ditchfield committed
53
KOEventViewerDialog::~KOEventViewerDialog() = default;
54

Laurent Montel's avatar
Laurent Montel committed
55
56
57
58
59
60
61
62
63
void KOEventViewerDialog::delayedDestruct()
{
    if (isVisible()) {
        hide();
    }

    deleteLater();
}

Laurent Montel's avatar
Laurent Montel committed
64
65
QPushButton *KOEventViewerDialog::editButton() const
{
Laurent Montel's avatar
Laurent Montel committed
66
    return mUser1Button;
Laurent Montel's avatar
Laurent Montel committed
67
68
}

Laurent Montel's avatar
Laurent Montel committed
69
void KOEventViewerDialog::setIncidence(const Akonadi::Item &incidence, const QDate &date)
70
{
Laurent Montel's avatar
Laurent Montel committed
71
    mEventViewer->setIncidence(incidence, date);
72
73
}

Laurent Montel's avatar
Laurent Montel committed
74
void KOEventViewerDialog::addText(const QString &text)
75
{
Laurent Montel's avatar
Laurent Montel committed
76
    mEventViewer->setHeaderText(text);
77
78
79
80
}

void KOEventViewerDialog::editIncidence()
{
Laurent Montel's avatar
Laurent Montel committed
81
    const Akonadi::Item item = mEventViewer->item();
82

Laurent Montel's avatar
Laurent Montel committed
83
84
    if (CalendarSupport::hasIncidence(item)) {
        // make sure korganizer is running or the part is shown
Laurent Montel's avatar
Laurent Montel committed
85
        const QString desktopFile = QStandardPaths::locate(QStandardPaths::ApplicationsLocation, QStringLiteral("org.kde.korganizer.desktop"));
86
        QString error;
87
        if (!QDBusConnection::sessionBus().interface()->startService(desktopFile).isValid()) {
Laurent Montel's avatar
Laurent Montel committed
88
89
90
            OrgKdeKorganizerKorganizerInterface korganizerIface(QStringLiteral("org.kde.korganizer"),
                                                                QStringLiteral("/Korganizer"),
                                                                QDBusConnection::sessionBus());
91
92
93
94
            korganizerIface.editIncidence(QString::number(item.id()));
        } else {
            qCWarning(KORGANIZER_LOG) << "Failure starting korganizer:" << error;
        }
Laurent Montel's avatar
Laurent Montel committed
95
    }
96
97
98
99
}

void KOEventViewerDialog::showIncidenceContext()
{
Laurent Montel's avatar
Laurent Montel committed
100
    const Akonadi::Item item = mEventViewer->item();
101

Laurent Montel's avatar
Laurent Montel committed
102
103
    if (CalendarSupport::hasIncidence(item)) {
        // make sure korganizer is running or the part is shown
Laurent Montel's avatar
Laurent Montel committed
104
        const QString desktopFile = QStandardPaths::locate(QStandardPaths::ApplicationsLocation, QStringLiteral("org.kde.korganizer.desktop"));
105
        QString error;
106
        if (!QDBusConnection::sessionBus().interface()->startService(desktopFile).isValid()) {
Laurent Montel's avatar
Laurent Montel committed
107
108
109
            OrgKdeKorganizerKorganizerInterface korganizerIface(QStringLiteral("org.kde.korganizer"),
                                                                QStringLiteral("/Korganizer"),
                                                                QDBusConnection::sessionBus());
110
111
112
113
            korganizerIface.showIncidenceContext(QString::number(item.id()));
        } else {
            qCWarning(KORGANIZER_LOG) << "Failure starting korganizer:" << error;
        }
Laurent Montel's avatar
Laurent Montel committed
114
    }
115
}