akonadi/contact
contactcompletionmodel.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "contactcompletionmodel_p.h"
00023
00024 #include "../kdescendantsproxymodel_p.h"
00025
00026 #include <akonadi/changerecorder.h>
00027 #include <akonadi/entitymimetypefiltermodel.h>
00028 #include <akonadi/itemfetchscope.h>
00029 #include <akonadi/session.h>
00030
00031 #include <kabc/addressee.h>
00032
00033 using namespace Akonadi;
00034
00035 QAbstractItemModel* ContactCompletionModel::mSelf = 0;
00036
00037 QAbstractItemModel* ContactCompletionModel::self()
00038 {
00039 if ( mSelf )
00040 return mSelf;
00041
00042 ChangeRecorder *monitor = new ChangeRecorder;
00043 monitor->fetchCollection( true );
00044 monitor->itemFetchScope().fetchFullPayload();
00045 monitor->setCollectionMonitored( Akonadi::Collection::root() );
00046 monitor->setMimeTypeMonitored( KABC::Addressee::mimeType() );
00047
00048 ContactCompletionModel *model = new ContactCompletionModel( monitor );
00049
00050 KDescendantsProxyModel *descModel = new KDescendantsProxyModel( model );
00051 descModel->setSourceModel( model );
00052
00053 EntityMimeTypeFilterModel *filter = new Akonadi::EntityMimeTypeFilterModel( model );
00054 filter->setSourceModel( descModel );
00055 filter->addMimeTypeExclusionFilter( Akonadi::Collection::mimeType() );
00056 filter->setHeaderGroup( Akonadi::EntityTreeModel::ItemListHeaders );
00057
00058 mSelf = filter;
00059
00060 return mSelf;
00061 }
00062
00063 ContactCompletionModel::ContactCompletionModel( ChangeRecorder *monitor, QObject *parent )
00064 : EntityTreeModel( monitor, parent )
00065 {
00066 }
00067
00068 ContactCompletionModel::~ContactCompletionModel()
00069 {
00070 }
00071
00072 QVariant ContactCompletionModel::entityData( const Item &item, int column, int role ) const
00073 {
00074 if ( !item.hasPayload<KABC::Addressee>() ) {
00075
00076 if ( role == Qt::DisplayRole )
00077 return item.remoteId();
00078
00079 return QVariant();
00080 }
00081
00082 if ( role == Qt::DisplayRole || role == Qt::EditRole ) {
00083 const KABC::Addressee contact = item.payload<KABC::Addressee>();
00084
00085 switch ( column ) {
00086 case NameColumn:
00087 if ( !contact.formattedName().isEmpty() )
00088 return contact.formattedName();
00089 else
00090 return contact.assembledName();
00091 break;
00092 case NameAndEmailColumn:
00093 {
00094 QString name = QString::fromLatin1( "%1 %2" ).arg( contact.givenName() )
00095 .arg( contact.familyName() ).simplified();
00096 if ( name.isEmpty() )
00097 name = contact.organization().simplified();
00098 if ( name.isEmpty() )
00099 return QString();
00100
00101 const QString email = contact.preferredEmail().simplified();
00102 if ( email.isEmpty() )
00103 return QString();
00104
00105 return QString::fromLatin1( "%1 <%2>" ).arg( name ).arg( email );
00106 }
00107 break;
00108 case EmailColumn:
00109 return contact.preferredEmail();
00110 break;
00111 }
00112 }
00113
00114 return EntityTreeModel::entityData( item, column, role );
00115 }
00116
00117 QVariant ContactCompletionModel::entityData( const Collection &collection, int column, int role ) const
00118 {
00119 return EntityTreeModel::entityData( collection, column, role );
00120 }
00121
00122 int ContactCompletionModel::columnCount( const QModelIndex &parent ) const
00123 {
00124 if ( !parent.isValid() )
00125 return 3;
00126 else
00127 return 0;
00128 }
00129
00130 int ContactCompletionModel::entityColumnCount( HeaderGroup ) const
00131 {
00132 return 3;
00133 }
00134
00135 #include "contactcompletionmodel_p.moc"