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 );
47 mCompleter->setCompletionColumn( Akonadi::ContactCompletionModel::NameAndEmailColumn );
48 connect( mCompleter, SIGNAL(activated(
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
70 KABC::Addressee::parseEmailAddress( text(), fullName, email );
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 )
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 );
159 email = requestPreferredEmail( contact );
162 QString name = contact.formattedName();
164 name = contact.assembledName();
173 mContactReference.setGid( contact.uid() );
176 if ( contact.preferredEmail() != email ) {
177 mContactReference.setPreferredEmail( email );
181 QString ContactGroupLineEdit::requestPreferredEmail(
const KABC::Addressee &contact )
const
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 ) {
204 action = menu.
exec( mapToGlobal(
QPoint( x() + width()/2, y() + height()/2 ) ) );
206 return emails.
first();
212 #include "moc_contactgrouplineedit_p.cpp"
const T & at(int i) const
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.
QString number(int n, int base)
int count(const T &value) const
int toInt(bool *ok) const
void setData(const QVariant &userData)
QVariant data(int role) const
Job that fetches items from the Akonadi storage.
QString fromLatin1(const char *str, int size)