incidenceresource.cpp 6.8 KB
Newer Older
1
/*
2
 * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
3
 *
4
 * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 */

#include "incidenceresource.h"
#include "resourcemanagement.h"
#include "resourcemodel.h"
#include "attendeecomboboxdelegate.h"
#include "attendeelineeditdelegate.h"
#include "incidencedatetime.h"

#include "ui_dialogdesktop.h"

#include <KDescendantsProxyModel>
#include <KEmailAddress>


using namespace IncidenceEditorNG;

Laurent Montel's avatar
Laurent Montel committed
22
class SwitchRoleProxy : public QSortFilterProxyModel
23
24
{
public:
Laurent Montel's avatar
Laurent Montel committed
25
    explicit SwitchRoleProxy(QObject *parent = nullptr)
26
27
28
29
        : QSortFilterProxyModel(parent)
    {
    }

30
    QVariant data(const QModelIndex &index, int role) const override
31
32
33
34
35
36
37
38
39
40
41
    {
        QVariant d;
        if (role == Qt::DisplayRole || role == Qt::EditRole) {
            d = QSortFilterProxyModel::data(index, ResourceModel::FullName);
            return d;
        }
        d = QSortFilterProxyModel::data(index, role);
        return d;
    }
};

Laurent Montel's avatar
Laurent Montel committed
42
IncidenceResource::IncidenceResource(IncidenceAttendee *ieAttendee, IncidenceDateTime *dateTime, Ui::EventOrTodoDesktop *ui)
Laurent Montel's avatar
Laurent Montel committed
43
    : IncidenceEditor(nullptr)
44
45
46
47
48
49
    , mUi(ui)
    , dataModel(ieAttendee->dataModel())
    , mDateTime(dateTime)
    , resourceDialog(new ResourceManagement())
{
    setObjectName(QStringLiteral("IncidenceResource"));
Laurent Montel's avatar
Laurent Montel committed
50
51
    connect(resourceDialog, &ResourceManagement::accepted, this,
            &IncidenceResource::dialogOkPressed);
52

Laurent Montel's avatar
Laurent Montel committed
53
54
55
56
    connect(mDateTime, &IncidenceDateTime::startDateChanged, this,
            &IncidenceResource::slotDateChanged);
    connect(mDateTime, &IncidenceDateTime::endDateChanged, this,
            &IncidenceResource::slotDateChanged);
57
58
59
60
61

    QStringList attrs;
    attrs << QStringLiteral("cn") <<  QStringLiteral("mail");

    completer = new QCompleter(this);
Laurent Montel's avatar
Laurent Montel committed
62
    auto *model = new ResourceModel(attrs, this);
63

Laurent Montel's avatar
Laurent Montel committed
64
    auto *proxyModel = new KDescendantsProxyModel(this);
65
    proxyModel->setSourceModel(model);
Laurent Montel's avatar
Laurent Montel committed
66
    auto *proxyModel2 = new SwitchRoleProxy(this);
67
68
69
70
71
72
73
    proxyModel2->setSourceModel(proxyModel);

    completer->setModel(proxyModel2);
    completer->setCompletionRole(ResourceModel::FullName);
    completer->setWrapAround(false);
    mUi->mNewResource->setCompleter(completer);

Laurent Montel's avatar
Laurent Montel committed
74
    auto *attendeeDelegate = new AttendeeLineEditDelegate(this);
75

Laurent Montel's avatar
Laurent Montel committed
76
    auto *filterProxyModel = new ResourceFilterProxyModel(this);
77
78
79
80
    filterProxyModel->setDynamicSortFilter(true);
    filterProxyModel->setSourceModel(dataModel);

    mUi->mResourcesTable->setModel(filterProxyModel);
Laurent Montel's avatar
Laurent Montel committed
81
82
    mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Role,
                                                   ieAttendee->roleDelegate());
83
    mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::FullName, attendeeDelegate);
Laurent Montel's avatar
Laurent Montel committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Status,
                                                   ieAttendee->stateDelegate());
    mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Response,
                                                   ieAttendee->responseDelegate());

    connect(mUi->mFindResourcesButton, &QPushButton::clicked, this,
            &IncidenceResource::findResources);
    connect(mUi->mBookResourceButton, &QPushButton::clicked, this,
            &IncidenceResource::bookResource);
    connect(filterProxyModel, &ResourceFilterProxyModel::layoutChanged, this,
            &IncidenceResource::layoutChanged);
    connect(filterProxyModel, &ResourceFilterProxyModel::layoutChanged, this,
            &IncidenceResource::updateCount);
    connect(filterProxyModel, &ResourceFilterProxyModel::rowsInserted, this,
            &IncidenceResource::updateCount);
    connect(filterProxyModel, &ResourceFilterProxyModel::rowsRemoved, this,
            &IncidenceResource::updateCount);
