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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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 <klocalizedstring.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 );
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 KABC::ContactGroup::ContactReference &reference )
124 {
125  Akonadi::Item item;
126  if ( !reference.gid().isEmpty() ) {
127  item.setGid( reference.gid() );
128  } else {
129  item.setId( reference.uid().toLongLong() );
130  }
131  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( item );
132  job->fetchScope().fetchFullPayload();
133  job->setProperty( "preferredEmail", reference.preferredEmail() );
134  connect( job, SIGNAL(result(KJob*)), SLOT(fetchDone(KJob*)) );
135 }
136 
137 void ContactGroupLineEdit::fetchDone( KJob *job )
138 {
139  Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
140 
141  if ( !fetchJob->items().isEmpty() ) {
142  const Akonadi::Item item = fetchJob->items().first();
143  updateView( item, fetchJob->property( "preferredEmail" ).toString() );
144  }
145 
146  connect( this, SIGNAL(textChanged(QString)), SLOT(invalidateReference()) );
147 }
148 
149 void ContactGroupLineEdit::updateView( const Akonadi::Item &item, const QString &preferredEmail )
150 {
151  if ( !item.hasPayload<KABC::Addressee>() ) {
152  return;
153  }
154 
155  const KABC::Addressee contact = item.payload<KABC::Addressee>();
156 
157  QString email( preferredEmail );
158  if ( email.isEmpty() ) {
159  email = requestPreferredEmail( contact );
160  }
161 
162  QString name = contact.formattedName();
163  if ( name.isEmpty() ) {
164  name = contact.assembledName();
165  }
166 
167  if ( email.isEmpty() ) {
168  setText( QString::fromLatin1( "%1" ).arg( name ) );
169  } else {
170  setText( QString::fromLatin1( "%1 <%2>" ).arg( name ).arg( email ) );
171  }
172 
173  mContactReference.setGid( contact.uid() );
174  mContactReference.setUid( QString::number( item.id() ) );
175 
176  if ( contact.preferredEmail() != email ) {
177  mContactReference.setPreferredEmail( email );
178  }
179 }
180 
181 QString ContactGroupLineEdit::requestPreferredEmail( const KABC::Addressee &contact ) const
182 {
183  const QStringList emails = contact.emails();
184 
185  if ( emails.isEmpty() ) {
186  qDebug( "ContactGroupLineEdit::requestPreferredEmail(): Warning!!! no email addresses available" );
187  return QString();
188  }
189 
190  if ( emails.count() == 1 ) {
191  return emails.first();
192  }
193 
194  QAction *action = 0;
195 
196  QMenu menu;
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 );
202  }
203 
204  action = menu.exec( mapToGlobal( QPoint( x() + width()/2, y() + height()/2 ) ) );
205  if ( !action ) {
206  return emails.first(); // use preferred email
207  }
208 
209  return emails.at( action->data().toInt() );
210 }
211 
212 #include "moc_contactgrouplineedit_p.cpp"
Akonadi::ItemFetchScope::fetchFullPayload
void fetchFullPayload(bool fetch=true)
Sets whether the full payload shall be fetched.
Definition: itemfetchscope.cpp:68
Akonadi::ItemFetchJob::items
Item::List items() const
Returns the fetched items.
Definition: itemfetchjob.cpp:228
Akonadi::ItemFetchJob::fetchScope
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Definition: itemfetchjob.cpp:256
Akonadi::EntityTreeModel::ItemRole
The Item.
Definition: entitytreemodel.h:331
Akonadi::ItemFetchJob
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:82
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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