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

akonadi/contact

  • 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  break;
106  case FamilyName:
107  return contact.familyName();
108  break;
109  case GivenName:
110  return contact.givenName();
111  break;
112  case Birthday:
113  if ( contact.birthday().date().isValid() ) {
114  return KGlobal::locale()->formatDate( contact.birthday().date(), KLocale::ShortDate );
115  }
116  break;
117  case HomeAddress:
118  {
119  const KABC::Address address = contact.address( KABC::Address::Home );
120  if ( !address.isEmpty() ) {
121  return address.formattedAddress();
122  }
123  }
124  break;
125  case BusinessAddress:
126  {
127  const KABC::Address address = contact.address( KABC::Address::Work );
128  if ( !address.isEmpty() ) {
129  return address.formattedAddress();
130  }
131  }
132  break;
133  case PhoneNumbers:
134  {
135  QStringList values;
136 
137  const KABC::PhoneNumber::List numbers = contact.phoneNumbers();
138  foreach ( const KABC::PhoneNumber &number, numbers ) {
139  values += number.number();
140  }
141 
142  return values.join( QLatin1String( "\n" ) );
143  }
144  break;
145  case PreferredEmail:
146  return contact.preferredEmail();
147  break;
148  case AllEmails:
149  return contact.emails().join( QLatin1String( "\n" ) );
150  break;
151  case Organization:
152  return contact.organization();
153  break;
154  case Role:
155  return contact.role();
156  break;
157  case Homepage:
158  return contact.url().url();
159  break;
160  case Note:
161  return contact.note();
162  break;
163  }
164  } else if ( role == DateRole ) {
165  if ( d->mColumns.at( column ) == Birthday ) {
166  return contact.birthday();
167  } else {
168  return QDate();
169  }
170  }
171  } else if ( item.mimeType() == KABC::ContactGroup::mimeType() ) {
172  if ( !item.hasPayload<KABC::ContactGroup>() ) {
173 
174  // Pass modeltest
175  if ( role == Qt::DisplayRole ) {
176  return item.remoteId();
177  }
178 
179  return QVariant();
180  }
181 
182  if ( role == Qt::DecorationRole ) {
183  if ( column == 0 ) {
184  return KIcon( QLatin1String( "x-mail-distribution-list" ) );
185  } else {
186  return QVariant();
187  }
188  } else if ( ( role == Qt::DisplayRole ) || ( role == Qt::EditRole ) ) {
189  switch ( d->mColumns.at( column ) ) {
190  case FullName:
191  {
192  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
193  return group.name();
194  }
195  break;
196  default:
197  return QVariant();
198  break;
199  }
200  }
201  }
202 
203  return EntityTreeModel::entityData( item, column, role );
204 }
205 
206 QVariant ContactsTreeModel::entityData( const Collection &collection, int column, int role ) const
207 {
208  if ( role == Qt::DisplayRole ) {
209  switch ( column ) {
210  case 0:
211  return EntityTreeModel::entityData( collection, column, role );
212  default:
213  return QString(); // pass model test
214  }
215  }
216 
217  return EntityTreeModel::entityData( collection, column, role );
218 }
219 
220 int ContactsTreeModel::entityColumnCount( HeaderGroup headerGroup ) const
221 {
222  if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
223  return 1;
224  } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
225  return d->mColumns.count();
226  } else {
227  return EntityTreeModel::entityColumnCount( headerGroup );
228  }
229 }
230 
231 QVariant ContactsTreeModel::entityHeaderData( int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup ) const
232 {
233  if ( role == Qt::DisplayRole ) {
234  if ( orientation == Qt::Horizontal ) {
235  if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
236 
237  if ( section >= 1 ) {
238  return QVariant();
239  }
240 
241  switch ( section ) {
242  case 0:
243  return i18nc( "@title:column address books overview", "Address Books" );
244  break;
245  }
246  } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
247  if ( section < 0 || section >= d->mColumns.count() ) {
248  return QVariant();
249  }
250 
251  switch ( d->mColumns.at( section ) ) {
252  case FullName:
253  return i18nc( "@title:column name of a person", "Name" );
254  break;
255  case FamilyName:
256  return i18nc( "@title:column family name of a person", "Family Name" );
257  break;
258  case GivenName:
259  return i18nc( "@title:column given name of a person", "Given Name" );
260  break;
261  case Birthday:
262  return KABC::Addressee::birthdayLabel();
263  break;
264  case HomeAddress:
265  return i18nc( "@title:column home address of a person", "Home" );
266  break;
267  case BusinessAddress:
268  return i18nc( "@title:column work address of a person", "Work" );
269  break;
270  case PhoneNumbers:
271  return i18nc( "@title:column phone numbers of a person", "Phone Numbers" );
272  break;
273  case PreferredEmail:
274  return i18nc( "@title:column the preferred email addresses of a person", "Preferred EMail" );
275  break;
276  case AllEmails:
277  return i18nc( "@title:column all email addresses of a person", "All EMails" );
278  break;
279  case Organization:
280  return KABC::Addressee::organizationLabel();
281  break;
282  case Role:
283  return KABC::Addressee::roleLabel();
284  break;
285  case Homepage:
286  return KABC::Addressee::urlLabel();
287  break;
288  case Note:
289  return KABC::Addressee::noteLabel();
290  break;
291  }
292  }
293  }
294  }
295 
296  return EntityTreeModel::entityHeaderData( section, orientation, role, headerGroup );
297 }
298 
Akonadi::ContactsTreeModel::Columns
QList< Column > Columns
Describes a list of columns of the contacts tree model.
Definition: contactstreemodel.h:156
Akonadi::ContactsTreeModel::Note
Shows the note.
Definition: contactstreemodel.h:150
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::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::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::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::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
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:47 by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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