Better wording
[kdepim.git] / calendarsupport / calendarmodel.cpp
blob251a615ff76b45946c0478f31ca78e29020f22b3
1 /*
2 Copyright (c) 2008 Bruno Virlet <bvirlet@kdemail.net>
3 2009 KDAB; Author: Frank Osterfeld <osterfeld@kde.org>
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.
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.
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.
21 #include "calendarmodel.h"
22 #include "utils.h"
24 #include <Akonadi/ChangeRecorder>
25 #include <Akonadi/ItemFetchScope>
27 #include <KDateTime>
28 #include <KIconLoader>
29 #include <KLocale>
31 #include <QPixmap>
33 using namespace CalendarSupport;
35 class CalendarModel::Private
37 public:
38 explicit Private( CalendarModel *qq )
39 :q( qq )
43 private:
44 CalendarModel *const q;
47 CalendarModel::CalendarModel( Akonadi::ChangeRecorder *monitor, QObject *parent )
48 : EntityTreeModel( monitor, parent ),
49 d( new Private( this ) )
51 monitor->itemFetchScope().fetchAllAttributes( true );
54 CalendarModel::~CalendarModel()
56 delete d;
59 static KDateTime primaryDateForIncidence( const Akonadi::Item &item )
61 if ( const KCalCore::Todo::Ptr t = CalendarSupport::todo( item ) ) {
62 return t->hasDueDate() ? t->dtDue() : KDateTime();
65 if ( const KCalCore::Event::Ptr e = CalendarSupport::event( item ) ) {
66 return ( !e->recurs() && !e->isMultiDay() ) ? e->dtStart() : KDateTime();
69 if ( const KCalCore::Journal::Ptr j = CalendarSupport::journal( item ) ) {
70 return j->dtStart();
73 return KDateTime();
76 QVariant CalendarModel::entityData( const Akonadi::Item &item, int column, int role ) const
78 const KCalCore::Incidence::Ptr incidence = CalendarSupport::incidence( item );
79 if ( !incidence ) {
80 return QVariant();
83 switch( role ) {
84 case Qt::DecorationRole:
85 if ( column != Summary ) {
86 return QVariant();
88 if ( incidence->type() == KCalCore::IncidenceBase::TypeTodo ) {
89 return SmallIcon( QLatin1String( "view-pim-tasks" ) );
91 if ( incidence->type() == KCalCore::IncidenceBase::TypeJournal ) {
92 return SmallIcon( QLatin1String( "view-pim-journal" ) );
94 if ( incidence->type() == KCalCore::IncidenceBase::TypeEvent ) {
95 return SmallIcon( QLatin1String( "view-calendar" ) );
97 return SmallIcon( QLatin1String( "network-wired" ) );
99 case Qt::DisplayRole:
100 switch( column ) {
101 case Summary:
102 return incidence->summary();
104 case DateTimeStart:
105 return incidence->dtStart().toString();
107 case DateTimeEnd:
108 return incidence->dateTime( KCalCore::Incidence::RoleEndTimeZone ).toString();
110 case DateTimeDue:
111 if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
112 return todo->dtDue().toString();
113 } else {
114 return QVariant();
117 case Priority:
118 if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
119 return todo->priority();
120 } else {
121 return QVariant();
124 case PercentComplete:
125 if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
126 return todo->percentComplete();
127 } else {
128 return QVariant();
131 case PrimaryDate:
132 return primaryDateForIncidence( item ).toString();
134 case Type:
136 return incidence->type();
137 default:
138 break;
141 case SortRole:
142 switch( column ) {
143 case Summary:
144 return incidence->summary();
146 case DateTimeStart:
147 return incidence->dtStart().toUtc().dateTime();
149 case DateTimeEnd:
150 return incidence->dateTime( KCalCore::Incidence::RoleEndTimeZone ).toUtc().dateTime();
152 case DateTimeDue:
153 if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
154 return todo->dtDue().toUtc().dateTime();
155 } else {
156 return QVariant();
159 case PrimaryDate:
160 return primaryDateForIncidence( item ).toUtc().dateTime();
162 case Priority:
163 if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
164 return todo->priority();
165 } else {
166 return QVariant();
169 case PercentComplete:
170 if ( KCalCore::Todo::Ptr todo = CalendarSupport::todo( item ) ) {
171 return todo->percentComplete();
172 } else {
173 return QVariant();
176 case Type:
177 return incidence->type();
179 default:
180 break;
183 return QVariant();
185 case RecursRole:
186 return incidence->recurs();
188 default:
189 return QVariant();
192 return QVariant();
195 QVariant CalendarModel::entityData( const Akonadi::Collection &collection,
196 int column, int role ) const
198 return EntityTreeModel::entityData( collection, column, role );
201 int CalendarModel::entityColumnCount( EntityTreeModel::HeaderGroup headerSet ) const
203 if ( headerSet == EntityTreeModel::ItemListHeaders ) {
204 return ItemColumnCount;
205 } else {
206 return CollectionColumnCount;
210 QVariant CalendarModel::entityHeaderData( int section, Qt::Orientation orientation,
211 int role, EntityTreeModel::HeaderGroup headerSet ) const
213 if ( role != Qt::DisplayRole || orientation != Qt::Horizontal ) {
214 return QVariant();
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();
238 if ( headerSet == EntityTreeModel::CollectionTreeHeaders ) {
239 switch ( section ) {
240 case CollectionTitle:
241 return i18nc( "@title:column calendar title", "Calendar" );
242 default:
243 return QVariant();
246 return QVariant();