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
58 static int contactAge(
const QDate &date )
60 QDate now = QDate::currentDate();
61 int age = now.year() - date.year();
62 if ( date > now.addYears( -age ) ) {
70 KABC::Addressee rawContact;
73 rawContact = localItem.
payload<KABC::Addressee>();
77 if ( rawContact.isEmpty() )
83 QString rowFmtStr = QString::fromLatin1(
85 "<td align=\"right\" valign=\"top\" width=\"30%\"><b><font color=\"grey\">%1</font></b></td>\n"
86 "<td align=\"left\" valign=\"top\" width=\"70%\"><font>%2</font></td>\n"
94 const QDate date = rawContact.birthday().date();
95 const int years = contactAge( date );
98 dynamicPart += rowFmtStr
99 .arg( KABC::Addressee::birthdayLabel() )
100 .arg( KGlobal::locale()->formatDate( date ) +
101 QLatin1String(
" " ) + i18np(
"(One year old)",
"(%1 years old)", years ) );
105 foreach (
const KABC::PhoneNumber &number, rawContact.phoneNumbers() ) {
108 if ( number.type() & KABC::PhoneNumber::Cell )
109 url = QString::fromLatin1(
"<a href=\"phone:?index=%1\">%2</a> (<a href=\"sms:?index=%1\">SMS</a>)" ).arg( counter ).arg( number.number() );
111 url = QString::fromLatin1(
"<a href=\"phone:?index=%1\">%2</a>" ).arg( counter ).arg( number.number() );
115 dynamicPart += rowFmtStr
116 .arg( number.typeLabel().replace( QLatin1String(
" " ), QLatin1String(
" " ) ) )
121 foreach (
const QString &email, rawContact.emails() ) {
122 QString type = i18nc(
"a contact's email address",
"Email" );
124 const QString fullEmail = QString::fromLatin1( KUrl::toPercentEncoding( rawContact.fullEmail( email ) ) );
126 dynamicPart += rowFmtStr.arg( type )
127 .arg( QString::fromLatin1(
"<a href=\"mailto:%1\">%2</a>" )
128 .arg( fullEmail, email ) );
132 if ( rawContact.url().isValid() ) {
133 QString url = rawContact.url().url();
134 if ( !url.startsWith( QLatin1String(
"http://" ) ) && !url.startsWith( QLatin1String(
"https://" ) ) )
135 url = QLatin1String(
"http://" ) + url;
137 url = KStringHandler::tagUrls( url );
138 dynamicPart += rowFmtStr.arg( i18n(
"Homepage" ) ).arg( url );
142 const QString blog = rawContact.custom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"BlogFeed" ) );
143 if ( !blog.isEmpty() )
144 dynamicPart += rowFmtStr.arg( i18n(
"Blog Feed" ) ).arg( KStringHandler::tagUrls( blog ) );
148 foreach (
const KABC::Address &address, rawContact.addresses() ) {
149 QString formattedAddress;
151 if ( address.label().isEmpty() ) {
152 formattedAddress = address.formattedAddress().trimmed();
154 formattedAddress = address.label();
157 formattedAddress = formattedAddress.replace( QLatin1Char(
'\n' ), QLatin1String(
"<br>" ) );
159 const QString url = QString::fromLatin1(
"%1 <a href=\"address:?index=%2\"><img src=\"map_icon\" alt=\"%3\"/></a>" )
160 .arg( formattedAddress )
162 .arg( i18n(
"Show address on map" ) );
165 dynamicPart += rowFmtStr
166 .arg( KABC::Address::typeLabel( address.type() ) )
172 if ( !rawContact.note().isEmpty() )
173 notes = rowFmtStr.arg( i18n(
"Notes" ) ).arg( rawContact.note().replace( QLatin1Char(
'\n' ), QLatin1String(
"<br>" ) ) ) ;
177 static QMap<QString, QString> titleMap;
178 if ( titleMap.isEmpty() ) {
179 titleMap.insert( QLatin1String(
"Department" ), i18n(
"Department" ) );
180 titleMap.insert( QLatin1String(
"Profession" ), i18n(
"Profession" ) );
181 titleMap.insert( QLatin1String(
"AssistantsName" ), i18n(
"Assistant's Name" ) );
182 titleMap.insert( QLatin1String(
"ManagersName" ), i18n(
"Manager's Name" ) );
183 titleMap.insert( QLatin1String(
"SpousesName" ), i18nc(
"Wife/Husband/...",
"Partner's Name" ) );
184 titleMap.insert( QLatin1String(
"Office" ), i18n(
"Office" ) );
185 titleMap.insert( QLatin1String(
"IMAddress" ), i18n(
"IM Address" ) );
186 titleMap.insert( QLatin1String(
"Anniversary" ), i18n(
"Anniversary" ) );
187 titleMap.insert( QLatin1String(
"AddressBook" ), i18n(
"Address Book" ) );
190 static QSet<QString> blacklistedKeys;
191 if ( blacklistedKeys.isEmpty() ) {
192 blacklistedKeys.insert( QLatin1String(
"CRYPTOPROTOPREF" ) );
193 blacklistedKeys.insert( QLatin1String(
"OPENPGPFP" ) );
194 blacklistedKeys.insert( QLatin1String(
"SMIMEFP" ) );
195 blacklistedKeys.insert( QLatin1String(
"CRYPTOSIGNPREF" ) );
196 blacklistedKeys.insert( QLatin1String(
"CRYPTOENCRYPTPREF" ) );
197 blacklistedKeys.insert( QLatin1String(
"MailPreferedFormatting" ) );
198 blacklistedKeys.insert( QLatin1String(
"MailAllowToRemoteContent") );
201 if ( !rawContact.customs().empty() ) {
202 const QStringList customs = rawContact.customs();
203 foreach ( QString custom, customs ) {
204 if ( custom.startsWith( QLatin1String(
"KADDRESSBOOK-" ) ) ) {
205 custom.remove( QLatin1String(
"KADDRESSBOOK-X-" ) );
206 custom.remove( QLatin1String(
"KADDRESSBOOK-" ) );
208 int pos = custom.indexOf( QLatin1Char(
':' ) );
209 QString key = custom.left( pos );
210 QString value = custom.mid( pos + 1 );
213 if ( key == QLatin1String(
"Anniversary" ) || key == QLatin1String(
"ANNIVERSARY" ) ) {
214 const QDateTime dateTime = QDateTime::fromString( value, Qt::ISODate );
215 value = KGlobal::locale()->formatDate( dateTime.date() );
219 else if ( key == QLatin1String(
"BlogFeed" ) )
222 else if ( blacklistedKeys.contains( key ) )
226 const QMap<QString, QString>::ConstIterator keyIt = titleMap.constFind( key );
227 if ( keyIt != titleMap.constEnd() ) {
232 if ( description.value( QLatin1String(
"key" ) ).toString() == key ) {
233 key = description.value( QLatin1String(
"title" ) ).toString();
234 if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"boolean" ) ) {
235 if ( value == QLatin1String(
"true" ) )
236 value = i18nc(
"Boolean value",
"yes" );
238 value = i18nc(
"Boolean value",
"no" );
239 }
else if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"date" ) ) {
240 const QDate date = QDate::fromString( value, Qt::ISODate );
241 value = KGlobal::locale()->formatDate( date, KLocale::ShortDate );
242 }
else if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"time" ) ) {
243 const QTime time = QTime::fromString( value, Qt::ISODate );
244 value = KGlobal::locale()->formatTime( time );
245 }
else if ( description.value( QLatin1String(
"type" ) ) == QLatin1String(
"datetime" ) ) {
246 const QDateTime dateTime = QDateTime::fromString( value, Qt::ISODate );
247 value = KGlobal::locale()->formatDateTime( dateTime, KLocale::ShortDate );
254 customData += rowFmtStr.arg( key ).arg( value ) ;
260 QString role = rawContact.title();
261 if ( role.isEmpty() )
262 role = rawContact.role();
263 if ( role.isEmpty() )
264 role = rawContact.custom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"X-Profession" ) );
266 QString strAddr = QString::fromLatin1(
267 "<div align=\"center\">"
268 "<table cellpadding=\"3\" cellspacing=\"0\">"
270 "<td align=\"right\" valign=\"top\" width=\"30%\" rowspan=\"3\">"
271 "<img src=\"%1\" width=\"100\" vspace=\"1\">"
273 "<td align=\"left\" width=\"70%\"><font size=\"+2\"><b>%2</b></font></td>"
276 "<td align=\"left\" width=\"70%\">%3</td>"
279 "<td align=\"left\" width=\"70%\">%4</td>"
281 .arg( QLatin1String(
"contact_photo" ) )
282 .arg( rawContact.realName() )
284 .arg( rawContact.organization() );
286 strAddr.append( dynamicPart );
287 strAddr.append( notes );
288 strAddr.append( customData );
289 strAddr.append( QString::fromLatin1(
"</table>" ) );
292 if(d->displayQRcode) {
293 KConfig config( QLatin1String(
"akonadi_contactrc" ) );
294 KConfigGroup group( &config, QLatin1String(
"View" ) );
295 if ( group.readEntry(
"QRCodes",
true ) ) {
296 strAddr.append( QString::fromLatin1(
297 "<p align=\"center\">"
298 "<img src=\"%1\" vspace=\"1\">"
299 "<img src=\"%2\" vspace=\"1\">"
302 .arg( QLatin1String(
"datamatrix" ) )
303 .arg( QLatin1String(
"qrcode" ) ) );
306 #endif // HAVE_PRISON
308 strAddr.append( QString::fromLatin1(
"</div>\n" ) );
313 const QString document = QString::fromLatin1(
316 " <style type=\"text/css\">"
317 " a {text-decoration:none; color:%1}"
320 "<body text=\"%1\" bgcolor=\"%2\">"
324 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).foreground().color().name() )
325 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).background().color().name() )
331 void StandardContactFormatter::setDisplayQRCode(
bool show )
333 d->displayQRcode = show;
336 bool StandardContactFormatter::displayQRCode()
const
338 return d->displayQRcode;