• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.1 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  return QVariant();
79  }
80 
81  const KABC::Addressee contact = item.payload<KABC::Addressee>();
82 
83  if ( role == Qt::DecorationRole ) {
84  if ( column == 0 ) {
85  const KABC::Picture picture = contact.photo();
86  if ( picture.isIntern() ) {
87  return picture.data().scaled( QSize( d->mIconSize, d->mIconSize ), Qt::KeepAspectRatio );
88  } else {
89  return KIcon( QLatin1String( "user-identity" ) );
90  }
91  }
92  return QVariant();
93  } else if ( (role == Qt::DisplayRole) || (role == Qt::EditRole) ) {
94  switch ( d->mColumns.at( column ) ) {
95  case FullName:
96  return contact.realName();
97  break;
98  case FamilyName:
99  return contact.familyName();
100  break;
101  case GivenName:
102  return contact.givenName();
103  break;
104  case Birthday:
105  if ( contact.birthday().date().isValid() )
106  return KGlobal::locale()->formatDate( contact.birthday().date(), KLocale::ShortDate );
107  break;
108  case HomeAddress:
109  {
110  const KABC::Address address = contact.address( KABC::Address::Home );
111  if ( !address.isEmpty() )
112  return address.formattedAddress();
113  }
114  break;
115  case BusinessAddress:
116  {
117  const KABC::Address address = contact.address( KABC::Address::Work );
118  if ( !address.isEmpty() )
119  return address.formattedAddress();
120  }
121  break;
122  case PhoneNumbers:
123  {
124  QStringList values;
125 
126  const KABC::PhoneNumber::List numbers = contact.phoneNumbers();
127  foreach ( const KABC::PhoneNumber &number, numbers )
128  values += number.number();
129 
130  return values.join( QLatin1String( "\n" ) );
131  }
132  break;
133  case PreferredEmail:
134  return contact.preferredEmail();
135  break;
136  case AllEmails:
137  return contact.emails().join( QLatin1String( "\n" ) );
138  break;
139  case Organization:
140  return contact.organization();
141  break;
142  case Role:
143  return contact.role();
144  break;
145  case Homepage:
146  return contact.url().url();
147  break;
148  case Note:
149  return contact.note();
150  break;
151  }
152  } else if ( role == DateRole ) {
153  if ( d->mColumns.at( column ) == Birthday )
154  return contact.birthday();
155  else
156  return QDate();
157  }
158  } else if ( item.mimeType() == KABC::ContactGroup::mimeType() ) {
159  if ( !item.hasPayload<KABC::ContactGroup>() ) {
160 
161  // Pass modeltest
162  if ( role == Qt::DisplayRole )
163  return item.remoteId();
164 
165  return QVariant();
166  }
167 
168  if ( role == Qt::DecorationRole ) {
169  if ( column == 0 )
170  return KIcon( QLatin1String( "x-mail-distribution-list" ) );
171  else
172  return QVariant();
173  } else if ( (role == Qt::DisplayRole) || (role == Qt::EditRole) ) {
174  switch ( d->mColumns.at( column ) ) {
175  case FullName:
176  {
177  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
178  return group.name();
179  }
180  break;
181  default:
182  return QVariant();
183  break;
184  }
185  }
186  }
187 
188  return EntityTreeModel::entityData( item, column, role );
189 }
190 
191 QVariant ContactsTreeModel::entityData( const Collection &collection, int column, int role ) const
192 {
193  if ( role == Qt::DisplayRole ) {
194  switch ( column ) {
195  case 0:
196  return EntityTreeModel::entityData( collection, column, role );
197  default:
198  return QString(); // pass model test
199  }
200  }
201 
202  return EntityTreeModel::entityData( collection, column, role );
203 }
204 
205 int ContactsTreeModel::entityColumnCount( HeaderGroup headerGroup ) const
206 {
207  if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
208  return 1;
209  } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
210  return d->mColumns.count();
211  } else {
212  return EntityTreeModel::entityColumnCount( headerGroup );
213  }
214 }
215 
216 QVariant ContactsTreeModel::entityHeaderData( int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup ) const
217 {
218  if ( role == Qt::DisplayRole ) {
219  if ( orientation == Qt::Horizontal ) {
220  if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) {
221 
222  if ( section >= 1 )
223  return QVariant();
224 
225  switch ( section ) {
226  case 0:
227  return i18nc( "@title:column address books overview", "Address Books" );
228  break;
229  }
230  } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
231  if ( section < 0 || section >= d->mColumns.count() )
232  return QVariant();
233 
234  switch ( d->mColumns.at( section ) ) {
235  case FullName:
236  return i18nc( "@title:column name of a person", "Name" );
237  break;
238  case FamilyName:
239  return i18nc( "@title:column family name of a person", "Family Name" );
240  break;
241  case GivenName:
242  return i18nc( "@title:column given name of a person", "Given Name" );
243  break;
244  case Birthday:
245  return KABC::Addressee::birthdayLabel();
246  break;
247  case HomeAddress:
248  return i18nc( "@title:column home address of a person", "Home" );
249  break;
250  case BusinessAddress:
251  return i18nc( "@title:column work address of a person", "Work" );
252  break;
253  case PhoneNumbers:
254  return i18nc( "@title:column phone numbers of a person", "Phone Numbers" );
255  break;
256  case PreferredEmail:
257  return i18nc( "@title:column the preferred email addresses of a person", "Preferred EMail" );
258  break;
259  case AllEmails:
260  return i18nc( "@title:column all email addresses of a person", "All EMails" );
261  break;
262  case Organization:
263  return KABC::Addressee::organizationLabel();
264  break;
265  case Role:
266  return KABC::Addressee::roleLabel();
267  break;
268  case Homepage:
269  return KABC::Addressee::urlLabel();
270  break;
271  case Note:
272  return KABC::Addressee::noteLabel();
273  break;
274  }
275  }
276  }
277  }
278 
279  return EntityTreeModel::entityHeaderData( section, orientation, role, headerGroup );
280 }
281 
282 #include "contactstreemodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Sep 24 2012 09:08:06 by doxygen 1.8.1.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.9.1 API Reference

Skip menu "kdepimlibs-4.9.1 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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