22 #include "standardcontactformatter.h"
24 #include <akonadi/item.h>
25 #include <kabc/addressee.h>
26 #include <kcolorscheme.h>
27 #include <kconfiggroup.h>
30 #include <kstringhandler.h>
32 #include <QtCore/QSet>
34 using namespace Akonadi;
36 class StandardContactFormatter::Private
48 StandardContactFormatter::StandardContactFormatter()
53 StandardContactFormatter::~StandardContactFormatter()
58 QString StandardContactFormatter::toHtml( HtmlForm form )
const
60 KABC::Addressee rawContact;
61 const Akonadi::Item localItem = item();
62 if ( localItem.isValid() && localItem.hasPayload<KABC::Addressee>() )
63 rawContact = localItem.payload<KABC::Addressee>();
65 rawContact = contact();
67 if ( rawContact.isEmpty() )
73 QString rowFmtStr = QString::fromLatin1(
75 "<td align=\"right\" valign=\"top\" width=\"30%\"><b><font color=\"grey\">%1</font></b></td>\n"
76 "<td align=\"left\" valign=\"top\" width=\"70%\"><font>%2</font></td>\n"
84 const QDate date = rawContact.birthday().date();
85 const int years = (date.daysTo( QDate::currentDate() ) / 365);
88 dynamicPart += rowFmtStr
89 .arg( KABC::Addressee::birthdayLabel() )
90 .arg( KGlobal::locale()->formatDate( date ) +
91 QLatin1String(
" " ) + i18np(
"(One year old)",
"(%1 years old)", years ) );
95 foreach (
const KABC::PhoneNumber &number, rawContact.phoneNumbers() ) {
98 if ( number.type() & KABC::PhoneNumber::Cell )
99 url = QString::fromLatin1(
"<a href=\"phone:?index=%1\">%2</a> (<a href=\"sms:?index=%1\">SMS</a>)" ).arg( counter ).arg( number.number() );
101 url = QString::fromLatin1(
"<a href=\"phone:?index=%1\">%2</a>" ).arg( counter ).arg( number.number() );
105 dynamicPart += rowFmtStr
106 .arg( number.typeLabel().replace( QLatin1String(
" " ), QLatin1String(
" " ) ) )
111 foreach (
const QString &email, rawContact.emails() ) {
112 QString type = i18nc(
"a contact's email address",
"Email" );
114 const QString fullEmail = QString::fromLatin1( KUrl::toPercentEncoding( rawContact.fullEmail( email ) ) );
116 dynamicPart += rowFmtStr.arg( type )
117 .arg( QString::fromLatin1(
"<a href=\"mailto:%1\">%2</a>" )
118 .arg( fullEmail, email ) );
122 if ( rawContact.url().isValid() ) {
123 QString url = rawContact.url().url();
124 if ( !url.startsWith( QLatin1String(
"http://" ) ) && !url.startsWith( QLatin1String(
"https://" ) ) )
125 url = QLatin1String(
"http://" ) + url;
127 url = KStringHandler::tagUrls( url );
128 dynamicPart += rowFmtStr.arg( i18n(
"Homepage" ) ).arg( url );
132 const QString blog = rawContact.custom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"BlogFeed" ) );
133 if ( !blog.isEmpty() )
134 dynamicPart += rowFmtStr.arg( i18n(
"Blog Feed" ) ).arg( KStringHandler::tagUrls( blog ) );
138 foreach (
const KABC::Address &address, rawContact.addresses() ) {
139 QString formattedAddress;
141 if ( address.label().isEmpty() ) {
142 formattedAddress = address.formattedAddress().trimmed();
144 formattedAddress = address.label();
147 formattedAddress = formattedAddress.replace( QLatin1Char(
'\n' ), QLatin1String(
"<br>" ) );
149 const QString url = QString::fromLatin1(
"%1 <a href=\"address:?index=%2\"><img src=\"map_icon\" alt=\"%3\"/></a>" )
150 .arg( formattedAddress )
152 .arg( i18n(
"Show address on map" ) );
155 dynamicPart += rowFmtStr
156 .arg( KABC::Address::typeLabel( address.type() ) )
162 if ( !rawContact.note().isEmpty() )
163 notes = rowFmtStr.arg( i18n(
"Notes" ) ).arg( rawContact.note().replace( QLatin1Char(
'\n' ), QLatin1String(
"<br>" ) ) ) ;
167 static QMap<QString, QString> titleMap;
168 if ( titleMap.isEmpty() ) {
169 titleMap.insert( QLatin1String(
"Department" ), i18n(
"Department" ) );
170 titleMap.insert( QLatin1String(
"Profession" ), i18n(
"Profession" ) );
171 titleMap.insert( QLatin1String(
"AssistantsName" ), i18n(
"Assistant's Name" ) );
172 titleMap.insert( QLatin1String(
"ManagersName" ), i18n(
"Manager's Name" ) );
173 titleMap.insert( QLatin1String(
"SpousesName" ), i18nc(
"Wife/Husband/...",
"Partner's Name" ) );
174 titleMap.insert( QLatin1String(
"Office" ), i18n(
"Office" ) );
175 titleMap.insert( QLatin1String(
"IMAddress" ), i18n(
"IM Address" ) );
176 titleMap.insert( QLatin1String(
"Anniversary" ), i18n(
"Anniversary" ) );
177 titleMap.insert( QLatin1String(
"AddressBook" ), i18n(
"Address Book" ) );
180 static QSet<QString> blacklistedKeys;
181 if ( blacklistedKeys.isEmpty() ) {
182 blacklistedKeys.insert( QLatin1String(
"CRYPTOPROTOPREF" ) );
183 blacklistedKeys.insert( QLatin1String(
"OPENPGPFP" ) );
184 blacklistedKeys.insert( QLatin1String(
"SMIMEFP" ) );
185 blacklistedKeys.insert( QLatin1String(
"CRYPTOSIGNPREF" ) );
186 blacklistedKeys.insert( QLatin1String(
"CRYPTOENCRYPTPREF" ) );
187 blacklistedKeys.insert( QLatin1String(
"MailPreferedFormatting" ) );
188 blacklistedKeys.insert( QLatin1String(
"MailAllowToRemoteContent") );
191 if ( !rawContact.customs().empty() ) {
192 const QStringList customs = rawContact.customs();
193 foreach ( QString custom, customs ) {
194 if ( custom.startsWith( QLatin1String(
"KADDRESSBOOK-" ) ) ) {
195 custom.remove( QLatin1String(
"KADDRESSBOOK-X-" ) );
196 custom.remove( QLatin1String(
"KADDRESSBOOK-" ) );
198 int pos = custom.indexOf( QLatin1Char(
':' ) );
199 QString key = custom.left( pos );
200 QString value = custom.mid( pos + 1 );
203 if ( key == QLatin1String(
"Anniversary" ) ) {
204 const QDateTime dateTime = QDateTime::fromString( value, Qt::ISODate );
205 value = KGlobal::locale()->formatDate( dateTime.date() );
209 else if ( key == QLatin1String(
"BlogFeed" ) )
212 else if ( blacklistedKeys.contains( key ) )
216 const QMap<QString, QString>::ConstIterator keyIt = titleMap.constFind( key );
217 if ( keyIt != titleMap.constEnd() ) {
221 foreach (
const QVariantMap &description, customFieldDescriptions() ) {
222 if ( description.value( QLatin1String(
"key" ) ).toString() == key ) {
223 key = description.value( QLatin1String(
"title" ) ).toString();
224 if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"boolean" ) ) {
225 if ( value == QLatin1String(
"true" ) )
226 value = i18nc(
"Boolean value",
"yes" );
228 value = i18nc(
"Boolean value",
"no" );
229 }
else if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"date" ) ) {
230 const QDate date = QDate::fromString( value, Qt::ISODate );
231 value = KGlobal::locale()->formatDate( date, KLocale::ShortDate );
232 }
else if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"time" ) ) {
233 const QTime time = QTime::fromString( value, Qt::ISODate );
234 value = KGlobal::locale()->formatTime( time );
235 }
else if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"datetime" ) ) {
236 const QDateTime dateTime = QDateTime::fromString( value, Qt::ISODate );
237 value = KGlobal::locale()->formatDateTime( dateTime, KLocale::ShortDate );
244 customData += rowFmtStr.arg( key ).arg( value ) ;
250 QString role = rawContact.title();
251 if ( role.isEmpty() )
252 role = rawContact.role();
253 if ( role.isEmpty() )
254 role = rawContact.custom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"X-Profession" ) );
256 QString strAddr = QString::fromLatin1(
257 "<div align=\"center\">"
258 "<table cellpadding=\"3\" cellspacing=\"0\">"
260 "<td align=\"right\" valign=\"top\" width=\"30%\" rowspan=\"3\">"
261 "<img src=\"%1\" width=\"100\" vspace=\"1\">"
263 "<td align=\"left\" width=\"70%\"><font size=\"+2\"><b>%2</b></font></td>"
266 "<td align=\"left\" width=\"70%\">%3</td>"
269 "<td align=\"left\" width=\"70%\">%4</td>"
271 .arg( QLatin1String(
"contact_photo" ) )
272 .arg( rawContact.realName() )
274 .arg( rawContact.organization() );
276 strAddr.append( dynamicPart );
277 strAddr.append( notes );
278 strAddr.append( customData );
279 strAddr.append( QString::fromLatin1(
"</table>" ) );
282 if(d->displayQRcode) {
283 KConfig config( QLatin1String(
"akonadi_contactrc" ) );
284 KConfigGroup group( &config, QLatin1String(
"View" ) );
285 if ( group.readEntry(
"QRCodes",
true ) ) {
286 strAddr.append( QString::fromLatin1(
287 "<p align=\"center\">"
288 "<img src=\"%1\" vspace=\"1\">"
289 "<img src=\"%2\" vspace=\"1\">"
292 .arg( QLatin1String(
"datamatrix" ) )
293 .arg( QLatin1String(
"qrcode" ) ) );
296 #endif // HAVE_PRISON
298 strAddr.append( QString::fromLatin1(
"</div>\n" ) );
300 if ( form == EmbeddableForm )
303 const QString document = QString::fromLatin1(
306 " <style type=\"text/css\">"
307 " a {text-decoration:none; color:%1}"
310 "<body text=\"%1\" bgcolor=\"%2\">"
314 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).foreground().color().name() )
315 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).background().color().name() )
321 void StandardContactFormatter::setDisplayQRCode(
bool show )
323 d->displayQRcode = show;
326 bool StandardContactFormatter::displayQRCode()
const
328 return d->displayQRcode;