• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi/contact

contactgrouplineedit.cpp
00001 /*
00002     This file is part of Akonadi Contact.
00003 
00004     Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or modify it
00007     under the terms of the GNU Library General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or (at your
00009     option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful, but WITHOUT
00012     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014     License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to the
00018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301, USA.
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(QModelIndex)),
00049            this, SLOT(autoCompleted(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(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(QString)), this, SLOT(invalidateReference()) );
00107   mContainsReference = true;
00108 
00109   updateView( item );
00110 
00111   connect( this, SIGNAL(textChanged(QString)), SLOT(invalidateReference()) );
00112 }
00113 
00114 void ContactGroupLineEdit::invalidateReference()
00115 {
00116   disconnect( this, SIGNAL(textChanged(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(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   const int numberOfEmails( emails.count() );
00183   for ( int i = 0; i < numberOfEmails; ++i ) {
00184     action = menu.addAction( emails.at( i ) );
00185     action->setData( i );
00186   }
00187 
00188   action = menu.exec( mapToGlobal( QPoint( x() + width()/2, y() + height()/2 ) ) );
00189   if ( !action )
00190     return emails.first(); // use preferred email
00191 
00192   return emails.at( action->data().toInt() );
00193 }
00194 
00195 #include "contactgrouplineedit_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:01:43 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal