• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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  if( contact.realName().isEmpty() ) {
97  if( contact.preferredEmail().isEmpty() ) {
98  return contact.familyName();
99  }
100  return contact.preferredEmail();
101  }
102  return contact.realName();
103  break;
104  case FamilyName:
105  return contact.familyName();
106  break;
107  case GivenName:
108  return contact.givenName();
109  break;
110  case Birthday:
111  if ( contact.birthday().date().isValid() )
112  return KGlobal::locale()->formatDate( contact.birthday().date(), KLocale::ShortDate );
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  break;
121  case BusinessAddress:
122  {
123  const KABC::Address address = contact.address( KABC::Address::Work );
124  if ( !address.isEmpty() )
125  return address.formattedAddress();
126  }
127  break;
128  case PhoneNumbers:
129  {
130  QStringList values;
131 
132  const KABC::PhoneNumber::List numbers = contact.phoneNumbers();
133  foreach ( const KABC::PhoneNumber &number, numbers )
134  values += number.number();
135 
136  return values.join( QLatin1String( "\n" ) );
137  }
138  break;
139  case PreferredEmail:
140  return contact.preferredEmail();
141  break;
142  case AllEmails:
143  return contact.emails().join( QLatin1String( "\n" ) );
144  break;
145  case Organization:
146  return contact.organization();
147  break;
148  case Role:
149  return contact.role();
150  break;
151  case Homepage:
152  return contact.url().url();
153  break;
154  case Note:
155  return contact.note();
156  break;
157  }
158  } else if ( role == DateRole ) {
159  if ( d->mColumns.at( column ) == Birthday )
160  return contact.birthday();
161  else
162  return QDate();
163  }
164  } else if ( item.mimeType() == KABC::ContactGroup::mimeType() ) {
165  if ( !item.hasPayload<KABC::ContactGroup>() ) {
166 
167  // Pass modeltest
168  if ( role == Qt::DisplayRole )
169  return item.remoteId();
170 
171  return QVariant();
172  }
173 
174  if ( role == Qt::DecorationRole ) {
175  if ( column == 0 )
176  return KIcon( QLatin1String( "x-mail-distribution-list" ) );
177  else
178  return QVariant();
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  switch ( section ) {
232  case 0:
233  return i18nc( "@title:column address books overview", "Address Books" );
234  break;
235  }
236  } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) {
237  if ( section < 0 || section >= d->mColumns.count() )
238  return QVariant();
239 
240  switch ( d->mColumns.at( section ) ) {
241  case FullName:
242  return i18nc( "@title:column name of a person", "Name" );
243  break;
244  case FamilyName:
245  return i18nc( "@title:column family name of a person", "Family Name" );
246  break;
247  case GivenName:
248  return i18nc( "@title:column given name of a person", "Given Name" );
249  break;
250  case Birthday:
251  return KABC::Addressee::birthdayLabel();
252  break;
253  case HomeAddress:
254  return i18nc( "@title:column home address of a person", "Home" );
255  break;
256  case BusinessAddress:
257  return i18nc( "@title:column work address of a person", "Work" );
258  break;
259  case PhoneNumbers:
260  return i18nc( "@title:column phone numbers of a person", "Phone Numbers" );
261  break;
262  case PreferredEmail:
263  return i18nc( "@title:column the preferred email addresses of a person", "Preferred EMail" );
264  break;
265  case AllEmails:
266  return i18nc( "@title:column all email addresses of a person", "All EMails" );
267  break;
268  case Organization:
269  return KABC::Addressee::organizationLabel();
270  break;
271  case Role:
272  return KABC::Addressee::roleLabel();
273  break;
274  case Homepage:
275  return KABC::Addressee::urlLabel();
276  break;
277  case Note:
278  return KABC::Addressee::noteLabel();
279  break;
280  }
281  }
282  }
283  }
284 
285  return EntityTreeModel::entityHeaderData( section, orientation, role, headerGroup );
286 }
287 
288 #include "contactstreemodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 11 2012 12:15:28 by doxygen 1.8.1.2 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.4 API Reference

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