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

kalarm

birthdaymodel.cpp

Go to the documentation of this file.
00001 /*
00002  *  birthdaymodel.cpp  -  model class for birthdays from address book
00003  *  Program:  kalarm
00004  *  Copyright © 2007,2008 by David Jarvie <djarvie@kde.org>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kalarm.h"
00022 
00023 #include <klocale.h>
00024 #include <kabc/addressbook.h>
00025 #include <kabc/stdaddressbook.h>
00026 #include <kdebug.h>
00027 
00028 #include <kcal/event.h>
00029 
00030 #include "alarmcalendar.h"
00031 #include "alarmevent.h"
00032 #include "birthdaymodel.moc"
00033 
00034 
00035 BirthdayModel*           BirthdayModel::mInstance = 0;
00036 const KABC::AddressBook* BirthdayModel::mAddressBook = 0;
00037 
00038 
00039 BirthdayModel* BirthdayModel::instance(QObject* parent)
00040 {
00041     if (!mInstance)
00042         mInstance = new BirthdayModel(parent);
00043     return mInstance;
00044 }
00045 
00046 void BirthdayModel::close()
00047 {
00048     if (mAddressBook)
00049     {
00050         KABC::StdAddressBook::close();
00051         mAddressBook = 0;
00052     }
00053     if (mInstance)
00054     {
00055         delete mInstance;
00056         mInstance = 0;
00057     }
00058 }
00059 
00060 BirthdayModel::BirthdayModel(QObject* parent)
00061     : QAbstractTableModel(parent)
00062 {
00063 }
00064 
00065 BirthdayModel::~BirthdayModel()
00066 {
00067     int count = mData.count();
00068     if (count)
00069     {
00070         beginRemoveRows(QModelIndex(), 0, count - 1);
00071         for (int row = 0;  row < count;  ++row)
00072         {
00073             delete mData[row];
00074             mData.removeAt(row);     // alarm exists, so remove from selection list
00075         }
00076         endRemoveRows();
00077     }
00078     if (this == mInstance)
00079         mInstance = 0;
00080 }
00081 
00082 void BirthdayModel::setPrefixSuffix(const QString& prefix, const QString& suffix)
00083 {
00084     if (prefix != mPrefix  ||  suffix != mSuffix)
00085     {
00086         // Text has changed - re-evaluate the selection list
00087         mPrefix = prefix;
00088         mSuffix = suffix;
00089         loadAddressBook();
00090     }
00091 }
00092 
00093 int BirthdayModel::rowCount(const QModelIndex& parent) const
00094 {
00095     if (parent.isValid())
00096         return 0;
00097     return mData.count();
00098 }
00099 
00100 int BirthdayModel::columnCount(const QModelIndex& parent) const
00101 {
00102     if (parent.isValid())
00103         return 0;
00104     return ColumnCount;
00105 }
00106 
00107 QModelIndex BirthdayModel::index(int row, int column, const QModelIndex& parent) const
00108 {
00109     if (parent.isValid()  ||  row >= mData.count())
00110         return QModelIndex();
00111     return createIndex(row, column, mData[row]);
00112 }
00113 
00114 QVariant BirthdayModel::data(const QModelIndex& index, int role) const
00115 {
00116     int column = index.column();
00117     Data* data = static_cast<Data*>(index.internalPointer());
00118     if (!data)
00119         return QVariant();
00120     if (role == Qt::DisplayRole)
00121     {
00122         switch (column)
00123         {
00124             case NameColumn:
00125                 return data->name;
00126             case DateColumn:
00127                 return data->birthday;
00128             default:
00129                 break;
00130         }
00131     }
00132     return QVariant();
00133 }
00134 
00135 QVariant BirthdayModel::headerData(int section, Qt::Orientation orientation, int role) const
00136 {
00137     if (orientation == Qt::Horizontal)
00138     {
00139         if (role == Qt::DisplayRole)
00140         {
00141             switch (section)
00142             {
00143                 case NameColumn:
00144                     return i18nc("@title:column Name of person", "Name");
00145                 case DateColumn:
00146                     return i18nc("@title:column", "Birthday");
00147             }
00148         }
00149     }
00150     return QVariant();
00151 }
00152 
00153 /******************************************************************************
00154 * Return the event referred to by an index.
00155 */
00156 BirthdayModel::Data* BirthdayModel::rowData(const QModelIndex& index) const
00157 {
00158     if (!index.isValid())
00159         return 0;
00160     return static_cast<Data*>(index.internalPointer());
00161 }
00162 
00163 /******************************************************************************
00164 * Load the address book in preparation for displaying the birthday selection list.
00165 */
00166 void BirthdayModel::loadAddressBook()
00167 {
00168     if (!mAddressBook)
00169     {
00170         mAddressBook = KABC::StdAddressBook::self(true);
00171         if (mAddressBook)
00172             connect(mAddressBook, SIGNAL(addressBookChanged(AddressBook*)), SLOT(refresh()));
00173     }
00174     else
00175         refresh();
00176     if (!mAddressBook)
00177         emit addrBookError();
00178 }
00179 
00180 /******************************************************************************
00181 * Initialise or update the birthday selection list by fetching all birthdays
00182 * from the address book and displaying those which do not already have alarms.
00183 */
00184 void BirthdayModel::refresh()
00185 {
00186     // Compile a list of all pending alarm messages which look like birthdays
00187     QStringList messageList;
00188     KAEvent event;
00189     KAEvent::List events = AlarmCalendar::resources()->events(KCalEvent::ACTIVE);
00190     for (int i = 0, end = events.count();  i < end;  ++i)
00191     {
00192         KAEvent* event = events[i];
00193         if (event->action() == KAEvent::MESSAGE
00194         &&  event->recurType() == KARecurrence::ANNUAL_DATE
00195         &&  (mPrefix.isEmpty()  ||  event->message().startsWith(mPrefix)))
00196             messageList.append(event->message());
00197     }
00198 
00199     // Fetch all birthdays from the address book
00200     QList<Data*> newEntries;
00201     for (KABC::AddressBook::ConstIterator abit = mAddressBook->begin();  abit != mAddressBook->end();  ++abit)
00202     {
00203         const KABC::Addressee& addressee = *abit;
00204         if (addressee.birthday().isValid())
00205         {
00206             // Create a list entry for this birthday
00207             QDate birthday = addressee.birthday().date();
00208             QString name = addressee.nickName();
00209             if (name.isEmpty())
00210                 name = addressee.realName();
00211             // Check if the birthday already has an alarm
00212             QString text = mPrefix + name + mSuffix;
00213             bool alarmExists = messageList.contains(text);
00214             // Check if the birthday is already in the selection list
00215             int row = -1;
00216             int count = mData.count();
00217             while (++row < count
00218                &&  (name != mData[row]->name  ||  birthday != mData[row]->birthday)) ;
00219             if (alarmExists  &&  row < count)
00220             {
00221                 beginRemoveRows(QModelIndex(), row, row);
00222                 delete mData[row];
00223                 mData.removeAt(row);     // alarm exists, so remove from selection list
00224                 endRemoveRows();
00225             }
00226             else if (!alarmExists  &&  row >= count)
00227                 newEntries += new Data(name, birthday);
00228         }
00229     }
00230     if (!newEntries.isEmpty())
00231     {
00232         int row = mData.count();
00233         beginInsertRows(QModelIndex(), row, row + newEntries.count() - 1);
00234         mData += newEntries;
00235         endInsertRows();
00236     }
00237 }
00238 
00239 
00240 BirthdaySortModel::BirthdaySortModel(QAbstractItemModel* baseModel, QObject* parent)
00241     : QSortFilterProxyModel(parent)
00242 {
00243     setSourceModel(baseModel);
00244 }
00245 
00246 /******************************************************************************
00247 * Return the event referred to by an index.
00248 */
00249 BirthdayModel::Data* BirthdaySortModel::rowData(const QModelIndex& index) const
00250 {
00251     return static_cast<BirthdayModel*>(sourceModel())->rowData(mapToSource(index));
00252 }

kalarm

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

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal