• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 <QAction>
33 #include <QCompleter>
34 #include <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 
76  KABC::ContactGroup::Data groupData( mContactData );
77  groupData.setName( fullName );
78  groupData.setEmail( email );
79 
80  return groupData;
81 }
82 
83 void ContactGroupLineEdit::setContactReference( const KABC::ContactGroup::ContactReference &reference )
84 {
85  mContactReference = reference;
86  mContainsReference = true;
87 
88  disconnect( this, SIGNAL(textChanged(QString)), this, SLOT(invalidateReference()) );
89 
90  updateView( reference.uid(), reference.preferredEmail() );
91 }
92 
93 KABC::ContactGroup::ContactReference ContactGroupLineEdit::contactReference() const
94 {
95  return mContactReference;
96 }
97 
98 void ContactGroupLineEdit::autoCompleted( const QModelIndex &index )
99 {
100  if ( !index.isValid() ) {
101  return;
102  }
103 
104  const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
105  if ( !item.isValid() ) {
106  return;
107  }
108 
109  disconnect( this, SIGNAL(textChanged(QString)), this, SLOT(invalidateReference()) );
110  mContainsReference = true;
111 
112  updateView( item );
113 
114  connect( this, SIGNAL(textChanged(QString)), SLOT(invalidateReference()) );
115 }
116 
117 void ContactGroupLineEdit::invalidateReference()
118 {
119  disconnect( this, SIGNAL(textChanged(QString)), this, SLOT(invalidateReference()) );
120  mContainsReference = false;
121 }
122 
123 void ContactGroupLineEdit::updateView( const QString &uid, const QString &preferredEmail )
124 {
125  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( Akonadi::Item( uid.toLongLong() ) );
126  job->fetchScope().fetchFullPayload();
127  job->setProperty( "preferredEmail", preferredEmail );
128  connect( job, SIGNAL(result(KJob*)), SLOT(fetchDone(KJob*)) );
129 }
130 
131 void ContactGroupLineEdit::fetchDone( KJob *job )
132 {
133  Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
134 
135  if ( !fetchJob->items().isEmpty() ) {
136  const Akonadi::Item item = fetchJob->items().first();
137  updateView( item, fetchJob->property( "preferredEmail" ).toString() );
138  }
139 
140  connect( this, SIGNAL(textChanged(QString)), SLOT(invalidateReference()) );
141 }
142 
143 void ContactGroupLineEdit::updateView( const Akonadi::Item &item, const QString &preferredEmail )
144 {
145  if ( !item.hasPayload<KABC::Addressee>() ) {
146  return;
147  }
148 
149  const KABC::Addressee contact = item.payload<KABC::Addressee>();
150 
151  QString email( preferredEmail );
152  if ( email.isEmpty() ) {
153  email = requestPreferredEmail( contact );
154  }
155 
156  QString name = contact.formattedName();
157  if ( name.isEmpty() ) {
158  name = contact.assembledName();
159  }
160 
161  if ( email.isEmpty() ) {
162  setText( QString::fromLatin1( "%1" ).arg( name ) );
163  } else {
164  setText( QString::fromLatin1( "%1 <%2>" ).arg( name ).arg( email ) );
165  }
166 
167  mContactReference.setUid( QString::number( item.id() ) );
168 
169  if ( contact.preferredEmail() != email ) {
170  mContactReference.setPreferredEmail( email );
171  }
172 }
173 
174 QString ContactGroupLineEdit::requestPreferredEmail( const KABC::Addressee &contact ) const
175 {
176  const QStringList emails = contact.emails();
177 
178  if ( emails.isEmpty() ) {
179  qDebug( "ContactGroupLineEdit::requestPreferredEmail(): Warning!!! no email addresses available" );
180  return QString();
181  }
182 
183  if ( emails.count() == 1 ) {
184  return emails.first();
185  }
186 
187  QAction *action = 0;
188 
189  QMenu menu;
190  menu.setTitle( i18n( "Select preferred email address" ) );
191  const int numberOfEmails( emails.count() );
192  for ( int i = 0; i < numberOfEmails; ++i ) {
193  action = menu.addAction( emails.at( i ) );
194  action->setData( i );
195  }
196 
197  action = menu.exec( mapToGlobal( QPoint( x() + width()/2, y() + height()/2 ) ) );
198  if ( !action ) {
199  return emails.first(); // use preferred email
200  }
201 
202  return emails.at( action->data().toInt() );
203 }
204 
205 #include "moc_contactgrouplineedit_p.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:34 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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