23 #include "emailaddressselectionproxymodel_p.h"
25 #include <akonadi/item.h>
26 #include <kabc/addressee.h>
27 #include <kabc/contactgroup.h>
28 #include <klocalizedstring.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 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>" );
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 ) {
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>();
82 }
else if ( role == EmailAddressRole ) {
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>();
91 }
else if ( role == Qt::ToolTipRole ) {
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>() );
105 int EmailAddressSelectionProxyModel::leafRowCount(
const QModelIndex &index )
const
108 if ( item.hasPayload<KABC::Addressee>() ) {
109 const KABC::Addressee contact = item.payload<KABC::Addressee>();
110 if ( contact.emails().count() == 1 ) {
113 return contact.emails().count();
115 }
else if ( item.hasPayload<KABC::ContactGroup>() ) {
116 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
117 return group.dataCount();
123 int EmailAddressSelectionProxyModel::leafColumnCount(
const QModelIndex &index )
const
126 if ( item.hasPayload<KABC::Addressee>() ) {
128 }
else if ( item.hasPayload<KABC::ContactGroup>() ) {
135 QVariant EmailAddressSelectionProxyModel::leafData(
const QModelIndex &index,
int row,
int,
int role )
const
137 if ( role == Qt::DisplayRole ) {
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 );
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() );
151 }
else if ( role == NameRole ) {
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();
162 }
else if ( role == EmailAddressRole ) {
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 );
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();
175 }
else if ( role == Qt::ToolTipRole ) {
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 ) );
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() );
189 return index.data( role );