00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "contactgrouplineedit_p.h"
00023
00024 #include "contactcompletionmodel_p.h"
00025
00026 #include <akonadi/entitytreemodel.h>
00027 #include <akonadi/itemfetchjob.h>
00028 #include <akonadi/itemfetchscope.h>
00029 #include <klocale.h>
00030
00031 #include <QtCore/QAbstractItemModel>
00032 #include <QtGui/QAction>
00033 #include <QtGui/QCompleter>
00034 #include <QtGui/QMenu>
00035
00036 ContactGroupLineEdit::ContactGroupLineEdit( QWidget *parent )
00037 : KLineEdit( parent ),
00038 mCompleter( 0 ),
00039 mContainsReference( false )
00040 {
00041 setClearButtonShown( true );
00042 }
00043
00044 void ContactGroupLineEdit::setCompletionModel( QAbstractItemModel *model )
00045 {
00046 mCompleter = new QCompleter( model, this );
00047 mCompleter->setCompletionColumn( Akonadi::ContactCompletionModel::NameAndEmailColumn );
00048 connect( mCompleter, SIGNAL( activated( const QModelIndex& ) ),
00049 this, SLOT( autoCompleted( const QModelIndex& ) ) );
00050
00051 setCompleter( mCompleter );
00052 }
00053
00054 bool ContactGroupLineEdit::containsReference() const
00055 {
00056 return mContainsReference;
00057 }
00058
00059 void ContactGroupLineEdit::setContactData( const KABC::ContactGroup::Data &groupData )
00060 {
00061 mContactData = groupData;
00062 mContainsReference = false;
00063
00064 setText( QString::fromLatin1( "%1 <%2>" ).arg( groupData.name() ).arg( groupData.email() ) );
00065 }
00066
00067 KABC::ContactGroup::Data ContactGroupLineEdit::contactData() const
00068 {
00069 QString fullName, email;
00070 KABC::Addressee::parseEmailAddress( text(), fullName, email );
00071
00072 if ( fullName.isEmpty() || email.isEmpty() )
00073 return KABC::ContactGroup::Data();
00074
00075 KABC::ContactGroup::Data groupData( mContactData );
00076 groupData.setName( fullName );
00077 groupData.setEmail( email );
00078
00079 return groupData;
00080 }
00081
00082 void ContactGroupLineEdit::setContactReference( const KABC::ContactGroup::ContactReference &reference )
00083 {
00084 mContactReference = reference;
00085 mContainsReference = true;
00086
00087 disconnect( this, SIGNAL( textChanged( const QString& ) ), this, SLOT( invalidateReference() ) );
00088
00089 updateView( reference.uid(), reference.preferredEmail() );
00090 }
00091
00092 KABC::ContactGroup::ContactReference ContactGroupLineEdit::contactReference() const
00093 {
00094 return mContactReference;
00095 }
00096
00097 void ContactGroupLineEdit::autoCompleted( const QModelIndex &index )
00098 {
00099 if ( !index.isValid() )
00100 return;
00101
00102 const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
00103 if ( !item.isValid() )
00104 return;
00105
00106 disconnect( this, SIGNAL( textChanged( const QString& ) ), this, SLOT( invalidateReference() ) );
00107 mContainsReference = true;
00108
00109 updateView( item );
00110
00111 connect( this, SIGNAL( textChanged( const QString& ) ), SLOT( invalidateReference() ) );
00112 }
00113
00114 void ContactGroupLineEdit::invalidateReference()
00115 {
00116 disconnect( this, SIGNAL( textChanged( const QString& ) ), this, SLOT( invalidateReference() ) );
00117 mContainsReference = false;
00118 }
00119
00120 void ContactGroupLineEdit::updateView( const QString &uid, const QString &preferredEmail )
00121 {
00122 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( Akonadi::Item( uid.toLongLong() ) );
00123 job->fetchScope().fetchFullPayload();
00124 job->setProperty( "preferredEmail", preferredEmail );
00125 connect( job, SIGNAL( result( KJob* ) ), SLOT( fetchDone( KJob* ) ) );
00126 }
00127
00128 void ContactGroupLineEdit::fetchDone( KJob *job )
00129 {
00130 Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
00131
00132 if ( !fetchJob->items().isEmpty() ) {
00133 const Akonadi::Item item = fetchJob->items().first();
00134 updateView( item, fetchJob->property( "preferredEmail" ).toString() );
00135 }
00136
00137 connect( this, SIGNAL( textChanged( const QString& ) ), SLOT( invalidateReference() ) );
00138 }
00139
00140 void ContactGroupLineEdit::updateView( const Akonadi::Item &item, const QString &preferredEmail )
00141 {
00142 if ( !item.hasPayload<KABC::Addressee>() )
00143 return;
00144
00145 const KABC::Addressee contact = item.payload<KABC::Addressee>();
00146
00147 QString email( preferredEmail );
00148 if ( email.isEmpty() )
00149 email = requestPreferredEmail( contact );
00150
00151 QString name = contact.formattedName();
00152 if ( name.isEmpty() )
00153 name = contact.assembledName();
00154
00155 if ( email.isEmpty() )
00156 setText( QString::fromLatin1( "%1" ).arg( name ) );
00157 else
00158 setText( QString::fromLatin1( "%1 <%2>" ).arg( name ).arg( email ) );
00159
00160 mContactReference.setUid( QString::number( item.id() ) );
00161
00162 if ( contact.preferredEmail() != email )
00163 mContactReference.setPreferredEmail( email );
00164 }
00165
00166 QString ContactGroupLineEdit::requestPreferredEmail( const KABC::Addressee &contact ) const
00167 {
00168 const QStringList emails = contact.emails();
00169
00170 if ( emails.isEmpty() ) {
00171 qDebug( "ContactGroupLineEdit::requestPreferredEmail(): Warning!!! no email addresses available" );
00172 return QString();
00173 }
00174
00175 if ( emails.count() == 1 )
00176 return emails.first();
00177
00178 QAction *action = 0;
00179
00180 QMenu menu;
00181 menu.setTitle( i18n( "Select preferred email address" ) );
00182 for ( int i = 0; i < emails.count(); ++i ) {
00183 action = menu.addAction( emails.at( i ) );
00184 action->setData( i );
00185 }
00186
00187 action = menu.exec( mapToGlobal( QPoint( x() + width()/2, y() + height()/2 ) ) );
00188 if ( !action )
00189 return emails.first();
00190
00191 return emails.at( action->data().toInt() );
00192 }
00193
00194 #include "contactgrouplineedit_p.moc"