incidenceresource.cpp 7.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Copyright (c) 2014 Sandro Knauß <knauss@kolabsys.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * As a special exception, permission is given to link this program
 * with any edition of Qt, and distribute the resulting executable,
 * without including the source code for Qt in the source distribution.
 */

#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>

#include <QCompleter>

using namespace IncidenceEditorNG;

Laurent Montel's avatar
Laurent Montel committed
39
class SwitchRoleProxy : public QSortFilterProxyModel
40
41
{
public:
Laurent Montel's avatar
Laurent Montel committed
42
    explicit SwitchRoleProxy(QObject *parent = nullptr)
43
44
45
46
        : QSortFilterProxyModel(parent)
    {
    }

47
    QVariant data(const QModelIndex &index, int role) const override
48
49
50
51
52
53
54
55
56
57
58
    {
        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
59
IncidenceResource::IncidenceResource(IncidenceAttendee *ieAttendee, IncidenceDateTime *dateTime, Ui::EventOrTodoDesktop *ui)
Laurent Montel's avatar
Laurent Montel committed
60
    : IncidenceEditor(nullptr)
61
62
63
64
65
66
    , mUi(ui)
    , dataModel(ieAttendee->dataModel())
    , mDateTime(dateTime)
    , resourceDialog(new ResourceManagement())
{
    setObjectName(QStringLiteral("IncidenceResource"));
Laurent Montel's avatar
Laurent Montel committed
67
68
    connect(resourceDialog, &ResourceManagement::accepted, this,
            &IncidenceResource::dialogOkPressed);
69

Laurent Montel's avatar
Laurent Montel committed
70
71
72
73
    connect(mDateTime, &IncidenceDateTime::startDateChanged, this,
            &IncidenceResource::slotDateChanged);
    connect(mDateTime, &IncidenceDateTime::endDateChanged, this,
            &IncidenceResource::slotDateChanged);
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

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

    completer = new QCompleter(this);
    ResourceModel *model = new ResourceModel(attrs, this);

    KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel(this);
    proxyModel->setSourceModel(model);
    SwitchRoleProxy *proxyModel2 = new SwitchRoleProxy(this);
    proxyModel2->setSourceModel(proxyModel);

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

    AttendeeLineEditDelegate *attendeeDelegate = new AttendeeLineEditDelegate(this);

    ResourceFilterProxyModel *filterProxyModel = new ResourceFilterProxyModel(this);
    filterProxyModel->setDynamicSortFilter(true);
    filterProxyModel->setSourceModel(dataModel);

    mUi->mResourcesTable->setModel(filterProxyModel);
Laurent Montel's avatar
Laurent Montel committed
98
99
    mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Role,
                                                   ieAttendee->roleDelegate());
100
    mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::FullName, attendeeDelegate);
Laurent Montel's avatar
Laurent Montel committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    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);
118
    // only update when FullName is changed
Laurent Montel's avatar
Laurent Montel committed
119
120
    connect(filterProxyModel, &ResourceFilterProxyModel::dataChanged, this,
            &IncidenceResource::updateCount);
121
122
}

123
void IncidenceResource::load(const KCalendarCore::Incidence::Ptr &incidence)
124
{
Allen Winter's avatar
Allen Winter committed
125
    Q_UNUSED(incidence);
126
127
128
129
130
131
132
133
    slotDateChanged();
}

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

134
void IncidenceResource::save(const KCalendarCore::Incidence::Ptr &incidence)
135
{
Allen Winter's avatar
Allen Winter committed
136
    Q_UNUSED(incidence);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    //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);
153
154
    KCalendarCore::Attendee attendee(name, email);
    attendee.setCuType(KCalendarCore::Attendee::Resource);
155
156
157
158
159
160
161
162
163
164
165
166
    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
167
168
        const QString name = QString::fromLatin1(item->ldapObject().value(QStringLiteral("cn")));
        const QString email = QString::fromLatin1(item->ldapObject().value(QStringLiteral("mail")));
169
170
        KCalendarCore::Attendee attendee(name, email);
        attendee.setCuType(KCalendarCore::Attendee::Resource);
171
172
173
174
175
176
177
178
179
180
        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
181
    headerView->setSectionResizeMode(AttendeeTableModel::Role, QHeaderView::ResizeToContents);
Laurent Montel's avatar
Laurent Montel committed
182
    headerView->setSectionResizeMode(AttendeeTableModel::FullName, QHeaderView::Stretch);
Laurent Montel's avatar
Laurent Montel committed
183
184
185
    headerView->setSectionResizeMode(AttendeeTableModel::Available, QHeaderView::ResizeToContents);
    headerView->setSectionResizeMode(AttendeeTableModel::Status, QHeaderView::ResizeToContents);
    headerView->setSectionResizeMode(AttendeeTableModel::Response, QHeaderView::ResizeToContents);
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
}

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;
}