101
    // only update when FullName is changed
Laurent Montel's avatar
Laurent Montel committed
102
103
    connect(filterProxyModel, &ResourceFilterProxyModel::dataChanged, this,
            &IncidenceResource::updateCount);
104
105
}

Laurent Montel's avatar
Laurent Montel committed
106
107
108
109
110
IncidenceResource::~IncidenceResource()
{
    delete resourceDialog;
}

111
void IncidenceResource::load(const KCalendarCore::Incidence::Ptr &incidence)
112
{
Allen Winter's avatar
Allen Winter committed
113
    Q_UNUSED(incidence);
114
115
116
117
118
119
120
121
    slotDateChanged();
}

void IncidenceResource::slotDateChanged()
{
    resourceDialog->slotDateChanged(mDateTime->startDate(), mDateTime->endDate());
}

122
void IncidenceResource::save(const KCalendarCore::Incidence::Ptr &incidence)
123
{
Allen Winter's avatar
Allen Winter committed
124
    Q_UNUSED(incidence);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    //all logic inside IncidenceAtendee (using same model)
}

bool IncidenceResource::isDirty() const
{
    //all logic inside IncidenceAtendee (using same model)
    return false;
}

void IncidenceResource::bookResource()
{
    if (mUi->mNewResource->text().trimmed().isEmpty()) {
        return;
    }
    QString name, email;
    KEmailAddress::extractEmailAddressAndName(mUi->mNewResource->text(), email, name);
141
142
    KCalendarCore::Attendee attendee(name, email);
    attendee.setCuType(KCalendarCore::Attendee::Resource);
143
144
145
146
147
148
149
150
151
152
153
154
    dataModel->insertAttendee(dataModel->rowCount(), attendee);
}

void IncidenceResource::findResources()
{
    resourceDialog->show();
}

void IncidenceResource::dialogOkPressed()
{
    ResourceItem::Ptr item = resourceDialog->selectedItem();
    if (item) {
Laurent Montel's avatar
Laurent Montel committed
155
156
        const QString name = QString::fromLatin1(item->ldapObject().value(QStringLiteral("cn")));
        const QString email = QString::fromLatin1(item->ldapObject().value(QStringLiteral("mail")));
157
158
        KCalendarCore::Attendee attendee(name, email);
        attendee.setCuType(KCalendarCore::Attendee::Resource);
159
160
161
162
163
164
165
166
167
168
        dataModel->insertAttendee(dataModel->rowCount(), attendee);
    }
}

void IncidenceResource::layoutChanged()
{
    QHeaderView *headerView = mUi->mResourcesTable->horizontalHeader();
    headerView->setSectionHidden(AttendeeTableModel::CuType, true);
    headerView->setSectionHidden(AttendeeTableModel::Name, true);
    headerView->setSectionHidden(AttendeeTableModel::Email, true);
Laurent Montel's avatar
Laurent Montel committed
169
    headerView->setSectionResizeMode(AttendeeTableModel::Role, QHeaderView::ResizeToContents);
Laurent Montel's avatar
Laurent Montel committed
170
    headerView->setSectionResizeMode(AttendeeTableModel::FullName, QHeaderView::Stretch);
Laurent Montel's avatar
Laurent Montel committed
171
172
173
    headerView->setSectionResizeMode(AttendeeTableModel::Available, QHeaderView::ResizeToContents);
    headerView->setSectionResizeMode(AttendeeTableModel::Status, QHeaderView::ResizeToContents);
    headerView->setSectionResizeMode(AttendeeTableModel::Response, QHeaderView::ResizeToContents);
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
}

void IncidenceResource::updateCount()
{
    Q_EMIT resourceCountChanged(resourceCount());
}

int IncidenceResource::resourceCount() const
{
    int c = 0;
    QModelIndex index;
    QAbstractItemModel *model = mUi->mResourcesTable->model();
    if (!model) {
        return 0;
    }
    const int nbRow = model->rowCount(QModelIndex());
    for (int i = 0; i < nbRow; ++i) {
        index = model->index(i, AttendeeTableModel::FullName);
        if (!model->data(index).toString().isEmpty()) {
            ++c;
        }
    }
    return c;
}