• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 
32 using namespace Akonadi;
33 
34 class ContactsTreeModel::Private
35 {
36  public:
37  Private()
38  : mColumns( ContactsTreeModel::Columns() << ContactsTreeModel::FullName ),
39  mIconSize( KIconLoader::global()->currentSize( KIconLoader::Small ) )
40  {
41  }
42 
43  Columns mColumns;
44  const int mIconSize;
45 };
46 
47 ContactsTreeModel::ContactsTreeModel( ChangeRecorder *monitor, QObject *parent )
48  : EntityTreeModel( monitor, parent ), d( new Private )
49 {
50 }
51 
52 ContactsTreeModel::~ContactsTreeModel()
53 {
54  delete d;
55 }
56 
57 void ContactsTreeModel::setColumns( const Columns &columns )
58 {
59  emit beginResetModel();
60  d->mColumns = columns;
61  emit endResetModel();
62 }
63 
64 ContactsTreeModel::Columns ContactsTreeModel::columns() const
65 {
66  return d->mColumns;
67 }
68 
69 QVariant ContactsTreeModel::entityData( const Item &item, int column, int role ) const
70 {
71  if ( item.mimeType() == KABC::Addressee::mimeType() ) {
72  if ( !item.hasPayload<KABC::Addressee>() ) {
73 
74  // Pass modeltest
75  if ( role == Qt::DisplayRole ) {
76  return item.remoteId();
77  }
78 
79  return QVariant();
80  }
81 
82  const KABC::Addressee contact = item.payload<KABC::Addressee>();
83 
84  if ( role == Qt::DecorationRole ) {
85  if ( column == 0 ) {
86  const KABC::Picture picture = contact.photo();
87  if ( picture.isIntern() ) {
88  return picture.data().scaled( QSize( d->mIconSize, d->mIconSize ), Qt::KeepAspectRatio );
89  } else {
90  return KIcon( QLatin1String( "user-identity" ) );
91  }
92  }
93  return QVariant();
94  } else if ( ( role == Qt::DisplayRole ) || ( role == Qt::EditRole ) ) {
95  switch ( d->mColumns.at( column ) ) {
96  case FullName:
97  if( contact.realName().isEmpty() ) {
98  if( contact.preferredEmail().isEmpty() ) {
99  return contact.familyName();
100  }
101  return contact.preferredEmail();
102  }
103  return contact.realName();
104  break;
105  case FamilyName:
106  return contact.familyName();
107  break;
108  case GivenName:
109  return contact.givenName();
110  break;
111  case Birthday:
112  if ( contact.birthday().date().isValid() ) {
113  return KGlobal::locale()->formatDate( contact.birthday().date(), KLocale::ShortDate );
114  }
115  break;
116  case HomeAddress:
117  {
118  const KABC::Address address = contact.address( KABC::Address::Home );
119  if ( !address.isEmpty() ) {
120  return address.formattedAddress();
121  }
122  }
123  break;
124  case BusinessAddress:
125  {
126  const KABC::Address address = contact.address( KABC::Address::Work );
127  if ( !address.isEmpty() ) {
128  return address.formattedAddress();
129  }
130  }
131  break;
132  case PhoneNumbers:
133  {
134  QStringList values;
135 
136  const KABC::PhoneNumber::List numbers = contact.phoneNumbers();
137  foreach ( const KABC::PhoneNumber &number, numbers ) {
138  values += number.number();
139  }
140 
141  return values.join( QLatin1String( "\n" ) );
142  }
143  break;
144  case PreferredEmail:
145  return contact.preferredEmail();
146  break;
147  case AllEmails:
148  return contact.emails().join( QLatin1String( "\n" ) );
149  break;
150  case Organization:
151  return contact.organization();
152  break;
153  case Role:
154  return contact.role();
155  break;
156  case Homepage:
157  return contact.url().url();
158  break;
159  case Note:
160  return contact.note();
161  break;
162  }
163  } else if ( role == DateRole ) {
164  if ( d->mColumns.at( column ) == Birthday ) {
165  return contact.birthday();
166  } else {
167  return QDate();
168  }
169  }
170  } else if ( item.mimeType() == KABC::ContactGroup::mimeType() ) {
171  if ( !item.hasPayload<KABC::ContactGroup>() ) {
172 
173  // Pass modeltest
174  if ( role == Qt::DisplayRole ) {
175  return item.remoteId();
176  }
177 
178  return QVariant();
179  }
180 
181  if ( role == Qt::DecorationRole ) {
182  if ( column == 0 ) {
183  return KIcon( QLatin1String( "x-mail-distribution-list" ) );
184  } else {
185  return QVariant();
186  }
187  } else if ( ( role == Qt::DisplayRole ) || ( role == Qt::EditRole ) ) {
188  switch ( d->mColumns.at( column ) ) {
189  case FullName:
190  {
191  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
192  return group.name();
193  }
194  break;
195  default:
196  return QVariant();
197  break;
198  }
199  }
200  }
201 
202  return EntityTreeModel::entityData( item, column, role );
203 }
204 
205 QVariant ContactsTreeModel::entityData( const Collection &collection, int column, int role ) const
206 {
207  if ( role == Qt::DisplayRole ) {
208  switch ( column ) {
209  case 0:
210  return EntityTreeModel::entityData( collection, column, role );
211  default:
212  return QString(); // pass model test
213  }
214  }
215 
216  return EntityTreeModel::entityData( collection, column, role );
217 }
218 
219 int ContactsTreeModel::entityColumnCount( HeaderGroup headerGroup ) const
220 {
221  if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
222  return 1;
223  } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
224  return d->mColumns.count();
225  } else {
226  return EntityTreeModel::entityColumnCount( headerGroup );
227  }
228 }
229 
230 QVariant ContactsTreeModel::entityHeaderData( int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup ) const
231 {
232  if ( role == Qt::DisplayRole ) {
233  if ( orientation == Qt::Horizontal ) {
234  if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
235 
236  if ( section >= 1 ) {
237  return QVariant();
238  }
239 
240  switch ( section ) {
241  case 0:
242  return i18nc( "@title:column address books overview", "Address Books" );
243  break;
244  }
245  } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
246  if ( section < 0 || section >= d->mColumns.count() ) {
247  return QVariant();
248  }
249 
250  switch ( d->mColumns.at( section ) ) {
251  case FullName:
252  return i18nc( "@title:column name of a person", "Name" );
253  break;
254  case FamilyName:
255  return i18nc( "@title:column family name of a person", "Family Name" );
256  break;
257  case GivenName:
258  return i18nc( "@title:column given name of a person", "Given Name" );
259  break;
260  case Birthday:
261  return KABC::Addressee::birthdayLabel();
262  break;
263  case HomeAddress:
264  return i18nc( "@title:column home address of a person", "Home" );
265  break;
266  case BusinessAddress:
267  return i18nc( "@title:column work address of a person", "Work" );
268  break;
269  case PhoneNumbers:
270  return i18nc( "@title:column phone numbers of a person", "Phone Numbers" );
271  break;
272  case PreferredEmail:
273  return i18nc( "@title:column the preferred email addresses of a person", "Preferred EMail" );
274  break;
275  case AllEmails:
276  return i18nc( "@title:column all email addresses of a person", "All EMails" );
277  break;
278  case Organization:
279  return KABC::Addressee::organizationLabel();
280  break;
281  case Role:
282  return KABC::Addressee::roleLabel();
283  break;
284  case Homepage:
285  return KABC::Addressee::urlLabel();
286  break;
287  case Note:
288  return KABC::Addressee::noteLabel();
289  break;
290  }
291  }
292  }
293  }
294 
295  return EntityTreeModel::entityHeaderData( section, orientation, role, headerGroup );
296 }
297 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:28:42 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 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