2 * Copyright (c) 2014 Sandro Knauß <knauss@kolabsys.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * As a special exception, permission is given to link this program
19 * with any edition of Qt, and distribute the resulting executable,
20 * without including the source code for Qt in the source distribution.
23 #include "incidenceresource.h"
24 #include "resourcemanagement.h"
25 #include "resourcemodel.h"
26 #include "attendeecomboboxdelegate.h"
27 #include "attendeelineeditdelegate.h"
28 #include "incidencedatetime.h"
30 #include "ui_dialogdesktop.h"
32 #include <KDescendantsProxyModel>
33 #include <KEmailAddress>
34 #include <KMessageBox>
35 #include <KGlobalSettings>
40 using namespace IncidenceEditorNG
;
42 class SwitchRoleProxy
: public QSortFilterProxyModel
46 explicit SwitchRoleProxy(QObject
*parent
= Q_NULLPTR
)
47 : QSortFilterProxyModel(parent
)
52 QVariant
data(const QModelIndex
&index
, int role
) const Q_DECL_OVERRIDE
55 if (role
== Qt::DisplayRole
|| role
== Qt::EditRole
) {
56 d
= QSortFilterProxyModel::data(index
, ResourceModel::FullName
);
59 d
= QSortFilterProxyModel::data(index
, role
);
64 IncidenceResource::IncidenceResource(IncidenceAttendee
*ieAttendee
, IncidenceDateTime
*dateTime
, Ui::EventOrTodoDesktop
*ui
)
67 , dataModel(ieAttendee
->dataModel())
69 , resourceDialog(new ResourceManagement())
71 setObjectName(QStringLiteral("IncidenceResource"));
72 connect(resourceDialog
, &ResourceManagement::accepted
, this, &IncidenceResource::dialogOkPressed
);
74 connect(mDateTime
, &IncidenceDateTime::startDateChanged
, this, &IncidenceResource::slotDateChanged
);
75 connect(mDateTime
, &IncidenceDateTime::endDateChanged
, this, &IncidenceResource::slotDateChanged
);
78 attrs
<< QStringLiteral("cn") << QStringLiteral("mail");
80 completer
= new QCompleter(this);
81 ResourceModel
*model
= new ResourceModel(attrs
, this);
83 KDescendantsProxyModel
*proxyModel
= new KDescendantsProxyModel(this);
84 proxyModel
->setSourceModel(model
);
85 SwitchRoleProxy
*proxyModel2
= new SwitchRoleProxy(this);
86 proxyModel2
->setSourceModel(proxyModel
);
88 completer
->setModel(proxyModel2
);
89 completer
->setCompletionRole(ResourceModel::FullName
);
90 completer
->setWrapAround(false);
91 mUi
->mNewResource
->setCompleter(completer
);
93 AttendeeLineEditDelegate
*attendeeDelegate
= new AttendeeLineEditDelegate(this);
94 attendeeDelegate
->setCompletionMode((KCompletion::CompletionMode
)KGlobalSettings::self()->completionMode());
96 ResourceFilterProxyModel
*filterProxyModel
= new ResourceFilterProxyModel(this);
97 filterProxyModel
->setDynamicSortFilter(true);
98 filterProxyModel
->setSourceModel(dataModel
);
100 mUi
->mResourcesTable
->setModel(filterProxyModel
);
101 mUi
->mResourcesTable
->setItemDelegateForColumn(AttendeeTableModel::Role
, ieAttendee
->roleDelegate());
102 mUi
->mResourcesTable
->setItemDelegateForColumn(AttendeeTableModel::FullName
, attendeeDelegate
);
103 mUi
->mResourcesTable
->setItemDelegateForColumn(AttendeeTableModel::Status
, ieAttendee
->stateDelegate());
104 mUi
->mResourcesTable
->setItemDelegateForColumn(AttendeeTableModel::Response
, ieAttendee
->responseDelegate());
106 connect(mUi
->mFindResourcesButton
, &QPushButton::clicked
, this, &IncidenceResource::findResources
);
107 connect(mUi
->mBookResourceButton
, &QPushButton::clicked
, this, &IncidenceResource::bookResource
);
108 connect(filterProxyModel
, &ResourceFilterProxyModel::layoutChanged
, this, &IncidenceResource::layoutChanged
);
109 connect(filterProxyModel
, &ResourceFilterProxyModel::layoutChanged
, this, &IncidenceResource::updateCount
);
110 connect(filterProxyModel
, &ResourceFilterProxyModel::rowsInserted
, this, &IncidenceResource::updateCount
);
111 connect(filterProxyModel
, &ResourceFilterProxyModel::rowsRemoved
, this, &IncidenceResource::updateCount
);
112 // only update when FullName is changed
113 connect(filterProxyModel
, &ResourceFilterProxyModel::dataChanged
, this, &IncidenceResource::updateCount
);
116 void IncidenceResource::load(const KCalCore::Incidence::Ptr
&incidence
)
121 void IncidenceResource::slotDateChanged()
123 resourceDialog
->slotDateChanged(mDateTime
->startDate(), mDateTime
->endDate());
126 void IncidenceResource::save(const KCalCore::Incidence::Ptr
&incidence
)
128 //all logic inside IncidenceAtendee (using same model)
131 bool IncidenceResource::isDirty() const
133 //all logic inside IncidenceAtendee (using same model)
137 void IncidenceResource::bookResource()
139 if (mUi
->mNewResource
->text().trimmed().isEmpty()) {
143 KEmailAddress::extractEmailAddressAndName(mUi
->mNewResource
->text(), email
, name
);
144 KCalCore::Attendee::Ptr
attendee(new KCalCore::Attendee(name
, email
));
145 attendee
->setCuType(KCalCore::Attendee::Resource
);
146 dataModel
->insertAttendee(dataModel
->rowCount(), attendee
);
149 void IncidenceResource::findResources()
151 resourceDialog
->show();
154 void IncidenceResource::dialogOkPressed()
156 ResourceItem::Ptr item
= resourceDialog
->selectedItem();
158 const QString name
= item
->ldapObject().value(QStringLiteral("cn"));
159 const QString email
= item
->ldapObject().value(QStringLiteral("mail"));
160 KCalCore::Attendee::Ptr
attendee(new KCalCore::Attendee(name
, email
));
161 attendee
->setCuType(KCalCore::Attendee::Resource
);
162 dataModel
->insertAttendee(dataModel
->rowCount(), attendee
);
166 void IncidenceResource::layoutChanged()
168 QHeaderView
*headerView
= mUi
->mResourcesTable
->horizontalHeader();
169 headerView
->setSectionHidden(AttendeeTableModel::CuType
, true);
170 headerView
->setSectionHidden(AttendeeTableModel::Name
, true);
171 headerView
->setSectionHidden(AttendeeTableModel::Email
, true);
172 headerView
->setResizeMode(AttendeeTableModel::Role
, QHeaderView::ResizeToContents
);
173 headerView
->setResizeMode(AttendeeTableModel::FullName
, QHeaderView::Stretch
);
174 headerView
->setResizeMode(AttendeeTableModel::Available
, QHeaderView::ResizeToContents
);
175 headerView
->setResizeMode(AttendeeTableModel::Status
, QHeaderView::ResizeToContents
);
176 headerView
->setResizeMode(AttendeeTableModel::Response
, QHeaderView::ResizeToContents
);
179 void IncidenceResource::updateCount()
181 Q_EMIT
resourceCountChanged(resourceCount());
184 int IncidenceResource::resourceCount() const
188 QAbstractItemModel
*model
= mUi
->mResourcesTable
->model();
192 const int nbRow
= model
->rowCount(QModelIndex());
193 for (int i
= 0; i
< nbRow
; ++i
) {
194 index
= model
->index(i
, AttendeeTableModel::FullName
);
195 if (!model
->data(index
).toString().isEmpty()) {