• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 <akonadi/collection.h>
31 #include <akonadi/collectionfetchjob.h>
32 #include <akonadi/entitydisplayattribute.h>
33 #include <akonadi/item.h>
34 #include <akonadi/itemfetchscope.h>
35 #include <kabc/addressee.h>
36 #include <kcolorscheme.h>
37 #include <kconfiggroup.h>
38 #include <kglobal.h>
39 #include <kicon.h>
40 #include <klocale.h>
41 #include <kstringhandler.h>
42 
43 #include <QVBoxLayout>
44 
45 #ifdef HAVE_PRISON
46 #include <prison/QRCodeBarcode>
47 #include <prison/DataMatrixBarcode>
48 #include <kabc/vcardconverter.h>
49 #endif // HAVE_PRISON
50 
51 using namespace Akonadi;
52 
53 class ContactViewer::Private
54 {
55  public:
56  Private( ContactViewer *parent )
57  : mParent( parent ), mParentCollectionFetchJob( 0 )
58  {
59  mStandardContactFormatter = new StandardContactFormatter;
60  mContactFormatter = mStandardContactFormatter;
61 #ifdef HAVE_PRISON
62  mQRCode = new prison::QRCodeBarcode();
63  mDataMatrix = new prison::DataMatrixBarcode();
64 #endif // HAVE_PRISON
65  }
66 
67  ~Private()
68  {
69  delete mStandardContactFormatter;
70 #ifdef HAVE_PRISON
71  delete mQRCode;
72  delete mDataMatrix;
73 #endif // HAVE_PRISON
74  }
75 
76  void updateView( const QVariantList &localCustomFieldDescriptions = QVariantList(), const QString &addressBookName = QString() )
77  {
78  static QPixmap defaultPixmap = KIcon( QLatin1String( "user-identity" ) ).pixmap( QSize( 100, 100 ) );
79 
80  mParent->setWindowTitle( i18n( "Contact %1", mCurrentContact.assembledName() ) );
81 
82  if ( mCurrentContact.photo().isIntern() ) {
83  mBrowser->document()->addResource( QTextDocument::ImageResource,
84  QUrl( QLatin1String( "contact_photo" ) ),
85  mCurrentContact.photo().data() );
86  } else {
87  mBrowser->document()->addResource( QTextDocument::ImageResource,
88  QUrl( QLatin1String( "contact_photo" ) ),
89  defaultPixmap );
90  }
91 
92  mBrowser->document()->addResource( QTextDocument::ImageResource,
93  QUrl( QLatin1String( "map_icon" ) ),
94  KIcon( QLatin1String( "document-open-remote" ) ).pixmap( QSize( 16, 16 ) ) );
95 
96 #ifdef HAVE_PRISON
97  KConfig config( QLatin1String( "akonadi_contactrc" ) );
98  KConfigGroup group( &config, QLatin1String( "View" ) );
99  if ( group.readEntry( "QRCodes", true ) ) {
100  KABC::VCardConverter converter;
101  KABC::Addressee addr( mCurrentContact );
102  addr.setPhoto( KABC::Picture() );
103  addr.setLogo( KABC::Picture() );
104  const QString data = QString::fromUtf8( converter.createVCard( addr ) );
105  mQRCode->setData( data );
106  mDataMatrix->setData( data );
107  mBrowser->document()->addResource( QTextDocument::ImageResource,
108  QUrl( QLatin1String( "qrcode" ) ),
109  mQRCode->toImage( QSizeF( 50, 50 ) ) );
110  mBrowser->document()->addResource( QTextDocument::ImageResource,
111  QUrl( QLatin1String( "datamatrix" ) ),
112  mDataMatrix->toImage( QSizeF( 50, 50 ) ) );
113  }
114 #endif // HAVE_PRISON
115 
116  // merge local and global custom field descriptions
117  QList<QVariantMap> customFieldDescriptions;
118  foreach ( const QVariant &entry, localCustomFieldDescriptions ) {
119  customFieldDescriptions << entry.toMap();
120  }
121 
122  const CustomField::List globalCustomFields = CustomFieldManager::globalCustomFieldDescriptions();
123  foreach ( const CustomField &field, globalCustomFields ) {
124  QVariantMap description;
125  description.insert( QLatin1String( "key" ), field.key() );
126  description.insert( QLatin1String( "title" ), field.title() );
127 
128  customFieldDescriptions << description;
129  }
130 
131  KABC::Addressee contact( mCurrentContact );
132  if ( !addressBookName.isEmpty() ) {
133  contact.insertCustom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "AddressBook" ), addressBookName );
134  }
135 
136  mContactFormatter->setContact( contact );
137  mContactFormatter->setCustomFieldDescriptions( customFieldDescriptions );
138 
139  mBrowser->setHtml( mContactFormatter->toHtml() );
140  }
141 
142  void slotMailClicked( const QString&, const QString &email )
143  {
144  QString name, address;
145 
146  // remove the 'mailto:' and split into name and email address
147  KABC::Addressee::parseEmailAddress( email.mid( 7 ), name, address );
148 
149  emit mParent->emailClicked( name, address );
150  }
151 
152  void slotUrlClicked( const QString &urlString )
153  {
154  KUrl url( urlString );
155  const QString urlScheme( url.scheme() );
156  if ( urlScheme == QLatin1String( "http" ) ||
157  urlScheme == QLatin1String( "https" ) ) {
158  emit mParent->urlClicked( url );
159  } else if ( urlScheme == QLatin1String( "phone" ) ) {
160  const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
161 
162  const KABC::PhoneNumber::List numbers = mCurrentContact.phoneNumbers();
163  if ( pos < numbers.count() ) {
164  emit mParent->phoneNumberClicked( numbers.at( pos ) );
165  }
166  } else if ( urlScheme == QLatin1String( "sms" ) ) {
167  const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
168 
169  const KABC::PhoneNumber::List numbers = mCurrentContact.phoneNumbers();
170  if ( pos < numbers.count() ) {
171  emit mParent->smsClicked( numbers.at( pos ) );
172  }
173  } else if ( urlScheme == QLatin1String( "address" ) ) {
174  const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
175 
176  const KABC::Address::List addresses = mCurrentContact.addresses();
177  if ( pos < addresses.count() ) {
178  emit mParent->addressClicked( addresses.at( pos ) );
179  }
180  }
181  }
182 
183  void slotParentCollectionFetched( KJob *job )
184  {
185  mParentCollectionFetchJob = 0;
186 
187  QString addressBookName;
188 
189  if ( !job->error() ) {
190  CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
191  if ( !fetchJob->collections().isEmpty() ) {
192  const Collection collection = fetchJob->collections().first();
193  if ( collection.hasAttribute<EntityDisplayAttribute>() ) {
194  addressBookName = collection.attribute<EntityDisplayAttribute>()->displayName();
195  } else {
196  addressBookName = collection.name();
197  }
198  }
199  }
200 
201  // load the local meta data of the item
202  ContactMetaData metaData;
203  metaData.load( mCurrentItem );
204 
205  updateView( metaData.customFieldDescriptions(), addressBookName );
206  }
207 
208  ContactViewer *mParent;
209  TextBrowser *mBrowser;
210  KABC::Addressee mCurrentContact;
211  Item mCurrentItem;
212  AbstractContactFormatter *mContactFormatter;
213  AbstractContactFormatter *mStandardContactFormatter;
214  CollectionFetchJob *mParentCollectionFetchJob;
215 #ifdef HAVE_PRISON
216  prison::AbstractBarcode* mQRCode;
217  prison::AbstractBarcode* mDataMatrix;
218 #endif // HAVE_PRISON
219 };
220 
221 ContactViewer::ContactViewer( QWidget *parent )
222  : QWidget( parent ), d( new Private( this ) )
223 {
224  QVBoxLayout *layout = new QVBoxLayout( this );
225  layout->setMargin( 0 );
226 
227  d->mBrowser = new TextBrowser;
228  d->mBrowser->setNotifyClick( true );
229 
230  connect( d->mBrowser, SIGNAL(mailClick(QString,QString)),
231  this, SLOT(slotMailClicked(QString,QString)) );
232  connect( d->mBrowser, SIGNAL(urlClick(QString)),
233  this, SLOT(slotUrlClicked(QString)) );
234 
235  layout->addWidget( d->mBrowser );
236 
237  // always fetch full payload for contacts
238  fetchScope().fetchFullPayload();
239  fetchScope().fetchAttribute<ContactMetaDataAttribute>();
240  fetchScope().setAncestorRetrieval( ItemFetchScope::Parent );
241 }
242 
243 ContactViewer::~ContactViewer()
244 {
245  delete d;
246 }
247 
248 Akonadi::Item ContactViewer::contact() const
249 {
250  return ItemMonitor::item();
251 }
252 
253 KABC::Addressee ContactViewer::rawContact() const
254 {
255  return d->mCurrentContact;
256 }
257 
258 void ContactViewer::setContactFormatter( AbstractContactFormatter *formatter )
259 {
260  if ( formatter == 0 ) {
261  d->mContactFormatter = d->mStandardContactFormatter;
262  } else {
263  d->mContactFormatter = formatter;
264  }
265 }
266 
267 void ContactViewer::setContact( const Akonadi::Item &contact )
268 {
269  ItemMonitor::setItem( contact );
270 }
271 
272 void ContactViewer::setRawContact( const KABC::Addressee &contact )
273 {
274  d->mCurrentContact = contact;
275 
276  d->updateView();
277 }
278 
279 void ContactViewer::itemChanged( const Item &contactItem )
280 {
281  if ( !contactItem.hasPayload<KABC::Addressee>() ) {
282  return;
283  }
284 
285  d->mCurrentItem = contactItem;
286  d->mCurrentContact = contactItem.payload<KABC::Addressee>();
287 
288  // stop any running fetch job
289  if ( d->mParentCollectionFetchJob ) {
290  disconnect( d->mParentCollectionFetchJob, SIGNAL(result(KJob*)), this, SLOT(slotParentCollectionFetched(KJob*)) );
291  delete d->mParentCollectionFetchJob;
292  d->mParentCollectionFetchJob = 0;
293  }
294 
295  d->mParentCollectionFetchJob = new CollectionFetchJob( contactItem.parentCollection(), CollectionFetchJob::Base, this );
296  connect( d->mParentCollectionFetchJob, SIGNAL(result(KJob*)), SLOT(slotParentCollectionFetched(KJob*)) );
297 }
298 
299 void ContactViewer::itemRemoved()
300 {
301  d->mBrowser->clear();
302 }
303 
304 #include "moc_contactviewer.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:28:42 by doxygen 1.8.3.1 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.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