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