akonadi/contact
contactviewer.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 00020 */ 00021 00022 #include "contactviewer.h" 00023 00024 #include "contactmetadata_p.h" 00025 #include "contactmetadataattribute_p.h" 00026 #include "customfieldmanager_p.h" 00027 #include "standardcontactformatter.h" 00028 #include "textbrowser_p.h" 00029 00030 #include <akonadi/collection.h> 00031 #include <akonadi/collectionfetchjob.h> 00032 #include <akonadi/entitydisplayattribute.h> 00033 #include <akonadi/item.h> 00034 #include <akonadi/itemfetchscope.h> 00035 #include <kabc/addressee.h> 00036 #include <kcolorscheme.h> 00037 #include <kconfiggroup.h> 00038 #include <kglobal.h> 00039 #include <kicon.h> 00040 #include <klocale.h> 00041 #include <kstringhandler.h> 00042 00043 #include <QtGui/QVBoxLayout> 00044 00045 #ifdef HAVE_PRISON 00046 #include <prison/QRCodeBarcode> 00047 #include <prison/DataMatrixBarcode> 00048 #include <kabc/vcardconverter.h> 00049 #endif // HAVE_PRISON 00050 00051 using namespace Akonadi; 00052 00053 class ContactViewer::Private 00054 { 00055 public: 00056 Private( ContactViewer *parent ) 00057 : mParent( parent ), mParentCollectionFetchJob( 0 ) 00058 { 00059 mStandardContactFormatter = new StandardContactFormatter; 00060 mContactFormatter = mStandardContactFormatter; 00061 #ifdef HAVE_PRISON 00062 mQRCode = new prison::QRCodeBarcode(); 00063 mDataMatrix = new prison::DataMatrixBarcode(); 00064 #endif // HAVE_PRISON 00065 } 00066 00067 ~Private() 00068 { 00069 delete mStandardContactFormatter; 00070 #ifdef HAVE_PRISON 00071 delete mQRCode; 00072 delete mDataMatrix; 00073 #endif // HAVE_PRISON 00074 } 00075 00076 void updateView( const QVariantList &localCustomFieldDescriptions = QVariantList(), const QString &addressBookName = QString() ) 00077 { 00078 static QPixmap defaultPixmap = KIcon( QLatin1String( "user-identity" ) ).pixmap( QSize( 100, 100 ) ); 00079 00080 mParent->setWindowTitle( i18n( "Contact %1", mCurrentContact.assembledName() ) ); 00081 00082 if ( mCurrentContact.photo().isIntern() ) { 00083 mBrowser->document()->addResource( QTextDocument::ImageResource, 00084 QUrl( QLatin1String( "contact_photo" ) ), 00085 mCurrentContact.photo().data() ); 00086 } else { 00087 mBrowser->document()->addResource( QTextDocument::ImageResource, 00088 QUrl( QLatin1String( "contact_photo" ) ), 00089 defaultPixmap ); 00090 } 00091 00092 mBrowser->document()->addResource( QTextDocument::ImageResource, 00093 QUrl( QLatin1String( "map_icon" ) ), 00094 KIcon( QLatin1String( "document-open-remote" ) ).pixmap( QSize( 16, 16 ) ) ); 00095 00096 #ifdef HAVE_PRISON 00097 KConfig config( QLatin1String( "akonadi_contactrc" ) ); 00098 KConfigGroup group( &config, QLatin1String( "View" ) ); 00099 if ( group.readEntry( "QRCodes", true ) ) { 00100 KABC::VCardConverter converter; 00101 KABC::Addressee addr(mCurrentContact); 00102 addr.setPhoto(KABC::Picture()); 00103 addr.setLogo(KABC::Picture()); 00104 const QString data = QString::fromUtf8( converter.createVCard( addr ) ); 00105 mQRCode->setData( data ); 00106 mDataMatrix->setData( data ); 00107 mBrowser->document()->addResource( QTextDocument::ImageResource, 00108 QUrl( QLatin1String( "qrcode" ) ), 00109 mQRCode->toImage( QSizeF(50,50) ) ); 00110 mBrowser->document()->addResource( QTextDocument::ImageResource, 00111 QUrl( QLatin1String( "datamatrix" ) ), 00112 mDataMatrix->toImage( QSizeF(50,50) ) ); 00113 } 00114 #endif // HAVE_PRISON 00115 00116 // merge local and global custom field descriptions 00117 QList<QVariantMap> customFieldDescriptions; 00118 foreach ( const QVariant &entry, localCustomFieldDescriptions ) 00119 customFieldDescriptions << entry.toMap(); 00120 00121 const CustomField::List globalCustomFields = CustomFieldManager::globalCustomFieldDescriptions(); 00122 foreach ( const CustomField &field, globalCustomFields ) { 00123 QVariantMap description; 00124 description.insert( QLatin1String( "key" ), field.key() ); 00125 description.insert( QLatin1String( "title" ), field.title() ); 00126 00127 customFieldDescriptions << description; 00128 } 00129 00130 KABC::Addressee contact( mCurrentContact ); 00131 if ( !addressBookName.isEmpty() ) { 00132 contact.insertCustom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "AddressBook" ), addressBookName ); 00133 } 00134 00135 mContactFormatter->setContact( contact ); 00136 mContactFormatter->setCustomFieldDescriptions( customFieldDescriptions ); 00137 00138 mBrowser->setHtml( mContactFormatter->toHtml() ); 00139 } 00140 00141 void slotMailClicked( const QString&, const QString &email ) 00142 { 00143 QString name, address; 00144 00145 // remove the 'mailto:' and split into name and email address 00146 KABC::Addressee::parseEmailAddress( email.mid( 7 ), name, address ); 00147 00148 emit mParent->emailClicked( name, address ); 00149 } 00150 00151 void slotUrlClicked( const QString &urlString ) 00152 { 00153 KUrl url( urlString ); 00154 const QString urlScheme( url.scheme() ); 00155 if ( urlScheme == QLatin1String( "http" ) || 00156 urlScheme == QLatin1String( "https" ) ) { 00157 emit mParent->urlClicked( url ); 00158 } else if ( urlScheme == QLatin1String( "phone" ) ) { 00159 const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt(); 00160 00161 const KABC::PhoneNumber::List numbers = mCurrentContact.phoneNumbers(); 00162 if ( pos < numbers.count() ) { 00163 emit mParent->phoneNumberClicked( numbers.at( pos ) ); 00164 } 00165 } else if ( urlScheme == QLatin1String( "sms" ) ) { 00166 const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt(); 00167 00168 const KABC::PhoneNumber::List numbers = mCurrentContact.phoneNumbers(); 00169 if ( pos < numbers.count() ) { 00170 emit mParent->smsClicked( numbers.at( pos ) ); 00171 } 00172 } else if ( urlScheme == QLatin1String( "address" ) ) { 00173 const int pos = url.queryItemValue( QLatin1String( "index" ) ).toInt(); 00174 00175 const KABC::Address::List addresses = mCurrentContact.addresses(); 00176 if ( pos < addresses.count() ) { 00177 emit mParent->addressClicked( addresses.at( pos ) ); 00178 } 00179 } 00180 } 00181 00182 void slotParentCollectionFetched( KJob *job ) 00183 { 00184 mParentCollectionFetchJob = 0; 00185 00186 QString addressBookName; 00187 00188 if ( !job->error() ) { 00189 CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job ); 00190 if ( !fetchJob->collections().isEmpty() ) { 00191 const Collection collection = fetchJob->collections().first(); 00192 if ( collection.hasAttribute<EntityDisplayAttribute>() ) 00193 addressBookName = collection.attribute<EntityDisplayAttribute>()->displayName(); 00194 else 00195 addressBookName = collection.name(); 00196 } 00197 } 00198 00199 // load the local meta data of the item 00200 ContactMetaData metaData; 00201 metaData.load( mCurrentItem ); 00202 00203 updateView( metaData.customFieldDescriptions(), addressBookName ); 00204 } 00205 00206 ContactViewer *mParent; 00207 TextBrowser *mBrowser; 00208 KABC::Addressee mCurrentContact; 00209 Item mCurrentItem; 00210 AbstractContactFormatter *mContactFormatter; 00211 AbstractContactFormatter *mStandardContactFormatter; 00212 CollectionFetchJob *mParentCollectionFetchJob; 00213 #ifdef HAVE_PRISON 00214 prison::AbstractBarcode* mQRCode; 00215 prison::AbstractBarcode* mDataMatrix; 00216 #endif // HAVE_PRISON 00217 }; 00218 00219 ContactViewer::ContactViewer( QWidget *parent ) 00220 : QWidget( parent ), d( new Private( this ) ) 00221 { 00222 QVBoxLayout *layout = new QVBoxLayout( this ); 00223 layout->setMargin( 0 ); 00224 00225 d->mBrowser = new TextBrowser; 00226 d->mBrowser->setNotifyClick( true ); 00227 00228 connect( d->mBrowser, SIGNAL(mailClick(QString,QString)), 00229 this, SLOT(slotMailClicked(QString,QString)) ); 00230 connect( d->mBrowser, SIGNAL(urlClick(QString)), 00231 this, SLOT(slotUrlClicked(QString)) ); 00232 00233 layout->addWidget( d->mBrowser ); 00234 00235 // always fetch full payload for contacts 00236 fetchScope().fetchFullPayload(); 00237 fetchScope().fetchAttribute<ContactMetaDataAttribute>(); 00238 fetchScope().setAncestorRetrieval( ItemFetchScope::Parent ); 00239 } 00240 00241 ContactViewer::~ContactViewer() 00242 { 00243 delete d; 00244 } 00245 00246 Akonadi::Item ContactViewer::contact() const 00247 { 00248 return ItemMonitor::item(); 00249 } 00250 00251 KABC::Addressee ContactViewer::rawContact() const 00252 { 00253 return d->mCurrentContact; 00254 } 00255 00256 void ContactViewer::setContactFormatter( AbstractContactFormatter *formatter ) 00257 { 00258 if ( formatter == 0 ) 00259 d->mContactFormatter = d->mStandardContactFormatter; 00260 else 00261 d->mContactFormatter = formatter; 00262 } 00263 00264 void ContactViewer::setContact( const Akonadi::Item &contact ) 00265 { 00266 ItemMonitor::setItem( contact ); 00267 } 00268 00269 void ContactViewer::setRawContact( const KABC::Addressee &contact ) 00270 { 00271 d->mCurrentContact = contact; 00272 00273 d->updateView(); 00274 } 00275 00276 void ContactViewer::itemChanged( const Item &contactItem ) 00277 { 00278 if ( !contactItem.hasPayload<KABC::Addressee>() ) 00279 return; 00280 00281 d->mCurrentItem = contactItem; 00282 d->mCurrentContact = contactItem.payload<KABC::Addressee>(); 00283 00284 // stop any running fetch job 00285 if ( d->mParentCollectionFetchJob ) { 00286 disconnect( d->mParentCollectionFetchJob, SIGNAL(result(KJob*)), this, SLOT(slotParentCollectionFetched(KJob*)) ); 00287 delete d->mParentCollectionFetchJob; 00288 d->mParentCollectionFetchJob = 0; 00289 } 00290 00291 d->mParentCollectionFetchJob = new CollectionFetchJob( contactItem.parentCollection(), CollectionFetchJob::Base, this ); 00292 connect( d->mParentCollectionFetchJob, SIGNAL(result(KJob*)), SLOT(slotParentCollectionFetched(KJob*)) ); 00293 } 00294 00295 void ContactViewer::itemRemoved() 00296 { 00297 d->mBrowser->clear(); 00298 } 00299 00300 #include "contactviewer.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:19:25 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:19:25 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.