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>
33 #include <QTextDocument>
35 using namespace Akonadi;
37 class StandardContactFormatter::Private
59 static int contactAge(
const QDate &date )
61 QDate now = QDate::currentDate();
62 int age = now.year() - date.year();
63 if ( date > now.addYears( -age ) ) {
71 KABC::Addressee rawContact;
72 const Akonadi::Item localItem =
item();
73 if ( localItem.isValid() && localItem.hasPayload<KABC::Addressee>() ) {
74 rawContact = localItem.payload<KABC::Addressee>();
79 if ( rawContact.isEmpty() ) {
86 QString rowFmtStr = QString::fromLatin1(
88 "<td align=\"right\" valign=\"top\" width=\"30%\"><b><font color=\"grey\">%1</font></b></td>\n"
89 "<td align=\"left\" valign=\"top\" width=\"70%\"><font>%2</font></td>\n"
97 const QDate date = rawContact.birthday().date();
98 const int years = contactAge( date );
100 if ( date.isValid() ) {
101 dynamicPart += rowFmtStr
102 .arg( KABC::Addressee::birthdayLabel() )
103 .arg( KGlobal::locale()->formatDate( date ) +
104 QLatin1String(
" " ) + i18np(
"(One year old)",
"(%1 years old)", years ) );
109 foreach (
const KABC::PhoneNumber &number, rawContact.phoneNumbers() ) {
112 if ( number.type() & KABC::PhoneNumber::Cell ) {
113 url = QString::fromLatin1(
"<a href=\"phone:?index=%1\">%2</a> (<a href=\"sms:?index=%1\">SMS</a>)" ).arg( counter ).arg( Qt::escape( number.number() ) );
115 url = QString::fromLatin1(
"<a href=\"phone:?index=%1\">%2</a>" ).arg( counter ).arg( Qt::escape( number.number() ) );
120 dynamicPart += rowFmtStr
121 .arg( number.typeLabel().replace( QLatin1String(
" " ), QLatin1String(
" " ) ) )
126 foreach (
const QString &email, rawContact.emails() ) {
127 const QString type = i18nc(
"a contact's email address",
"Email" );
129 const QString fullEmail = QString::fromLatin1( KUrl::toPercentEncoding( rawContact.fullEmail( email ) ) );
131 dynamicPart += rowFmtStr.arg( type )
132 .arg( QString::fromLatin1(
"<a href=\"mailto:%1\">%2</a>" )
133 .arg( fullEmail, email ) );
137 if ( rawContact.url().isValid() ) {
138 QString url = rawContact.url().url();
139 if ( !url.startsWith( QLatin1String(
"http://" ) ) && !url.startsWith( QLatin1String(
"https://" ) ) ) {
140 url = QLatin1String(
"http://" ) + url;
143 url = KStringHandler::tagUrls( Qt::escape( url ) );
144 dynamicPart += rowFmtStr.arg( i18n(
"Homepage" ) ).arg( url );
148 const QString blog = rawContact.custom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"BlogFeed" ) );
149 if ( !blog.isEmpty() ) {
150 dynamicPart += rowFmtStr.arg( i18n(
"Blog Feed" ) ).arg( KStringHandler::tagUrls( Qt::escape( blog ) ) );
155 foreach (
const KABC::Address &address, rawContact.addresses() ) {
156 QString formattedAddress;
158 if ( address.label().isEmpty() ) {
159 formattedAddress = Qt::escape( address.formattedAddress().trimmed() );
161 formattedAddress = Qt::escape( address.label() );
164 formattedAddress = formattedAddress.replace( QLatin1Char(
'\n' ), QLatin1String(
"<br>" ) );
166 const QString url = QString::fromLatin1(
"%1 <a href=\"address:?index=%2\"><img src=\"map_icon\" alt=\"%3\"/></a>" )
167 .arg( formattedAddress )
169 .arg( i18n(
"Show address on map" ) );
172 dynamicPart += rowFmtStr
173 .arg( KABC::Address::typeLabel( address.type() ) )
179 if ( !rawContact.note().isEmpty() ) {
180 notes = rowFmtStr.arg( i18n(
"Notes" ) ).arg( Qt::escape( rawContact.note() ).replace( QLatin1Char(
'\n' ), QLatin1String(
"<br>" ) ) ) ;
185 static QMap<QString, QString> titleMap;
186 if ( titleMap.isEmpty() ) {
187 titleMap.insert( QLatin1String(
"Department" ), i18n(
"Department" ) );
188 titleMap.insert( QLatin1String(
"Profession" ), i18n(
"Profession" ) );
189 titleMap.insert( QLatin1String(
"AssistantsName" ), i18n(
"Assistant's Name" ) );
190 titleMap.insert( QLatin1String(
"ManagersName" ), i18n(
"Manager's Name" ) );
191 titleMap.insert( QLatin1String(
"SpousesName" ), i18nc(
"Wife/Husband/...",
"Partner's Name" ) );
192 titleMap.insert( QLatin1String(
"Office" ), i18n(
"Office" ) );
193 titleMap.insert( QLatin1String(
"IMAddress" ), i18n(
"IM Address" ) );
194 titleMap.insert( QLatin1String(
"Anniversary" ), i18n(
"Anniversary" ) );
195 titleMap.insert( QLatin1String(
"AddressBook" ), i18n(
"Address Book" ) );
198 static QSet<QString> blacklistedKeys;
199 if ( blacklistedKeys.isEmpty() ) {
200 blacklistedKeys.insert( QLatin1String(
"CRYPTOPROTOPREF" ) );
201 blacklistedKeys.insert( QLatin1String(
"OPENPGPFP" ) );
202 blacklistedKeys.insert( QLatin1String(
"SMIMEFP" ) );
203 blacklistedKeys.insert( QLatin1String(
"CRYPTOSIGNPREF" ) );
204 blacklistedKeys.insert( QLatin1String(
"CRYPTOENCRYPTPREF" ) );
205 blacklistedKeys.insert( QLatin1String(
"MailPreferedFormatting" ) );
206 blacklistedKeys.insert( QLatin1String(
"MailAllowToRemoteContent" ) );
209 if ( !rawContact.customs().empty() ) {
210 const QStringList customs = rawContact.customs();
211 foreach ( QString custom, customs ) {
212 if ( custom.startsWith( QLatin1String(
"KADDRESSBOOK-" ) ) ) {
213 custom.remove( QLatin1String(
"KADDRESSBOOK-X-" ) );
214 custom.remove( QLatin1String(
"KADDRESSBOOK-" ) );
216 int pos = custom.indexOf( QLatin1Char(
':' ) );
217 QString key = custom.left( pos );
218 QString value = custom.mid( pos + 1 );
221 if ( key == QLatin1String(
"Anniversary" ) || key == QLatin1String(
"ANNIVERSARY" ) ) {
222 const QDateTime dateTime = QDateTime::fromString( value, Qt::ISODate );
223 value = KGlobal::locale()->formatDate( dateTime.date() );
224 }
else if ( key == QLatin1String(
"BlogFeed" ) ) {
226 }
else if ( blacklistedKeys.contains( key ) ) {
231 const QMap<QString, QString>::ConstIterator keyIt = titleMap.constFind( key );
232 if ( keyIt != titleMap.constEnd() ) {
237 if ( description.value( QLatin1String(
"key" ) ).toString() == key ) {
238 key = description.value( QLatin1String(
"title" ) ).toString();
239 if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"boolean" ) ) {
240 if ( value == QLatin1String(
"true" ) ) {
241 value = i18nc(
"Boolean value",
"yes" );
243 value = i18nc(
"Boolean value",
"no" );
245 }
else if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"date" ) ) {
246 const QDate date = QDate::fromString( value, Qt::ISODate );
247 value = KGlobal::locale()->formatDate( date, KLocale::ShortDate );
248 }
else if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"time" ) ) {
249 const QTime time = QTime::fromString( value, Qt::ISODate );
250 value = KGlobal::locale()->formatTime( time );
251 }
else if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"datetime" ) ) {
252 const QDateTime dateTime = QDateTime::fromString( value, Qt::ISODate );
253 value = KGlobal::locale()->formatDateTime( dateTime, KLocale::ShortDate );
260 customData += rowFmtStr.arg( key ).arg( Qt::escape( value ) ) ;
266 QString role = rawContact.title();
267 if ( role.isEmpty() ) {
268 role = rawContact.role();
270 if ( role.isEmpty() ) {
271 role = rawContact.custom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"X-Profession" ) );
274 QString strAddr = QString::fromLatin1(
275 "<div align=\"center\">"
276 "<table cellpadding=\"3\" cellspacing=\"0\">"
278 "<td align=\"right\" valign=\"top\" width=\"30%\" rowspan=\"3\">"
279 "<img src=\"%1\" width=\"100\" vspace=\"1\">"
281 "<td align=\"left\" width=\"70%\"><font size=\"+2\"><b>%2</b></font></td>"
284 "<td align=\"left\" width=\"70%\">%3</td>"
287 "<td align=\"left\" width=\"70%\">%4</td>"
289 .arg( QLatin1String(
"contact_photo" ) )
290 .arg( Qt::escape( rawContact.realName() ) )
291 .arg( Qt::escape( role ) )
292 .arg( Qt::escape( rawContact.organization() ) );
294 strAddr.append( dynamicPart );
295 strAddr.append( notes );
296 strAddr.append( customData );
297 strAddr.append( QString::fromLatin1(
"</table>" ) );
300 if(d->displayQRcode) {
301 KConfig config( QLatin1String(
"akonadi_contactrc" ) );
302 KConfigGroup group( &config, QLatin1String(
"View" ) );
303 if ( group.readEntry(
"QRCodes",
true ) ) {
304 strAddr.append( QString::fromLatin1(
305 "<p align=\"center\">"
306 "<img src=\"%1\" vspace=\"1\">"
307 "<img src=\"%2\" vspace=\"1\">"
310 .arg( QLatin1String(
"datamatrix" ) )
311 .arg( QLatin1String(
"qrcode" ) ) );
314 #endif // HAVE_PRISON
316 strAddr.append( QString::fromLatin1(
"</div>\n" ) );
322 const QString document = QString::fromLatin1(
325 " <style type=\"text/css\">"
326 " a {text-decoration:none; color:%1}"
329 "<body text=\"%1\" bgcolor=\"%2\">"
333 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).foreground().color().name() )
334 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).background().color().name() )
340 void StandardContactFormatter::setDisplayQRCode(
bool show )
342 d->displayQRcode = show;
345 bool StandardContactFormatter::displayQRCode()
const
347 return d->displayQRcode;