diff --git a/kaddressbookgrantlee/src/CMakeLists.txt b/kaddressbookgrantlee/src/CMakeLists.txt index 8520f1a..6a7ac42 100644 --- a/kaddressbookgrantlee/src/CMakeLists.txt +++ b/kaddressbookgrantlee/src/CMakeLists.txt @@ -1,122 +1,121 @@ add_definitions(-DTRANSLATION_DOMAIN=\"libkaddressbookgrantlee\") set(kaddressbook_grantlee_LIB_SRCS widget/grantleecontactviewer.cpp formatter/grantleecontactformatter.cpp formatter/grantleecontactgroupformatter.cpp formatter/grantleecontactutils.cpp ) set(kaddressbook_grantlee_object_LIB_SRCS contactobject/contactgrantleeaddressobject.cpp contactobject/contactgrantleecryptoobject.cpp contactobject/contactgrantleeimobject.cpp contactobject/contactgrantleephoneobject.cpp contactobject/contactgrantleewebsite.cpp - contactobject/contactgrantleeemail.cpp ) set(kaddressbook_grantlee_printing_LIB_SRCS printing/contactgrantleeprintobject.cpp printing/grantleeprint.cpp ) add_library(KF5KaddressbookGrantlee ${kaddressbook_grantlee_LIB_SRCS} ${kaddressbook_grantlee_printing_LIB_SRCS} ${kaddressbook_grantlee_object_LIB_SRCS}) generate_export_header(KF5KaddressbookGrantlee BASE_NAME kaddressbook_grantlee) add_library(KF5::KaddressbookGrantlee ALIAS KF5KaddressbookGrantlee) target_include_directories(KF5KaddressbookGrantlee INTERFACE "$") target_include_directories(KF5KaddressbookGrantlee PUBLIC "$") target_link_libraries(KF5KaddressbookGrantlee PUBLIC Grantlee5::Templates PRIVATE KF5::ConfigWidgets KF5::Service KF5::GrantleeTheme KF5::AkonadiCore KF5::ContactEditor KF5::Contacts KF5::AkonadiContact KF5::IconThemes KF5::Libkleo KF5::I18n KF5::PimCommon ) set_target_properties(KF5KaddressbookGrantlee PROPERTIES VERSION ${KADDRESSBOOKGRANTLEE_VERSION_STRING} SOVERSION ${KADDRESSBOOKGRANTLEE_SOVERSION} EXPORT_NAME KaddressbookGrantlee ) install(TARGETS KF5KaddressbookGrantlee EXPORT KF5KaddressbookGrantleeTargets ${KF5_INSTALL_TARGETS_DEFAULT_ARGS} ${LIBRARY_NAMELINK} ) ecm_generate_headers(KaddressbookGrantlee_CamelCaseformatter_HEADERS HEADER_NAMES GrantleeContactFormatter GrantleeContactGroupFormatter REQUIRED_HEADERS KaddressbookGrantlee_formatter_HEADERS RELATIVE formatter PREFIX KaddressbookGrantlee ) ecm_generate_headers(KaddressbookGrantlee_CamelCasewidgets_HEADERS HEADER_NAMES GrantleeContactViewer REQUIRED_HEADERS KaddressbookGrantlee_widgets_HEADERS RELATIVE widget PREFIX KaddressbookGrantlee ) ecm_generate_headers(KaddressbookGrantlee_CamelCaseprinting_HEADERS HEADER_NAMES GrantleePrint REQUIRED_HEADERS KaddressbookGrantlee_printing_HEADERS RELATIVE printing PREFIX KaddressbookGrantlee ) ecm_generate_pri_file(BASE_NAME KaddressbookGrantlee LIB_NAME KF5KaddressbookGrantlee DEPS "Grantlee5" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KDE_INSTALL_INCLUDEDIR_KF5}/KaddressbookGrantlee ) install(FILES ${KaddressbookGrantlee_CamelCasewidgets_HEADERS} ${KaddressbookGrantlee_CamelCaseformatter_HEADERS} ${KaddressbookGrantlee_CamelCaseprinting_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KaddressbookGrantlee COMPONENT Devel ) install(FILES ${KaddressbookGrantlee_HEADERS} ${KaddressbookGrantlee_widgets_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/kaddressbook_grantlee_export.h ${KaddressbookGrantlee_formatter_HEADERS} ${KaddressbookGrantlee_printing_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/kaddressbookgrantlee COMPONENT Devel ) install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) if (BUILD_TESTING) add_subdirectory(printing/autotests) endif() diff --git a/kaddressbookgrantlee/src/contactobject/contactgrantleeemail.cpp b/kaddressbookgrantlee/src/contactobject/contactgrantleeemail.cpp deleted file mode 100644 index 191fd2c..0000000 --- a/kaddressbookgrantlee/src/contactobject/contactgrantleeemail.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - Copyright (C) 2016-2019 Montel Laurent - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; see the file COPYING. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#include "contactgrantleeemail.h" -using namespace KAddressBookGrantlee; - -ContactGrantleeEmail::ContactGrantleeEmail(const KContacts::Addressee &addressee, const KContacts::Email &email, QObject *parent) - : QObject(parent) - , mEmail(email) - , mAddressee(addressee) -{ -} - -ContactGrantleeEmail::~ContactGrantleeEmail() -{ -} - -QString ContactGrantleeEmail::email() const -{ - const QString email = mEmail.mail(); - return mAddressee.fullEmail(email); -} - -bool ContactGrantleeEmail::preferred() const -{ - const QStringList value = mEmail.parameters().value(QStringLiteral("type")); - //Validate PREF and pref - return value.contains(QLatin1String("pref"), Qt::CaseInsensitive); -} - -QString ContactGrantleeEmail::emailFormatted() const -{ - const QString email = mEmail.mail(); - const QString fullEmail = QString::fromLatin1(QUrl::toPercentEncoding(mAddressee.fullEmail(email))); - - const QString url = QStringLiteral("%2") - .arg(fullEmail, email); - return url; -} diff --git a/kaddressbookgrantlee/src/contactobject/contactgrantleeemail.h b/kaddressbookgrantlee/src/contactobject/contactgrantleeemail.h deleted file mode 100644 index 430fa9d..0000000 --- a/kaddressbookgrantlee/src/contactobject/contactgrantleeemail.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - Copyright (C) 2016-2019 Montel Laurent - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; see the file COPYING. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#ifndef PRINT_CONTACTGRANTLEEEMAIL_H -#define PRINT_CONTACTGRANTLEEEMAIL_H - -#include - -#include -#include - -namespace KAddressBookGrantlee { -class ContactGrantleeEmail : public QObject -{ - Q_OBJECT - Q_PROPERTY(QString emailFormatted READ emailFormatted) - Q_PROPERTY(QString email READ email) - Q_PROPERTY(bool preferred READ preferred) -public: - explicit ContactGrantleeEmail(const KContacts::Addressee &addressee, const KContacts::Email &email, QObject *parent = nullptr); - ~ContactGrantleeEmail(); - Q_REQUIRED_RESULT QString emailFormatted() const; - - Q_REQUIRED_RESULT QString email() const; - Q_REQUIRED_RESULT bool preferred() const; -private: - KContacts::Email mEmail; - KContacts::Addressee mAddressee; -}; -} -#endif // PRINT_CONTACTGRANTLEEEMAIL_H diff --git a/kaddressbookgrantlee/src/formatter/grantleecontactformatter.cpp b/kaddressbookgrantlee/src/formatter/grantleecontactformatter.cpp index 21e7712..7da0bf5 100644 --- a/kaddressbookgrantlee/src/formatter/grantleecontactformatter.cpp +++ b/kaddressbookgrantlee/src/formatter/grantleecontactformatter.cpp @@ -1,559 +1,558 @@ /* This file is part of KAddressBook. Copyright (c) 2010 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "grantleecontactformatter.h" #include "grantleetheme/grantleetheme.h" #include "grantleecontactutils.h" #include "../contactobject/contactgrantleeaddressobject.h" #include "../contactobject/contactgrantleephoneobject.h" #include "../contactobject/contactgrantleeimobject.h" #include "../contactobject/contactgrantleecryptoobject.h" #include "../contactobject/contactgrantleewebsite.h" -#include "../contactobject/contactgrantleeemail.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace KAddressBookGrantlee; class Q_DECL_HIDDEN GrantleeContactFormatter::Private { public: Private() : forceDisableQRCode(false) , showQRCode(true) { KConfig config(QStringLiteral("akonadi_contactrc")); KConfigGroup group(&config, QStringLiteral("View")); showQRCode = group.readEntry("QRCodes", true); mEngine = new Grantlee::Engine; mTemplateLoader = QSharedPointer(new Grantlee::FileSystemTemplateLoader()); } ~Private() { delete mEngine; mTemplateLoader.clear(); } void changeGrantleePath(const QString &path) { mTemplateLoader->setTemplateDirs(QStringList() << path); mEngine->addTemplateLoader(mTemplateLoader); mSelfcontainedTemplate = mEngine->loadByName(QStringLiteral("contact.html")); if (mSelfcontainedTemplate->error()) { mErrorMessage += mSelfcontainedTemplate->errorString() + QStringLiteral("
"); } mEmbeddableTemplate = mEngine->loadByName(QStringLiteral("contact_embedded.html")); if (mEmbeddableTemplate->error()) { mErrorMessage += mEmbeddableTemplate->errorString() + QStringLiteral("
"); } } QVector mObjects; Grantlee::Engine *mEngine = nullptr; QSharedPointer mTemplateLoader; Grantlee::Template mSelfcontainedTemplate; Grantlee::Template mEmbeddableTemplate; QString mErrorMessage; bool forceDisableQRCode = false; bool showQRCode = false; }; GrantleeContactFormatter::GrantleeContactFormatter() : d(new Private) { } GrantleeContactFormatter::~GrantleeContactFormatter() { delete d; } void GrantleeContactFormatter::setAbsoluteThemePath(const QString &path) { d->changeGrantleePath(path); } void GrantleeContactFormatter::setGrantleeTheme(const GrantleeTheme::Theme &theme) { d->changeGrantleePath(theme.absolutePath()); } void GrantleeContactFormatter::setForceDisableQRCode(bool b) { if (d->forceDisableQRCode != b) { d->forceDisableQRCode = b; } } bool GrantleeContactFormatter::forceDisableQRCode() const { return d->forceDisableQRCode; } void GrantleeContactFormatter::setShowQRCode(bool b) { d->showQRCode = b; } inline static void setHashField(QVariantHash &hash, const QString &name, const QString &value) { if (!value.isEmpty()) { hash.insert(name, value); } } static QVariantHash phoneNumberHash(const KContacts::PhoneNumber &phoneNumber, int counter) { QVariantHash numberObject; setHashField(numberObject, QStringLiteral("type"), phoneNumber.typeLabel()); setHashField(numberObject, QStringLiteral("number"), phoneNumber.number()); if (!phoneNumber.isEmpty()) { const QString url = QStringLiteral("%2"). arg(counter). arg(phoneNumber.number()); numberObject.insert(QStringLiteral("numberLink"), url); if (phoneNumber.type() & KContacts::PhoneNumber::Cell) { const QString url = QStringLiteral("").arg(counter); numberObject.insert(QStringLiteral("smsLink"), url); } } return numberObject; } static QVariantHash imAddressHash(const QString &typeKey, const QString &imAddress) { QVariantHash addressObject; const QString dispLabel = i18nc("@title:row label for an Instant Messaging address, %1 is I18Ned protocol name", "IM (%1)", IMProtocols::self()->name(typeKey)); setHashField(addressObject, QStringLiteral("type"), dispLabel); setHashField(addressObject, QStringLiteral("imAddress"), imAddress); const QString iconUrl = QUrl::fromLocalFile(KIconLoader::global()->iconPath(IMProtocols::self()->icon(typeKey), -KIconLoader::SizeSmall)).url(); const QString url = QStringLiteral("").arg(iconUrl, QString::number(KIconLoader::SizeSmall)); addressObject.insert(QStringLiteral("imIcon"), url); return addressObject; } static QVariantHash addressHash(const KContacts::Address &address, int counter) { QVariantHash addressObject; setHashField(addressObject, QStringLiteral("type"), KContacts::Address::typeLabel(address.type())); setHashField(addressObject, QStringLiteral("street"), address.street()); setHashField(addressObject, QStringLiteral("postOfficeBox"), address.postOfficeBox()); setHashField(addressObject, QStringLiteral("locality"), address.locality()); setHashField(addressObject, QStringLiteral("region"), address.region()); setHashField(addressObject, QStringLiteral("postalCode"), address.postalCode()); setHashField(addressObject, QStringLiteral("country"), address.country()); setHashField(addressObject, QStringLiteral("label"), address.label()); setHashField(addressObject, QStringLiteral("formattedAddress"), address.formattedAddress()); QString formattedAddress; if (address.label().isEmpty()) { formattedAddress = address.formattedAddress().trimmed(); } else { formattedAddress = address.label(); } if (!formattedAddress.isEmpty()) { //Replace all \n by only one
formattedAddress = formattedAddress.replace(QRegularExpression(QStringLiteral("\n+")), QStringLiteral("
")); const QString link = QStringLiteral("%2"). arg(counter); QString url = link.arg(formattedAddress); addressObject.insert(QStringLiteral("formattedAddressLink"), url); url = link.arg(QStringLiteral("")); addressObject.insert(QStringLiteral("formattedAddressIcon"), url); } return addressObject; } static int contactAge(const QDate &date) { QDate now = QDate::currentDate(); int age = now.year() - date.year(); if (date > now.addYears(-age)) { age--; } return age; } QString GrantleeContactFormatter::toHtml(HtmlForm form) const { if (!d->mErrorMessage.isEmpty()) { return d->mErrorMessage; } KContacts::Addressee rawContact; const Akonadi::Item localItem = item(); if (localItem.isValid() && localItem.hasPayload()) { rawContact = localItem.payload(); } else { rawContact = contact(); } if (rawContact.isEmpty()) { return QString(); } QVariantHash contactObject; GrantleeContactUtils grantleeContactUtil; // Name parts setHashField(contactObject, QStringLiteral("name"), rawContact.realName()); setHashField(contactObject, QStringLiteral("formattedName"), rawContact.formattedName()); setHashField(contactObject, QStringLiteral("prefix"), rawContact.prefix()); setHashField(contactObject, QStringLiteral("givenName"), rawContact.givenName()); setHashField(contactObject, QStringLiteral("additionalName"), rawContact.additionalName()); setHashField(contactObject, QStringLiteral("familyName"), rawContact.familyName()); setHashField(contactObject, QStringLiteral("suffix"), rawContact.suffix()); setHashField(contactObject, QStringLiteral("nickName"), rawContact.nickName()); // Dates const QDate birthday = rawContact.birthday().date(); if (birthday.isValid()) { grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("birthdayi18n")); const QString formattedBirthday = QLocale().toString(birthday); contactObject.insert(QStringLiteral("birthday"), formattedBirthday); const int years = contactAge(birthday); contactObject.insert(QStringLiteral("age"), QString::number(years)); contactObject.insert(QStringLiteral("birthdayage"), QString(formattedBirthday +QStringLiteral("  ") +i18np("(One year old)", "(%1 years old)", years))); } const QDate anniversary = QDate::fromString(rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Anniversary")), Qt::ISODate); if (anniversary.isValid()) { contactObject.insert(QStringLiteral("anniversary"), QLocale().toString(anniversary)); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("anniversaryi18n")); } // Emails QStringList emails; const QStringList emailsList = rawContact.emails(); for (const QString &email : emailsList) { const QString fullEmail = QString::fromLatin1(QUrl::toPercentEncoding(rawContact.fullEmail(email))); const QString url = QStringLiteral("%2") .arg(fullEmail, email); emails << url; } grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("emailsi18n")); contactObject.insert(QStringLiteral("emails"), emails); // Phone numbers QVariantList phoneNumbers; int counter = 0; const KContacts::PhoneNumber::List lstPhone = rawContact.phoneNumbers(); phoneNumbers.reserve(lstPhone.count()); for (const KContacts::PhoneNumber &phoneNumber : lstPhone) { phoneNumbers.append(phoneNumberHash(phoneNumber, counter)); counter++; } contactObject.insert(QStringLiteral("phoneNumbers"), phoneNumbers); // IM QVariantList imAddresses; const QStringList customs = rawContact.customs(); for (const QString &custom : customs) { if (custom.startsWith(QLatin1String("messaging/"))) { int pos = custom.indexOf(QLatin1Char(':')); QString key = custom.left(pos); key.remove(QStringLiteral("-All")); const QString value = custom.mid(pos + 1); imAddresses.append(imAddressHash(key, value)); } } contactObject.insert(QStringLiteral("imAddresses"), imAddresses); // Homepage QVariantList websites; const KContacts::ResourceLocatorUrl::List extraUrlList = rawContact.extraUrlList(); for (const KContacts::ResourceLocatorUrl &resourceLocator : extraUrlList) { QString url = resourceLocator.url().url(); if (!url.startsWith(QLatin1String("http://")) && !url.startsWith(QLatin1String("https://"))) { url = QStringLiteral("http://") + url; } websites.append(KStringHandler::tagUrls(url)); } if (!websites.isEmpty()) { contactObject.insert(QStringLiteral("websites"), websites); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("websitei18n")); } // Blog Feed const QString blog = rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("BlogFeed")); if (!blog.isEmpty()) { contactObject.insert(QStringLiteral("blogUrl"), KStringHandler::tagUrls(blog)); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("blogUrli18n")); } // Address Book const QString addressBookName = rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("AddressBook")); if (!addressBookName.isEmpty()) { contactObject.insert(QStringLiteral("addressBookName"), addressBookName); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("addressBookNamei18n")); } // Addresses QVariantList addresses; counter = 0; const KContacts::Address::List lstAddresses = rawContact.addresses(); addresses.reserve(lstAddresses.count()); for (const KContacts::Address &address : lstAddresses) { addresses.append(addressHash(address, counter)); counter++; } // Note if (!rawContact.note().isEmpty()) { const QString notes = QStringLiteral("%1").arg(rawContact.note().replace(QLatin1Char('\n'), QStringLiteral("
"))); contactObject.insert(QStringLiteral("note"), notes); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("notei18n")); } contactObject.insert(QStringLiteral("addresses"), addresses); setHashField(contactObject, QStringLiteral("mailer"), rawContact.mailer()); setHashField(contactObject, QStringLiteral("title"), rawContact.title()); setHashField(contactObject, QStringLiteral("role"), rawContact.role()); QString job = rawContact.title(); if (job.isEmpty()) { job = rawContact.role(); } if (job.isEmpty()) { job = rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Profession")); } setHashField(contactObject, QStringLiteral("job"), job); setHashField(contactObject, QStringLiteral("organization"), rawContact.organization()); setHashField(contactObject, QStringLiteral("department"), rawContact.department()); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("departmenti18n")); //setHashField( contactObject, QStringLiteral( "note" ), rawContact.note() ); setHashField(contactObject, QStringLiteral("profession"), rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Profession"))); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("Professioni18n")); setHashField(contactObject, QStringLiteral("office"), rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Office"))); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("officei18n")); setHashField(contactObject, QStringLiteral("manager"), rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-ManagersName"))); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("manageri18n")); setHashField(contactObject, QStringLiteral("assistant"), rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-AssistantsName"))); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("assistanti18n")); setHashField(contactObject, QStringLiteral("spouse"), rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-SpousesName"))); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("spousei18n")); setHashField(contactObject, QStringLiteral("imAddress"), rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-IMAddress"))); grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("imAddressi18n")); // Custom fields QVariantList customFields; QVariantList customFieldsUrl; static QSet blacklistedKeys; if (blacklistedKeys.isEmpty()) { blacklistedKeys.insert(QStringLiteral("CRYPTOPROTOPREF")); blacklistedKeys.insert(QStringLiteral("OPENPGPFP")); blacklistedKeys.insert(QStringLiteral("SMIMEFP")); blacklistedKeys.insert(QStringLiteral("CRYPTOSIGNPREF")); blacklistedKeys.insert(QStringLiteral("CRYPTOENCRYPTPREF")); blacklistedKeys.insert(QStringLiteral("Anniversary")); blacklistedKeys.insert(QStringLiteral("BlogFeed")); blacklistedKeys.insert(QStringLiteral("Profession")); blacklistedKeys.insert(QStringLiteral("Office")); blacklistedKeys.insert(QStringLiteral("ManagersName")); blacklistedKeys.insert(QStringLiteral("AssistantsName")); blacklistedKeys.insert(QStringLiteral("SpousesName")); blacklistedKeys.insert(QStringLiteral("IMAddress")); blacklistedKeys.insert(QStringLiteral("AddressBook")); blacklistedKeys.insert(QStringLiteral("MailPreferedFormatting")); blacklistedKeys.insert(QStringLiteral("MailAllowToRemoteContent")); blacklistedKeys.insert(QStringLiteral("MAILPREFEREDFORMATTING")); blacklistedKeys.insert(QStringLiteral("MAILALLOWTOREMOTECONTENT")); } if (!customs.empty()) { for (QString custom : qAsConst(customs)) { //krazy:exclude=foreach if (custom.startsWith(QLatin1String("KADDRESSBOOK-"))) { custom.remove(QStringLiteral("KADDRESSBOOK-X-")); custom.remove(QStringLiteral("KADDRESSBOOK-")); int pos = custom.indexOf(QLatin1Char(':')); QString key = custom.left(pos); QString value = custom.mid(pos + 1); if (blacklistedKeys.contains(key)) { continue; } bool addUrl = false; // check whether it is a custom local field for (int i = 0; i < customFieldDescriptions().size(); ++i) { const QVariantMap description = customFieldDescriptions().at(i); if (description.value(QStringLiteral("key")).toString() == key) { key = description.value(QStringLiteral("title")).toString(); const QString descriptionType = description.value(QStringLiteral("type")).toString(); if (descriptionType == QLatin1String("boolean")) { if (value == QLatin1String("true")) { value = i18nc("Boolean value", "yes"); } else { value = i18nc("Boolean value", "no"); } } else if (descriptionType == QLatin1String("date")) { const QDate date = QDate::fromString(value, Qt::ISODate); value = QLocale().toString(date, QLocale::ShortFormat); } else if (descriptionType == QLatin1String("time")) { const QTime time = QTime::fromString(value, Qt::ISODate); value = QLocale::system().toString(time, QLocale::ShortFormat); } else if (descriptionType == QLatin1String("datetime")) { const QDateTime dateTime = QDateTime::fromString(value, Qt::ISODate); value = QLocale().toString(dateTime, QLocale::ShortFormat); } else if (descriptionType == QLatin1String("url")) { value = KStringHandler::tagUrls(value.toHtmlEscaped()); addUrl = true; } break; } } QVariantHash customFieldObject; customFieldObject.insert(QStringLiteral("title"), key); customFieldObject.insert(QStringLiteral("value"), value); if (addUrl) { customFieldsUrl.append(customFieldObject); } else { customFields.append(customFieldObject); } } } } contactObject.insert(QStringLiteral("customFields"), customFields); contactObject.insert(QStringLiteral("customFieldsUrl"), customFieldsUrl); if (!d->forceDisableQRCode) { if (d->showQRCode) { contactObject.insert(QStringLiteral("hasqrcode"), QStringLiteral("true")); } } QVariantHash colorsObject; colorsObject.insert( QStringLiteral("linkColor"), KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name()); colorsObject.insert( QStringLiteral("textColor"), KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name()); colorsObject.insert( QStringLiteral("backgroundColor"), KColorScheme(QPalette::Active, KColorScheme::View).background().color().name()); QVariantHash mapping; mapping.insert(QStringLiteral("contact"), contactObject); mapping.insert(QStringLiteral("colors"), colorsObject); Grantlee::Context context(mapping); if (form == SelfcontainedForm) { return d->mSelfcontainedTemplate->render(&context); } else if (form == EmbeddableForm) { return d->mEmbeddableTemplate->render(&context); } else { return QString(); } } diff --git a/kaddressbookgrantlee/src/printing/contactgrantleeprintobject.cpp b/kaddressbookgrantlee/src/printing/contactgrantleeprintobject.cpp index 122b411..3aecf6b 100644 --- a/kaddressbookgrantlee/src/printing/contactgrantleeprintobject.cpp +++ b/kaddressbookgrantlee/src/printing/contactgrantleeprintobject.cpp @@ -1,318 +1,301 @@ /* Copyright (C) 2013-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "contactgrantleeprintobject.h" #include "../contactobject/contactgrantleeaddressobject.h" #include "../contactobject/contactgrantleephoneobject.h" #include "../contactobject/contactgrantleeimobject.h" #include "../contactobject/contactgrantleecryptoobject.h" #include "../contactobject/contactgrantleewebsite.h" -#include "../contactobject/contactgrantleeemail.h" #include #include #include #include #include +#include #include using namespace KAddressBookGrantlee; ContactGrantleePrintObject::ContactGrantleePrintObject(const KContacts::Addressee &address, QObject *parent) : QObject(parent) , mAddress(address) { const auto addresses = address.addresses(); mListAddress.reserve(addresses.size()); for (const KContacts::Address &addr : addresses) { mListAddress << new ContactGrantleeAddressObject(addr); } const auto webSites = address.extraUrlList(); mListWebSite.reserve(webSites.size()); for (const KContacts::ResourceLocatorUrl &webSite : webSites) { mListWebSite << new ContactGrantleeWebSite(webSite); } const auto phoneNumbers = address.phoneNumbers(); mListPhones.reserve(phoneNumbers.size()); for (const KContacts::PhoneNumber &phone : phoneNumbers) { mListPhones << new ContactGrantleePhoneObject(phone); } - const auto emails = address.emailList(); - mListEmails.reserve(emails.size()); - for (const KContacts::Email &email : emails) { - mListEmails << new ContactGrantleeEmail(mAddress, email); - } - const QStringList customs = mAddress.customs(); for (const QString &custom : customs) { if (custom.startsWith(QLatin1String("messaging/"))) { const int pos = custom.indexOf(QLatin1Char(':')); QString key = custom.left(pos); key.remove(QStringLiteral("-All")); const QString value = custom.mid(pos + 1); mListIm << new ContactGrantleeImObject(key, value); } } mCryptoObject = new ContactGrantleeCryptoObject(address); } ContactGrantleePrintObject::~ContactGrantleePrintObject() { qDeleteAll(mListAddress); qDeleteAll(mListPhones); qDeleteAll(mListIm); qDeleteAll(mListWebSite); - qDeleteAll(mListEmails); delete mCryptoObject; } QString ContactGrantleePrintObject::realName() const { return mAddress.realName(); } QString ContactGrantleePrintObject::formattedName() const { return mAddress.formattedName(); } QString ContactGrantleePrintObject::prefix() const { return mAddress.prefix(); } QString ContactGrantleePrintObject::givenName() const { return mAddress.givenName(); } QString ContactGrantleePrintObject::additionalName() const { return mAddress.additionalName(); } QString ContactGrantleePrintObject::familyName() const { return mAddress.familyName(); } QString ContactGrantleePrintObject::suffix() const { return mAddress.suffix(); } QString ContactGrantleePrintObject::nickName() const { return mAddress.nickName(); } QString ContactGrantleePrintObject::name() const { return mAddress.name(); } QString ContactGrantleePrintObject::languages() const { QString result; QStringList langList; const KContacts::Lang::List lstLangs = mAddress.langs(); langList.reserve(lstLangs.count()); for (const KContacts::Lang &lang : lstLangs) { langList.append(lang.language()); } if (!langList.isEmpty()) { result = langList.join(QLatin1Char(',')); } return result; } -QStringList ContactGrantleePrintObject::emails() const +QVariantList ContactGrantleePrintObject::emails() const { - QStringList emails; - const QStringList lstEmails(mAddress.emails()); - for (const QString &email : lstEmails) { - const QString fullEmail = QString::fromLatin1(QUrl::toPercentEncoding(mAddress.fullEmail(email))); - - const QString url = QStringLiteral("%2") - .arg(fullEmail, email); - emails << url; - } - return emails; + // ### temporary hack until this becomes a KContact::Addressee gadget itself and we can remove this + const auto idx = KContacts::Addressee::staticMetaObject.indexOfProperty("emails"); + Q_ASSERT(idx >= 0); + const auto prop = KContacts::Addressee::staticMetaObject.property(idx); + return prop.readOnGadget(&mAddress).toList(); } QString ContactGrantleePrintObject::organization() const { return mAddress.organization(); } QString ContactGrantleePrintObject::note() const { return mAddress.note().replace(QLatin1Char('\n'), QStringLiteral("
")); } QString ContactGrantleePrintObject::webPage() const { return mAddress.url().url().toDisplayString(); } QString ContactGrantleePrintObject::webSite() const { return webPage(); } QString ContactGrantleePrintObject::title() const { return mAddress.title(); } QString ContactGrantleePrintObject::preferredEmail() const { return mAddress.preferredEmail(); } QString ContactGrantleePrintObject::role() const { return mAddress.role(); } QString ContactGrantleePrintObject::birthday() const { return QLocale().toString(mAddress.birthday().date(), QLocale::LongFormat); } QString ContactGrantleePrintObject::department() const { return mAddress.department(); } QVariant ContactGrantleePrintObject::addresses() const { return QVariant::fromValue(mListAddress); } QVariant ContactGrantleePrintObject::webSites() const { return QVariant::fromValue(mListWebSite); } -QVariant ContactGrantleePrintObject::emailsList() const -{ - return QVariant::fromValue(mListEmails); -} - QVariant ContactGrantleePrintObject::phones() const { return QVariant::fromValue(mListPhones); } QVariant ContactGrantleePrintObject::instantManging() const { return QVariant::fromValue(mListIm); } QVariant ContactGrantleePrintObject::geo() const { return QVariant::fromValue(mAddress.geo()); } QVariant ContactGrantleePrintObject::crypto() const { return QVariant::fromValue(mCryptoObject); } QString ContactGrantleePrintObject::addressBookName() const { const QString addressBookName = mAddress.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("AddressBook")); return addressBookName; } QString ContactGrantleePrintObject::photo() const { if (mAddress.photo().isEmpty()) { return QString(); } else { const QString photoStr = QStringLiteral("  ") .arg(imgToDataUrl(mAddress.photo().data()), QString::number(60), QString::number(60)); return photoStr; } } QString ContactGrantleePrintObject::imgToDataUrl(const QImage &image) const { QByteArray ba; QBuffer buffer(&ba); buffer.open(QIODevice::WriteOnly); image.save(&buffer, "PNG"); return QStringLiteral("data:image/%1;base64,%2").arg(QStringLiteral("PNG"), QString::fromLatin1(ba.toBase64())); } QString ContactGrantleePrintObject::logo() const { if (mAddress.logo().isEmpty()) { return QString(); } else { const QString photoStr = QStringLiteral("  ") .arg(imgToDataUrl(mAddress.logo().data()), QString::number(60), QString::number(60)); return photoStr; } } QString ContactGrantleePrintObject::anniversary() const { const QDate anniversary = QDate::fromString(mAddress.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Anniversary")), Qt::ISODate); if (anniversary.isValid()) { return QLocale().toString(anniversary); } return QString(); } QString ContactGrantleePrintObject::profession() const { return mAddress.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Profession")); } QString ContactGrantleePrintObject::office() const { return mAddress.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Office")); } QString ContactGrantleePrintObject::manager() const { return mAddress.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-ManagersName")); } QString ContactGrantleePrintObject::assistant() const { return mAddress.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-AssistantsName")); } QString ContactGrantleePrintObject::spouse() const { return mAddress.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-SpousesName")); } diff --git a/kaddressbookgrantlee/src/printing/contactgrantleeprintobject.h b/kaddressbookgrantlee/src/printing/contactgrantleeprintobject.h index d71fb6e..eef68a5 100644 --- a/kaddressbookgrantlee/src/printing/contactgrantleeprintobject.h +++ b/kaddressbookgrantlee/src/printing/contactgrantleeprintobject.h @@ -1,120 +1,117 @@ /* Copyright (C) 2013-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef CONTACTGRANTLEEPRINTOBJECT_H #define CONTACTGRANTLEEPRINTOBJECT_H #include #include #include namespace KAddressBookGrantlee { class ContactGrantleeCryptoObject; class ContactGrantleePrintObject : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name) Q_PROPERTY(QString realName READ realName) Q_PROPERTY(QString formattedName READ formattedName) Q_PROPERTY(QString prefix READ prefix) Q_PROPERTY(QString givenName READ givenName) Q_PROPERTY(QString additionalName READ additionalName) Q_PROPERTY(QString familyName READ familyName) Q_PROPERTY(QString suffix READ suffix) Q_PROPERTY(QString nickName READ nickName) - Q_PROPERTY(QStringList emails READ emails) Q_PROPERTY(QString organization READ organization) Q_PROPERTY(QString note READ note) Q_PROPERTY(QString webPage READ webPage) Q_PROPERTY(QString webSite READ webSite) Q_PROPERTY(QString title READ title) Q_PROPERTY(QString preferredEmail READ preferredEmail) Q_PROPERTY(QString role READ role) Q_PROPERTY(QString birthday READ birthday) Q_PROPERTY(QString department READ department) Q_PROPERTY(QVariant addresses READ addresses) Q_PROPERTY(QVariant phones READ phones) Q_PROPERTY(QVariant instantManging READ instantManging) Q_PROPERTY(QString addressBookName READ addressBookName) Q_PROPERTY(QString photo READ photo) Q_PROPERTY(QString logo READ logo) Q_PROPERTY(QVariant crypto READ crypto) Q_PROPERTY(QString anniversary READ anniversary) Q_PROPERTY(QString profession READ profession) Q_PROPERTY(QString office READ office) Q_PROPERTY(QString manager READ manager) Q_PROPERTY(QString assistant READ assistant) Q_PROPERTY(QString spouse READ spouse) Q_PROPERTY(QString languages READ languages) Q_PROPERTY(QVariant webSites READ webSites) - Q_PROPERTY(QVariant emailsList READ emailsList) + Q_PROPERTY(QVariantList emails READ emails) //Add more functions public: explicit ContactGrantleePrintObject(const KContacts::Addressee &address, QObject *parent = nullptr); ~ContactGrantleePrintObject(); Q_REQUIRED_RESULT QString name() const; Q_REQUIRED_RESULT QString realName() const; Q_REQUIRED_RESULT QString formattedName() const; Q_REQUIRED_RESULT QString prefix() const; Q_REQUIRED_RESULT QString givenName() const; Q_REQUIRED_RESULT QString additionalName() const; Q_REQUIRED_RESULT QString familyName() const; Q_REQUIRED_RESULT QString suffix() const; Q_REQUIRED_RESULT QString nickName() const; - Q_REQUIRED_RESULT QStringList emails() const; + Q_REQUIRED_RESULT QVariantList emails() const; Q_REQUIRED_RESULT QString organization() const; Q_REQUIRED_RESULT QString note() const; //webPage deprecated. Q_REQUIRED_RESULT QString webPage() const; Q_REQUIRED_RESULT QString webSite() const; Q_REQUIRED_RESULT QString title() const; Q_REQUIRED_RESULT QString preferredEmail() const; Q_REQUIRED_RESULT QString role() const; Q_REQUIRED_RESULT QString birthday() const; Q_REQUIRED_RESULT QString department() const; Q_REQUIRED_RESULT QVariant addresses() const; Q_REQUIRED_RESULT QVariant phones() const; Q_REQUIRED_RESULT QString addressBookName() const; Q_REQUIRED_RESULT QVariant instantManging() const; Q_REQUIRED_RESULT QVariant geo() const; Q_REQUIRED_RESULT QString photo() const; Q_REQUIRED_RESULT QString logo() const; Q_REQUIRED_RESULT QVariant crypto() const; Q_REQUIRED_RESULT QString anniversary() const; Q_REQUIRED_RESULT QString profession() const; Q_REQUIRED_RESULT QString office() const; Q_REQUIRED_RESULT QString manager() const; Q_REQUIRED_RESULT QString assistant() const; Q_REQUIRED_RESULT QString spouse() const; Q_REQUIRED_RESULT QString languages() const; Q_REQUIRED_RESULT QVariant webSites() const; - Q_REQUIRED_RESULT QVariant emailsList() const; private: QString imgToDataUrl(const QImage &image) const; QList mListAddress; QList mListPhones; QList mListIm; QList mListWebSite; - QList mListEmails; ContactGrantleeCryptoObject *mCryptoObject = nullptr; KContacts::Addressee mAddress; }; } Q_DECLARE_METATYPE(QList) #endif // CONTACTGRANTLEEPRINTOBJECT_H diff --git a/kaddressbookgrantlee/src/printing/grantleeprint.cpp b/kaddressbookgrantlee/src/printing/grantleeprint.cpp index 68df62d..766e518 100644 --- a/kaddressbookgrantlee/src/printing/grantleeprint.cpp +++ b/kaddressbookgrantlee/src/printing/grantleeprint.cpp @@ -1,114 +1,116 @@ /* Copyright (C) 2015-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "grantleeprint.h" #include "contactgrantleeprintobject.h" #include "formatter/grantleecontactutils.h" #include #include #include #include #include // Grantlee has no Q_GADGET support yet #define GRANTLEE_MAKE_GADGET(Class) \ GRANTLEE_BEGIN_LOOKUP(Class) \ const auto idx = Class::staticMetaObject.indexOfProperty(property.toUtf8().constData()); \ if (idx < 0) { \ return {};} \ const auto mp = Class::staticMetaObject.property(idx); \ return mp.readOnGadget(&object); \ GRANTLEE_END_LOOKUP +GRANTLEE_MAKE_GADGET(KContacts::Email) GRANTLEE_MAKE_GADGET(KContacts::Geo) using namespace KAddressBookGrantlee; GrantleePrint::GrantleePrint(QObject *parent) : PimCommon::GenericGrantleeFormatter(parent) { init(); } GrantleePrint::GrantleePrint(const QString &themePath, QObject *parent) : PimCommon::GenericGrantleeFormatter(QStringLiteral("theme.html"), themePath, parent) { init(); } GrantleePrint::~GrantleePrint() { } void GrantleePrint::init() { + Grantlee::registerMetaType(); Grantlee::registerMetaType(); } QString GrantleePrint::contactsToHtml(const KContacts::Addressee::List &contacts) { if (!errorMessage().isEmpty()) { return errorMessage(); } if (contacts.isEmpty()) { return QString(); } QVariantList contactsList; QList lst; const int numberContacts(contacts.count()); lst.reserve(numberContacts); contactsList.reserve(numberContacts); for (const KContacts::Addressee &address : contacts) { ContactGrantleePrintObject *contactPrintObject = new ContactGrantleePrintObject(address); lst.append(contactPrintObject); contactsList << QVariant::fromValue(static_cast(contactPrintObject)); } QVariantHash mapping; QVariantHash contactI18n; GrantleeContactUtils grantleeContactUtil; grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("birthdayi18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("anniversaryi18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("emailsi18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("websitei18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("blogUrli18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("addressBookNamei18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("notei18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("departmenti18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("Professioni18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("officei18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("manageri18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("assistanti18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("spousei18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("imAddressi18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("latitudei18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("longitudei18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("organizationi18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("titlei18n")); grantleeContactUtil.insertVariableToQVariantHash(contactI18n, QStringLiteral("nextcontacti18n")); mapping.insert(QStringLiteral("contacti18n"), contactI18n); mapping.insert(QStringLiteral("contacts"), contactsList); const QString content = render(mapping); qDeleteAll(lst); return content; }