akonadi
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 const int groupDataCount( group.dataCount() ); 00039 for ( uint i = 0; i < groupDataCount; ++i ) { 00040 txt += QLatin1String( "<li>" ); 00041 txt += group.data( i ).name() + QLatin1Char( ' ' ); 00042 txt += QLatin1String( "<em>" ); 00043 txt += group.data( i ).email(); 00044 txt += QLatin1String("</em></li>" ); 00045 } 00046 txt += QLatin1String( "</ul>" ); 00047 txt += QLatin1String( "</qt>" ); 00048 00049 return txt; 00050 } 00051 00052 static QString createToolTip( const QString &name, const QString &email ) 00053 { 00054 return QString::fromLatin1( "<qt>%1<b>%2</b></qt>" ) 00055 .arg( name.isEmpty() ? QString() : name + QLatin1String( "<br/>" ) ) 00056 .arg( email ); 00057 } 00058 00059 EmailAddressSelectionProxyModel::EmailAddressSelectionProxyModel( QObject *parent ) 00060 : LeafExtensionProxyModel( parent ) 00061 { 00062 } 00063 00064 EmailAddressSelectionProxyModel::~EmailAddressSelectionProxyModel() 00065 { 00066 } 00067 00068 QVariant EmailAddressSelectionProxyModel::data( const QModelIndex &index, int role ) const 00069 { 00070 const QVariant value = LeafExtensionProxyModel::data( index, role ); 00071 00072 if ( !value.isValid() ) { // index is not a leaf child 00073 if ( role == NameRole ) { 00074 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00075 if ( item.hasPayload<KABC::Addressee>() ) { 00076 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00077 return contact.realName(); 00078 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00079 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00080 return group.name(); 00081 } 00082 } else if ( role == EmailAddressRole ) { 00083 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00084 if ( item.hasPayload<KABC::Addressee>() ) { 00085 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00086 return contact.preferredEmail(); 00087 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00088 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00089 return group.name(); // the name must be resolved by the caller 00090 } 00091 } else if ( role == Qt::ToolTipRole ) { 00092 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00093 if ( item.hasPayload<KABC::Addressee>() ) { 00094 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00095 return createToolTip( contact.realName(), contact.preferredEmail() ); 00096 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00097 return createToolTip( item.payload<KABC::ContactGroup>() ); 00098 } 00099 } 00100 } 00101 00102 return value; 00103 } 00104 00105 int EmailAddressSelectionProxyModel::leafRowCount( const QModelIndex &index ) const 00106 { 00107 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00108 if ( item.hasPayload<KABC::Addressee>() ) { 00109 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00110 if ( contact.emails().count() == 1 ) 00111 return 0; 00112 else 00113 return contact.emails().count(); 00114 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00115 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00116 return group.dataCount(); 00117 } else { 00118 return 0; 00119 } 00120 } 00121 00122 int EmailAddressSelectionProxyModel::leafColumnCount( const QModelIndex &index ) const 00123 { 00124 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00125 if ( item.hasPayload<KABC::Addressee>() ) 00126 return 1; 00127 else if ( item.hasPayload<KABC::ContactGroup>() ) 00128 return 1; 00129 else 00130 return 0; 00131 } 00132 00133 QVariant EmailAddressSelectionProxyModel::leafData( const QModelIndex &index, int row, int, int role ) const 00134 { 00135 if ( role == Qt::DisplayRole ) { 00136 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00137 if ( item.hasPayload<KABC::Addressee>() ) { 00138 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00139 if ( row >= 0 && row < contact.emails().count() ) 00140 return contact.emails().at( row ); 00141 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00142 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00143 if ( row >= 0 && row < (int)group.dataCount() ) 00144 return i18nc( "Name and email address of a contact", "%1 <%2>", 00145 group.data( row ).name(), group.data( row ).email() ); 00146 } 00147 } else if ( role == NameRole ) { 00148 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00149 if ( item.hasPayload<KABC::Addressee>() ) { 00150 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00151 return contact.realName(); 00152 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00153 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00154 if ( row >= 0 && row < (int)group.dataCount() ) 00155 return group.data( row ).name(); 00156 } 00157 } else if ( role == EmailAddressRole ) { 00158 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00159 if ( item.hasPayload<KABC::Addressee>() ) { 00160 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00161 if ( row >= 0 && row < contact.emails().count() ) 00162 return contact.emails().at( row ); 00163 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00164 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00165 if ( row >= 0 && row < (int)group.dataCount() ) 00166 return group.data( row ).email(); 00167 } 00168 } else if ( role == Qt::ToolTipRole ) { 00169 const Akonadi::Item item = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00170 if ( item.hasPayload<KABC::Addressee>() ) { 00171 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00172 if ( row >= 0 && row < contact.emails().count() ) 00173 return createToolTip( contact.realName(), contact.emails().at( row ) ); 00174 } else if ( item.hasPayload<KABC::ContactGroup>() ) { 00175 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00176 if ( row >= 0 && row < (int)group.dataCount() ) 00177 return createToolTip( group.data( row ).name(), group.data( row ).email() ); 00178 } 00179 } else 00180 return index.data( role ); 00181 00182 return QVariant(); 00183 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:09:22 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:09:22 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.