Changeset View
Changeset View
Standalone View
Standalone View
kaddressbookgrantlee/src/formatter/grantleecontactformatter.cpp
Show All 16 Lines | 1 | /* | |||
---|---|---|---|---|---|
17 | along with this library; see the file COPYING.LIB. If not, write to the | 17 | along with this library; see the file COPYING.LIB. If not, write to the | ||
18 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 18 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
19 | 02110-1301, USA. | 19 | 02110-1301, USA. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "grantleecontactformatter.h" | 22 | #include "grantleecontactformatter.h" | ||
23 | #include "grantleetheme/grantleetheme.h" | 23 | #include "grantleetheme/grantleetheme.h" | ||
24 | #include "grantleecontactutils.h" | 24 | #include "grantleecontactutils.h" | ||
25 | #include "../contactobject/contactgrantleeaddressobject.h" | | |||
26 | #include "../contactobject/contactgrantleeimobject.h" | 25 | #include "../contactobject/contactgrantleeimobject.h" | ||
27 | #include "../contactobject/contactgrantleecryptoobject.h" | 26 | #include "../contactobject/contactgrantleecryptoobject.h" | ||
28 | #include "../contactobject/contactgrantleewebsite.h" | 27 | #include "../contactobject/contactgrantleewebsite.h" | ||
29 | 28 | | |||
30 | #include <KContacts/Addressee> | 29 | #include <KContacts/Addressee> | ||
31 | #include <grantlee/context.h> | 30 | #include <grantlee/context.h> | ||
32 | #include <grantlee/engine.h> | 31 | #include <grantlee/engine.h> | ||
33 | #include <grantlee/metatype.h> | 32 | #include <grantlee/metatype.h> | ||
Show All 22 Lines | 54 | #define GRANTLEE_MAKE_GADGET(Class) \ | |||
56 | GRANTLEE_BEGIN_LOOKUP(Class) \ | 55 | GRANTLEE_BEGIN_LOOKUP(Class) \ | ||
57 | const auto idx = Class::staticMetaObject.indexOfProperty(property.toUtf8().constData()); \ | 56 | const auto idx = Class::staticMetaObject.indexOfProperty(property.toUtf8().constData()); \ | ||
58 | if (idx < 0) { \ | 57 | if (idx < 0) { \ | ||
59 | return {};} \ | 58 | return {};} \ | ||
60 | const auto mp = Class::staticMetaObject.property(idx); \ | 59 | const auto mp = Class::staticMetaObject.property(idx); \ | ||
61 | return mp.readOnGadget(&object); \ | 60 | return mp.readOnGadget(&object); \ | ||
62 | GRANTLEE_END_LOOKUP | 61 | GRANTLEE_END_LOOKUP | ||
63 | 62 | | |||
63 | GRANTLEE_MAKE_GADGET(KContacts::Address) | ||||
64 | GRANTLEE_MAKE_GADGET(KContacts::Email) | 64 | GRANTLEE_MAKE_GADGET(KContacts::Email) | ||
65 | GRANTLEE_MAKE_GADGET(KContacts::PhoneNumber) | 65 | GRANTLEE_MAKE_GADGET(KContacts::PhoneNumber) | ||
66 | 66 | | |||
67 | class Q_DECL_HIDDEN GrantleeContactFormatter::Private | 67 | class Q_DECL_HIDDEN GrantleeContactFormatter::Private | ||
68 | { | 68 | { | ||
69 | public: | 69 | public: | ||
70 | Private() | 70 | Private() | ||
71 | : forceDisableQRCode(false) | 71 | : forceDisableQRCode(false) | ||
Show All 38 Lines | |||||
110 | QString mErrorMessage; | 110 | QString mErrorMessage; | ||
111 | bool forceDisableQRCode = false; | 111 | bool forceDisableQRCode = false; | ||
112 | bool showQRCode = false; | 112 | bool showQRCode = false; | ||
113 | }; | 113 | }; | ||
114 | 114 | | |||
115 | GrantleeContactFormatter::GrantleeContactFormatter() | 115 | GrantleeContactFormatter::GrantleeContactFormatter() | ||
116 | : d(new Private) | 116 | : d(new Private) | ||
117 | { | 117 | { | ||
118 | Grantlee::registerMetaType<KContacts::Address>(); | ||||
118 | Grantlee::registerMetaType<KContacts::Email>(); | 119 | Grantlee::registerMetaType<KContacts::Email>(); | ||
119 | Grantlee::registerMetaType<KContacts::PhoneNumber>(); | 120 | Grantlee::registerMetaType<KContacts::PhoneNumber>(); | ||
120 | } | 121 | } | ||
121 | 122 | | |||
122 | GrantleeContactFormatter::~GrantleeContactFormatter() | 123 | GrantleeContactFormatter::~GrantleeContactFormatter() | ||
123 | { | 124 | { | ||
124 | delete d; | 125 | delete d; | ||
125 | } | 126 | } | ||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | 163 | { | |||
171 | const QString iconUrl = QUrl::fromLocalFile(KIconLoader::global()->iconPath(IMProtocols::self()->icon(typeKey), | 172 | const QString iconUrl = QUrl::fromLocalFile(KIconLoader::global()->iconPath(IMProtocols::self()->icon(typeKey), | ||
172 | -KIconLoader::SizeSmall)).url(); | 173 | -KIconLoader::SizeSmall)).url(); | ||
173 | const QString url = QStringLiteral("<img src=\"%1\" align=\"top\" height=\"%2\" width=\"%2\"/>").arg(iconUrl, QString::number(KIconLoader::SizeSmall)); | 174 | const QString url = QStringLiteral("<img src=\"%1\" align=\"top\" height=\"%2\" width=\"%2\"/>").arg(iconUrl, QString::number(KIconLoader::SizeSmall)); | ||
174 | addressObject.insert(QStringLiteral("imIcon"), url); | 175 | addressObject.insert(QStringLiteral("imIcon"), url); | ||
175 | 176 | | |||
176 | return addressObject; | 177 | return addressObject; | ||
177 | } | 178 | } | ||
178 | 179 | | |||
179 | static QVariantHash addressHash(const KContacts::Address &address, int counter) | | |||
180 | { | | |||
181 | QVariantHash addressObject; | | |||
182 | | ||||
183 | setHashField(addressObject, QStringLiteral("type"), | | |||
184 | KContacts::Address::typeLabel(address.type())); | | |||
185 | | ||||
186 | setHashField(addressObject, QStringLiteral("street"), address.street()); | | |||
187 | | ||||
188 | setHashField(addressObject, QStringLiteral("postOfficeBox"), address.postOfficeBox()); | | |||
189 | | ||||
190 | setHashField(addressObject, QStringLiteral("locality"), address.locality()); | | |||
191 | | ||||
192 | setHashField(addressObject, QStringLiteral("region"), address.region()); | | |||
193 | | ||||
194 | setHashField(addressObject, QStringLiteral("postalCode"), address.postalCode()); | | |||
195 | | ||||
196 | setHashField(addressObject, QStringLiteral("country"), address.country()); | | |||
197 | | ||||
198 | setHashField(addressObject, QStringLiteral("label"), address.label()); | | |||
199 | | ||||
200 | setHashField(addressObject, QStringLiteral("formattedAddress"), address.formattedAddress()); | | |||
201 | | ||||
202 | QString formattedAddress; | | |||
203 | | ||||
204 | if (address.label().isEmpty()) { | | |||
205 | formattedAddress = address.formattedAddress().trimmed(); | | |||
206 | } else { | | |||
207 | formattedAddress = address.label(); | | |||
208 | } | | |||
209 | | ||||
210 | if (!formattedAddress.isEmpty()) { | | |||
211 | //Replace all \n by only one <br/> | | |||
212 | formattedAddress = formattedAddress.replace(QRegularExpression(QStringLiteral("\n+")), QStringLiteral("<br/>")); | | |||
213 | | ||||
214 | const QString link = QStringLiteral("<a href=\"address:?index=%1\">%2</a>"). | | |||
215 | arg(counter); | | |||
216 | QString url = link.arg(formattedAddress); | | |||
217 | addressObject.insert(QStringLiteral("formattedAddressLink"), url); | | |||
218 | | ||||
219 | url = link.arg(QStringLiteral("<img src=\"map_icon\" align=\"top\"/>")); | | |||
220 | addressObject.insert(QStringLiteral("formattedAddressIcon"), url); | | |||
221 | } | | |||
222 | | ||||
223 | return addressObject; | | |||
224 | } | | |||
225 | | ||||
226 | static int contactAge(const QDate &date) | 180 | static int contactAge(const QDate &date) | ||
227 | { | 181 | { | ||
228 | QDate now = QDate::currentDate(); | 182 | QDate now = QDate::currentDate(); | ||
229 | int age = now.year() - date.year(); | 183 | int age = now.year() - date.year(); | ||
230 | if (date > now.addYears(-age)) { | 184 | if (date > now.addYears(-age)) { | ||
231 | age--; | 185 | age--; | ||
232 | } | 186 | } | ||
233 | return age; | 187 | return age; | ||
▲ Show 20 Lines • Show All 104 Lines • ▼ Show 20 Line(s) | 191 | { | |||
338 | const QString addressBookName | 292 | const QString addressBookName | ||
339 | = rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("AddressBook")); | 293 | = rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("AddressBook")); | ||
340 | if (!addressBookName.isEmpty()) { | 294 | if (!addressBookName.isEmpty()) { | ||
341 | contactObject.insert(QStringLiteral("addressBookName"), addressBookName); | 295 | contactObject.insert(QStringLiteral("addressBookName"), addressBookName); | ||
342 | grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("addressBookNamei18n")); | 296 | grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("addressBookNamei18n")); | ||
343 | } | 297 | } | ||
344 | 298 | | |||
345 | // Addresses | 299 | // Addresses | ||
346 | QVariantList addresses; | 300 | contactObject.insert(QStringLiteral("addresses"), QVariant::fromValue(rawContact.addresses())); | ||
347 | int counter = 0; | 301 | | ||
348 | const KContacts::Address::List lstAddresses = rawContact.addresses(); | | |||
349 | addresses.reserve(lstAddresses.count()); | | |||
350 | for (const KContacts::Address &address : lstAddresses) { | | |||
351 | addresses.append(addressHash(address, counter)); | | |||
352 | counter++; | | |||
353 | } | | |||
354 | // Note | 302 | // Note | ||
355 | if (!rawContact.note().isEmpty()) { | 303 | if (!rawContact.note().isEmpty()) { | ||
356 | const QString notes = QStringLiteral("<a>%1</a>").arg(rawContact.note().replace(QLatin1Char('\n'), QStringLiteral("<br>"))); | 304 | const QString notes = QStringLiteral("<a>%1</a>").arg(rawContact.note().replace(QLatin1Char('\n'), QStringLiteral("<br>"))); | ||
357 | contactObject.insert(QStringLiteral("note"), notes); | 305 | contactObject.insert(QStringLiteral("note"), notes); | ||
358 | grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("notei18n")); | 306 | grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("notei18n")); | ||
359 | } | 307 | } | ||
360 | 308 | | |||
361 | contactObject.insert(QStringLiteral("addresses"), addresses); | | |||
362 | 309 | | |||
363 | setHashField(contactObject, QStringLiteral("mailer"), rawContact.mailer()); | 310 | setHashField(contactObject, QStringLiteral("mailer"), rawContact.mailer()); | ||
364 | 311 | | |||
365 | setHashField(contactObject, QStringLiteral("title"), rawContact.title()); | 312 | setHashField(contactObject, QStringLiteral("title"), rawContact.title()); | ||
366 | 313 | | |||
367 | setHashField(contactObject, QStringLiteral("role"), rawContact.role()); | 314 | setHashField(contactObject, QStringLiteral("role"), rawContact.role()); | ||
368 | 315 | | |||
369 | QString job = rawContact.title(); | 316 | QString job = rawContact.title(); | ||
▲ Show 20 Lines • Show All 163 Lines • Show Last 20 Lines |