akonadi/contact
contactstreemodel.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2009 Stephen Kelly <steveire@gmail.com> 00005 Copyright (c) 2009 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 "contactstreemodel.h" 00024 00025 #include <kabc/addressee.h> 00026 #include <kabc/contactgroup.h> 00027 #include <kglobal.h> 00028 #include <kicon.h> 00029 #include <kiconloader.h> 00030 #include <klocale.h> 00031 00032 using namespace Akonadi; 00033 00034 class ContactsTreeModel::Private 00035 { 00036 public: 00037 Private() 00038 : mColumns( ContactsTreeModel::Columns() << ContactsTreeModel::FullName ), 00039 mIconSize( KIconLoader::global()->currentSize( KIconLoader::Small ) ) 00040 { 00041 } 00042 00043 Columns mColumns; 00044 const int mIconSize; 00045 }; 00046 00047 ContactsTreeModel::ContactsTreeModel( ChangeRecorder *monitor, QObject *parent ) 00048 : EntityTreeModel( monitor, parent ), d( new Private ) 00049 { 00050 } 00051 00052 ContactsTreeModel::~ContactsTreeModel() 00053 { 00054 delete d; 00055 } 00056 00057 void ContactsTreeModel::setColumns( const Columns &columns ) 00058 { 00059 emit beginResetModel(); 00060 d->mColumns = columns; 00061 emit endResetModel(); 00062 } 00063 00064 ContactsTreeModel::Columns ContactsTreeModel::columns() const 00065 { 00066 return d->mColumns; 00067 } 00068 00069 QVariant ContactsTreeModel::entityData( const Item &item, int column, int role ) const 00070 { 00071 if ( item.mimeType() == KABC::Addressee::mimeType() ) { 00072 if ( !item.hasPayload<KABC::Addressee>() ) { 00073 00074 // Pass modeltest 00075 if ( role == Qt::DisplayRole ) 00076 return item.remoteId(); 00077 00078 return QVariant(); 00079 } 00080 00081 const KABC::Addressee contact = item.payload<KABC::Addressee>(); 00082 00083 if ( role == Qt::DecorationRole ) { 00084 if ( column == 0 ) { 00085 const KABC::Picture picture = contact.photo(); 00086 if ( picture.isIntern() ) { 00087 return picture.data().scaled( QSize( d->mIconSize, d->mIconSize ), Qt::KeepAspectRatio ); 00088 } else { 00089 return KIcon( QLatin1String( "user-identity" ) ); 00090 } 00091 } 00092 return QVariant(); 00093 } else if ( (role == Qt::DisplayRole) || (role == Qt::EditRole) ) { 00094 switch ( d->mColumns.at( column ) ) { 00095 case FullName: 00096 return contact.realName(); 00097 break; 00098 case FamilyName: 00099 return contact.familyName(); 00100 break; 00101 case GivenName: 00102 return contact.givenName(); 00103 break; 00104 case Birthday: 00105 if ( contact.birthday().isValid() ) 00106 return KGlobal::locale()->formatDate( contact.birthday().date(), KLocale::ShortDate ); 00107 break; 00108 case HomeAddress: 00109 { 00110 const KABC::Address address = contact.address( KABC::Address::Home ); 00111 if ( !address.isEmpty() ) 00112 return address.formattedAddress(); 00113 } 00114 break; 00115 case BusinessAddress: 00116 { 00117 const KABC::Address address = contact.address( KABC::Address::Work ); 00118 if ( !address.isEmpty() ) 00119 return address.formattedAddress(); 00120 } 00121 break; 00122 case PhoneNumbers: 00123 { 00124 QStringList values; 00125 00126 const KABC::PhoneNumber::List numbers = contact.phoneNumbers(); 00127 foreach ( const KABC::PhoneNumber &number, numbers ) 00128 values += number.number(); 00129 00130 return values.join( QLatin1String( "\n" ) ); 00131 } 00132 break; 00133 case PreferredEmail: 00134 return contact.preferredEmail(); 00135 break; 00136 case AllEmails: 00137 return contact.emails().join( QLatin1String( "\n" ) ); 00138 break; 00139 case Organization: 00140 return contact.organization(); 00141 break; 00142 case Role: 00143 return contact.role(); 00144 break; 00145 case Homepage: 00146 return contact.url().url(); 00147 break; 00148 case Note: 00149 return contact.note(); 00150 break; 00151 } 00152 } else if ( role == DateRole ) { 00153 if ( d->mColumns.at( column ) == Birthday ) 00154 return contact.birthday(); 00155 else 00156 return QDate(); 00157 } 00158 } else if ( item.mimeType() == KABC::ContactGroup::mimeType() ) { 00159 if ( !item.hasPayload<KABC::ContactGroup>() ) { 00160 00161 // Pass modeltest 00162 if ( role == Qt::DisplayRole ) 00163 return item.remoteId(); 00164 00165 return QVariant(); 00166 } 00167 00168 if ( role == Qt::DecorationRole ) { 00169 if ( column == 0 ) 00170 return KIcon( QLatin1String( "x-mail-distribution-list" ) ); 00171 else 00172 return QVariant(); 00173 } else if ( (role == Qt::DisplayRole) || (role == Qt::EditRole) ) { 00174 switch ( d->mColumns.at( column ) ) { 00175 case FullName: 00176 { 00177 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00178 return group.name(); 00179 } 00180 break; 00181 default: 00182 return QVariant(); 00183 break; 00184 } 00185 } 00186 } 00187 00188 return EntityTreeModel::entityData( item, column, role ); 00189 } 00190 00191 QVariant ContactsTreeModel::entityData( const Collection &collection, int column, int role ) const 00192 { 00193 if ( role == Qt::DisplayRole ) { 00194 switch ( column ) { 00195 case 0: 00196 return EntityTreeModel::entityData( collection, column, role ); 00197 default: 00198 return QString(); // pass model test 00199 } 00200 } 00201 00202 return EntityTreeModel::entityData( collection, column, role ); 00203 } 00204 00205 int ContactsTreeModel::entityColumnCount( HeaderGroup headerGroup ) const 00206 { 00207 if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) { 00208 return 1; 00209 } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) { 00210 return d->mColumns.count(); 00211 } else { 00212 return EntityTreeModel::entityColumnCount( headerGroup ); 00213 } 00214 } 00215 00216 QVariant ContactsTreeModel::entityHeaderData( int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup ) const 00217 { 00218 if ( role == Qt::DisplayRole ) { 00219 if ( orientation == Qt::Horizontal ) { 00220 if ( headerGroup == EntityTreeModel::CollectionTreeHeaders ) { 00221 00222 if ( section >= 1 ) 00223 return QVariant(); 00224 00225 switch ( section ) { 00226 case 0: 00227 return i18nc( "@title:column address books overview", "Address Books" ); 00228 break; 00229 } 00230 } else if ( headerGroup == EntityTreeModel::ItemListHeaders ) { 00231 if ( section < 0 || section >= d->mColumns.count() ) 00232 return QVariant(); 00233 00234 switch ( d->mColumns.at( section ) ) { 00235 case FullName: 00236 return i18nc( "@title:column name of a person", "Name" ); 00237 break; 00238 case FamilyName: 00239 return i18nc( "@title:column family name of a person", "Family Name" ); 00240 break; 00241 case GivenName: 00242 return i18nc( "@title:column given name of a person", "Given Name" ); 00243 break; 00244 case Birthday: 00245 return KABC::Addressee::birthdayLabel(); 00246 break; 00247 case HomeAddress: 00248 return i18nc( "@title:column home address of a person", "Home" ); 00249 break; 00250 case BusinessAddress: 00251 return i18nc( "@title:column work address of a person", "Work" ); 00252 break; 00253 case PhoneNumbers: 00254 return i18nc( "@title:column phone numbers of a person", "Phone Numbers" ); 00255 break; 00256 case PreferredEmail: 00257 return i18nc( "@title:column the preferred email addresses of a person", "Preferred EMail" ); 00258 break; 00259 case AllEmails: 00260 return i18nc( "@title:column all email addresses of a person", "All EMails" ); 00261 break; 00262 case Organization: 00263 return KABC::Addressee::organizationLabel(); 00264 break; 00265 case Role: 00266 return KABC::Addressee::roleLabel(); 00267 break; 00268 case Homepage: 00269 return KABC::Addressee::urlLabel(); 00270 break; 00271 case Note: 00272 return KABC::Addressee::noteLabel(); 00273 break; 00274 } 00275 } 00276 } 00277 } 00278 00279 return EntityTreeModel::entityHeaderData( section, orientation, role, headerGroup ); 00280 } 00281 00282 #include "contactstreemodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:01:47 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:01:47 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.