File indexing completed on 2024-12-22 04:55:36
0001 /* 0002 * birthdaymodel.cpp - model class for birthdays from address book 0003 * Program: kalarm 0004 * SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org> 0005 * SPDX-FileCopyrightText: 2007-2022 David Jarvie <djarvie@kde.org> 0006 * 0007 * SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include "birthdaymodel.h" 0011 0012 #include <Akonadi/ChangeRecorder> 0013 #include <Akonadi/EntityDisplayAttribute> 0014 #include <Akonadi/ItemFetchScope> 0015 #include <Akonadi/Session> 0016 #include <KContacts/Addressee> 0017 0018 #include <QLocale> 0019 0020 0021 BirthdayModel* BirthdayModel::mInstance = nullptr; 0022 0023 BirthdayModel::BirthdayModel(Akonadi::ChangeRecorder* recorder) 0024 : Akonadi::ContactsTreeModel(recorder) 0025 { 0026 setColumns({FullName, Birthday}); 0027 } 0028 0029 BirthdayModel::~BirthdayModel() 0030 { 0031 if (this == mInstance) 0032 mInstance = nullptr; 0033 } 0034 0035 BirthdayModel* BirthdayModel::instance() 0036 { 0037 if (!mInstance) 0038 { 0039 Akonadi::Session* session = new Akonadi::Session("KAlarm::BirthdayModelSession"); 0040 0041 Akonadi::ItemFetchScope scope; 0042 scope.fetchFullPayload(true); 0043 scope.fetchAttribute<Akonadi::EntityDisplayAttribute>(); 0044 0045 auto recorder = new Akonadi::ChangeRecorder; 0046 recorder->setSession(session); 0047 recorder->fetchCollection(true); 0048 recorder->setItemFetchScope(scope); 0049 recorder->setCollectionMonitored(Akonadi::Collection::root()); 0050 recorder->setMimeTypeMonitored(KContacts::Addressee::mimeType(), true); 0051 0052 mInstance = new BirthdayModel(recorder); 0053 } 0054 0055 return mInstance; 0056 } 0057 0058 QVariant BirthdayModel::entityData(const Akonadi::Item& item, int column, int role) const 0059 { 0060 if (columns().at(column) == Birthday && role == Qt::DisplayRole) 0061 { 0062 const QDate date = Akonadi::ContactsTreeModel::entityData(item, column, DateRole).toDate(); 0063 if (date.isValid()) 0064 return QLocale().toString(date, QLocale::ShortFormat); 0065 } 0066 return Akonadi::ContactsTreeModel::entityData(item, column, role); 0067 } 0068 0069 /*============================================================================*/ 0070 0071 BirthdaySortModel::BirthdaySortModel(QObject* parent) 0072 : QSortFilterProxyModel(parent) 0073 { 0074 } 0075 0076 /****************************************************************************** 0077 * Set a new prefix and suffix for the alarm message, and set the selection list 0078 * based on them. 0079 */ 0080 void BirthdaySortModel::setPrefixSuffix(const QString& prefix, const QString& suffix, const QStringList& alarmMessageList) 0081 { 0082 mPrefix = prefix; 0083 mSuffix = suffix; 0084 mContactsWithAlarm = alarmMessageList; 0085 0086 invalidateFilter(); 0087 } 0088 0089 bool BirthdaySortModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const 0090 { 0091 const QModelIndex nameIndex = sourceModel()->index(sourceRow, 0, sourceParent); 0092 const QModelIndex birthdayIndex = sourceModel()->index(sourceRow, 1, sourceParent); 0093 0094 // If the birthday is invalid, the second column is empty 0095 if (birthdayIndex.data(Qt::DisplayRole).toString().isEmpty()) 0096 return false; 0097 0098 const QString text = mPrefix + nameIndex.data(Qt::DisplayRole).toString() + mSuffix; 0099 if (mContactsWithAlarm.contains(text)) 0100 return false; 0101 0102 return true; 0103 } 0104 0105 #include "moc_birthdaymodel.cpp" 0106 0107 // vim: et sw=4: