23 #include "contactstreemodel.h"
25 #include <kabc/addressee.h>
26 #include <kabc/contactgroup.h>
29 #include <kiconloader.h>
31 #include <klocalizedstring.h>
33 using namespace Akonadi;
35 class ContactsTreeModel::Private
40 mIconSize( KIconLoader::global()->currentSize( KIconLoader::Small ) )
49 : EntityTreeModel( monitor, parent ), d( new Private )
60 emit beginResetModel();
70 QVariant ContactsTreeModel::entityData(
const Item &item,
int column,
int role )
const
72 if ( item.mimeType() == KABC::Addressee::mimeType() ) {
73 if ( !item.hasPayload<KABC::Addressee>() ) {
76 if ( role == Qt::DisplayRole ) {
77 return item.remoteId();
83 const KABC::Addressee contact = item.payload<KABC::Addressee>();
85 if ( role == Qt::DecorationRole ) {
87 const KABC::Picture picture = contact.photo();
88 if ( picture.isIntern() ) {
89 return picture.data().scaled( QSize( d->mIconSize, d->mIconSize ), Qt::KeepAspectRatio );
91 return KIcon( QLatin1String(
"user-identity" ) );
95 }
else if ( ( role == Qt::DisplayRole ) || ( role == Qt::EditRole ) ) {
96 switch ( d->mColumns.at( column ) ) {
98 if( contact.realName().isEmpty() ) {
99 if( contact.preferredEmail().isEmpty() ) {
100 return contact.familyName();
102 return contact.preferredEmail();
104 return contact.realName();
107 return contact.familyName();
110 return contact.givenName();
113 if ( contact.birthday().date().isValid() ) {
114 return KGlobal::locale()->formatDate( contact.birthday().date(), KLocale::ShortDate );
119 const KABC::Address address = contact.address( KABC::Address::Home );
120 if ( !address.isEmpty() ) {
121 return address.formattedAddress();
127 const KABC::Address address = contact.address( KABC::Address::Work );
128 if ( !address.isEmpty() ) {
129 return address.formattedAddress();
137 const KABC::PhoneNumber::List numbers = contact.phoneNumbers();
138 foreach (
const KABC::PhoneNumber &number, numbers ) {
139 values += number.number();
142 return values.join( QLatin1String(
"\n" ) );
146 return contact.preferredEmail();
149 return contact.emails().join( QLatin1String(
"\n" ) );
152 return contact.organization();
155 return contact.role();
158 return contact.url().url();
161 return contact.note();
165 if ( d->mColumns.at( column ) ==
Birthday ) {
166 return contact.birthday();
171 }
else if ( item.mimeType() == KABC::ContactGroup::mimeType() ) {
172 if ( !item.hasPayload<KABC::ContactGroup>() ) {
175 if ( role == Qt::DisplayRole ) {
176 return item.remoteId();
182 if ( role == Qt::DecorationRole ) {
184 return KIcon( QLatin1String(
"x-mail-distribution-list" ) );
188 }
else if ( ( role == Qt::DisplayRole ) || ( role == Qt::EditRole ) ) {
189 switch ( d->mColumns.at( column ) ) {
192 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
203 return EntityTreeModel::entityData( item, column, role );
206 QVariant ContactsTreeModel::entityData(
const Collection &collection,
int column,
int role )
const
208 if ( role == Qt::DisplayRole ) {
211 return EntityTreeModel::entityData( collection, column, role );
217 return EntityTreeModel::entityData( collection, column, role );
220 int ContactsTreeModel::entityColumnCount( HeaderGroup headerGroup )
const
222 if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
224 }
else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
225 return d->mColumns.count();
227 return EntityTreeModel::entityColumnCount( headerGroup );
231 QVariant ContactsTreeModel::entityHeaderData(
int section, Qt::Orientation orientation,
int role, HeaderGroup headerGroup )
const
233 if ( role == Qt::DisplayRole ) {
234 if ( orientation == Qt::Horizontal ) {
235 if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
237 if ( section >= 1 ) {
243 return i18nc(
"@title:column address books overview",
"Address Books" );
246 }
else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
247 if ( section < 0 || section >= d->mColumns.count() ) {
251 switch ( d->mColumns.at( section ) ) {
253 return i18nc(
"@title:column name of a person",
"Name" );
256 return i18nc(
"@title:column family name of a person",
"Family Name" );
259 return i18nc(
"@title:column given name of a person",
"Given Name" );
262 return KABC::Addressee::birthdayLabel();
265 return i18nc(
"@title:column home address of a person",
"Home" );
268 return i18nc(
"@title:column work address of a person",
"Work" );
271 return i18nc(
"@title:column phone numbers of a person",
"Phone Numbers" );
274 return i18nc(
"@title:column the preferred email addresses of a person",
"Preferred EMail" );
277 return i18nc(
"@title:column all email addresses of a person",
"All EMails" );
280 return KABC::Addressee::organizationLabel();
283 return KABC::Addressee::roleLabel();
286 return KABC::Addressee::urlLabel();
289 return KABC::Addressee::noteLabel();
296 return EntityTreeModel::entityHeaderData( section, orientation, role, headerGroup );