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

akonadi

  • 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 <klocalizedstring.h>
29
30using namespace Akonadi;
31
32static 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 uint 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
52static 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
59EmailAddressSelectionProxyModel::EmailAddressSelectionProxyModel( QObject *parent )
60 : LeafExtensionProxyModel( parent )
61{
62}
63
64EmailAddressSelectionProxyModel::~EmailAddressSelectionProxyModel()
65{
66}
67
68QVariant 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
105int 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 }
115 } else if ( item.hasPayload<KABC::ContactGroup>() ) {
116 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
117 return group.dataCount();
118 } else {
119 return 0;
120 }
121}
122
123int EmailAddressSelectionProxyModel::leafColumnCount( const QModelIndex &index ) const
124{
125 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
126 if ( item.hasPayload<KABC::Addressee>() ) {
127 return 1;
128 } else if ( item.hasPayload<KABC::ContactGroup>() ) {
129 return 1;
130 } else {
131 return 0;
132 }
133}
134
135QVariant EmailAddressSelectionProxyModel::leafData( const QModelIndex &index, int row, int, int role ) const
136{
137 if ( role == Qt::DisplayRole ) {
138 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
139 if ( item.hasPayload<KABC::Addressee>() ) {
140 const KABC::Addressee contact = item.payload<KABC::Addressee>();
141 if ( row >= 0 && row < contact.emails().count() ) {
142 return contact.emails().at( row );
143 }
144 } else if ( item.hasPayload<KABC::ContactGroup>() ) {
145 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
146 if ( row >= 0 && row < (int)group.dataCount() ) {
147 return i18nc( "Name and email address of a contact", "%1 <%2>",
148 group.data( row ).name(), group.data( row ).email() );
149 }
150 }
151 } else if ( role == NameRole ) {
152 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
153 if ( item.hasPayload<KABC::Addressee>() ) {
154 const KABC::Addressee contact = item.payload<KABC::Addressee>();
155 return contact.realName();
156 } else if ( item.hasPayload<KABC::ContactGroup>() ) {
157 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
158 if ( row >= 0 && row < (int)group.dataCount() ) {
159 return group.data( row ).name();
160 }
161 }
162 } else if ( role == EmailAddressRole ) {
163 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
164 if ( item.hasPayload<KABC::Addressee>() ) {
165 const KABC::Addressee contact = item.payload<KABC::Addressee>();
166 if ( row >= 0 && row < contact.emails().count() ) {
167 return contact.emails().at( row );
168 }
169 } else if ( item.hasPayload<KABC::ContactGroup>() ) {
170 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
171 if ( row >= 0 && row < (int)group.dataCount() ) {
172 return group.data( row ).email();
173 }
174 }
175 } else if ( role == Qt::ToolTipRole ) {
176 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
177 if ( item.hasPayload<KABC::Addressee>() ) {
178 const KABC::Addressee contact = item.payload<KABC::Addressee>();
179 if ( row >= 0 && row < contact.emails().count() ) {
180 return createToolTip( contact.realName(), contact.emails().at( row ) );
181 }
182 } else if ( item.hasPayload<KABC::ContactGroup>() ) {
183 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
184 if ( row >= 0 && row < (int)group.dataCount() ) {
185 return createToolTip( group.data( row ).name(), group.data( row ).email() );
186 }
187 }
188 } else
189 return index.data( role );
190
191 return QVariant();
192}
Akonadi
FreeBusyManager::Singleton.
Definition: actionstatemanager_p.h:28
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jul 21 2022 00:00:00 by doxygen 1.9.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.14.10 API Reference

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