~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/calendar/akonadi/calendarmodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2008 Bruno Virlet <bvirlet@kdemail.net>
 
3
                2009 KDAB; Author: Frank Osterfeld <osterfeld@kde.org>
 
4
 
 
5
  This library is free software; you can redistribute it and/or modify it
 
6
  under the terms of the GNU Library General Public License as published by
 
7
  the Free Software Foundation; either version 2 of the License, or (at your
 
8
  option) any later version.
 
9
 
 
10
  This library is distributed in the hope that it will be useful, but WITHOUT
 
11
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
12
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
 
13
  License for more details.
 
14
 
 
15
  You should have received a copy of the GNU Library General Public License
 
16
  along with this library; see the file COPYING.LIB.  If not, write to the
 
17
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
18
  02110-1301, USA.
 
19
*/
 
20
 
 
21
#include "calendarmodel.h"
 
22
#include "utils.h"
 
23
 
 
24
#include <Akonadi/ChangeRecorder>
 
25
#include <Akonadi/ItemFetchScope>
 
26
 
 
27
#include <KDateTime>
 
28
#include <KIconLoader>
 
29
#include <KLocale>
 
30
 
 
31
#include <QPixmap>
 
32
 
 
33
using namespace CalendarSupport;
 
34
 
 
35
class CalendarModel::Private
 
36
{
 
37
  public:
 
38
    explicit Private( CalendarModel *qq )
 
39
    :q( qq )
 
40
    {
 
41
    }
 
42
 
 
43
  private:
 
44
    CalendarModel *const q;
 
45
};
 
46
 
 
47
CalendarModel::CalendarModel( Akonadi::ChangeRecorder *monitor, QObject *parent )
 
48
  : EntityTreeModel( monitor, parent ),
 
49
    d( new Private( this ) )
 
50
{
 
51
  monitor->itemFetchScope().fetchAllAttributes( true );
 
52
}
 
53
 
 
54
CalendarModel::~CalendarModel()
 
55
{
 
56
  delete d;
 
57
}
 
58
 
 
59
static KDateTime primaryDateForIncidence( const Akonadi::Item &item )
 
60
{
 
61
  if ( const KCalCore::Todo::Ptr t = CalendarSupport::todo( item ) ) {
 
62
    return t->hasDueDate() ? t->dtDue() : KDateTime();
 
63
  }
 
64
 
 
65
  if ( const KCalCore::Event::Ptr e = CalendarSupport::event( item ) ) {
 
66
    return ( !e->recurs() && !e->isMultiDay() ) ? e->dtStart() : KDateTime();
 
67
  }
 
68
 
 
69
  if ( const KCalCore::Journal::Ptr j = CalendarSupport::journal( item ) ) {
 
70
    return j->dtStart();
 
71
  }
 
72
 
 
73
  return KDateTime();
 
74
}
 
75
 
 
76
QVariant CalendarModel::entityData( const Akonadi::Item &item, int column, int role ) const
 
77
{
 
78
  const KCalCore::Incidence::Ptr incidence = CalendarSupport::incidence( item );
 
79
  if ( !incidence ) {
 
80
    return QVariant();
 
81
  }
 
82
 
 
83
  switch( role ) {
 
84
  case Qt::DecorationRole:
 
85
    if ( column != Summary ) {
 
86
      return QVariant();
 
87
    }
 
88
    if ( incidence->type() == KCalCore::IncidenceBase::TypeTodo ) {
 
89
      return SmallIcon( QLatin1String( "view-pim-tasks" ) );
 
90
    }
 
91
    if ( incidence->type() == KCalCore::IncidenceBase::TypeJournal ) {
 
92
      return SmallIcon( QLatin1String( "view-pim-journal" ) );
 
93
    }
 
94
    if ( incidence->type() == KCalCore::IncidenceBase::TypeEvent ) {
 
95
      return SmallIcon( QLatin1String( "view-calendar" ) );
 
96
    }
 
97
    return SmallIcon( QLatin1String( "network-wired" ) );
 
98
 
 
99
  case Qt::DisplayRole:
 
100
    switch( column ) {
 
101
    case Summary:
 
102
      return incidence->summary();
 
103
 
 
104
    case DateTimeStart:
 
105
      return incidence->dtStart().toString();
 
106
 
 
107
    case DateTimeEnd:
 
108
      return incidence->dateTime( KCalCore::Incidence::RoleEndTimeZone ).toString();
 
109
 
 
110
    case DateTimeDue:
 
111
      if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
 
112
        return todo->dtDue().toString();
 
113
      } else {
 
114
        return QVariant();
 
115
      }
 
116
 
 
117
    case Priority:
 
118
      if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
 
119
        return todo->priority();
 
120
      } else {
 
121
        return QVariant();
 
122
      }
 
123
 
 
124
    case PercentComplete:
 
125
      if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
 
126
        return todo->percentComplete();
 
127
      } else {
 
128
        return QVariant();
 
129
      }
 
