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

akonadi/contact

  • 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
36ContactGroupLineEdit::ContactGroupLineEdit( QWidget *parent )
37 : KLineEdit( parent ),
38 mCompleter( 0 ),
39 mContainsReference( false )
40{
41 setClearButtonShown( true );
42}
43
44void 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
54bool ContactGroupLineEdit::containsReference() const
55{
56 return mContainsReference;
57}
58
59void 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
67KABC::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
83void 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
93KABC::ContactGroup::ContactReference ContactGroupLineEdit::contactReference() const
94{
95 return mContactReference;
96}
97
98void 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
117void ContactGroupLineEdit::invalidateReference()
118{
119 disconnect( this, SIGNAL(textChanged(QString)), this, SLOT(invalidateReference()) );
120 mContainsReference = false;
121}
122
123void 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
137void 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
149void 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
181QString 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"
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Wed Jan 24 2024 00:00:00 by doxygen 1.10.0 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.14.10 API Reference

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