~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to mobile/calendar/eventlistproxy.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2010 Volker Krause <vkrause@kde.org>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
    02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "eventlistproxy.h"
 
21
 
 
22
#include <akonadi/item.h>
 
23
 
 
24
#include <KCalCore/Event>
 
25
#include <KCalCore/Todo>
 
26
#include <KCalUtils/IncidenceFormatter>
 
27
 
 
28
#include <KGlobal>
 
29
#include <KLocale>
 
30
#include <KSystemTimeZones>
 
31
 
 
32
#include <QtGui/QItemSelection>
 
33
 
 
34
EventListProxy::EventListProxy(QObject* parent) : ListProxy(parent)
 
35
{
 
36
  setDynamicSortFilter( true );
 
37
  sort( 0, Qt::DescendingOrder );
 
38
 
 
39
  mCurrentDateTimeReference = QDateTime::currentMSecsSinceEpoch();
 
40
  mToday = KDateTime::currentLocalDateTime();
 
41
}
 
42
 
 
43
QVariant EventListProxy::data(const QModelIndex& index, int role) const
 
44
{
 
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>();
 
48
    switch ( role ) {
 
49
      case SummaryRole:
 
50
        return incidence->summary();
 
51
      case BeginRole:
 
52
        return KGlobal::locale()->formatDateTime( incidence->dtStart(), KLocale::FancyShortDate );
 
53
      case DurationRole:
 
54
        return KCalUtils::IncidenceFormatter::durationString( incidence );
 
55
    }
 
56
  }
 
57
  return QSortFilterProxyModel::data(index, role);
 
58
}
 
59
 
 
60
void EventListProxy::setSourceModel( QAbstractItemModel *model )
 
61
{
 
62
  if ( sourceModel() )
 
63
    disconnect( sourceModel(), SIGNAL( dataChanged( const QModelIndex&, const QModelIndex& ) ),
 
64
                this, SLOT( dataChanged( const QModelIndex&, const QModelIndex& ) ) );
 
65
 
 
66
  ListProxy::setSourceModel( model );
 
67
 
 
68
  connect( sourceModel(), SIGNAL( dataChanged( const QModelIndex&, const QModelIndex& ) ),
 
69
           this, SLOT( dataChanged( const QModelIndex&, const QModelIndex& ) ) );
 
70
 
 
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 );
 
77
}
 
78
 
 
79
KDateTime EventListProxy::startDateTimeForItem( const Akonadi::Item &item ) const
 
80
{
 
81
  const QHash<Akonadi::Item::Id, DateTimeHashEntry>::const_iterator it = mDateTimeHash.constFind( item.id() );
 
82
  if ( it != mDateTimeHash.constEnd() )
 
83
    return (*it).startDateTime;
 
84
 
 
85
  const KCalCore::Event::Ptr event = item.payload<KCalCore::Event::Ptr>();
 
86
 
 
87
  DateTimeHashEntry entry;
 
88
  entry.startDateTime = event->dtStart();
 
89
  entry.endDateTime = event->dtEnd();
 
90
 
 
91
  mDateTimeHash.insert( item.id(), entry );
 
92
 
 
93
  return entry.startDateTime;
 
94
}
 
95
 
 
96
KDateTime EventListProxy::endDateTimeForItem( const Akonadi::Item &item ) const
 
97
{
 
98
  const QHash<Akonadi::Item::Id, DateTimeHashEntry>::const_iterator it = mDateTimeHash.constFind( item.id() );
 
99
  if ( it != mDateTimeHash.constEnd() )
 
100
    return (*it).endDateTime;
 
101
 
 
102
  const KCalCore::Event::Ptr event = item.payload<KCalCore::Event::Ptr>();
 
103
 
 
104
  DateTimeHashEntry entry;
 
105
  entry.startDateTime = event->dtStart();
 
106
  entry.endDateTime = event->dtEnd();
 
107
 
 
108
  mDateTimeHash.insert( item.id(), entry );
 
109
 
 
110
  return entry.endDateTime;
 
111
}
 
112
 
 
113
bool EventListProxy::lessThan(const QModelIndex& left, const QModelIndex& right) const
 
114
{
 
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();
 
119
 
 
120
  const KDateTime leftDateTimeStart = startDateTimeForItem( leftItem );
 
121
  const KDateTime leftDateTimeEnd = endDateTimeForItem( leftItem );
 
122
  const KDateTime rightDateTimeStart = startDateTimeForItem( rightItem );
 
123
  const KDateTime rightDateTimeEnd = endDateTimeForItem( rightItem );
 
124
 
 
125
  if ( leftDateTimeStart == rightDateTimeStart )
 
126
    return leftItem.id() < rightItem.id();
 
127
 
 
128
  if ( (mCurrentDateTimeReference + 60000) < QDateTime::currentMSecsSinceEpoch() ) {
 
129
    mCurrentDateTimeReference = QDateTime::currentMSecsSinceEpoch();
 
130
    mToday = KDateTime::currentLocalDateTime();
 
131
  }
 
132
 
 
133
  const bool leftIsFuture = (leftDateTimeEnd >= mToday);
 
134
  const bool rightIsFuture = (rightDateTimeEnd >= mToday);
 
135
 
 
136
  if ( leftIsFuture != rightIsFuture ) {
 
137
    return !leftIsFuture;
 
138
  }
 
139
 
 
140
  if ( leftIsFuture )
 
141
    return leftDateTimeStart > rightDateTimeStart;
 
142
  else
 
143
    return leftDateTimeStart < rightDateTimeStart;
 
144
}
 
145
 
 
146
void EventListProxy::dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight )
 
147
{
 
148
  QItemSelection selection;
 
149
  selection.select( topLeft, bottomRight );
 
150
 
 
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>() )
 
154
      continue;
 
155
 
 
156
    const KCalCore::Event::Ptr event = item.payload<KCalCore::Event::Ptr>();
 
157
 
 
158
    DateTimeHashEntry entry;
 
159
    entry.startDateTime = event->dtStart();
 
160
    entry.endDateTime = event->dtEnd();
 
161
 
 
162
    mDateTimeHash.insert( item.id(), entry );
 
163
  }
 
164
}
 
165
 
 
166
#include "eventlistproxy.moc"