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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
  • contact
contactstreemodel.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
5  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
6 
7  This library is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Library General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but WITHOUT
13  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15  License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to the
19  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  02110-1301, USA.
21 */
22 
23 #include "contactstreemodel.h"
24 
25 #include <kabc/addressee.h>
26 #include <kabc/contactgroup.h>
27 #include <kglobal.h>
28 #include <kicon.h>
29 #include <kiconloader.h>
30 #include <klocale.h>
31 #include <klocalizedstring.h>
32 
33 using namespace Akonadi;
34 
35 class ContactsTreeModel::Private
36 {
37  public:
38  Private()
39  : mColumns( ContactsTreeModel::Columns() << ContactsTreeModel::FullName ),
40  mIconSize( KIconLoader::global()->currentSize( KIconLoader::Small ) )
41  {
42  }
43 
44  Columns mColumns;
45  const int mIconSize;
46 };
47 
48 ContactsTreeModel::ContactsTreeModel( ChangeRecorder *monitor, QObject *parent )
49  : EntityTreeModel( monitor, parent ), d( new Private )
50 {
51 }
52 
53 ContactsTreeModel::~ContactsTreeModel()
54 {
55  delete d;
56 }
57 
58 void ContactsTreeModel::setColumns( const Columns &columns )
59 {
60  emit beginResetModel();
61  d->mColumns = columns;
62  emit endResetModel();
63 }
64 
65 ContactsTreeModel::Columns ContactsTreeModel::columns() const
66 {
67  return d->mColumns;
68 }
69 
70 QVariant ContactsTreeModel::entityData( const Item &item, int column, int role ) const
71 {
72  if ( item.mimeType() == KABC::Addressee::mimeType() ) {
73  if ( !item.hasPayload<KABC::Addressee>() ) {
74 
75  // Pass modeltest
76  if ( role == Qt::DisplayRole ) {
77  return item.remoteId();
78  }
79 
80  return QVariant();
81  }
82 
83  const KABC::Addressee contact = item.payload<KABC::Addressee>();
84 
85  if ( role == Qt::DecorationRole ) {
86  if ( column == 0 ) {
87  const KABC::Picture picture = contact.photo();
88  if ( picture.isIntern() ) {
89  return picture.data().scaled( QSize( d->mIconSize, d->mIconSize ), Qt::KeepAspectRatio );
90  } else {
91  return KIcon( QLatin1String( "user-identity" ) );
92  }
93  }
94  return QVariant();
95  } else if ( ( role == Qt::DisplayRole ) || ( role == Qt::EditRole ) ) {
96  switch ( d->mColumns.at( column ) ) {
97  case FullName:
98  if ( contact.realName().isEmpty() ) {
99  if ( contact.preferredEmail().isEmpty() ) {
100  return contact.familyName();
101  }
102  return contact.preferredEmail();
103  }
104  return contact.realName();
105  case FamilyName:
106  return contact.familyName();
107  case GivenName:
108  return contact.givenName();
109  case Birthday:
110  if ( contact.birthday().date().isValid() ) {
111  return KGlobal::locale()->formatDate( contact.birthday().date(), KLocale::ShortDate );
112  }
113  break;
114  case HomeAddress:
115  {
116  const KABC::Address address = contact.address( KABC::Address::Home );
117  if ( !address.isEmpty() ) {
118  return address.formattedAddress();
119  }
120  }
121  break;
122  case BusinessAddress:
123  {
124  const KABC::Address address = contact.address( KABC::Address::Work );
125  if ( !address.isEmpty() ) {
126  return address.formattedAddress();
127  }
128  }
129  break;
130  case PhoneNumbers:
131  {
132  QStringList values;
133 
134  const KABC::PhoneNumber::List numbers = contact.phoneNumbers();
135  foreach ( const KABC::PhoneNumber &number, numbers ) {
136  values += number.number();
137  }
138 
139  return values.join( QLatin1String( "\n" ) );
140  }
141  break;
142  case PreferredEmail:
143  return contact.preferredEmail();
144  case AllEmails:
145  return contact.emails().join( QLatin1String( "\n" ) );
146  case Organization:
147  return contact.organization();
148  case Role:
149  return contact.role();
150  case Homepage:
151  return contact.url().url();
152  case Note:
153  return contact.note();
154  }
155  } else if ( role == DateRole ) {
156  if ( d->mColumns.at( column ) == Birthday ) {
157  return contact.birthday();
158  } else {
159  return QDate();
160  }
161  }
162  } else if ( item.mimeType() == KABC::ContactGroup::mimeType() ) {
163  if ( !item.hasPayload<KABC::ContactGroup>() ) {
164 
165  // Pass modeltest
166  if ( role == Qt::DisplayRole ) {
167  return item.remoteId();
168  }
169 
170  return QVariant();
171  }
172 
173  if ( role == Qt::DecorationRole ) {
174  if ( column == 0 ) {
175  return KIcon( QLatin1String( "x-mail-distribution-list" ) );
176  } else {
177  return QVariant();
178  }
179  } else if ( ( role == Qt::DisplayRole ) || ( role == Qt::EditRole ) ) {
180  switch ( d->mColumns.at( column ) ) {
181  case FullName:
182  {
183  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
184  return group.name();
185  }
186  break;
187  default:
188  return QVariant();
189  break;
190  }
191  }
192  }
193 
194  return EntityTreeModel::entityData( item, column, role );
195 }
196 
197 QVariant ContactsTreeModel::entityData( const Collection &collection, int column, int role ) const
198 {
199  if ( role == Qt::DisplayRole ) {
200  switch ( column ) {
201  case 0:
202  return EntityTreeModel::entityData( collection, column, role );
203  default:
204  return QString(); // pass model test
205  }
206  }
207 
208  return EntityTreeModel::entityData( collection, column, role );
209 }
210 
211 int ContactsTreeModel::entityColumnCount( HeaderGroup headerGroup ) const
212 {
213  if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
214  return 1;
215  } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
216  return d->mColumns.count();
217  } else {
218  return EntityTreeModel::entityColumnCount( headerGroup );
219  }
220 }
221 
222 QVariant ContactsTreeModel::entityHeaderData( int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup ) const
223 {
224  if ( role == Qt::DisplayRole ) {
225  if ( orientation == Qt::Horizontal ) {
226  if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
227 
228  if ( section >= 1 ) {
229  return QVariant();
230  }
231 
232  switch ( section ) {
233  case 0:
234  return i18nc( "@title:column address books overview", "Address Books" );
235  break;
236  }
237  } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
238  if ( section < 0 || section >= d->mColumns.count() ) {
239  return QVariant();
240  }
241 
242  switch ( d->mColumns.at( section ) ) {
243  case FullName:
244  return i18nc( "@title:column name of a person", "Name" );
245  case FamilyName:
246  return i18nc( "@title:column family name of a person", "Family Name" );
247  case GivenName:
248  return i18nc( "@title:column given name of a person", "Given Name" );
249  case Birthday:
250  return KABC::Addressee::birthdayLabel();
251  case HomeAddress:
252  return i18nc( "@title:column home address of a person", "Home" );
253  case BusinessAddress:
254  return i18nc( "@title:column work address of a person", "Work" );
255  case PhoneNumbers:
256  return i18nc( "@title:column phone numbers of a person", "Phone Numbers" );
257  case PreferredEmail:
258  return i18nc( "@title:column the preferred email addresses of a person", "Preferred EMail" );
259  case AllEmails:
260  return i18nc( "@title:column all email addresses of a person", "All EMails" );
261  case Organization:
262  return KABC::Addressee::organizationLabel();
263  case Role:
264  return KABC::Addressee::roleLabel();
265  case Homepage:
266  return KABC::Addressee::urlLabel();
267  case Note:
268  return KABC::Addressee::noteLabel();
269  }
270  }
271  }
272  }
273 
274  return EntityTreeModel::entityHeaderData( section, orientation, role, headerGroup );
275 }
276 
Akonadi::ContactsTreeModel::Columns
QList< Column > Columns
Describes a list of columns of the contacts tree model.
Definition: contactstreemodel.h:156
Akonadi::EntityTreeModel::entityData
virtual QVariant entityData(const Item &item, int column, int role=Qt::DisplayRole) const
Provided for convenience of subclasses.
Definition: entitytreemodel.cpp:136
Akonadi::ContactsTreeModel::Note
Shows the note.
Definition: contactstreemodel.h:150
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::ContactsTreeModel::GivenName
Shows the given name.
Definition: contactstreemodel.h:100
Akonadi::ContactsTreeModel::HomeAddress
Shows the formatted home address.
Definition: contactstreemodel.h:110
Akonadi::ContactsTreeModel::PreferredEmail
Shows the preferred email address.
Definition: contactstreemodel.h:125
Akonadi::EntityTreeModel::entityHeaderData
virtual QVariant entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
Reimplement this to provide different header data.
Definition: entitytreemodel.cpp:695
Akonadi::ContactsTreeModel::AllEmails
Shows all email address.
Definition: contactstreemodel.h:130
Akonadi::ContactsTreeModel::DateRole
The QDate object for the current index.
Definition: contactstreemodel.h:162
Akonadi::EntityTreeModel::CollectionTreeHeaders
Header information for a collection-only tree.
Definition: entitytreemodel.h:383
Akonadi::ContactsTreeModel::ContactsTreeModel
ContactsTreeModel(ChangeRecorder *monitor, QObject *parent=0)
Creates a new contacts tree model.
Definition: contactstreemodel.cpp:48
Akonadi::ContactsTreeModel::FullName
Shows the formatted name or, if empty, the assembled name.
Definition: contactstreemodel.h:90
Akonadi::ContactsTreeModel::setColumns
void setColumns(const Columns &columns)
Sets the columns that the model should show.
Definition: contactstreemodel.cpp:58
Akonadi::ContactsTreeModel::FamilyName
Shows the family name.
Definition: contactstreemodel.h:95
Akonadi::EntityTreeModel::ItemListHeaders
Header information for a list of items.
Definition: entitytreemodel.h:384
Akonadi::ContactsTreeModel
A model for contacts and contact groups as available in Akonadi.
Definition: contactstreemodel.h:78
Akonadi::ContactsTreeModel::Organization
Shows organization name.
Definition: contactstreemodel.h:135
Akonadi::EntityTreeModel
A model for collections and items together.
Definition: entitytreemodel.h:317
Akonadi::ContactsTreeModel::columns
Columns columns() const
Returns the columns that the model currently shows.
Definition: contactstreemodel.cpp:65
Akonadi::ContactsTreeModel::Role
Shows the role of a contact.
Definition: contactstreemodel.h:140
Akonadi::ContactsTreeModel::BusinessAddress
Shows the formatted business address.
Definition: contactstreemodel.h:115
Akonadi::ContactsTreeModel::Birthday
Shows the birthday.
Definition: contactstreemodel.h:105
Akonadi::ContactsTreeModel::PhoneNumbers
Shows the phone numbers.
Definition: contactstreemodel.h:120
Akonadi::ContactsTreeModel::~ContactsTreeModel
virtual ~ContactsTreeModel()
Destroys the contacts tree model.
Definition: contactstreemodel.cpp:53
Akonadi::ContactsTreeModel::Homepage
Shows homepage url.
Definition: contactstreemodel.h:145
Akonadi::ChangeRecorder
Records and replays change notification.
Definition: changerecorder.h:47
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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