130
 
 
131
    case PrimaryDate:
 
132
      return primaryDateForIncidence( item ).toString();
 
133
 
 
134
    case Type:
 
135
 
 
136
      return incidence->type();
 
137
    default:
 
138
      break;
 
139
    }
 
140
 
 
141
    case SortRole:
 
142
    switch( column ) {
 
143
    case Summary:
 
144
      return incidence->summary();
 
145
 
 
146
    case DateTimeStart:
 
147
      return incidence->dtStart().toUtc().dateTime();
 
148
 
 
149
    case DateTimeEnd:
 
150
      return incidence->dateTime( KCalCore::Incidence::RoleEndTimeZone ).toUtc().dateTime();
 
151
 
 
152
    case DateTimeDue:
 
153
      if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
 
154
        return todo->dtDue().toUtc().dateTime();
 
155
      } else {
 
156
        return QVariant();
 
157
      }
 
158
 
 
159
    case PrimaryDate:
 
160
      return primaryDateForIncidence( item ).toUtc().dateTime();
 
161
 
 
162
    case Priority:
 
163
      if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
 
164
        return todo->priority();
 
165
      } else {
 
166
        return QVariant();
 
167
      }
 
168
 
 
169
    case PercentComplete:
 
170
      if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
 
171
        return todo->percentComplete();
 
172
      } else {
 
173
        return QVariant();
 
174
      }
 
175
 
 
176
    case Type:
 
177
      return incidence->type();
 
178
 
 
179
    default:
 
180
      break;
 
181
    }
 
182
 
 
183
    return QVariant();
 
184
 
 
185
  case RecursRole:
 
186
    return incidence->recurs();
 
187
 
 
188
  default:
 
189
    return QVariant();
 
190
  }
 
191
 
 
192
  return QVariant();
 
193
}
 
194
 
 
195
QVariant CalendarModel::entityData( const Akonadi::Collection &collection,
 
196
                                    int column, int role ) const
 
197
{
 
198
  return EntityTreeModel::entityData( collection, column, role );
 
199
}
 
200
 
 
201
int CalendarModel::entityColumnCount( EntityTreeModel::HeaderGroup headerSet ) const
 
202
{
 
203
  if ( headerSet == EntityTreeModel::ItemListHeaders ) {
 
204
    return ItemColumnCount;
 
205
  } else {
 
206
    return CollectionColumnCount;
 
207
  }
 
208
}
 
209
 
 
210
QVariant CalendarModel::entityHeaderData( int section, Qt::Orientation orientation,
 
211
                                          int role, EntityTreeModel::HeaderGroup headerSet ) const
 
212
{
 
213
  if ( role != Qt::DisplayRole || orientation != Qt::Horizontal ) {
 
214
    return QVariant();
 
215
  }
 
216
 
 
217
  if ( headerSet == EntityTreeModel::ItemListHeaders ) {
 
218
    switch( section ) {
 
219
    case Summary:
 
220
      return i18nc( "@title:column calendar event summary", "Summary" );
 
221
    case DateTimeStart:
 
222
      return i18nc( "@title:column calendar event start date and time", "Start Date and Time" );
 
223
    case DateTimeEnd:
 
224
      return i18nc( "@title:column calendar event end date and time", "End Date and Time" );
 
225
    case Type:
 
226
      return i18nc( "@title:column calendar event type", "Type" );
 
227
    case DateTimeDue:
 
228
      return i18nc( "@title:column todo item due date and time", "Due Date and Time" );
 
229
    case Priority:
 
230
      return i18nc( "@title:column todo item priority", "Priority" );
 
231
    case PercentComplete:
 
232
      return i18nc( "@title:column todo item completion in percent", "Complete" );
 
233
    default:
 
234
      return QVariant();
 
235
    }
 
236
  }
 
237
 
 
238
  if ( headerSet == EntityTreeModel::CollectionTreeHeaders ) {
 
239
    switch ( section ) {
 
240
    case CollectionTitle:
 
241
      return i18nc( "@title:column calendar title", "Calendar" );
 
242
    default:
 
243
      return QVariant();
 
244
    }
 
245
  }
 
246
  return QVariant();
 
247
}
 
248