• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm

  • sources
  • kde-4.14
  • kdepim
  • kalarm
birthdaymodel.cpp
Go to the documentation of this file.
1 /*
2  * birthdaymodel.cpp - model class for birthdays from address book
3  * Program: kalarm
4  * Copyright © 2009 by Tobias Koenig <tokoe@kde.org>
5  * Copyright © 2007-2011 by David Jarvie <djarvie@kde.org>
6  *
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.
11  *
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.
16  *
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.
20  */
21 
22 #include "birthdaymodel.h"
23 #include "alarmcalendar.h"
24 
25 #include <kalarmcal/kaevent.h>
26 
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>
32 
33 #include <kglobal.h>
34 #include <klocale.h>
35 
36 using namespace KAlarmCal;
37 
38 
39 BirthdayModel* BirthdayModel::mInstance = 0;
40 
41 BirthdayModel::BirthdayModel(Akonadi::ChangeRecorder* recorder)
42  : Akonadi::ContactsTreeModel(recorder)
43 {
44  setColumns(Columns() << FullName << Birthday);
45 }
46 
47 BirthdayModel::~BirthdayModel()
48 {
49  if (this == mInstance)
50  mInstance = 0;
51 }
52 
53 BirthdayModel* BirthdayModel::instance()
54 {
55  if (!mInstance)
56  {
57  Akonadi::Session* session = new Akonadi::Session("KAlarm::BirthdayModelSession");
58 
59  Akonadi::ItemFetchScope scope;
60  scope.fetchFullPayload(true);
61  scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
62 
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);
69 
70  mInstance = new BirthdayModel(recorder);
71  }
72 
73  return mInstance;
74 }
75 
76 QVariant BirthdayModel::entityData(const Akonadi::Item& item, int column, int role) const
77 {
78  if (columns().at(column) == Birthday && role == Qt::DisplayRole)
79  {
80  QDate date = Akonadi::ContactsTreeModel::entityData(item, column, DateRole).toDate();
81  if (date.isValid())
82  return KGlobal::locale()->formatDate(date, KLocale::ShortDate);
83  }
84  return Akonadi::ContactsTreeModel::entityData(item, column, role);
85 }
86 
87 
88 BirthdaySortModel::BirthdaySortModel(QObject* parent)
89  : QSortFilterProxyModel(parent)
90 {
91 }
92 
93 void BirthdaySortModel::setPrefixSuffix(const QString& prefix, const QString& suffix)
94 {
95  mContactsWithAlarm.clear();
96  mPrefix = prefix;
97  mSuffix = suffix;
98 
99  KAEvent event;
100  const KAEvent::List events = AlarmCalendar::resources()->events(CalEvent::ACTIVE);
101  for (int i = 0, end = events.count(); i < end; ++i)
102  {
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());
108  }
109 
110  invalidateFilter();
111 }
112 
113 bool BirthdaySortModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
114 {
115  const QModelIndex nameIndex = sourceModel()->index(sourceRow, 0, sourceParent);
116  const QModelIndex birthdayIndex = sourceModel()->index(sourceRow, 1, sourceParent);
117 
118  // If the birthday is invalid, the second column is empty
119  if (birthdayIndex.data(Qt::DisplayRole).toString().isEmpty())
120  return false;
121 
122  const QString text = mPrefix + nameIndex.data(Qt::DisplayRole).toString() + mSuffix;
123  if (mContactsWithAlarm.contains(text))
124  return false;
125 
126  return true;
127 }
128 
129 // vim: et sw=4:
QList::clear
void clear()
QModelIndex
QAbstractItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
BirthdaySortModel::setPrefixSuffix
void setPrefixSuffix(const QString &prefix, const QString &suffix)
Definition: birthdaymodel.cpp:93
text
virtual QByteArray text(quint32 serialNumber) const =0
date
time_t date() const
AlarmCalendar::resources
static AlarmCalendar * resources()
Definition: alarmcalendar.h:130
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
alarmcalendar.h
BirthdaySortModel::BirthdaySortModel
BirthdaySortModel(QObject *parent=0)
Definition: birthdaymodel.cpp:88
BirthdayModel
Provides the global model for all contacts.
Definition: birthdaymodel.h:39
BirthdayModel::instance
static BirthdayModel * instance()
Returns the global contact model instance.
Definition: birthdaymodel.cpp:53
QObject::event
virtual bool event(QEvent *e)
QList::append
void append(const T &value)
BirthdaySortModel::filterAcceptsRow
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
Definition: birthdaymodel.cpp:113
QObject
QString::isEmpty
bool isEmpty() const
QDate::isValid
bool isValid() const
QSortFilterProxyModel::invalidateFilter
void invalidateFilter()
QDate
QString
BirthdayModel::entityData
virtual QVariant entityData(const Akonadi::Item &, int column, int role=Qt::DisplayRole) const
Definition: birthdaymodel.cpp:76
QSortFilterProxyModel
QAbstractProxyModel::sourceModel
QAbstractItemModel * sourceModel() const
QModelIndex::data
QVariant data(int role) const
AlarmCalendar::events
KAEvent::List events(CalEvent::Types s=CalEvent::EMPTY) const
Definition: alarmcalendar.h:96
BirthdayModel::~BirthdayModel
~BirthdayModel()
Destroys the global contact model.
Definition: birthdaymodel.cpp:47
birthdaymodel.h
QVariant::toString
QString toString() const
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal