contactgroupviewer.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 "contactgroupviewer.h" 00023 00024 #include "contactgroupexpandjob.h" 00025 #include "standardcontactgroupformatter.h" 00026 #include "textbrowser_p.h" 00027 00028 #include <akonadi/collectionfetchjob.h> 00029 #include <akonadi/entitydisplayattribute.h> 00030 #include <akonadi/item.h> 00031 #include <akonadi/itemfetchjob.h> 00032 #include <akonadi/itemfetchscope.h> 00033 #include <kabc/addressee.h> 00034 #include <kabc/contactgroup.h> 00035 #include <kcolorscheme.h> 00036 #include <kglobal.h> 00037 #include <kicon.h> 00038 #include <klocale.h> 00039 #include <kstringhandler.h> 00040 00041 #include <QtGui/QVBoxLayout> 00042 00043 using namespace Akonadi; 00044 00045 class ContactGroupViewer::Private 00046 { 00047 public: 00048 Private( ContactGroupViewer *parent ) 00049 : mParent( parent ), mExpandJob( 0 ), mParentCollectionFetchJob( 0 ) 00050 { 00051 mBrowser = new TextBrowser; 00052 00053 static QPixmap groupPixmap = KIcon( QLatin1String( "x-mail-distribution-list" ) ).pixmap( QSize( 100, 100 ) ); 00054 mBrowser->document()->addResource( QTextDocument::ImageResource, 00055 QUrl( QLatin1String( "group_photo" ) ), 00056 groupPixmap ); 00057 00058 mStandardContactGroupFormatter = new StandardContactGroupFormatter; 00059 mContactGroupFormatter = mStandardContactGroupFormatter; 00060 } 00061 00062 ~Private() 00063 { 00064 delete mStandardContactGroupFormatter; 00065 } 00066 00067 void updateView() 00068 { 00069 mParent->setWindowTitle( i18n( "Contact Group %1", mCurrentGroupName ) ); 00070 00071 KABC::ContactGroup group; 00072 group.setName( mCurrentGroupName ); 00073 foreach ( const KABC::Addressee &contact, mCurrentContacts ) 00074 group.append( KABC::ContactGroup::Data( contact.realName(), contact.preferredEmail() ) ); 00075 00076 mContactGroupFormatter->setContactGroup( group ); 00077 00078 QList<QVariantMap> additionalFields; 00079 00080 if ( !mCurrentAddressBookName.isEmpty() ) { 00081 QVariantMap addressBookName; 00082 addressBookName.insert( QLatin1String( "title" ), i18n( "Address Book" ) ); 00083 addressBookName.insert( QLatin1String( "value" ), mCurrentAddressBookName ); 00084 00085 additionalFields << addressBookName; 00086 } 00087 00088 mContactGroupFormatter->setAdditionalFields( additionalFields ); 00089 00090 mBrowser->setHtml( mContactGroupFormatter->toHtml() ); 00091 } 00092 00093 void slotMailClicked( const QString&, const QString &email ) 00094 { 00095 QString name, address; 00096 00097 // remove the 'mailto:' and split into name and email address 00098 KABC::Addressee::parseEmailAddress( email.mid( 7 ), name, address ); 00099 00100 emit mParent->emailClicked( name, address ); 00101 } 00102 00103 void _k_expandResult( KJob *job ) 00104 { 00105 mExpandJob = 0; 00106 00107 if ( !job->error() ) { 00108 ContactGroupExpandJob *expandJob = qobject_cast<ContactGroupExpandJob*>( job ); 00109 mCurrentContacts = expandJob->contacts(); 00110 } 00111 00112 // stop any running fetch job 00113 if ( mParentCollectionFetchJob ) { 00114 mParent->disconnect( mParentCollectionFetchJob, SIGNAL(result(KJob*)), mParent, SLOT(slotParentCollectionFetched(KJob*)) ); 00115 delete mParentCollectionFetchJob; 00116 mParentCollectionFetchJob = 0; 00117 } 00118 00119 mParentCollectionFetchJob = new CollectionFetchJob( mCurrentItem.parentCollection(), CollectionFetchJob::Base, mParent ); 00120 mParent->connect( mParentCollectionFetchJob, SIGNAL(result(KJob*)), SLOT(slotParentCollectionFetched(KJob*)) ); 00121 } 00122 00123 void slotParentCollectionFetched( KJob *job ) 00124 { 00125 mParentCollectionFetchJob = 0; 00126 mCurrentAddressBookName.clear(); 00127 00128 if ( !job->error() ) { 00129 CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job ); 00130 if ( !fetchJob->collections().isEmpty() ) { 00131 const Collection collection = fetchJob->collections().first(); 00132 if ( collection.hasAttribute<EntityDisplayAttribute>() ) 00133 mCurrentAddressBookName = collection.attribute<EntityDisplayAttribute>()->displayName(); 00134 else 00135 mCurrentAddressBookName = collection.name(); 00136 } 00137 } 00138 00139 updateView(); 00140 } 00141 00142 ContactGroupViewer *mParent; 00143 TextBrowser *mBrowser; 00144 QString mCurrentGroupName; 00145 KABC::Addressee::List mCurrentContacts; 00146 QString mCurrentAddressBookName; 00147 Item mCurrentItem; 00148 ContactGroupExpandJob *mExpandJob; 00149 CollectionFetchJob *mParentCollectionFetchJob; 00150 AbstractContactGroupFormatter *mStandardContactGroupFormatter; 00151 AbstractContactGroupFormatter *mContactGroupFormatter; 00152 }; 00153 00154 ContactGroupViewer::ContactGroupViewer( QWidget *parent ) 00155 : QWidget( parent ), d( new Private( this ) ) 00156 { 00157 QVBoxLayout *layout = new QVBoxLayout( this ); 00158 layout->setMargin( 0 ); 00159 00160 d->mBrowser->setNotifyClick( true ); 00161 00162 connect( d->mBrowser, SIGNAL(mailClick(QString,QString)), 00163 this, SLOT(slotMailClicked(QString,QString)) ); 00164 00165 layout->addWidget( d->mBrowser ); 00166 00167 // always fetch full payload for contact groups 00168 fetchScope().fetchFullPayload(); 00169 fetchScope().setAncestorRetrieval( ItemFetchScope::Parent ); 00170 } 00171 00172 ContactGroupViewer::~ContactGroupViewer() 00173 { 00174 delete d; 00175 } 00176 00177 Akonadi::Item ContactGroupViewer::contactGroup() const 00178 { 00179 return ItemMonitor::item(); 00180 } 00181 00182 void ContactGroupViewer::setContactGroup( const Akonadi::Item &group ) 00183 { 00184 ItemMonitor::setItem( group ); 00185 } 00186 00187 void ContactGroupViewer::setContactGroupFormatter( AbstractContactGroupFormatter *formatter ) 00188 { 00189 if ( formatter == 0 ) 00190 d->mContactGroupFormatter = d->mStandardContactGroupFormatter; 00191 else 00192 d->mContactGroupFormatter = formatter; 00193 } 00194 00195 void ContactGroupViewer::itemChanged( const Item &item ) 00196 { 00197 if ( !item.hasPayload<KABC::ContactGroup>() ) 00198 return; 00199 00200 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>(); 00201 d->mCurrentGroupName = group.name(); 00202 d->mCurrentItem = item; 00203 00204 if ( d->mExpandJob ) { 00205 disconnect( d->mExpandJob, SIGNAL(result(KJob*)), this, SLOT(_k_expandResult(KJob*)) ); 00206 d->mExpandJob->kill(); 00207 } 00208 00209 d->mExpandJob = new ContactGroupExpandJob( group ); 00210 connect( d->mExpandJob, SIGNAL(result(KJob*)), SLOT(_k_expandResult(KJob*)) ); 00211 d->mExpandJob->start(); 00212 } 00213 00214 void ContactGroupViewer::itemRemoved() 00215 { 00216 d->mBrowser->clear(); 00217 } 00218 00219 #include "contactgroupviewer.moc"