2 * birthdaymodel.cpp - model class for birthdays from address book
4 * Copyright © 2009 by Tobias Koenig <tokoe@kde.org>
5 * Copyright © 2007-2011 by David Jarvie <djarvie@kde.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "birthdaymodel.h"
23 #include "alarmcalendar.h"
25 #include <kalarmcal/kaevent.h>
27 #include <akonadi/changerecorder.h>
28 #include <akonadi/entitydisplayattribute.h>
29 #include <akonadi/itemfetchscope.h>
30 #include <akonadi/session.h>
31 #include <kabc/addressee.h>
36 using namespace KAlarmCal
;
39 BirthdayModel
* BirthdayModel::mInstance
= 0;
41 BirthdayModel::BirthdayModel(Akonadi::ChangeRecorder
* recorder
)
42 : Akonadi::ContactsTreeModel(recorder
)
44 setColumns(Columns() << FullName
<< Birthday
);
47 BirthdayModel::~BirthdayModel()
49 if (this == mInstance
)
53 BirthdayModel
* BirthdayModel::instance()
57 Akonadi::Session
* session
= new Akonadi::Session("KAlarm::BirthdayModelSession");
59 Akonadi::ItemFetchScope scope
;
60 scope
.fetchFullPayload(true);
61 scope
.fetchAttribute
<Akonadi::EntityDisplayAttribute
>();
63 Akonadi::ChangeRecorder
* recorder
= new Akonadi::ChangeRecorder
;
64 recorder
->setSession(session
);
65 recorder
->fetchCollection(true);
66 recorder
->setItemFetchScope(scope
);
67 recorder
->setCollectionMonitored(Akonadi::Collection::root());
68 recorder
->setMimeTypeMonitored(KABC::Addressee::mimeType(), true);
70 mInstance
= new BirthdayModel(recorder
);
76 QVariant
BirthdayModel::entityData(const Akonadi::Item
& item
, int column
, int role
) const
78 if (columns().at(column
) == Birthday
&& role
== Qt::DisplayRole
)
80 QDate date
= Akonadi::ContactsTreeModel::entityData(item
, column
, DateRole
).toDate();
82 return KGlobal::locale()->formatDate(date
, KLocale::ShortDate
);
84 return Akonadi::ContactsTreeModel::entityData(item
, column
, role
);
88 BirthdaySortModel::BirthdaySortModel(QObject
* parent
)
89 : QSortFilterProxyModel(parent
)
93 void BirthdaySortModel::setPrefixSuffix(const QString
& prefix
, const QString
& suffix
)
95 mContactsWithAlarm
.clear();
100 const KAEvent::List events
= AlarmCalendar::resources()->events(CalEvent::ACTIVE
);
101 for (int i
= 0, end
= events
.count(); i
< end
; ++i
)
103 KAEvent
* event
= events
[i
];
104 if (event
->actionSubType() == KAEvent::MESSAGE
105 && event
->recurType() == KARecurrence::ANNUAL_DATE
106 && (prefix
.isEmpty() || event
->message().startsWith(prefix
)))
107 mContactsWithAlarm
.append(event
->message());
113 bool BirthdaySortModel::filterAcceptsRow(int sourceRow
, const QModelIndex
& sourceParent
) const
115 const QModelIndex nameIndex
= sourceModel()->index(sourceRow
, 0, sourceParent
);
116 const QModelIndex birthdayIndex
= sourceModel()->index(sourceRow
, 1, sourceParent
);
118 // If the birthday is invalid, the second column is empty
119 if (birthdayIndex
.data(Qt::DisplayRole
).toString().isEmpty())
122 const QString text
= mPrefix
+ nameIndex
.data(Qt::DisplayRole
).toString() + mSuffix
;
123 if (mContactsWithAlarm
.contains(text
))