• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi

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

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal