akonadi
standardcontactgroupformatter.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2010 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 "standardcontactgroupformatter.h" 00023 00024 #include <akonadi/contact/contactgroupexpandjob.h> 00025 #include <akonadi/item.h> 00026 #include <kabc/addressee.h> 00027 #include <kcolorscheme.h> 00028 #include <kglobal.h> 00029 #include <klocale.h> 00030 #include <kstringhandler.h> 00031 00032 using namespace Akonadi; 00033 00034 StandardContactGroupFormatter::StandardContactGroupFormatter() 00035 : d( 0 ) 00036 { 00037 } 00038 00039 StandardContactGroupFormatter::~StandardContactGroupFormatter() 00040 { 00041 } 00042 00043 QString StandardContactGroupFormatter::toHtml( HtmlForm form ) const 00044 { 00045 KABC::ContactGroup group; 00046 const Akonadi::Item localItem = item(); 00047 if ( localItem.isValid() && localItem.hasPayload<KABC::ContactGroup>() ) 00048 group = localItem.payload<KABC::ContactGroup>(); 00049 else 00050 group = contactGroup(); 00051 00052 if ( group.name().isEmpty() && group.count() == 0 ) // empty group 00053 return QString(); 00054 00055 if ( group.contactReferenceCount() != 0 ) { 00056 // we got a contact group with unresolved references -> we have to resolve it ourself 00057 // this shouldn't be the normal case, actually the calling code should pass in an already resolved 00058 // contact group 00059 ContactGroupExpandJob *job = new ContactGroupExpandJob( group ); 00060 if ( job->exec() ) { 00061 group.removeAllContactData(); 00062 foreach ( const KABC::Addressee &contact, job->contacts() ) { 00063 group.append( KABC::ContactGroup::Data( contact.realName(), contact.preferredEmail() ) ); 00064 } 00065 } 00066 } 00067 00068 // Assemble all parts 00069 QString strGroup = QString::fromLatin1( 00070 "<table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">" 00071 "<tr>" 00072 "<td align=\"right\" valign=\"top\" width=\"30%\">" 00073 "<img src=\"%1\" width=\"100\" vspace=\"1\">" // image 00074 "</td>" 00075 "<td align=\"left\" width=\"70%\"><font size=\"+2\"><b>%2</b></font></td>" // name 00076 "</tr>" 00077 "</table>" ) 00078 .arg( QLatin1String( "group_photo" ) ) 00079 .arg( group.name() ); 00080 00081 strGroup += QLatin1String( "<table width=\"100%\">" ); 00082 00083 for ( uint i = 0; i < group.dataCount(); ++i ) { 00084 const KABC::ContactGroup::Data data = group.data( i ); 00085 00086 if ( data.email().isEmpty() ) { 00087 strGroup.append( QString::fromLatin1( "<tr><td align=\"right\" width=\"50%\"><b><font size=\"-1\" color=\"grey\">%1</font></b></td>" 00088 "<td width=\"50%\"></td></tr>" ) 00089 .arg( data.name() ) ); 00090 } else { 00091 KABC::Addressee contact; 00092 contact.setFormattedName( data.name() ); 00093 contact.insertEmail( data.email() ); 00094 00095 const QString fullEmail = QLatin1String( "<a href=\"mailto:" ) + QString::fromLatin1( KUrl::toPercentEncoding( contact.fullEmail() ) ) + QString::fromLatin1( "\">%1</a>" ).arg( contact.preferredEmail() ); 00096 00097 strGroup.append( QString::fromLatin1( "<tr><td align=\"right\" width=\"50%\"><b><font size=\"-1\" color=\"grey\">%1</font></b></td>" 00098 "<td valign=\"bottom\" align=\"left\" width=\"50%\"><font size=\"-1\"><%2></font></td></tr>" ) 00099 .arg( contact.realName() ) 00100 .arg( fullEmail ) ); 00101 } 00102 } 00103 00104 foreach ( const QVariantMap &map, additionalFields() ) { 00105 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>" 00106 "<td valign=\"bottom\" align=\"left\" width=\"50%\"><font size=\"-1\">%2</font></td></tr>" ) 00107 .arg( map.value( QLatin1String( "title" ) ).toString() ) 00108 .arg( map.value( QLatin1String( "value" ) ).toString() ) ); 00109 } 00110 00111 strGroup.append( QString::fromLatin1( "</table>\n" ) ); 00112 00113 QString document = QString::fromLatin1( "<div align=\"center\">%1</div>" ).arg( strGroup ); 00114 00115 if ( form == EmbeddableForm ) 00116 return document; 00117 00118 document = QString::fromLatin1( 00119 "<html>" 00120 "<head>" 00121 " <style type=\"text/css\">" 00122 " a {text-decoration:none; color:%1}" 00123 " </style>" 00124 "</head>" 00125 "<body text=\"%1\" bgcolor=\"%2\">" // text and background color 00126 "%3" // contact group part 00127 "</body>" 00128 "</html>" ) 00129 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).foreground().color().name() ) 00130 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).background().color().name() ) 00131 .arg( document ); 00132 00133 return document; 00134 }