22 #include "contactgrouplineedit_p.h"
24 #include "contactcompletionmodel_p.h"
26 #include <akonadi/entitytreemodel.h>
27 #include <akonadi/itemfetchjob.h>
28 #include <akonadi/itemfetchscope.h>
29 #include <klocalizedstring.h>
31 #include <QtCore/QAbstractItemModel>
36 ContactGroupLineEdit::ContactGroupLineEdit( QWidget *parent )
37 : KLineEdit( parent ),
39 mContainsReference( false )
41 setClearButtonShown(
true );
44 void ContactGroupLineEdit::setCompletionModel( QAbstractItemModel *model )
46 mCompleter =
new QCompleter( model,
this );
47 mCompleter->setCompletionColumn( Akonadi::ContactCompletionModel::NameAndEmailColumn );
48 connect( mCompleter, SIGNAL(activated(QModelIndex)),
49 this, SLOT(autoCompleted(QModelIndex)) );
51 setCompleter( mCompleter );
54 bool ContactGroupLineEdit::containsReference()
const
56 return mContainsReference;
59 void ContactGroupLineEdit::setContactData(
const KABC::ContactGroup::Data &groupData )
61 mContactData = groupData;
62 mContainsReference =
false;
64 setText( QString::fromLatin1(
"%1 <%2>" ).arg( groupData.name() ).arg( groupData.email() ) );
67 KABC::ContactGroup::Data ContactGroupLineEdit::contactData()
const
69 QString fullName, email;
70 KABC::Addressee::parseEmailAddress( text(), fullName, email );
72 if ( fullName.isEmpty() || email.isEmpty() ) {
73 return KABC::ContactGroup::Data();
76 KABC::ContactGroup::Data groupData( mContactData );
77 groupData.setName( fullName );
78 groupData.setEmail( email );
83 void ContactGroupLineEdit::setContactReference(
const KABC::ContactGroup::ContactReference &reference )
85 mContactReference = reference;
86 mContainsReference =
true;
88 disconnect(
this, SIGNAL(textChanged(QString)),
this, SLOT(invalidateReference()) );
90 updateView( reference );
93 KABC::ContactGroup::ContactReference ContactGroupLineEdit::contactReference()
const
95 return mContactReference;
98 void ContactGroupLineEdit::autoCompleted(
const QModelIndex &index )
100 if ( !index.isValid() ) {
105 if ( !item.isValid() ) {
109 disconnect(
this, SIGNAL(textChanged(QString)),
this, SLOT(invalidateReference()) );
110 mContainsReference =
true;
114 connect(
this, SIGNAL(textChanged(QString)), SLOT(invalidateReference()) );
117 void ContactGroupLineEdit::invalidateReference()
119 disconnect(
this, SIGNAL(textChanged(QString)),
this, SLOT(invalidateReference()) );
120 mContainsReference =
false;
123 void ContactGroupLineEdit::updateView(
const KABC::ContactGroup::ContactReference &reference )
126 if ( !reference.gid().isEmpty() ) {
127 item.setGid( reference.gid() );
129 item.setId( reference.uid().toLongLong() );
133 job->setProperty(
"preferredEmail", reference.preferredEmail() );
134 connect( job, SIGNAL(result(KJob*)), SLOT(fetchDone(KJob*)) );
137 void ContactGroupLineEdit::fetchDone( KJob *job )
141 if ( !fetchJob->
items().isEmpty() ) {
142 const Akonadi::Item item = fetchJob->
items().first();
143 updateView( item, fetchJob->property(
"preferredEmail" ).toString() );
146 connect(
this, SIGNAL(textChanged(QString)), SLOT(invalidateReference()) );
149 void ContactGroupLineEdit::updateView(
const Akonadi::Item &item,
const QString &preferredEmail )
151 if ( !item.hasPayload<KABC::Addressee>() ) {
155 const KABC::Addressee contact = item.payload<KABC::Addressee>();
157 QString email( preferredEmail );
158 if ( email.isEmpty() ) {
159 email = requestPreferredEmail( contact );
162 QString name = contact.formattedName();
163 if ( name.isEmpty() ) {
164 name = contact.assembledName();
167 if ( email.isEmpty() ) {
168 setText( QString::fromLatin1(
"%1" ).arg( name ) );
170 setText( QString::fromLatin1(
"%1 <%2>" ).arg( name ).arg( email ) );
173 mContactReference.setGid( contact.uid() );
174 mContactReference.setUid( QString::number( item.id() ) );
176 if ( contact.preferredEmail() != email ) {
177 mContactReference.setPreferredEmail( email );
181 QString ContactGroupLineEdit::requestPreferredEmail(
const KABC::Addressee &contact )
const
183 const QStringList emails = contact.emails();
185 if ( emails.isEmpty() ) {
186 qDebug(
"ContactGroupLineEdit::requestPreferredEmail(): Warning!!! no email addresses available" );
190 if ( emails.count() == 1 ) {
191 return emails.first();
197 menu.setTitle( i18n(
"Select preferred email address" ) );
198 const int numberOfEmails( emails.count() );
199 for (
int i = 0; i < numberOfEmails; ++i ) {
200 action = menu.addAction( emails.at( i ) );
201 action->setData( i );
204 action = menu.exec( mapToGlobal( QPoint( x() + width()/2, y() + height()/2 ) ) );
206 return emails.first();
209 return emails.at( action->data().toInt() );
212 #include "moc_contactgrouplineedit_p.cpp"
void fetchFullPayload(bool fetch=true)
Sets whether the full payload shall be fetched.
Item::List items() const
Returns the fetched items.
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Job that fetches items from the Akonadi storage.