akonadi/contact
contactgroupviewer.cpp
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/item.h>
00028 #include <akonadi/itemfetchjob.h>
00029 #include <akonadi/itemfetchscope.h>
00030 #include <kabc/addressee.h>
00031 #include <kabc/contactgroup.h>
00032 #include <kcolorscheme.h>
00033 #include <kglobal.h>
00034 #include <kicon.h>
00035 #include <klocale.h>
00036 #include <kstringhandler.h>
00037
00038 #include <QtGui/QVBoxLayout>
00039
00040 using namespace Akonadi;
00041
00042 static QString contactsAsHtml( const QString &groupName, const KABC::Addressee::List &contacts );
00043
00044 class ContactGroupViewer::Private
00045 {
00046 public:
00047 Private( ContactGroupViewer *parent )
00048 : mParent( parent ), mExpandJob( 0 )
00049 {
00050 }
00051
00052 void slotMailClicked( const QString&, const QString &email )
00053 {
00054 QString name, address;
00055
00056
00057 KABC::Addressee::parseEmailAddress( email.mid( 7 ), name, address );
00058
00059 emit mParent->emailClicked( name, address );
00060 }
00061
00062 void _k_expandResult( KJob *job )
00063 {
00064 ContactGroupExpandJob *expandJob = qobject_cast<ContactGroupExpandJob*>( job );
00065
00066 const KABC::Addressee::List contacts = expandJob->contacts();
00067
00068 mBrowser->setHtml( contactsAsHtml( mGroupName, contacts ) );
00069
00070 mExpandJob = 0;
00071 }
00072
00073 ContactGroupViewer *mParent;
00074 TextBrowser *mBrowser;
00075 QString mGroupName;
00076 ContactGroupExpandJob *mExpandJob;
00077 };
00078
00079 ContactGroupViewer::ContactGroupViewer( QWidget *parent )
00080 : QWidget( parent ), d( new Private( this ) )
00081 {
00082 QVBoxLayout *layout = new QVBoxLayout( this );
00083
00084 d->mBrowser = new TextBrowser;
00085 d->mBrowser->setNotifyClick( true );
00086
00087 connect( d->mBrowser, SIGNAL( mailClick( const QString&, const QString& ) ),
00088 this, SLOT( slotMailClicked( const QString&, const QString& ) ) );
00089
00090 layout->addWidget( d->mBrowser );
00091
00092
00093 fetchScope().fetchFullPayload();
00094 }
00095
00096 ContactGroupViewer::~ContactGroupViewer()
00097 {
00098 delete d;
00099 }
00100
00101 Akonadi::Item ContactGroupViewer::contactGroup() const
00102 {
00103 return ItemMonitor::item();
00104 }
00105
00106 void ContactGroupViewer::setContactGroup( const Akonadi::Item &group )
00107 {
00108 ItemMonitor::setItem( group );
00109 }
00110
00111 void ContactGroupViewer::itemChanged( const Item &item )
00112 {
00113 if ( !item.hasPayload<KABC::ContactGroup>() )
00114 return;
00115
00116 static QPixmap groupPixmap = KIcon( QLatin1String( "x-mail-distribution-list" ) ).pixmap( QSize( 100, 140 ) );
00117
00118 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
00119 d->mGroupName = group.name();
00120
00121 setWindowTitle( i18n( "Contact Group %1", group.name() ) );
00122
00123 d->mBrowser->document()->addResource( QTextDocument::ImageResource,
00124 QUrl( QLatin1String( "group_photo" ) ),
00125 groupPixmap );
00126
00127 if ( d->mExpandJob ) {
00128 disconnect( d->mExpandJob, SIGNAL( result( KJob* ) ), this, SLOT( _k_expandResult( KJob* ) ) );
00129 d->mExpandJob->kill();
00130 }
00131
00132 d->mExpandJob = new ContactGroupExpandJob( group );
00133 connect( d->mExpandJob, SIGNAL( result( KJob* ) ), SLOT( _k_expandResult( KJob* ) ) );
00134 d->mExpandJob->start();
00135 }
00136
00137 void ContactGroupViewer::itemRemoved()
00138 {
00139 d->mBrowser->clear();
00140 }
00141
00142 static QString contactsAsHtml( const QString &groupName, const KABC::Addressee::List &contacts )
00143 {
00144
00145 QString strGroup = QString::fromLatin1(
00146 "<table cellpadding=\"1\" cellspacing=\"0\" width=\"100%\">"
00147 "<tr>"
00148 "<td align=\"right\" valign=\"top\" width=\"30%\">"
00149 "<img src=\"%1\" width=\"75\" height=\"105\" vspace=\"1\">"
00150 "</td>"
00151 "<td align=\"left\" width=\"70%\"><font size=\"+2\"><b>%2</b></font></td>"
00152 "</tr>"
00153 "</table>" )
00154 .arg( QLatin1String( "group_photo" ) )
00155 .arg( groupName );
00156
00157 strGroup += QLatin1String( "<table width=\"100%\">" );
00158
00159
00160 foreach ( const KABC::Addressee &contact, contacts ) {
00161 if ( contact.preferredEmail().isEmpty() ) {
00162 strGroup.append( QString::fromLatin1( "<tr><td align=\"right\" width=\"50%\"><b><font size=\"-1\" color=\"grey\">%1</font></b></td>"
00163 "<td width=\"50%\"></td></tr>" )
00164 .arg( contact.realName() ) );
00165 } else {
00166 const QString fullEmail = QLatin1String( "<a href=\"mailto:" ) + QString::fromLatin1( KUrl::toPercentEncoding( contact.fullEmail() ) ) + QString::fromLatin1( "\">%1</a>" ).arg( contact.preferredEmail() );
00167
00168 strGroup.append( QString::fromLatin1( "<tr><td align=\"right\" width=\"50%\"><b><font size=\"-1\" color=\"grey\">%1</font></b></td>"
00169 "<td valign=\"bottom\" align=\"left\" width=\"50%\"><font size=\"-1\"><%2></font></td></tr>" )
00170 .arg( contact.realName() )
00171 .arg( fullEmail ) );
00172 }
00173 }
00174
00175 strGroup.append( QString::fromLatin1( "</table>\n" ) );
00176
00177 const QString document = QString::fromLatin1(
00178 "<html>"
00179 "<head>"
00180 " <style type=\"text/css\">"
00181 " a {text-decoration:none; color:%1}"
00182 " </style>"
00183 "</head>"
00184 "<body text=\"%1\" bgcolor=\"%2\">"
00185 "%3"
00186 "</body>"
00187 "</html>" )
00188 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).foreground().color().name() )
00189 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).background().color().name() )
00190 .arg( strGroup );
00191
00192 return document;
00193 }
00194
00195 #include "contactgroupviewer.moc"