Commit 4dd3676b authored by Tobias Koenig's avatar Tobias Koenig

Changed the design of AbstractContactFormatter

svn path=/trunk/KDE/kdepimlibs/; revision=1127126
parent cc874f54
......@@ -21,8 +21,55 @@
#include "abstractcontactformatter.h"
#include <akonadi/item.h>
#include <kabc/addressee.h>
using namespace Akonadi;
class AbstractContactFormatter::Private
{
public:
KABC::Addressee mContact;
Akonadi::Item mItem;
QList<QVariantMap> mCustomFieldDescriptions;
};
AbstractContactFormatter::AbstractContactFormatter()
: d( new Private )
{
}
AbstractContactFormatter::~AbstractContactFormatter()
{
delete d;
}
void AbstractContactFormatter::setContact( const KABC::Addressee &contact )
{
d->mContact = contact;
}
KABC::Addressee AbstractContactFormatter::contact() const
{
return d->mContact;
}
void AbstractContactFormatter::setItem( const Akonadi::Item &item )
{
d->mItem = item;
}
Akonadi::Item AbstractContactFormatter::item() const
{
return d->mItem;
}
void AbstractContactFormatter::setCustomFieldDescriptions( const QList<QVariantMap> &descriptions )
{
d->mCustomFieldDescriptions = descriptions;
}
QList<QVariantMap> AbstractContactFormatter::customFieldDescriptions() const
{
return d->mCustomFieldDescriptions;
}
......@@ -31,14 +31,18 @@ class Addressee;
}
namespace Akonadi {
class Item;
/**
* @short The interface for all contact formatters.
*
* This is the interface that can be used to format an Akonadi
* item with a contact payload or a contact itself as HTML.
*
* @see StandardContactFormatter
* @author Tobias Koenig <tokoe@kde.org>
* @since 4.5
*/
//AK_REVIEW: extend api docs (usage), move class to libkabc
class AKONADI_CONTACT_EXPORT AbstractContactFormatter
{
public:
......@@ -52,30 +56,74 @@ class AKONADI_CONTACT_EXPORT AbstractContactFormatter
UserForm = SelfcontainedForm + 42 ///< Point for extension
};
/**
* Creates a new abstract contact formatter.
*/
AbstractContactFormatter();
/**
* Destroys the abstract contact formatter.
*/
virtual ~AbstractContactFormatter();
/**
* Sets the @p contact that shall be formatted.
* Sets the @p contact that will be formatted.
*/
//AK_REVIEW: add getter and make it non-abstract
virtual void setContact( const KABC::Addressee &contact ) = 0;
void setContact( const KABC::Addressee &contact );
//AK_REVIEW: add method setItem( const Akonadi::Item ) and getter
/**
* Returns the contact that will be formatted.
*/
KABC::Addressee contact() const;
/**
* Sets the @p item who's payload will be formatted.
*
* @note The payload must be a valid KABC::Addressee object.
*/
void setItem( const Akonadi::Item &item );
/**
* Sets the custom field @p descriptions that shall be used.
* Returns the item who's payload will be formatted.
*/
//AK_REVIEW: change QVariantList to QList<QMap<QString, QVariant>>
//AK_REVIEW: add getter and make it non-abstract
virtual void setCustomFieldDescriptions( const QVariantList &descriptions ) = 0;
Akonadi::Item item() const;
/**
* Returns the contact formatted as HTML
* Sets the custom field @p descriptions that will be used.
*
* The description list contains a QVariantMap for each custom field
* with the following keys:
* - key (string) The identifier of the field
* - title (string) The i18n'ed title of the field
* - type (string) The type description of the field
* Possible values for type description are
* - text
* - numeric
* - boolean
* - date
* - time
* - datetime
*/
void setCustomFieldDescriptions( const QList<QVariantMap> &descriptions );
/**
* Returns the custom field descriptions that will be used.
*/
QList<QVariantMap> customFieldDescriptions() const;
/**
* This method must be reimplemented to return the contact formatted as HTML
* according to the requested @p form.
*/
virtual QString toHtml( HtmlForm form = SelfcontainedForm ) const = 0;
private:
//@cond PRIVATE
Q_DISABLE_COPY( AbstractContactFormatter )
class Private;
Private* const d;
//@endcond
};
}
......
......@@ -74,7 +74,9 @@ class ContactViewer::Private
}
// merge local and global custom field descriptions
QVariantList customFieldDescriptions( localCustomFieldDescriptions );
QList<QVariantMap> customFieldDescriptions;
foreach ( const QVariant &entry, localCustomFieldDescriptions )
customFieldDescriptions << entry.toMap();
const CustomField::List globalCustomFields = CustomFieldManager::globalCustomFieldDescriptions();
foreach ( const CustomField &field, globalCustomFields ) {
......
......@@ -21,6 +21,7 @@
#include "standardcontactformatter.h"
#include <akonadi/item.h>
#include <kabc/addressee.h>
#include <kcolorscheme.h>
#include <kglobal.h>
......@@ -31,36 +32,25 @@
using namespace Akonadi;
class StandardContactFormatter::Private
{
public:
KABC::Addressee mContact;
QVariantList mCustomFieldDescriptions;
};
StandardContactFormatter::StandardContactFormatter()
: d( new Private )
: d( 0 )
{
}
StandardContactFormatter::~StandardContactFormatter()
{
delete d;
}
void StandardContactFormatter::setContact( const KABC::Addressee &contact )
{
d->mContact = contact;
}
void StandardContactFormatter::setCustomFieldDescriptions( const QVariantList &descriptions )
{
d->mCustomFieldDescriptions = descriptions;
}
QString StandardContactFormatter::toHtml( HtmlForm form ) const
{
if ( d->mContact.isEmpty() )
KABC::Addressee rawContact;
const Akonadi::Item localItem = item();
if ( localItem.isValid() && localItem.hasPayload<KABC::Addressee>() )
rawContact = localItem.payload<KABC::Addressee>();
else
rawContact = contact();
if ( rawContact.isEmpty() )
return QString();
// We'll be building a table to display the vCard in.
......@@ -77,7 +67,7 @@ QString StandardContactFormatter::toHtml( HtmlForm form ) const
QString dynamicPart;
// Birthday
const QDate date = d->mContact.birthday().date();
const QDate date = rawContact.birthday().date();
const int years = (date.daysTo( QDate::currentDate() ) / 365);
if ( date.isValid() )
......@@ -88,7 +78,7 @@ QString StandardContactFormatter::toHtml( HtmlForm form ) const
// Phone Numbers
int counter = 0;
foreach ( const KABC::PhoneNumber &number, d->mContact.phoneNumbers() ) {
foreach ( const KABC::PhoneNumber &number, rawContact.phoneNumbers() ) {
const QString url = QString::fromLatin1( "<a href=\"phone:?index=%1\">%2</a>" ).arg( counter ).arg( number.number() );
counter++;
......@@ -98,10 +88,10 @@ QString StandardContactFormatter::toHtml( HtmlForm form ) const
}
// EMails
foreach ( const QString &email, d->mContact.emails() ) {
foreach ( const QString &email, rawContact.emails() ) {
QString type = i18nc( "a contact's email address", "Email" );
const QString fullEmail = QString::fromLatin1( KUrl::toPercentEncoding( d->mContact.fullEmail( email ) ) );
const QString fullEmail = QString::fromLatin1( KUrl::toPercentEncoding( rawContact.fullEmail( email ) ) );
dynamicPart += rowFmtStr.arg( type )
.arg( QString::fromLatin1( "<a href=\"mailto:%1\">%2</a>" )
......@@ -109,8 +99,8 @@ QString StandardContactFormatter::toHtml( HtmlForm form ) const
}
// Homepage
if ( d->mContact.url().isValid() ) {
QString url = d->mContact.url().url();
if ( rawContact.url().isValid() ) {
QString url = rawContact.url().url();
if ( !url.startsWith( QLatin1String( "http://" ) ) && !url.startsWith( QLatin1String( "https://" ) ) )
url = QLatin1String( "http://" ) + url;
......@@ -119,13 +109,13 @@ QString StandardContactFormatter::toHtml( HtmlForm form ) const
}
// Blog Feed
const QString blog = d->mContact.custom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "BlogFeed" ) );
const QString blog = rawContact.custom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "BlogFeed" ) );
if ( !blog.isEmpty() )
dynamicPart += rowFmtStr.arg( i18n( "Blog Feed" ) ).arg( KStringHandler::tagUrls( blog ) );
// Addresses
counter = 0;
foreach ( const KABC::Address &address, d->mContact.addresses() ) {
foreach ( const KABC::Address &address, rawContact.addresses() ) {
QString formattedAddress;
if ( address.label().isEmpty() ) {
......@@ -146,8 +136,8 @@ QString StandardContactFormatter::toHtml( HtmlForm form ) const
// Note
QString notes;
if ( !d->mContact.note().isEmpty() )
notes = rowFmtStr.arg( i18n( "Notes" ) ).arg( d->mContact.note().replace( QLatin1Char( '\n' ), QLatin1String( "<br>" ) ) ) ;
if ( !rawContact.note().isEmpty() )
notes = rowFmtStr.arg( i18n( "Notes" ) ).arg( rawContact.note().replace( QLatin1Char( '\n' ), QLatin1String( "<br>" ) ) ) ;
// Custom Data
QString customData;
......@@ -173,8 +163,8 @@ QString StandardContactFormatter::toHtml( HtmlForm form ) const
blacklistedKeys.insert( QLatin1String( "CRYPTOENCRYPTPREF" ) );
}
if ( !d->mContact.customs().empty() ) {
const QStringList customs = d->mContact.customs();
if ( !rawContact.customs().empty() ) {
const QStringList customs = rawContact.customs();
foreach ( QString custom, customs ) { //krazy:exclude=foreach
if ( custom.startsWith( QLatin1String( "KADDRESSBOOK-" ) ) ) {
custom.remove( QLatin1String( "KADDRESSBOOK-X-" ) );
......@@ -203,10 +193,9 @@ QString StandardContactFormatter::toHtml( HtmlForm form ) const
key = keyIt.value();
} else {
// check whether it is a custom local field
foreach ( const QVariant &description, d->mCustomFieldDescriptions ) {
const QVariantMap field = description.toMap();
if ( field.value( QLatin1String( "key" ) ).toString() == key ) {
key = field.value( QLatin1String( "title" ) ).toString();
foreach ( const QVariantMap &description, customFieldDescriptions() ) {
if ( description.value( QLatin1String( "key" ) ).toString() == key ) {
key = description.value( QLatin1String( "title" ) ).toString();
break;
}
}
......@@ -218,11 +207,11 @@ QString StandardContactFormatter::toHtml( HtmlForm form ) const
}
// Assemble all parts
QString role = d->mContact.title();
QString role = rawContact.title();
if ( role.isEmpty() )
role = d->mContact.role();
role = rawContact.role();
if ( role.isEmpty() )
role = d->mContact.custom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "X-Profession" ) );
role = rawContact.custom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "X-Profession" ) );
QString strAddr = QString::fromLatin1(
"<div align=\"center\">"
......@@ -240,9 +229,9 @@ QString StandardContactFormatter::toHtml( HtmlForm form ) const
"<td align=\"left\" width=\"70%\">%4</td>" // organization
"</tr>")
.arg( QLatin1String( "contact_photo" ) )
.arg( d->mContact.realName() )
.arg( rawContact.realName() )
.arg( role )
.arg( d->mContact.organization() );
.arg( rawContact.organization() );
strAddr.append( dynamicPart );
strAddr.append( notes );
......
......@@ -63,16 +63,6 @@ class AKONADI_CONTACT_EXPORT StandardContactFormatter : public AbstractContactFo
*/
virtual ~StandardContactFormatter();
/**
* Sets the @p contact that shall be formatted.
*/
virtual void setContact( const KABC::Addressee &contact );
/**
* Sets the custom field @p descriptions that shall be used.
*/
virtual void setCustomFieldDescriptions( const QVariantList &descriptions );
/**
* Returns the contact formatted as HTML
*/
......@@ -82,8 +72,6 @@ class AKONADI_CONTACT_EXPORT StandardContactFormatter : public AbstractContactFo
//@cond PRIVATE
class Private;
Private* const d;
Q_DISABLE_COPY( StandardContactFormatter )
//@endcond
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment