• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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 <QtGui/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  const CustomField::List globalCustomFields = CustomFieldManager::globalCustomFieldDescriptions();
122  foreach ( const CustomField &field, globalCustomFields ) {
123  QVariantMap description;
124  description.insert( QLatin1String( "key" ), field.key() );
125  description.insert( QLatin1String( "title" ), field.title() );
126 
127  customFieldDescriptions << description;
128  }
129 
130  KABC::Addressee contact( mCurrentContact );
131  if ( !addressBookName.isEmpty() ) {
132  contact.insertCustom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "AddressBook" ), addressBookName );
133  }
134 
135  mContactFormatter->setContact( contact );
136  mContactFormatter->setCustomFieldDescriptions( customFieldDescriptions );
137 
138  mBrowser->setHtml( mContactFormatter->toHtml() );
139  }
140 
141  void slotMailClicked( const QString&, const QString &email )
142  {
143  QString name, address;
144 
145  // remove the 'mailto:' and split into name and email address
146  KABC::Addressee::parseEmailAddress( email.mid( 7 ), name, address );
147 
148  emit mParent->emailClicked( name, address );
149  }
150 
151  void slotUrlClicked( const QString &urlString )
152  {
153  KUrl url( urlString );
154  const QString urlScheme( url.scheme() );
155  if ( urlScheme == QLatin1String( "http" ) ||
156  urlScheme == QLatin1String( "https" ) ) {
157  emit mParent->urlClicked( url );
158  } else if ( urlScheme == QLatin1String( "phone" ) ) {
159  const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
160 
161  const KABC::PhoneNumber::List numbers = mCurrentContact.phoneNumbers();
162  if ( pos < numbers.count() ) {
163  emit mParent->phoneNumberClicked( numbers.at( pos ) );
164  }
165  } else if ( urlScheme == QLatin1String( "sms" ) ) {
166  const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
167 
168  const KABC::PhoneNumber::List numbers = mCurrentContact.phoneNumbers();
169  if ( pos < numbers.count() ) {
170  emit mParent->smsClicked( numbers.at( pos ) );
171  }
172  } else if ( urlScheme == QLatin1String( "address" ) ) {
173  const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
174 
175  const KABC::Address::List addresses = mCurrentContact.addresses();
176  if ( pos < addresses.count() ) {
177  emit mParent->addressClicked( addresses.at( pos ) );
178  }
179  }
180  }
181 
182  void slotParentCollectionFetched( KJob *job )
183  {
184  mParentCollectionFetchJob = 0;
185 
186  QString addressBookName;
187 
188  if ( !job->error() ) {
189  CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
190  if ( !fetchJob->collections().isEmpty() ) {
191  const Collection collection = fetchJob->collections().first();
192  if ( collection.hasAttribute<EntityDisplayAttribute>() )
193  addressBookName = collection.attribute<EntityDisplayAttribute>()->displayName();
194  else
195  addressBookName = collection.name();
196  }
197  }
198 
199  // load the local meta data of the item
200  ContactMetaData metaData;
201  metaData.load( mCurrentItem );
202 
203  updateView( metaData.customFieldDescriptions(), addressBookName );
204  }
205 
206  ContactViewer *mParent;
207  TextBrowser *mBrowser;
208  KABC::Addressee mCurrentContact;
209  Item mCurrentItem;
210  AbstractContactFormatter *mContactFormatter;
211  AbstractContactFormatter *mStandardContactFormatter;
212  CollectionFetchJob *mParentCollectionFetchJob;
213 #ifdef HAVE_PRISON
214  prison::AbstractBarcode* mQRCode;
215  prison::AbstractBarcode* mDataMatrix;
216 #endif // HAVE_PRISON
217 };
218 
219 ContactViewer::ContactViewer( QWidget *parent )
220  : QWidget( parent ), d( new Private( this ) )
221 {
222  QVBoxLayout *layout = new QVBoxLayout( this );
223  layout->setMargin( 0 );
224 
225  d->mBrowser = new TextBrowser;
226  d->mBrowser->setNotifyClick( true );
227 
228  connect( d->mBrowser, SIGNAL(mailClick(QString,QString)),
229  this, SLOT(slotMailClicked(QString,QString)) );
230  connect( d->mBrowser, SIGNAL(urlClick(QString)),
231  this, SLOT(slotUrlClicked(QString)) );
232 
233  layout->addWidget( d->mBrowser );
234 
235  // always fetch full payload for contacts
236  fetchScope().fetchFullPayload();
237  fetchScope().fetchAttribute<ContactMetaDataAttribute>();
238  fetchScope().setAncestorRetrieval( ItemFetchScope::Parent );
239 }
240 
241 ContactViewer::~ContactViewer()
242 {
243  delete d;
244 }
245 
246 Akonadi::Item ContactViewer::contact() const
247 {
248  return ItemMonitor::item();
249 }
250 
251 KABC::Addressee ContactViewer::rawContact() const
252 {
253  return d->mCurrentContact;
254 }
255 
256 void ContactViewer::setContactFormatter( AbstractContactFormatter *formatter )
257 {
258  if ( formatter == 0 )
259  d->mContactFormatter = d->mStandardContactFormatter;
260  else
261  d->mContactFormatter = formatter;
262 }
263 
264 void ContactViewer::setContact( const Akonadi::Item &contact )
265 {
266  ItemMonitor::setItem( contact );
267 }
268 
269 void ContactViewer::setRawContact( const KABC::Addressee &contact )
270 {
271  d->mCurrentContact = contact;
272 
273  d->updateView();
274 }
275 
276 void ContactViewer::itemChanged( const Item &contactItem )
277 {
278  if ( !contactItem.hasPayload<KABC::Addressee>() )
279  return;
280 
281  d->mCurrentItem = contactItem;
282  d->mCurrentContact = contactItem.payload<KABC::Addressee>();
283 
284  // stop any running fetch job
285  if ( d->mParentCollectionFetchJob ) {
286  disconnect( d->mParentCollectionFetchJob, SIGNAL(result(KJob*)), this, SLOT(slotParentCollectionFetched(KJob*)) );
287  delete d->mParentCollectionFetchJob;
288  d->mParentCollectionFetchJob = 0;
289  }
290 
291  d->mParentCollectionFetchJob = new CollectionFetchJob( contactItem.parentCollection(), CollectionFetchJob::Base, this );
292  connect( d->mParentCollectionFetchJob, SIGNAL(result(KJob*)), SLOT(slotParentCollectionFetched(KJob*)) );
293 }
294 
295 void ContactViewer::itemRemoved()
296 {
297  d->mBrowser->clear();
298 }
299 
300 #include "contactviewer.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:36:30 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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