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

akonadi/contact

  • akonadi
  • contact
contactviewer.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 "contactviewer.h"
23
24#include "contactmetadata_p.h"
25#include "contactmetadataattribute_p.h"
26#include "customfieldmanager_p.h"
27#include "standardcontactformatter.h"
28#include "textbrowser_p.h"
29
30#include "editor/im/improtocols.h"
31
32#include <akonadi/collection.h>
33#include <akonadi/collectionfetchjob.h>
34#include <akonadi/entitydisplayattribute.h>
35#include <akonadi/item.h>
36#include <akonadi/itemfetchscope.h>
37#include <kabc/addressee.h>
38#include <kcolorscheme.h>
39#include <kconfiggroup.h>
40#include <kglobal.h>
41#include <kicon.h>
42#include <klocalizedstring.h>
43#include <kstringhandler.h>
44
45#include <QVBoxLayout>
46
47#ifdef HAVE_PRISON
48#include <prison/QRCodeBarcode>
49#include <prison/DataMatrixBarcode>
50#include <kabc/vcardconverter.h>
51#endif // HAVE_PRISON
52
53using namespace Akonadi;
54
55class ContactViewer::Private
56{
57 public:
58 Private( ContactViewer *parent )
59 : mParent( parent ), mParentCollectionFetchJob( 0 )
60 {
61 mStandardContactFormatter = new StandardContactFormatter;
62 mContactFormatter = mStandardContactFormatter;
63#ifdef HAVE_PRISON
64 mQRCode = new prison::QRCodeBarcode();
65 mDataMatrix = new prison::DataMatrixBarcode();
66#endif // HAVE_PRISON
67 }
68
69 ~Private()
70 {
71 delete mStandardContactFormatter;
72#ifdef HAVE_PRISON
73 delete mQRCode;
74 delete mDataMatrix;
75#endif // HAVE_PRISON
76 }
77
78 void updateView( const QVariantList &localCustomFieldDescriptions = QVariantList(), const QString &addressBookName = QString() )
79 {
80 static QPixmap defaultPixmap = KIcon( QLatin1String( "user-identity" ) ).pixmap( QSize( 100, 100 ) );
81 static QPixmap defaultMapPixmap = KIcon( QLatin1String( "document-open-remote" ) ).pixmap( QSize( 16, 16 ) );
82 static QPixmap defaultSmsPixmap = KIcon( IMProtocols::self()->icon( QString::fromLatin1( "messaging/sms" ) ) ).pixmap( QSize( 16, 16 ) );
83 mParent->setWindowTitle( i18n( "Contact %1", mCurrentContact.assembledName() ) );
84
85 if ( mCurrentContact.photo().isIntern() ) {
86 mBrowser->document()->addResource( QTextDocument::ImageResource,
87 QUrl( QLatin1String( "contact_photo" ) ),
88 mCurrentContact.photo().data() );
89 } else if (!mCurrentContact.photo().url().isEmpty()) {
90 mBrowser->document()->addResource( QTextDocument::ImageResource,
91 QUrl( QLatin1String( "contact_photo" ) ),
92 defaultPixmap );
93 } else {
94 mBrowser->document()->addResource( QTextDocument::ImageResource,
95 QUrl( QLatin1String( "contact_photo" ) ),
96 defaultPixmap );
97 }
98
99 if ( mCurrentContact.logo().isIntern() ) {
100 mBrowser->document()->addResource( QTextDocument::ImageResource,
101 QUrl( QLatin1String( "contact_logo" ) ),
102 mCurrentContact.logo().data() );
103 } else if (!mCurrentContact.logo().url().isEmpty()) {
104 //TODO
105 }
106
107 mBrowser->document()->addResource( QTextDocument::ImageResource,
108 QUrl( QLatin1String( "map_icon" ) ),
109 defaultMapPixmap );
110
111 mBrowser->document()->addResource( QTextDocument::ImageResource,
112 QUrl( QLatin1String( "sms_icon" ) ),
113 defaultSmsPixmap);
114
115#ifdef HAVE_PRISON
116 KConfig config( QLatin1String( "akonadi_contactrc" ) );
117 KConfigGroup group( &config, QLatin1String( "View" ) );
118 if ( group.readEntry( "QRCodes", true ) ) {
119 KABC::VCardConverter converter;
120 KABC::Addressee addr( mCurrentContact );
121 addr.setPhoto( KABC::Picture() );
122 addr.setLogo( KABC::Picture() );
123 const QString data = QString::fromUtf8( converter.createVCard( addr ) );
124 mQRCode->setData( data );
125 mDataMatrix->setData( data );
126 mBrowser->document()->addResource( QTextDocument::ImageResource,
127 QUrl( QLatin1String( "qrcode" ) ),
128 mQRCode->toImage( QSizeF( 50, 50 ) ) );
129 mBrowser->document()->addResource( QTextDocument::ImageResource,
130 QUrl( QLatin1String( "datamatrix" ) ),
131 mDataMatrix->toImage( QSizeF( 50, 50 ) ) );
132 }
133#endif // HAVE_PRISON
134
135 // merge local and global custom field descriptions
136 QList<QVariantMap> customFieldDescriptions;
137 foreach ( const QVariant &entry, localCustomFieldDescriptions ) {
138 customFieldDescriptions << entry.toMap();
139 }
140
141 const CustomField::List globalCustomFields = CustomFieldManager::globalCustomFieldDescriptions();
142 foreach ( const CustomField &field, globalCustomFields ) {
143 QVariantMap description;
144 description.insert( QLatin1String( "key" ), field.key() );
145 description.insert( QLatin1String( "title" ), field.title() );
146
147 customFieldDescriptions << description;
148 }
149
150 KABC::Addressee contact( mCurrentContact );
151 if ( !addressBookName.isEmpty() ) {
152 contact.insertCustom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "AddressBook" ), addressBookName );
153 }
154
155 mContactFormatter->setContact( contact );
156 mContactFormatter->setCustomFieldDescriptions( customFieldDescriptions );
157
158 mBrowser->setHtml( mContactFormatter->toHtml() );
159 }
160
161 void slotMailClicked( const QString&, const QString &email )
162 {
163 QString name, address;
164
165 // remove the 'mailto:' and split into name and email address
166 KABC::Addressee::parseEmailAddress( email.mid( 7 ), name, address );
167
168 emit mParent->emailClicked( name, address );
169 }
170
171 void slotUrlClicked( const QString &urlString )
172 {
173 KUrl url( urlString );
174 const QString urlScheme( url.scheme() );
175 if ( urlScheme == QLatin1String( "http" ) ||
176 urlScheme == QLatin1String( "https" ) ) {
177 emit mParent->urlClicked( url );
178 } else if ( urlScheme == QLatin1String( "phone" ) ) {
179 const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
180
181 const KABC::PhoneNumber::List numbers = mCurrentContact.phoneNumbers();
182 if ( pos < numbers.count() ) {
183 emit mParent->phoneNumberClicked( numbers.at( pos ) );
184 }
185 } else if ( urlScheme == QLatin1String( "sms" ) ) {
186 const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
187
188 const KABC::PhoneNumber::List numbers = mCurrentContact.phoneNumbers();
189 if ( pos < numbers.count() ) {
190 emit mParent->smsClicked( numbers.at( pos ) );
191 }
192 } else if ( urlScheme == QLatin1String( "address" ) ) {
193 const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
194
195 const KABC::Address::List addresses = mCurrentContact.addresses();
196 if ( pos < addresses.count() ) {
197 emit mParent->addressClicked( addresses.at( pos ) );
198 }
199 }
200 }
201
202 void slotParentCollectionFetched( KJob *job )
203 {
204 mParentCollectionFetchJob = 0;
205
206 QString addressBookName;
207
208 if ( !job->error() ) {
209 CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
210 if ( !fetchJob->collections().isEmpty() ) {
211 const Collection collection = fetchJob->collections().first();
212 addressBookName = collection.displayName();
213 }
214 }
215
216 // load the local meta data of the item
217 ContactMetaData metaData;
218 metaData.load( mCurrentItem );
219
220 updateView( metaData.customFieldDescriptions(), addressBookName );
221 }
222
223 ContactViewer *mParent;
224 TextBrowser *mBrowser;
225 KABC::Addressee mCurrentContact;
226 Item mCurrentItem;
227 AbstractContactFormatter *mContactFormatter;
228 AbstractContactFormatter *mStandardContactFormatter;
229 CollectionFetchJob *mParentCollectionFetchJob;
230#ifdef HAVE_PRISON
231 prison::AbstractBarcode* mQRCode;
232 prison::AbstractBarcode* mDataMatrix;
233#endif // HAVE_PRISON
234};
235
236ContactViewer::ContactViewer( QWidget *parent )
237 : QWidget( parent ), d( new Private( this ) )
238{
239 QVBoxLayout *layout = new QVBoxLayout( this );
240 layout->setMargin( 0 );
241
242 d->mBrowser = new TextBrowser;
243 d->mBrowser->setNotifyClick( true );
244
245 connect( d->mBrowser, SIGNAL(mailClick(QString,QString)),
246 this, SLOT(slotMailClicked(QString,QString)) );
247 connect( d->mBrowser, SIGNAL(urlClick(QString)),
248 this, SLOT(slotUrlClicked(QString)) );
249
250 layout->addWidget( d->mBrowser );
251
252 // always fetch full payload for contacts
253 fetchScope().fetchFullPayload();
254 fetchScope().fetchAttribute<ContactMetaDataAttribute>();
255 fetchScope().setAncestorRetrieval( ItemFetchScope::Parent );
256}
257
258ContactViewer::~ContactViewer()
259{
260 delete d;
261}
262
263Akonadi::Item ContactViewer::contact() const
264{
265 return ItemMonitor::item();
266}
267
268KABC::Addressee ContactViewer::rawContact() const
269{
270 return d->mCurrentContact;
271}
272
273void ContactViewer::setContactFormatter( AbstractContactFormatter *formatter )
274{
275 if ( formatter == 0 ) {
276 d->mContactFormatter = d->mStandardContactFormatter;
277 } else {
278 d->mContactFormatter = formatter;
279 }
280}
281
282void ContactViewer::setContact( const Akonadi::Item &contact )
283{
284 ItemMonitor::setItem( contact );
285}
286
287void ContactViewer::setRawContact( const KABC::Addressee &contact )
288{
289 d->mCurrentContact = contact;
290
291 d->updateView();
292}
293
294void ContactViewer::itemChanged( const Item &contactItem )
295{
296 if ( !contactItem.hasPayload<KABC::Addressee>() ) {
297 return;
298 }
299
300 d->mCurrentItem = contactItem;
301 d->mCurrentContact = contactItem.payload<KABC::Addressee>();
302
303 // stop any running fetch job
304 if ( d->mParentCollectionFetchJob ) {
305 disconnect( d->mParentCollectionFetchJob, SIGNAL(result(KJob*)), this, SLOT(slotParentCollectionFetched(KJob*)) );
306 delete d->mParentCollectionFetchJob;
307 d->mParentCollectionFetchJob = 0;
308 }
309
310 d->mParentCollectionFetchJob = new CollectionFetchJob( contactItem.parentCollection(), CollectionFetchJob::Base, this );
311 connect( d->mParentCollectionFetchJob, SIGNAL(result(KJob*)), SLOT(slotParentCollectionFetched(KJob*)) );
312}
313
314void ContactViewer::itemRemoved()
315{
316 d->mBrowser->clear();
317}
318
319#include "moc_contactviewer.cpp"
Akonadi::AbstractContactFormatter
The interface for all contact formatters.
Definition abstractcontactformatter.h:47
Akonadi::AbstractContactFormatter::toHtml
virtual QString toHtml(HtmlForm form=SelfcontainedForm) const =0
This method must be reimplemented to return the contact formatted as HTML according to the requested ...
Akonadi::AbstractContactFormatter::setContact
void setContact(const KABC::Addressee &contact)
Sets the contact that will be formatted.
Definition abstractcontactformatter.cpp:47
Akonadi::AbstractContactFormatter::setCustomFieldDescriptions
void setCustomFieldDescriptions(const QList< QVariantMap > &descriptions)
Sets the custom field descriptions that will be used.
Definition abstractcontactformatter.cpp:67
Akonadi::ContactMetaDataAttribute
Attribute to store contact specific meta data.
Definition contactmetadataattribute_p.h:39
Akonadi::ContactMetaData
A helper class for storing contact specific settings.
Definition contactmetadata_p.h:37
Akonadi::ContactMetaData::customFieldDescriptions
QVariantList customFieldDescriptions() const
Returns the descriptions of the custom fields of the contact.
Definition contactmetadata.cpp:101
Akonadi::ContactMetaData::load
void load(const Akonadi::Item &contact)
Loads the meta data for the given contact.
Definition contactmetadata.cpp:52
Akonadi::ContactViewer
A viewer component for contacts in Akonadi.
Definition contactviewer.h:77
Akonadi::ContactViewer::emailClicked
void emailClicked(const QString &name, const QString &email)
This signal is emitted whenever the user has clicked on an email address in the viewer.
Akonadi::ContactViewer::rawContact
KABC::Addressee rawContact() const
Returns the raw contact that is currently displayed.
Definition contactviewer.cpp:268
Akonadi::ContactViewer::contact
Akonadi::Item contact() const
Returns the contact that is currently displayed.
Definition contactviewer.cpp:263
Akonadi::ContactViewer::phoneNumberClicked
void phoneNumberClicked(const KABC::PhoneNumber &number)
This signal is emitted whenever the user has clicked on a phone number (that includes fax numbers as ...
Akonadi::ContactViewer::~ContactViewer
~ContactViewer()
Destroys the contact viewer.
Definition contactviewer.cpp:258
Akonadi::ContactViewer::smsClicked
void smsClicked(const KABC::PhoneNumber &number)
This signal is emitted whenever the user has clicked on a SMS link of a phone number in the viewer.
Akonadi::ContactViewer::urlClicked
void urlClicked(const KUrl &url)
This signal is emitted whenever the user has clicked on a url (e.g.
Akonadi::ContactViewer::setRawContact
void setRawContact(const KABC::Addressee &contact)
Sets the raw contact object that shall be displayed in the viewer.
Definition contactviewer.cpp:287
Akonadi::ContactViewer::addressClicked
void addressClicked(const KABC::Address &address)
This signal is emitted whenever the user has clicked on an address in the viewer.
Akonadi::ContactViewer::setContact
void setContact(const Akonadi::Item &contact)
Sets the contact that shall be displayed in the viewer.
Definition contactviewer.cpp:282
Akonadi::ContactViewer::setContactFormatter
void setContactFormatter(AbstractContactFormatter *formatter)
Sets the contact formatter that should be used for formatting the contact.
Definition contactviewer.cpp:273
Akonadi::ContactViewer::ContactViewer
ContactViewer(QWidget *parent=0)
Creates a new contact viewer.
Definition contactviewer.cpp:236
Akonadi::StandardContactFormatter
A class that formats a contact as HTML code.
Definition standardcontactformatter.h:54
Akonadi::TextBrowser
A convenience class to remove the 'Copy Link Location' action from the context menu of KTextBrowser.
Definition textbrowser_p.h:35
CustomField
A class that represents non-standard contact fields.
Definition customfields_p.h:48
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