• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 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 
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  mBrowser->document()->addResource( QTextDocument::ImageResource,
95  QUrl( QLatin1String( "map_icon" ) ),
96  KIcon( QLatin1String( "document-open-remote" ) ).pixmap( QSize( 16, 16 ) ) );
97 
98  mBrowser->document()->addResource( QTextDocument::ImageResource,
99  QUrl( QLatin1String( "sms_icon" ) ),
100  KIcon( IMProtocols::self()->icon( QString::fromLatin1( "messaging/sms" ) ) ).pixmap( QSize( 16, 16 ) ) );
101 
102 #ifdef HAVE_PRISON
103  KConfig config( QLatin1String( "akonadi_contactrc" ) );
104  KConfigGroup group( &config, QLatin1String( "View" ) );
105  if ( group.readEntry( "QRCodes", true ) ) {
106  KABC::VCardConverter converter;
107  KABC::Addressee addr( mCurrentContact );
108  addr.setPhoto( KABC::Picture() );
109  addr.setLogo( KABC::Picture() );
110  const QString data = QString::fromUtf8( converter.createVCard( addr ) );
111  mQRCode->setData( data );
112  mDataMatrix->setData( data );
113  mBrowser->document()->addResource( QTextDocument::ImageResource,
114  QUrl( QLatin1String( "qrcode" ) ),
115  mQRCode->toImage( QSizeF( 50, 50 ) ) );
116  mBrowser->document()->addResource( QTextDocument::ImageResource,
117  QUrl( QLatin1String( "datamatrix" ) ),
118  mDataMatrix->toImage( QSizeF( 50, 50 ) ) );
119  }
120 #endif // HAVE_PRISON
121 
122  // merge local and global custom field descriptions
123  QList<QVariantMap> customFieldDescriptions;
124  foreach ( const QVariant &entry, localCustomFieldDescriptions ) {
125  customFieldDescriptions << entry.toMap();
126  }
127 
128  const CustomField::List globalCustomFields = CustomFieldManager::globalCustomFieldDescriptions();
129  foreach ( const CustomField &field, globalCustomFields ) {
130  QVariantMap description;
131  description.insert( QLatin1String( "key" ), field.key() );
132  description.insert( QLatin1String( "title" ), field.title() );
133 
134  customFieldDescriptions << description;
135  }
136 
137  KABC::Addressee contact( mCurrentContact );
138  if ( !addressBookName.isEmpty() ) {
139  contact.insertCustom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "AddressBook" ), addressBookName );
140  }
141 
142  mContactFormatter->setContact( contact );
143  mContactFormatter->setCustomFieldDescriptions( customFieldDescriptions );
144 
145  mBrowser->setHtml( mContactFormatter->toHtml() );
146  }
147 
148  void slotMailClicked( const QString&, const QString &email )
149  {
150  QString name, address;
151 
152  // remove the 'mailto:' and split into name and email address
153  KABC::Addressee::parseEmailAddress( email.mid( 7 ), name, address );
154 
155  emit mParent->emailClicked( name, address );
156  }
157 
158  void slotUrlClicked( const QString &urlString )
159  {
160  KUrl url( urlString );
161  const QString urlScheme( url.scheme() );
162  if ( urlScheme == QLatin1String( "http" ) ||
163  urlScheme == QLatin1String( "https" ) ) {
164  emit mParent->urlClicked( url );
165  } else if ( urlScheme == QLatin1String( "phone" ) ) {
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->phoneNumberClicked( numbers.at( pos ) );
171  }
172  } else if ( urlScheme == QLatin1String( "sms" ) ) {
173  const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
174 
175  const KABC::PhoneNumber::List numbers = mCurrentContact.phoneNumbers();
176  if ( pos < numbers.count() ) {
177  emit mParent->smsClicked( numbers.at( pos ) );
178  }
179  } else if ( urlScheme == QLatin1String( "address" ) ) {
180  const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt();
181 
182  const KABC::Address::List addresses = mCurrentContact.addresses();
183  if ( pos < addresses.count() ) {
184  emit mParent->addressClicked( addresses.at( pos ) );
185  }
186  }
187  }
188 
189  void slotParentCollectionFetched( KJob *job )
190  {
191  mParentCollectionFetchJob = 0;
192 
193  QString addressBookName;
194 
195  if ( !job->error() ) {
196  CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
197  if ( !fetchJob->collections().isEmpty() ) {
198  const Collection collection = fetchJob->collections().first();
199  addressBookName = collection.displayName();
200  }
201  }
202 
203  // load the local meta data of the item
204  ContactMetaData metaData;
205  metaData.load( mCurrentItem );
206 
207  updateView( metaData.customFieldDescriptions(), addressBookName );
208  }
209 
210  ContactViewer *mParent;
211  TextBrowser *mBrowser;
212  KABC::Addressee mCurrentContact;
213  Item mCurrentItem;
214  AbstractContactFormatter *mContactFormatter;
215  AbstractContactFormatter *mStandardContactFormatter;
216  CollectionFetchJob *mParentCollectionFetchJob;
217 #ifdef HAVE_PRISON
218  prison::AbstractBarcode* mQRCode;
219  prison::AbstractBarcode* mDataMatrix;
220 #endif // HAVE_PRISON
221 };
222 
223 ContactViewer::ContactViewer( QWidget *parent )
224  : QWidget( parent ), d( new Private( this ) )
225 {
226  QVBoxLayout *layout = new QVBoxLayout( this );
227  layout->setMargin( 0 );
228 
229  d->mBrowser = new TextBrowser;
230  d->mBrowser->setNotifyClick( true );
231 
232  connect( d->mBrowser, SIGNAL(mailClick(QString,QString)),
233  this, SLOT(slotMailClicked(QString,QString)) );
234  connect( d->mBrowser, SIGNAL(urlClick(QString)),
235  this, SLOT(slotUrlClicked(QString)) );
236 
237  layout->addWidget( d->mBrowser );
238 
239  // always fetch full payload for contacts
240  fetchScope().fetchFullPayload();
241  fetchScope().fetchAttribute<ContactMetaDataAttribute>();
242  fetchScope().setAncestorRetrieval( ItemFetchScope::Parent );
243 }
244 
245 ContactViewer::~ContactViewer()
246 {
247  delete d;
248 }
249 
250 Akonadi::Item ContactViewer::contact() const
251 {
252  return ItemMonitor::item();
253 }
254 
255 KABC::Addressee ContactViewer::rawContact() const
256 {
257  return d->mCurrentContact;
258 }
259 
260 void ContactViewer::setContactFormatter( AbstractContactFormatter *formatter )
261 {
262  if ( formatter == 0 ) {
263  d->mContactFormatter = d->mStandardContactFormatter;
264  } else {
265  d->mContactFormatter = formatter;
266  }
267 }
268 
269 void ContactViewer::setContact( const Akonadi::Item &contact )
270 {
271  ItemMonitor::setItem( contact );
272 }
273 
274 void ContactViewer::setRawContact( const KABC::Addressee &contact )
275 {
276  d->mCurrentContact = contact;
277 
278  d->updateView();
279 }
280 
281 void ContactViewer::itemChanged( const Item &contactItem )
282 {
283  if ( !contactItem.hasPayload<KABC::Addressee>() ) {
284  return;
285  }
286 
287  d->mCurrentItem = contactItem;
288  d->mCurrentContact = contactItem.payload<KABC::Addressee>();
289 
290  // stop any running fetch job
291  if ( d->mParentCollectionFetchJob ) {
292  disconnect( d->mParentCollectionFetchJob, SIGNAL(result(KJob*)), this, SLOT(slotParentCollectionFetched(KJob*)) );
293  delete d->mParentCollectionFetchJob;
294  d->mParentCollectionFetchJob = 0;
295  }
296 
297  d->mParentCollectionFetchJob = new CollectionFetchJob( contactItem.parentCollection(), CollectionFetchJob::Base, this );
298  connect( d->mParentCollectionFetchJob, SIGNAL(result(KJob*)), SLOT(slotParentCollectionFetched(KJob*)) );
299 }
300 
301 void ContactViewer::itemRemoved()
302 {
303  d->mBrowser->clear();
304 }
305 
306 #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:223
Akonadi::ContactMetaData::load
void load(const Akonadi::Item &contact)
Loads the meta data for the given contact.
Definition: contactmetadata.cpp:52
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:250
Akonadi::ContactViewer::~ContactViewer
~ContactViewer()
Destroys the contact viewer.
Definition: contactviewer.cpp:245
Akonadi::TextBrowser
A convenience class to remove the &#39;Copy Link Location&#39; 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::ContactMetaData
A helper class for storing contact specific settings.
Definition: contactmetadata_p.h:36
Akonadi::ContactViewer::setContact
void setContact(const Akonadi::Item &contact)
Sets the contact that shall be displayed in the viewer.
Definition: contactviewer.cpp:269
Akonadi::ContactViewer::setContactFormatter
void setContactFormatter(AbstractContactFormatter *formatter)
Sets the contact formatter that should be used for formatting the contact.
Definition: contactviewer.cpp:260
Akonadi::StandardContactFormatter
A class that formats a contact as HTML code.
Definition: standardcontactformatter.h:53
Akonadi::ContactViewer
A viewer component for contacts in Akonadi.
Definition: contactviewer.h:76
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:255
Akonadi::ContactViewer::setRawContact
void setRawContact(const KABC::Addressee &contact)
Sets the raw contact object that shall be displayed in the viewer.
Definition: contactviewer.cpp:274
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:47 by doxygen 1.8.5 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.11.3 API Reference

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