akonadi/contact
emailaddressselectionproxymodel.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2010 KDAB 00005 Author: Tobias Koenig <tokoe@kde.org> 00006 00007 This library is free software; you can redistribute it and/or modify it 00008 under the terms of the GNU Library General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or (at your 00010 option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, but WITHOUT 00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00015 License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to the 00019 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00020 02110-1301, USA. 00021 */ 00022 00023 #include "emailaddressselectionproxymodel_p.h" 00024 00025 #include <akonadi/item.h> 00026 #include <kabc/addressee.h> 00027 #include <kabc/contactgroup.h> 00028 #include <klocale.h> 00029 00030 using namespace Akonadi; 00031 00032 static QString createToolTip( const KABC::ContactGroup &group ) 00033 { 00034 QString txt = QLatin1String( "<qt>" ); 00035 00036 txt += QString::fromLatin1( "<b>%1</b>" ).arg( i18n( "Distribution List %1", group.name() ) ); 00037 txt += QLatin1String( "<ul>" ); 00038 for ( uint i = 0; i < group.dataCount(); ++i ) { 00039 txt += QLatin1String( "<li>" ); 00040 txt += group.data( i ).name() + QLatin1Char( ' ' ); 00041 txt += QLatin1String( "<em>" ); 00042 txt += group.data( i ).email(); 00043 txt += QLatin1String("</em></li>" ); 00044 } 00045 txt += QLatin1String( "</ul>" ); 00046 txt += QLatin1String( "</qt>" ); 00047 00048 return txt; 00049 } 00050 00051 static QString createToolTip( const QString &name, const QString &email ) 00052 { 00053 return QString::fromLatin1( "<qt>%1<b>%2</b></qt>" ) 00054 .arg( name.isEmpty() ? QString() : name + QLatin1String( "<br/>" ) ) 00055 .arg( email ); 00056 } 00057 00058 EmailAddressSelectionProxyModel::EmailAddressSelectionProxyModel( QObject *parent ) 00059 : LeafExtensionProxyModel( parent ) 00060 { 00061 } 00062 00063 EmailAddressSelectionProxyModel::~EmailAddressSelectionProxyModel() 00064 { 00065 } 00066 00067 QVariant EmailAddressSelectionProxyModel::data( const QModelIndex &index, int role ) const 00068 { 00069 const QVariant value = LeafExtensionProxyModel::data( index, role ); 00070 00071 if ( !value.isValid() ) { // index is not a leaf child 00072 if ( role == NameRole ) { 00073 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00074 if ( item.hasPayload<KABC::Addressee>() ) { 00075 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00076 return contact.realName(); 00077 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00078 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00079 return group.name(); 00080 } 00081 } else if ( role == EmailAddressRole ) { 00082 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00083 if ( item.hasPayload<KABC::Addressee>() ) { 00084 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00085 return contact.preferredEmail(); 00086 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00087 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00088 return group.name(); // the name must be resolved by the caller 00089 } 00090 } else if ( role == Qt::ToolTipRole ) { 00091 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00092 if ( item.hasPayload<KABC::Addressee>() ) { 00093 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00094 return createToolTip( contact.realName(), contact.preferredEmail() ); 00095 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00096 return createToolTip( item.payload<KABC::ContactGroup>() ); 00097 } 00098 } 00099 } 00100 00101 return value; 00102 } 00103 00104 int EmailAddressSelectionProxyModel::leafRowCount( const QModelIndex &index ) const 00105 { 00106 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00107 if ( item.hasPayload<KABC::Addressee>() ) { 00108 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00109 if ( contact.emails().count() == 1 ) 00110 return 0; 00111 else 00112 return contact.emails().count(); 00113 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00114 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00115 return group.dataCount(); 00116 } else { 00117 return 0; 00118 } 00119 } 00120 00121 int EmailAddressSelectionProxyModel::leafColumnCount( const QModelIndex &index ) const 00122 { 00123 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00124 if ( item.hasPayload<KABC::Addressee>() ) 00125 return 1; 00126 else if ( item.hasPayload<KABC::ContactGroup>() ) 00127 return 1; 00128 else 00129 return 0; 00130 } 00131 00132 QVariant EmailAddressSelectionProxyModel::leafData( const QModelIndex &index, int row, int, int role ) const 00133 { 00134 if ( role == Qt::DisplayRole ) { 00135 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00136 if ( item.hasPayload<KABC::Addressee>() ) { 00137 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00138 if ( row >= 0 && row < contact.emails().count() ) 00139 return contact.emails().at( row ); 00140 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00141 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00142 if ( row >= 0 && row < (int)group.dataCount() ) 00143 return i18nc( "Name and email address of a contact", "%1 <%2>", group.data( row ).name(), group.data( row ).email() ); //krazy:exclude=i18ncheckarg 00144 } 00145 } else if ( role == NameRole ) { 00146 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00147 if ( item.hasPayload<KABC::Addressee>() ) { 00148 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00149 return contact.realName(); 00150 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00151 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00152 if ( row >= 0 && row < (int)group.dataCount() ) 00153 return group.data( row ).name(); 00154 } 00155 } else if ( role == EmailAddressRole ) { 00156 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00157 if ( item.hasPayload<KABC::Addressee>() ) { 00158 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00159 if ( row >= 0 && row < contact.emails().count() ) 00160 return contact.emails().at( row ); 00161 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00162 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00163 if ( row >= 0 && row < (int)group.dataCount() ) 00164 return group.data( row ).email(); 00165 } 00166 } else if ( role == Qt::ToolTipRole ) { 00167 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00168 if ( item.hasPayload<KABC::Addressee>() ) { 00169 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00170 if ( row >= 0 && row < contact.emails().count() ) 00171 return createToolTip( contact.realName(), contact.emails().at( row ) ); 00172 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00173 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00174 if ( row >= 0 && row < (int)group.dataCount() ) 00175 return createToolTip( group.data( row ).name(), group.data( row ).email() ); 00176 } 00177 } else 00178 return index.data( role ); 00179 00180 return QVariant(); 00181 }