french -> French
[kdepim.git] / calendarsupport / kcalmodel.cpp
blobcd89d9b10c54a710ad81c36688b1dc8178cee1b5
1 /*
2 Copyright (c) 2008 Bruno Virlet <bvirlet@kdemail.net>
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
17 02110-1301, USA.
20 #include "kcalmodel.h"
22 #include <Akonadi/Collection>
23 #include <Akonadi/Item>
24 #include <Akonadi/ItemFetchScope>
26 #include <KCalCore/FreeBusy>
27 #include <KCalCore/Event>
28 #include <KCalCore/Journal>
29 #include <KCalCore/Todo>
31 #include <KIconLoader>
32 #include <KLocale>
34 #include <QPixmap>
36 using namespace CalendarSupport;
38 class KCalModel::Private
40 public:
41 Private( KCalModel *model )
42 :q( model )
46 static QStringList allMimeTypes()
48 QStringList types;
49 types << KCalCore::Event::eventMimeType()
50 << KCalCore::Todo::todoMimeType()
51 << KCalCore::Journal::journalMimeType()
52 << KCalCore::FreeBusy::freeBusyMimeType();
53 return types;
55 bool collectionMatchesMimeTypes() const
57 Q_FOREACH ( const QString &type, allMimeTypes() ) {
58 if ( q->collection().contentMimeTypes().contains( type ) ) {
59 return true;
62 return false;
65 bool collectionIsValid()
67 return
68 !q->collection().isValid() ||
69 collectionMatchesMimeTypes() ||
70 q->collection().contentMimeTypes() == QStringList( QLatin1String( "inode/directory" ) );
73 private:
74 KCalModel *q;
77 KCalModel::KCalModel( QObject *parent )
78 : ItemModel( parent ), d( new Private( this ) )
80 fetchScope().fetchFullPayload();
83 KCalModel::~KCalModel()
85 delete d;
88 QStringList KCalModel::mimeTypes() const
90 return
91 QStringList()
92 << QLatin1String( "text/uri-list" )
93 << d->allMimeTypes();
96 int KCalModel::columnCount( const QModelIndex & ) const
98 if ( d->collectionIsValid() ) {
99 return 4;
100 } else {
101 return 1;
105 int KCalModel::rowCount( const QModelIndex & ) const
107 if ( d->collectionIsValid() ) {
108 return ItemModel::rowCount();
109 } else {
110 return 1;
114 QVariant KCalModel::data( const QModelIndex &index, int role ) const
116 if ( role == ItemModel::IdRole ) {
117 return ItemModel::data( index, role );
120 if ( !index.isValid() || index.row() >= rowCount() ) {
121 return QVariant();
124 // guard against use with collections that do not have the right contents
125 if ( !d->collectionIsValid() ) {
126 if ( role == Qt::DisplayRole ) {
127 return i18nc( "@info",
128 "This model can only handle event, task, journal or free-busy list folders. "
129 "The current collection holds mimetypes: %1",
130 collection().contentMimeTypes().join( QLatin1String( "," ) ) );
132 return QVariant();
135 const Akonadi::Item item = itemForIndex( index );
137 if ( !item.hasPayload<KCalCore::Incidence::Ptr>() ) {
138 return QVariant();
141 const KCalCore::Incidence::Ptr incidence = item.payload<KCalCore::Incidence::Ptr>();
143 // Icon for the model entry
144 switch( role ) {
145 case Qt::DecorationRole:
146 if ( index.column() == 0 ) {
147 if ( incidence->type() == KCalCore::Incidence::TypeTodo ) {
148 return SmallIcon( QLatin1String( "view-pim-tasks" ) );
149 } else if ( incidence->type() == KCalCore::Incidence::TypeJournal ) {
150 return SmallIcon( QLatin1String( "view-pim-journal" ) );
151 } else if ( incidence->type() == KCalCore::Incidence::TypeEvent ) {
152 return SmallIcon( QLatin1String( "view-calendar" ) );
153 } else {
154 return SmallIcon( QLatin1String( "network-wired" ) );
157 break;
158 case Qt::DisplayRole:
159 switch( index.column() ) {
160 case Summary:
161 return incidence->summary();
162 break;
163 case DateTimeStart:
164 return incidence->dtStart().toString();
165 break;
166 case DateTimeEnd:
167 return incidence->dateTime( KCalCore::Incidence::RoleEnd ).toString();
168 break;
169 case Type:
170 return incidence->type();
171 break;
172 default:
173 break;
175 break;
176 default:
177 return QVariant();
178 break;
181 return QVariant();
184 QVariant KCalModel::headerData( int section, Qt::Orientation orientation, int role ) const
186 if ( !d->collectionIsValid() ) {
187 return QVariant();
190 if ( role == Qt::DisplayRole && orientation == Qt::Horizontal ) {
191 switch( section ) {
192 case Summary:
193 return i18nc( "@title:column, calendar event summary", "Summary" );
194 case DateTimeStart:
195 return i18nc( "@title:column, calendar event start date and time", "Start date and time" );
196 case DateTimeEnd:
197 return i18nc( "@title:column, calendar event end date and time", "End date and time" );
198 case Type:
199 return i18nc( "@title:column, calendar event type", "Type" );
200 default:
201 return QString();
205 return Akonadi::ItemModel::headerData( section, orientation, role );