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

akonadi

  • akonadi
  • contact
contactgrouplineedit.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "contactgrouplineedit_p.h"
23 
24 #include "contactcompletionmodel_p.h"
25 
26 #include <akonadi/entitytreemodel.h>
27 #include <akonadi/itemfetchjob.h>
28 #include <akonadi/itemfetchscope.h>
29 #include <klocale.h>
30 
31 #include <QtCore/QAbstractItemModel>
32 #include <QtGui/QAction>
33 #include <QtGui/QCompleter>
34 #include <QtGui/QMenu>
35 
36 ContactGroupLineEdit::ContactGroupLineEdit( QWidget *parent )
37  : KLineEdit( parent ),
38  mCompleter( 0 ),
39  mContainsReference( false )
40 {
41  setClearButtonShown( true );
42 }
43 
44 void ContactGroupLineEdit::setCompletionModel( QAbstractItemModel *model )
45 {
46  mCompleter = new QCompleter( model, this );
47  mCompleter->setCompletionColumn( Akonadi::ContactCompletionModel::NameAndEmailColumn );
48  connect( mCompleter, SIGNAL(activated(QModelIndex)),
49  this, SLOT(autoCompleted(QModelIndex)) );
50 
51  setCompleter( mCompleter );
52 }
53 
54 bool ContactGroupLineEdit::containsReference() const
55 {
56  return mContainsReference;
57 }
58 
59 void ContactGroupLineEdit::setContactData( const KABC::ContactGroup::Data &groupData )
60 {
61  mContactData = groupData;
62  mContainsReference = false;
63 
64  setText( QString::fromLatin1( "%1 <%2>" ).arg( groupData.name() ).arg( groupData.email() ) );
65 }
66 
67 KABC::ContactGroup::Data ContactGroupLineEdit::contactData() const
68 {
69  QString fullName, email;
70  KABC::Addressee::parseEmailAddress( text(), fullName, email );
71 
72  if ( fullName.isEmpty() || email.isEmpty() )
73  return KABC::ContactGroup::Data();
74 
75  KABC::ContactGroup::Data groupData( mContactData );
76  groupData.setName( fullName );
77  groupData.setEmail( email );
78 
79  return groupData;
80 }
81 
82 void ContactGroupLineEdit::setContactReference( const KABC::ContactGroup::ContactReference &reference )
83 {
84  mContactReference = reference;
85  mContainsReference = true;
86 
87  disconnect( this, SIGNAL(textChanged(QString)), this, SLOT(invalidateReference()) );
88 
89  updateView( reference.uid(), reference.preferredEmail() );
90 }
91 
92 KABC::ContactGroup::ContactReference ContactGroupLineEdit::contactReference() const
93 {
94  return mContactReference;
95 }
96 
97 void ContactGroupLineEdit::autoCompleted( const QModelIndex &index )
98 {
99  if ( !index.isValid() )
100  return;
101 
102  const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
103  if ( !item.isValid() )
104  return;
105 
106  disconnect( this, SIGNAL(textChanged(QString)), this, SLOT(invalidateReference()) );
107  mContainsReference = true;
108 
109  updateView( item );
110 
111  connect( this, SIGNAL(textChanged(QString)), SLOT(invalidateReference()) );
112 }
113 
114 void ContactGroupLineEdit::invalidateReference()
115 {
116  disconnect( this, SIGNAL(textChanged(QString)), this, SLOT(invalidateReference()) );
117  mContainsReference = false;
118 }
119 
120 void ContactGroupLineEdit::updateView( const QString &uid, const QString &preferredEmail )
121 {
122  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( Akonadi::Item( uid.toLongLong() ) );
123  job->fetchScope().fetchFullPayload();
124  job->setProperty( "preferredEmail", preferredEmail );
125  connect( job, SIGNAL(result(KJob*)), SLOT(fetchDone(KJob*)) );
126 }
127 
128 void ContactGroupLineEdit::fetchDone( KJob *job )
129 {
130  Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
131 
132  if ( !fetchJob->items().isEmpty() ) {
133  const Akonadi::Item item = fetchJob->items().first();
134  updateView( item, fetchJob->property( "preferredEmail" ).toString() );
135  }
136 
137  connect( this, SIGNAL(textChanged(QString)), SLOT(invalidateReference()) );
138 }
139 
140 void ContactGroupLineEdit::updateView( const Akonadi::Item &item, const QString &preferredEmail )
141 {
142  if ( !item.hasPayload<KABC::Addressee>() )
143  return;
144 
145  const KABC::Addressee contact = item.payload<KABC::Addressee>();
146 
147  QString email( preferredEmail );
148  if ( email.isEmpty() )
149  email = requestPreferredEmail( contact );
150 
151  QString name = contact.formattedName();
152  if ( name.isEmpty() )
153  name = contact.assembledName();
154 
155  if ( email.isEmpty() )
156  setText( QString::fromLatin1( "%1" ).arg( name ) );
157  else
158  setText( QString::fromLatin1( "%1 <%2>" ).arg( name ).arg( email ) );
159 
160  mContactReference.setUid( QString::number( item.id() ) );
161 
162  if ( contact.preferredEmail() != email )
163  mContactReference.setPreferredEmail( email );
164 }
165 
166 QString ContactGroupLineEdit::requestPreferredEmail( const KABC::Addressee &contact ) const
167 {
168  const QStringList emails = contact.emails();
169 
170  if ( emails.isEmpty() ) {
171  qDebug( "ContactGroupLineEdit::requestPreferredEmail(): Warning!!! no email addresses available" );
172  return QString();
173  }
174 
175  if ( emails.count() == 1 )
176  return emails.first();
177 
178  QAction *action = 0;
179 
180  QMenu menu;
181  menu.setTitle( i18n( "Select preferred email address" ) );
182  const int numberOfEmails( emails.count() );
183  for ( int i = 0; i < numberOfEmails; ++i ) {
184  action = menu.addAction( emails.at( i ) );
185  action->setData( i );
186  }
187 
188  action = menu.exec( mapToGlobal( QPoint( x() + width()/2, y() + height()/2 ) ) );
189  if ( !action )
190  return emails.first(); // use preferred email
191 
192  return emails.at( action->data().toInt() );
193 }
194 
195 #include "contactgrouplineedit_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Nov 26 2012 16:48:19 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.9.3 API Reference

Skip menu "kdepimlibs-4.9.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