• Skip to content
  • Skip to link menu
KDE 4.5 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi/contact

standardcontactformatter.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 "standardcontactformatter.h"
00023 
00024 #include <akonadi/item.h>
00025 #include <kabc/addressee.h>
00026 #include <kcolorscheme.h>
00027 #include <kglobal.h>
00028 #include <klocale.h>
00029 #include <kstringhandler.h>
00030 
00031 #include <QtCore/QSet>
00032 
00033 using namespace Akonadi;
00034 
00035 StandardContactFormatter::StandardContactFormatter()
00036   : d( 0 )
00037 {
00038 }
00039 
00040 StandardContactFormatter::~StandardContactFormatter()
00041 {
00042 }
00043 
00044 QString StandardContactFormatter::toHtml( HtmlForm form ) const
00045 {
00046   KABC::Addressee rawContact;
00047   const Akonadi::Item localItem = item();
00048   if ( localItem.isValid() && localItem.hasPayload<KABC::Addressee>() )
00049     rawContact = localItem.payload<KABC::Addressee>();
00050   else
00051     rawContact = contact();
00052 
00053   if ( rawContact.isEmpty() )
00054     return QString();
00055 
00056   // We'll be building a table to display the vCard in.
00057   // Each row of the table will be built using this string for its HTML.
00058 
00059   QString rowFmtStr = QString::fromLatin1(
00060         "<tr>"
00061         "<td align=\"right\" valign=\"top\" width=\"30%\"><b><font size=\"-1\" color=\"grey\">%1</font></b></td>\n"
00062         "<td align=\"left\" valign=\"top\" width=\"70%\"><font size=\"-1\">%2</font></td>\n"
00063         "</tr>\n"
00064         );
00065 
00066   // Build the table's rows here
00067   QString dynamicPart;
00068 
00069   // Birthday
00070   const QDate date = rawContact.birthday().date();
00071   const int years = (date.daysTo( QDate::currentDate() ) / 365);
00072 
00073   if ( date.isValid() )
00074     dynamicPart += rowFmtStr
00075       .arg( KABC::Addressee::birthdayLabel() )
00076       .arg( KGlobal::locale()->formatDate( date ) +
00077             QLatin1String( "&nbsp;&nbsp;" ) + i18np( "(One year old)", "(%1 years old)", years ) );
00078 
00079   // Phone Numbers
00080   int counter = 0;
00081   foreach ( const KABC::PhoneNumber &number, rawContact.phoneNumbers() ) {
00082       const QString url = QString::fromLatin1( "<a href=\"phone:?index=%1\">%2</a>" ).arg( counter ).arg( number.number() );
00083       counter++;
00084 
00085       dynamicPart += rowFmtStr
00086         .arg( number.typeLabel().replace( QLatin1String( " " ), QLatin1String( "&nbsp;" ) ) )
00087         .arg( url );
00088   }
00089 
00090   // EMails
00091   foreach ( const QString &email, rawContact.emails() ) {
00092     QString type = i18nc( "a contact's email address", "Email" );
00093 
00094     const QString fullEmail = QString::fromLatin1( KUrl::toPercentEncoding( rawContact.fullEmail( email ) ) );
00095 
00096     dynamicPart += rowFmtStr.arg( type )
00097       .arg( QString::fromLatin1( "<a href=\"mailto:%1\">%2</a>" )
00098       .arg( fullEmail, email ) );
00099   }
00100 
00101   // Homepage
00102   if ( rawContact.url().isValid() ) {
00103     QString url = rawContact.url().url();
00104     if ( !url.startsWith( QLatin1String( "http://" ) ) && !url.startsWith( QLatin1String( "https://" ) ) )
00105       url = QLatin1String( "http://" ) + url;
00106 
00107     url = KStringHandler::tagUrls( url );
00108     dynamicPart += rowFmtStr.arg( i18n( "Homepage" ) ).arg( url );
00109   }
00110 
00111   // Blog Feed
00112   const QString blog = rawContact.custom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "BlogFeed" ) );
00113   if ( !blog.isEmpty() )
00114     dynamicPart += rowFmtStr.arg( i18n( "Blog Feed" ) ).arg( KStringHandler::tagUrls( blog ) );
00115 
00116   // Addresses
00117   counter = 0;
00118   foreach ( const KABC::Address &address, rawContact.addresses() ) {
00119     QString formattedAddress;
00120 
00121     if ( address.label().isEmpty() ) {
00122       formattedAddress = address.formattedAddress().trimmed();
00123     } else {
00124       formattedAddress = address.label();
00125     }
00126 
00127     formattedAddress = formattedAddress.replace( QLatin1Char( '\n' ), QLatin1String( "<br>" ) );
00128 
00129     const QString url = QString::fromLatin1( "<a href=\"address:?index=%1\">%2</a>" ).arg( counter).arg( formattedAddress );
00130     counter++;
00131 
00132     dynamicPart += rowFmtStr
00133       .arg( KABC::Address::typeLabel( address.type() ) )
00134       .arg( url );
00135   }
00136 
00137   // Note
00138   QString notes;
00139   if ( !rawContact.note().isEmpty() )
00140     notes = rowFmtStr.arg( i18n( "Notes" ) ).arg( rawContact.note().replace( QLatin1Char( '\n' ), QLatin1String( "<br>" ) ) ) ;
00141 
00142   // Custom Data
00143   QString customData;
00144   static QMap<QString, QString> titleMap;
00145   if ( titleMap.isEmpty() ) {
00146     titleMap.insert( QLatin1String( "Department" ), i18n( "Department" ) );
00147     titleMap.insert( QLatin1String( "Profession" ), i18n( "Profession" ) );
00148     titleMap.insert( QLatin1String( "AssistantsName" ), i18n( "Assistant's Name" ) );
00149     titleMap.insert( QLatin1String( "ManagersName" ), i18n( "Manager's Name" ) );
00150     titleMap.insert( QLatin1String( "SpousesName" ), i18nc( "Wife/Husband/...", "Partner's Name" ) );
00151     titleMap.insert( QLatin1String( "Office" ), i18n( "Office" ) );
00152     titleMap.insert( QLatin1String( "IMAddress" ), i18n( "IM Address" ) );
00153     titleMap.insert( QLatin1String( "Anniversary" ), i18n( "Anniversary" ) );
00154     titleMap.insert( QLatin1String( "AddressBook" ), i18n( "Address Book" ) );
00155   }
00156 
00157   static QSet<QString> blacklistedKeys;
00158   if ( blacklistedKeys.isEmpty() ) {
00159     blacklistedKeys.insert( QLatin1String( "CRYPTOPROTOPREF" ) );
00160     blacklistedKeys.insert( QLatin1String( "OPENPGPFP" ) );
00161     blacklistedKeys.insert( QLatin1String( "SMIMEFP" ) );
00162     blacklistedKeys.insert( QLatin1String( "CRYPTOSIGNPREF" ) );
00163     blacklistedKeys.insert( QLatin1String( "CRYPTOENCRYPTPREF" ) );
00164   }
00165 
00166   if ( !rawContact.customs().empty() ) {
00167     const QStringList customs = rawContact.customs();
00168     foreach ( QString custom, customs ) { //krazy:exclude=foreach
00169       if ( custom.startsWith( QLatin1String( "KADDRESSBOOK-" ) ) ) {
00170         custom.remove( QLatin1String( "KADDRESSBOOK-X-" ) );
00171         custom.remove( QLatin1String( "KADDRESSBOOK-" ) );
00172 
00173         int pos = custom.indexOf( QLatin1Char( ':' ) );
00174         QString key = custom.left( pos );
00175         QString value = custom.mid( pos + 1 );
00176 
00177         // convert anniversary correctly
00178         if ( key == QLatin1String( "Anniversary" ) ) {
00179           const QDateTime dateTime = QDateTime::fromString( value, Qt::ISODate );
00180           value = KGlobal::locale()->formatDate( dateTime.date() );
00181         }
00182 
00183         // blog is handled separated
00184         if ( key == QLatin1String( "BlogFeed" ) )
00185           continue;
00186 
00187         if ( blacklistedKeys.contains( key ) )
00188           continue;
00189 
00190         // check whether we have a mapping for the title
00191         const QMap<QString, QString>::ConstIterator keyIt = titleMap.constFind( key );
00192         if ( keyIt != titleMap.constEnd() ) {
00193           key = keyIt.value();
00194         } else {
00195           // check whether it is a custom local field
00196           foreach ( const QVariantMap &description, customFieldDescriptions() ) {
00197             if ( description.value( QLatin1String( "key" ) ).toString() == key ) {
00198               key = description.value( QLatin1String( "title" ) ).toString();
00199               if ( description.value( QLatin1String( "type" ) ) == QLatin1String( "boolean" ) ) {
00200                 if ( value == QLatin1String( "true" ) )
00201                   value = i18nc( "Boolean value", "yes" );
00202                 else
00203                   value = i18nc( "Boolean value", "no" );
00204               } else if ( description.value( QLatin1String( "type" ) ) == QLatin1String( "date" ) ) {
00205                 const QDate date = QDate::fromString( value, Qt::ISODate );
00206                 value = KGlobal::locale()->formatDate( date, KLocale::ShortDate );
00207               } else if ( description.value( QLatin1String( "type" ) ) == QLatin1String( "time" ) ) {
00208                 const QTime time = QTime::fromString( value, Qt::ISODate );
00209                 value = KGlobal::locale()->formatTime( time );
00210               } else if ( description.value( QLatin1String( "type" ) ) == QLatin1String( "datetime" ) ) {
00211                 const QDateTime dateTime = QDateTime::fromString( value, Qt::ISODate );
00212                 value = KGlobal::locale()->formatDateTime( dateTime, KLocale::ShortDate );
00213               }
00214               break;
00215             }
00216           }
00217         }
00218 
00219         customData += rowFmtStr.arg( key ).arg( value ) ;
00220       }
00221     }
00222   }
00223 
00224   // Assemble all parts
00225   QString role = rawContact.title();
00226   if ( role.isEmpty() )
00227     role = rawContact.role();
00228   if ( role.isEmpty() )
00229     role = rawContact.custom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "X-Profession" ) );
00230 
00231   QString strAddr = QString::fromLatin1(
00232     "<div align=\"center\">"
00233     "<table cellpadding=\"3\" cellspacing=\"0\">"
00234     "<tr>"
00235     "<td align=\"right\" valign=\"top\" width=\"30%\" rowspan=\"3\">"
00236     "<img src=\"%1\" width=\"100\" vspace=\"1\">" // image
00237     "</td>"
00238     "<td align=\"left\" width=\"70%\"><font size=\"+2\"><b>%2</b></font></td>" // name
00239     "</tr>"
00240     "<tr>"
00241     "<td align=\"left\" width=\"70%\">%3</td>"  // role
00242     "</tr>"
00243     "<tr>"
00244     "<td align=\"left\" width=\"70%\">%4</td>"  // organization
00245     "</tr>")
00246       .arg( QLatin1String( "contact_photo" ) )
00247       .arg( rawContact.realName() )
00248       .arg( role )
00249       .arg( rawContact.organization() );
00250 
00251   strAddr.append( dynamicPart );
00252   strAddr.append( notes );
00253   strAddr.append( customData );
00254   strAddr.append( QString::fromLatin1( "</table></div>\n" ) );
00255 
00256   if ( form == EmbeddableForm )
00257     return strAddr;
00258 
00259   const QString document = QString::fromLatin1(
00260     "<html>"
00261     "<head>"
00262     " <style type=\"text/css\">"
00263     "  a {text-decoration:none; color:%1}"
00264     " </style>"
00265     "</head>"
00266     "<body text=\"%1\" bgcolor=\"%2\">" // text and background color
00267     "%3" // contact part
00268     "</body>"
00269     "</html>" )
00270      .arg( KColorScheme( QPalette::Active, KColorScheme::View ).foreground().color().name() )
00271      .arg( KColorScheme( QPalette::Active, KColorScheme::View ).background().color().name() )
00272      .arg( strAddr );
00273 
00274   return document;
00275 }

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal