kalarm
birthdaymodel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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);
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
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
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
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
00182
00183
00184 void BirthdayModel::refresh()
00185 {
00186
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
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
00207 QDate birthday = addressee.birthday().date();
00208 QString name = addressee.nickName();
00209 if (name.isEmpty())
00210 name = addressee.realName();
00211
00212 QString text = mPrefix + name + mSuffix;
00213 bool alarmExists = messageList.contains(text);
00214
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);
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
00248
00249 BirthdayModel::Data* BirthdaySortModel::rowData(const QModelIndex& index) const
00250 {
00251 return static_cast<BirthdayModel*>(sourceModel())->rowData(mapToSource(index));
00252 }