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