23 #include "contactstreemodel.h"
25 #include <kabc/addressee.h>
26 #include <kabc/contactgroup.h>
29 #include <kiconloader.h>
32 using namespace Akonadi;
34 class ContactsTreeModel::Private
39 mIconSize( KIconLoader::global()->currentSize( KIconLoader::Small ) )
48 : EntityTreeModel( monitor, parent ), d( new Private )
59 emit beginResetModel();
69 QVariant ContactsTreeModel::entityData(
const Item &item,
int column,
int role )
const
71 if ( item.mimeType() == KABC::Addressee::mimeType() ) {
72 if ( !item.hasPayload<KABC::Addressee>() ) {
75 if ( role == Qt::DisplayRole ) {
76 return item.remoteId();
82 const KABC::Addressee contact = item.payload<KABC::Addressee>();
84 if ( role == Qt::DecorationRole ) {
86 const KABC::Picture picture = contact.photo();
87 if ( picture.isIntern() ) {
88 return picture.data().scaled( QSize( d->mIconSize, d->mIconSize ), Qt::KeepAspectRatio );
90 return KIcon( QLatin1String(
"user-identity" ) );
94 }
else if ( ( role == Qt::DisplayRole ) || ( role == Qt::EditRole ) ) {
95 switch ( d->mColumns.at( column ) ) {
97 if( contact.realName().isEmpty() ) {
98 if( contact.preferredEmail().isEmpty() ) {
99 return contact.familyName();
101 return contact.preferredEmail();
103 return contact.realName();
106 return contact.familyName();
109 return contact.givenName();
112 if ( contact.birthday().date().isValid() ) {
113 return KGlobal::locale()->formatDate( contact.birthday().date(), KLocale::ShortDate );
118 const KABC::Address address = contact.address( KABC::Address::Home );
119 if ( !address.isEmpty() ) {
120 return address.formattedAddress();
126 const KABC::Address address = contact.address( KABC::Address::Work );
127 if ( !address.isEmpty() ) {
128 return address.formattedAddress();
136 const KABC::PhoneNumber::List numbers = contact.phoneNumbers();
137 foreach (
const KABC::PhoneNumber &number, numbers ) {
138 values += number.number();
141 return values.join( QLatin1String(
"\n" ) );
145 return contact.preferredEmail();
148 return contact.emails().join( QLatin1String(
"\n" ) );
151 return contact.organization();
154 return contact.role();
157 return contact.url().url();
160 return contact.note();
164 if ( d->mColumns.at( column ) ==
Birthday ) {
165 return contact.birthday();
170 }
else if ( item.mimeType() == KABC::ContactGroup::mimeType() ) {
171 if ( !item.hasPayload<KABC::ContactGroup>() ) {
174 if ( role == Qt::DisplayRole ) {
175 return item.remoteId();
181 if ( role == Qt::DecorationRole ) {
183 return KIcon( QLatin1String(
"x-mail-distribution-list" ) );
187 }
else if ( ( role == Qt::DisplayRole ) || ( role == Qt::EditRole ) ) {
188 switch ( d->mColumns.at( column ) ) {
191 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
202 return EntityTreeModel::entityData( item, column, role );
205 QVariant ContactsTreeModel::entityData(
const Collection &collection,
int column,
int role )
const
207 if ( role == Qt::DisplayRole ) {
210 return EntityTreeModel::entityData( collection, column, role );
216 return EntityTreeModel::entityData( collection, column, role );
219 int ContactsTreeModel::entityColumnCount( HeaderGroup headerGroup )
const
221 if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
223 }
else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
224 return d->mColumns.count();
226 return EntityTreeModel::entityColumnCount( headerGroup );
230 QVariant ContactsTreeModel::entityHeaderData(
int section, Qt::Orientation orientation,
int role, HeaderGroup headerGroup )
const
232 if ( role == Qt::DisplayRole ) {
233 if ( orientation == Qt::Horizontal ) {
234 if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
236 if ( section >= 1 ) {
242 return i18nc(
"@title:column address books overview",
"Address Books" );
245 }
else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
246 if ( section < 0 || section >= d->mColumns.count() ) {
250 switch ( d->mColumns.at( section ) ) {
252 return i18nc(
"@title:column name of a person",
"Name" );
255 return i18nc(
"@title:column family name of a person",
"Family Name" );
258 return i18nc(
"@title:column given name of a person",
"Given Name" );
261 return KABC::Addressee::birthdayLabel();
264 return i18nc(
"@title:column home address of a person",
"Home" );
267 return i18nc(
"@title:column work address of a person",
"Work" );
270 return i18nc(
"@title:column phone numbers of a person",
"Phone Numbers" );
273 return i18nc(
"@title:column the preferred email addresses of a person",
"Preferred EMail" );
276 return i18nc(
"@title:column all email addresses of a person",
"All EMails" );
279 return KABC::Addressee::organizationLabel();
282 return KABC::Addressee::roleLabel();
285 return KABC::Addressee::urlLabel();
288 return KABC::Addressee::noteLabel();
295 return EntityTreeModel::entityHeaderData( section, orientation, role, headerGroup );