00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "contactgroupviewer.h"
00023
00024 #include "contactgroupexpandjob.h"
00025 #include "textbrowser_p.h"
00026
00027 #include <akonadi/collectionfetchjob.h>
00028 #include <akonadi/entitydisplayattribute.h>
00029 #include <akonadi/item.h>
00030 #include <akonadi/itemfetchjob.h>
00031 #include <akonadi/itemfetchscope.h>
00032 #include <kabc/addressee.h>
00033 #include <kabc/contactgroup.h>
00034 #include <kcolorscheme.h>
00035 #include <kglobal.h>
00036 #include <kicon.h>
00037 #include <klocale.h>
00038 #include <kstringhandler.h>
00039
00040 #include <QtGui/QVBoxLayout>
00041
00042 using namespace Akonadi;
00043
00044 static QString contactsAsHtml( const QString &groupName, const KABC::Addressee::List &contacts,
00045 const QString &addressBookName );
00046
00047 class ContactGroupViewer::Private
00048 {
00049 public:
00050 Private( ContactGroupViewer *parent )
00051 : mParent( parent ), mExpandJob( 0 ), mParentCollectionFetchJob( 0 )
00052 {
00053 mBrowser = new TextBrowser;
00054
00055 static QPixmap groupPixmap = KIcon( QLatin1String( "x-mail-distribution-list" ) ).pixmap( QSize( 100, 100 ) );
00056 mBrowser->document()->addResource( QTextDocument::ImageResource,
00057 QUrl( QLatin1String( "group_photo" ) ),
00058 groupPixmap );
00059 }
00060
00061 void updateView()
00062 {
00063 mParent->setWindowTitle( i18n( "Contact Group %1", mCurrentGroupName ) );
00064 mBrowser->setHtml( contactsAsHtml( mCurrentGroupName, mCurrentContacts, mCurrentAddressBookName ) );
00065 }
00066
00067 void slotMailClicked( const QString&, const QString &email )
00068 {
00069 QString name, address;
00070
00071
00072 KABC::Addressee::parseEmailAddress( email.mid( 7 ), name, address );
00073
00074 emit mParent->emailClicked( name, address );
00075 }
00076
00077 void _k_expandResult( KJob *job )
00078 {
00079 mExpandJob = 0;
00080
00081 if ( !job->error() ) {
00082 ContactGroupExpandJob *expandJob = qobject_cast<ContactGroupExpandJob*>( job );
00083 mCurrentContacts = expandJob->contacts();
00084 }
00085
00086
00087 if ( mParentCollectionFetchJob ) {
00088 mParent->disconnect( mParentCollectionFetchJob, SIGNAL( result( KJob* ) ), mParent, SLOT( slotParentCollectionFetched( KJob* ) ) );
00089 delete mParentCollectionFetchJob;
00090 mParentCollectionFetchJob = 0;
00091 }
00092
00093 mParentCollectionFetchJob = new CollectionFetchJob( mCurrentItem.parentCollection(), CollectionFetchJob::Base, mParent );
00094 mParent->connect( mParentCollectionFetchJob, SIGNAL( result( KJob* ) ), SLOT( slotParentCollectionFetched( KJob* ) ) );
00095 }
00096
00097 void slotParentCollectionFetched( KJob *job )
00098 {
00099 mParentCollectionFetchJob = 0;
00100 mCurrentAddressBookName.clear();
00101
00102 if ( !job->error() ) {
00103 CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
00104 if ( !fetchJob->collections().isEmpty() ) {
00105 const Collection collection = fetchJob->collections().first();
00106 if ( collection.hasAttribute<EntityDisplayAttribute>() )
00107 mCurrentAddressBookName = collection.attribute<EntityDisplayAttribute>()->displayName();
00108 else
00109 mCurrentAddressBookName = collection.name();
00110 }
00111 }
00112
00113 updateView();
00114 }
00115
00116 ContactGroupViewer *mParent;
00117 TextBrowser *mBrowser;
00118 QString mCurrentGroupName;
00119 KABC::Addressee::List mCurrentContacts;
00120 QString mCurrentAddressBookName;
00121 Item mCurrentItem;
00122 ContactGroupExpandJob *mExpandJob;
00123 CollectionFetchJob *mParentCollectionFetchJob;
00124 };
00125
00126 ContactGroupViewer::ContactGroupViewer( QWidget *parent )
00127 : QWidget( parent ), d( new Private( this ) )
00128 {
00129 QVBoxLayout *layout = new QVBoxLayout( this );
00130 layout->setMargin( 0 );
00131
00132 d->mBrowser->setNotifyClick( true );
00133
00134 connect( d->mBrowser, SIGNAL( mailClick( const QString&, const QString& ) ),
00135 this, SLOT( slotMailClicked( const QString&, const QString& ) ) );
00136
00137 layout->addWidget( d->mBrowser );
00138
00139
00140 fetchScope().fetchFullPayload();
00141 fetchScope().setAncestorRetrieval( ItemFetchScope::Parent );
00142 }
00143
00144 ContactGroupViewer::~ContactGroupViewer()
00145 {
00146 delete d;
00147 }
00148
00149 Akonadi::Item ContactGroupViewer::contactGroup() const
00150 {
00151 return ItemMonitor::item();
00152 }
00153
00154 void ContactGroupViewer::setContactGroup( const Akonadi::Item &group )
00155 {
00156 ItemMonitor::setItem( group );
00157 }
00158
00159 void ContactGroupViewer::itemChanged( const Item &item )
00160 {
00161 if ( !item.hasPayload<KABC::ContactGroup>() )
00162 return;
00163
00164 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
00165 d->mCurrentGroupName = group.name();
00166 d->mCurrentItem = item;
00167
00168 if ( d->mExpandJob ) {
00169 disconnect( d->mExpandJob, SIGNAL( result( KJob* ) ), this, SLOT( _k_expandResult( KJob* ) ) );
00170 d->mExpandJob->kill();
00171 }
00172
00173 d->mExpandJob = new ContactGroupExpandJob( group );
00174 connect( d->mExpandJob, SIGNAL( result( KJob* ) ), SLOT( _k_expandResult( KJob* ) ) );
00175 d->mExpandJob->start();
00176 }
00177
00178 void ContactGroupViewer::itemRemoved()
00179 {
00180 d->mBrowser->clear();
00181 }
00182
00183 static QString contactsAsHtml( const QString &groupName, const KABC::Addressee::List &contacts,
00184 const QString &addressBookName )
00185 {
00186
00187 QString strGroup = QString::fromLatin1(
00188 "<table cellpadding=\"1\" cellspacing=\"0\" width=\"100%\">"
00189 "<tr>"
00190 "<td align=\"right\" valign=\"top\" width=\"30%\">"
00191 "<img src=\"%1\" width=\"75\" height=\"105\" vspace=\"1\">"
00192 "</td>"
00193 "<td align=\"left\" width=\"70%\"><font size=\"+2\"><b>%2</b></font></td>"
00194 "</tr>"
00195 "</table>" )
00196 .arg( QLatin1String( "group_photo" ) )
00197 .arg( groupName );
00198
00199 strGroup += QLatin1String( "<table width=\"100%\">" );
00200
00201
00202 foreach ( const KABC::Addressee &contact, contacts ) {
00203 if ( contact.preferredEmail().isEmpty() ) {
00204 strGroup.append( QString::fromLatin1( "<tr><td align=\"right\" width=\"50%\"><b><font size=\"-1\" color=\"grey\">%1</font></b></td>"
00205 "<td width=\"50%\"></td></tr>" )
00206 .arg( contact.realName() ) );
00207 } else {
00208 const QString fullEmail = QLatin1String( "<a href=\"mailto:" ) + QString::fromLatin1( KUrl::toPercentEncoding( contact.fullEmail() ) ) + QString::fromLatin1( "\">%1</a>" ).arg( contact.preferredEmail() );
00209
00210 strGroup.append( QString::fromLatin1( "<tr><td align=\"right\" width=\"50%\"><b><font size=\"-1\" color=\"grey\">%1</font></b></td>"
00211 "<td valign=\"bottom\" align=\"left\" width=\"50%\"><font size=\"-1\"><%2></font></td></tr>" )
00212 .arg( contact.realName() )
00213 .arg( fullEmail ) );
00214 }
00215 }
00216
00217 if ( !addressBookName.isEmpty() ) {
00218 strGroup.append( QString::fromLatin1( "<tr><td colspan=\"2\"> </td></tr><tr><td align=\"right\" width=\"30%\"><b><font size=\"-1\" color=\"grey\">%1</font></b></td>"
00219 "<td valign=\"bottom\" align=\"left\" width=\"50%\"><font size=\"-1\">%2</font></td></tr>" )
00220 .arg( i18n( "Address Book" ) )
00221 .arg( addressBookName ) );
00222 }
00223
00224 strGroup.append( QString::fromLatin1( "</table>\n" ) );
00225
00226 const QString document = QString::fromLatin1(
00227 "<html>"
00228 "<head>"
00229 " <style type=\"text/css\">"
00230 " a {text-decoration:none; color:%1}"
00231 " </style>"
00232 "</head>"
00233 "<body text=\"%1\" bgcolor=\"%2\">"
00234 "%3"
00235 "</body>"
00236 "</html>" )
00237 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).foreground().color().name() )
00238 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).background().color().name() )
00239 .arg( strGroup );
00240
00241 return document;
00242 }
00243
00244 #include "contactgroupviewer.moc"