23 #include "emailaddressselectionproxymodel_p.h"
25 #include <akonadi/item.h>
26 #include <kabc/addressee.h>
27 #include <kabc/contactgroup.h>
30 using namespace Akonadi;
32 static QString createToolTip(
const KABC::ContactGroup &group )
34 QString txt = QLatin1String(
"<qt>" );
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>" );
46 txt += QLatin1String(
"</ul>" );
47 txt += QLatin1String(
"</qt>" );
52 static QString createToolTip(
const QString &name,
const QString &email )
54 return QString::fromLatin1(
"<qt>%1<b>%2</b></qt>" )
55 .arg( name.isEmpty() ? QString() : name + QLatin1String(
"<br/>" ) )
59 EmailAddressSelectionProxyModel::EmailAddressSelectionProxyModel( QObject *parent )
60 : LeafExtensionProxyModel( parent )
64 EmailAddressSelectionProxyModel::~EmailAddressSelectionProxyModel()
68 QVariant EmailAddressSelectionProxyModel::data(
const QModelIndex &index,
int role )
const
70 const QVariant value = LeafExtensionProxyModel::data( index, role );
72 if ( !value.isValid() ) {
73 if ( role == NameRole ) {
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>();
82 }
else if ( role == EmailAddressRole ) {
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>();
91 }
else if ( role == Qt::ToolTipRole ) {
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>() );
105 int EmailAddressSelectionProxyModel::leafRowCount(
const QModelIndex &index )
const
109 const KABC::Addressee contact = item.
payload<KABC::Addressee>();
110 if ( contact.emails().count() == 1 )
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();
122 int EmailAddressSelectionProxyModel::leafColumnCount(
const QModelIndex &index )
const
127 else if ( item.
hasPayload<KABC::ContactGroup>() )
133 QVariant EmailAddressSelectionProxyModel::leafData(
const QModelIndex &index,
int row,
int,
int role )
const
135 if ( role == Qt::DisplayRole ) {
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() );
147 }
else if ( role == NameRole ) {
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();
157 }
else if ( role == EmailAddressRole ) {
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();
168 }
else if ( role == Qt::ToolTipRole ) {
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() );
180 return index.data( role );