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

akonadi/contact

  • akonadi
  • contact
emailaddressselectionproxymodel.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2010 KDAB
5  Author: 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 "emailaddressselectionproxymodel_p.h"
24 
25 #include <akonadi/item.h>
26 #include <kabc/addressee.h>
27 #include <kabc/contactgroup.h>
28 #include <klocale.h>
29 
30 using namespace Akonadi;
31 
32 static QString createToolTip( const KABC::ContactGroup &group )
33 {
34  QString txt = QLatin1String( "<qt>" );
35 
36  txt += QString::fromLatin1( "<b>%1</b>" ).arg( i18n( "Distribution List %1", group.name() ) );
37  txt += QLatin1String( "<ul>" );
38  const int groupDataCount( group.dataCount() );
39  for ( uint i = 0; i < groupDataCount; ++i ) {
40  txt += QLatin1String( "<li>" );
41  txt += group.data( i ).name() + QLatin1Char( ' ' );
42  txt += QLatin1String( "<em>" );
43  txt += group.data( i ).email();
44  txt += QLatin1String("</em></li>" );
45  }
46  txt += QLatin1String( "</ul>" );
47  txt += QLatin1String( "</qt>" );
48 
49  return txt;
50 }
51 
52 static QString createToolTip( const QString &name, const QString &email )
53 {
54  return QString::fromLatin1( "<qt>%1<b>%2</b></qt>" )
55  .arg( name.isEmpty() ? QString() : name + QLatin1String( "<br/>" ) )
56  .arg( email );
57 }
58 
59 EmailAddressSelectionProxyModel::EmailAddressSelectionProxyModel( QObject *parent )
60  : LeafExtensionProxyModel( parent )
61 {
62 }
63 
64 EmailAddressSelectionProxyModel::~EmailAddressSelectionProxyModel()
65 {
66 }
67 
68 QVariant EmailAddressSelectionProxyModel::data( const QModelIndex &index, int role ) const
69 {
70  const QVariant value = LeafExtensionProxyModel::data( index, role );
71 
72  if ( !value.isValid() ) { // index is not a leaf child
73  if ( role == NameRole ) {
74  const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
75  if ( item.hasPayload<KABC::Addressee>() ) {
76  const KABC::Addressee contact = item.payload<KABC::Addressee>();
77  return contact.realName();
78  } else if ( item.hasPayload<KABC::ContactGroup>() ) {
79  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
80  return group.name();
81  }
82  } else if ( role == EmailAddressRole ) {
83  const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
84  if ( item.hasPayload<KABC::Addressee>() ) {
85  const KABC::Addressee contact = item.payload<KABC::Addressee>();
86  return contact.preferredEmail();
87  } else if ( item.hasPayload<KABC::ContactGroup>() ) {
88  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
89  return group.name(); // the name must be resolved by the caller
90  }
91  } else if ( role == Qt::ToolTipRole ) {
92  const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
93  if ( item.hasPayload<KABC::Addressee>() ) {
94  const KABC::Addressee contact = item.payload<KABC::Addressee>();
95  return createToolTip( contact.realName(), contact.preferredEmail() );
96  } else if ( item.hasPayload<KABC::ContactGroup>() ) {
97  return createToolTip( item.payload<KABC::ContactGroup>() );
98  }
99  }
100  }
101 
102  return value;
103 }
104 
105 int EmailAddressSelectionProxyModel::leafRowCount( const QModelIndex &index ) const
106 {
107  const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
108  if ( item.hasPayload<KABC::Addressee>() ) {
109  const KABC::Addressee contact = item.payload<KABC::Addressee>();
110  if ( contact.emails().count() == 1 )
111  return 0;
112  else
113  return contact.emails().count();
114  } else if ( item.hasPayload<KABC::ContactGroup>() ) {
115  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
116  return group.dataCount();
117  } else {
118  return 0;
119  }
120 }
121 
122 int EmailAddressSelectionProxyModel::leafColumnCount( const QModelIndex &index ) const
123 {
124  const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
125  if ( item.hasPayload<KABC::Addressee>() )
126  return 1;
127  else if ( item.hasPayload<KABC::ContactGroup>() )
128  return 1;
129  else
130  return 0;
131 }
132 
133 QVariant EmailAddressSelectionProxyModel::leafData( const QModelIndex &index, int row, int, int role ) const
134 {
135  if ( role == Qt::DisplayRole ) {
136  const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
137  if ( item.hasPayload<KABC::Addressee>() ) {
138  const KABC::Addressee contact = item.payload<KABC::Addressee>();
139  if ( row >= 0 && row < contact.emails().count() )
140  return contact.emails().at( row );
141  } else if ( item.hasPayload<KABC::ContactGroup>() ) {
142  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
143  if ( row >= 0 && row < (int)group.dataCount() )
144  return i18nc( "Name and email address of a contact", "%1 <%2>",
145  group.data( row ).name(), group.data( row ).email() );
146  }
147  } else if ( role == NameRole ) {
148  const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
149  if ( item.hasPayload<KABC::Addressee>() ) {
150  const KABC::Addressee contact = item.payload<KABC::Addressee>();
151  return contact.realName();
152  } else if ( item.hasPayload<KABC::ContactGroup>() ) {
153  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
154  if ( row >= 0 && row < (int)group.dataCount() )
155  return group.data( row ).name();
156  }
157  } else if ( role == EmailAddressRole ) {
158  const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
159  if ( item.hasPayload<KABC::Addressee>() ) {
160  const KABC::Addressee contact = item.payload<KABC::Addressee>();
161  if ( row >= 0 && row < contact.emails().count() )
162  return contact.emails().at( row );
163  } else if ( item.hasPayload<KABC::ContactGroup>() ) {
164  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
165  if ( row >= 0 && row < (int)group.dataCount() )
166  return group.data( row ).email();
167  }
168  } else if ( role == Qt::ToolTipRole ) {
169  const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
170  if ( item.hasPayload<KABC::Addressee>() ) {
171  const KABC::Addressee contact = item.payload<KABC::Addressee>();
172  if ( row >= 0 && row < contact.emails().count() )
173  return createToolTip( contact.realName(), contact.emails().at( row ) );
174  } else if ( item.hasPayload<KABC::ContactGroup>() ) {
175  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
176  if ( row >= 0 && row < (int)group.dataCount() )
177  return createToolTip( group.data( row ).name(), group.data( row ).email() );
178  }
179  } else
180  return index.data( role );
181 
182  return QVariant();
183 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Dec 10 2012 13:48:34 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