2
Copyright (c) 2010 Volker Krause <vkrause@kde.org>
4
This library is free software; you can redistribute it and/or modify it
5
under the terms of the GNU Library General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or (at your
7
option) any later version.
9
This library is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12
License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to the
16
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
#include "eventlistproxy.h"
22
#include <akonadi/item.h>
24
#include <KCalCore/Event>
25
#include <KCalCore/Todo>
26
#include <KCalUtils/IncidenceFormatter>
30
#include <KSystemTimeZones>
32
#include <QtGui/QItemSelection>
34
EventListProxy::EventListProxy(QObject* parent) : ListProxy(parent)
36
setDynamicSortFilter( true );
37
sort( 0, Qt::DescendingOrder );
39
mCurrentDateTimeReference = QDateTime::currentMSecsSinceEpoch();
40
mToday = KDateTime::currentLocalDateTime();
43
QVariant EventListProxy::data(const QModelIndex& index, int role) const
45
const Akonadi::Item item = QSortFilterProxyModel::data( index, Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
46
if ( item.isValid() && item.hasPayload<KCalCore::Incidence::Ptr>() ) {
47
const KCalCore::Incidence::Ptr incidence = item.payload<KCalCore::Incidence::Ptr>();
50
return incidence->summary();
52
return KGlobal::locale()->formatDateTime( incidence->dtStart(), KLocale::FancyShortDate );
54
return KCalUtils::IncidenceFormatter::durationString( incidence );
57
return QSortFilterProxyModel::data(index, role);
60
void EventListProxy::setSourceModel( QAbstractItemModel *model )
63
disconnect( sourceModel(), SIGNAL( dataChanged( const QModelIndex&, const QModelIndex& ) ),
64
this, SLOT( dataChanged( const QModelIndex&, const QModelIndex& ) ) );
66
ListProxy::setSourceModel( model );
68
connect( sourceModel(), SIGNAL( dataChanged( const QModelIndex&, const QModelIndex& ) ),
69
this, SLOT( dataChanged( const QModelIndex&, const QModelIndex& ) ) );
71
QHash<int, QByteArray> names = roleNames();
72
names.insert( Akonadi::EntityTreeModel::ItemIdRole, "itemId" );
73
names.insert( SummaryRole, "summary" );
74
names.insert( BeginRole, "begin" );
75
names.insert( DurationRole, "duration" );
76
setRoleNames( names );
79
KDateTime EventListProxy::startDateTimeForItem( const Akonadi::Item &item ) const
81
const QHash<Akonadi::Item::Id, DateTimeHashEntry>::const_iterator it = mDateTimeHash.constFind( item.id() );
82
if ( it != mDateTimeHash.constEnd() )
83
return (*it).startDateTime;
85
const KCalCore::Event::Ptr event = item.payload<KCalCore::Event::Ptr>();
87
DateTimeHashEntry entry;
88
entry.startDateTime = event->dtStart();
89
entry.endDateTime = event->dtEnd();
91
mDateTimeHash.insert( item.id(), entry );
93
return entry.startDateTime;
96
KDateTime EventListProxy::endDateTimeForItem( const Akonadi::Item &item ) const
98
const QHash<Akonadi::Item::Id, DateTimeHashEntry>::const_iterator it = mDateTimeHash.constFind( item.id() );
99
if ( it != mDateTimeHash.constEnd() )
100
return (*it).endDateTime;
102
const KCalCore::Event::Ptr event = item.payload<KCalCore::Event::Ptr>();
104
DateTimeHashEntry entry;
105
entry.startDateTime = event->dtStart();
106
entry.endDateTime = event->dtEnd();
108
mDateTimeHash.insert( item.id(), entry );
110
return entry.endDateTime;
113
bool EventListProxy::lessThan(const QModelIndex& left, const QModelIndex& right) const
115
const Akonadi::Item leftItem = left.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
116
const Akonadi::Item rightItem = right.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
117
if ( !leftItem.hasPayload<KCalCore::Event::Ptr>() || !rightItem.hasPayload<KCalCore::Event::Ptr>() )
118
return leftItem.id() < rightItem.id();
120
const KDateTime leftDateTimeStart = startDateTimeForItem( leftItem );
121
const KDateTime leftDateTimeEnd = endDateTimeForItem( leftItem );
122
const KDateTime rightDateTimeStart = startDateTimeForItem( rightItem );
123
const KDateTime rightDateTimeEnd = endDateTimeForItem( rightItem );
125
if ( leftDateTimeStart == rightDateTimeStart )
126
return leftItem.id() < rightItem.id();
128
if ( (mCurrentDateTimeReference + 60000) < QDateTime::currentMSecsSinceEpoch() ) {
129
mCurrentDateTimeReference = QDateTime::currentMSecsSinceEpoch();
130
mToday = KDateTime::currentLocalDateTime();
133
const bool leftIsFuture = (leftDateTimeEnd >= mToday);
134
const bool rightIsFuture = (rightDateTimeEnd >= mToday);
136
if ( leftIsFuture != rightIsFuture ) {
137
return !leftIsFuture;
141
return leftDateTimeStart > rightDateTimeStart;
143
return leftDateTimeStart < rightDateTimeStart;
146
void EventListProxy::dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight )
148
QItemSelection selection;
149
selection.select( topLeft, bottomRight );
151
foreach ( const QModelIndex &index, selection.indexes() ) {
152
const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
153
if ( !item.hasPayload<KCalCore::Event::Ptr>() )
156
const KCalCore::Event::Ptr event = item.payload<KCalCore::Event::Ptr>();
158
DateTimeHashEntry entry;
159
entry.startDateTime = event->dtStart();
160
entry.endDateTime = event->dtEnd();
162
mDateTimeHash.insert( item.id(), entry );
166
#include "eventlistproxy.moc"