Commit 69c11af3 authored by Volker Krause's avatar Volker Krause
Browse files

Port Grantlee contact printing model to KContacts::Addressee too

Summary:
This means for the data both display and printing now use the same model,
so improvements to that will benefit both sides. Printing however isn't
able to use i18n in the templates yet, so we can't remove the i18n
properties here yet.

Reviewers: dvratil

Reviewed By: dvratil

Subscribers: dvratil, kde-pim

Tags: #kde_pim

Differential Revision: https://phabricator.kde.org/D20727
parent 936e7d18
cmake_minimum_required(VERSION 3.5)
set(PIM_VERSION "5.11.43")
set(PIM_VERSION "5.11.44")
project(kdepim-apps-lib VERSION ${PIM_VERSION})
set(KF5_MIN_VERSION "5.57.0")
......
......@@ -5,21 +5,11 @@ set(kaddressbook_grantlee_LIB_SRCS
formatter/grantleecontactformatter.cpp
formatter/grantleecontactgroupformatter.cpp
formatter/grantleecontactutils.cpp
contactobject/contactgrantleewrapper.cpp
printing/grantleeprint.cpp
)
set(kaddressbook_grantlee_object_LIB_SRCS
contactobject/contactgrantleecryptoobject.cpp
contactobject/contactgrantleeimobject.cpp
contactobject/contactgrantleewrapper.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})
add_library(KF5KaddressbookGrantlee ${kaddressbook_grantlee_LIB_SRCS})
generate_export_header(KF5KaddressbookGrantlee BASE_NAME kaddressbook_grantlee)
add_library(KF5::KaddressbookGrantlee ALIAS KF5KaddressbookGrantlee)
......
/*
Copyright (C) 2014-2019 Montel Laurent <montel@kde.org>
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 "contactgrantleecryptoobject.h"
#include "Libkleo/Enum"
using namespace KAddressBookGrantlee;
inline QString loadCustom(const KContacts::Addressee &contact, const QString &key)
{
return contact.custom(QStringLiteral("KADDRESSBOOK"), key);
}
ContactGrantleeCryptoObject::ContactGrantleeCryptoObject(const KContacts::Addressee &address, QObject *parent)
: QObject(parent)
, mAddress(address)
{
}
ContactGrantleeCryptoObject::~ContactGrantleeCryptoObject()
{
}
QString ContactGrantleeCryptoObject::signaturePreference() const
{
return Kleo::signingPreferenceToLabel(Kleo::stringToSigningPreference(loadCustom(mAddress, QStringLiteral("CRYPTOSIGNPREF"))));
}
QString ContactGrantleeCryptoObject::cryptoPreference() const
{
return Kleo::encryptionPreferenceToLabel(Kleo::stringToEncryptionPreference(loadCustom(mAddress, QStringLiteral("CRYPTOENCRYPTPREF"))));
}
/*
Copyright (C) 2014-2019 Montel Laurent <montel@kde.org>
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_CONTACTGRANTLEECRYPTOOBJECT_H
#define PRINT_CONTACTGRANTLEECRYPTOOBJECT_H
#include <QObject>
#include <KContacts/Addressee>
namespace KAddressBookGrantlee {
class ContactGrantleeCryptoObject : public QObject
{
Q_OBJECT
Q_PROPERTY(QString signaturePreference READ signaturePreference)
Q_PROPERTY(QString cryptoPreference READ cryptoPreference)
public:
explicit ContactGrantleeCryptoObject(const KContacts::Addressee &address, QObject *parent = nullptr);
~ContactGrantleeCryptoObject();
Q_REQUIRED_RESULT QString signaturePreference() const;
Q_REQUIRED_RESULT QString cryptoPreference() const;
private:
KContacts::Addressee mAddress;
};
}
#endif // PRINT_CONTACTGRANTLEECRYPTOOBJECT_H
/*
Copyright (C) 2013-2019 Montel Laurent <montel@kde.org>
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 "contactgrantleeimobject.h"
#include "contacteditor/improtocols.h"
using namespace KAddressBookGrantlee;
ContactGrantleeImObject::ContactGrantleeImObject(const QString &type, const QString &address, QObject *parent)
: QObject(parent)
, mType(type)
, mAddress(address)
{
}
ContactGrantleeImObject::~ContactGrantleeImObject()
{
}
QString ContactGrantleeImObject::type() const
{
return IMProtocols::self()->name(mType);
}
QString ContactGrantleeImObject::address() const
{
return mAddress;
}
/*
Copyright (C) 2013-2019 Montel Laurent <montel@kde.org>
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_CONTACTGRANTLEEIMOBJECT_H
#define PRINT_CONTACTGRANTLEEIMOBJECT_H
#include <QObject>
namespace KAddressBookGrantlee {
class ContactGrantleeImObject : public QObject
{
Q_OBJECT
Q_PROPERTY(QString type READ type)
Q_PROPERTY(QString address READ address)
public:
explicit ContactGrantleeImObject(const QString &type, const QString &address, QObject *parent = nullptr);
~ContactGrantleeImObject();
Q_REQUIRED_RESULT QString type() const;
Q_REQUIRED_RESULT QString address() const;
private:
QString mType;
QString mAddress;
};
}
#endif // PRINT_CONTACTGRANTLEEIMOBJECT_H
......@@ -21,14 +21,25 @@
#include <contacteditor/improtocols.h>
#include <Libkleo/Enum>
#include <KLocalizedString>
#include <QBuffer>
#include <QImage>
#include <QLocale>
using namespace KAddressBookGrantlee;
static_assert(sizeof(KContacts::Addressee) == sizeof(KAddressBookGrantlee::ContactGrantleeWrapper), "Grantlee wrapper must not have member variables to prevent sliciing issues");
ContactGrantleeWrapper::ContactGrantleeWrapper() = default;
ContactGrantleeWrapper::ContactGrantleeWrapper(const KContacts::Addressee& addr)
: KContacts::Addressee(addr)
{
}
QString ContactGrantleeWrapper::addressBookLabel() const
{
return i18n("Address Book");
......@@ -79,6 +90,41 @@ int ContactGrantleeWrapper::age() const
return age;
}
QString ContactGrantleeWrapper::cryptoPreference() const
{
return Kleo::encryptionPreferenceToLabel(Kleo::stringToEncryptionPreference(custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("CRYPTOENCRYPTPREF"))));
}
QString ContactGrantleeWrapper::signaturePreference() const
{
return Kleo::signingPreferenceToLabel(Kleo::stringToSigningPreference(custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("CRYPTOSIGNPREF"))));
}
static QString imgToDataUrl(const QImage &image)
{
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 ContactGrantleeWrapper::logoImgElement() const
{
if (logo().isEmpty()) {
return {};
}
return QStringLiteral("<img src=\"%1\" width=\"%2\" height=\"%3\"> &nbsp;").arg(imgToDataUrl(logo().data()), QString::number(60), QString::number(60));
}
QString ContactGrantleeWrapper::photoImgElement() const
{
if (photo().isEmpty()) {
return {};
}
return QStringLiteral("<img src=\"%1\" width=\"%2\" height=\"%3\"> &nbsp;").arg(imgToDataUrl(photo().data()), QString::number(60), QString::number(60));
}
QString ContactGrantleeWrapper::formattedBirthday() const
{
return QLocale().toString(birthday().date());
......
......@@ -44,6 +44,12 @@ class ContactGrantleeWrapper : public KContacts::Addressee
Q_PROPERTY(QString addressBookName READ addressBookName)
Q_PROPERTY(int age READ age)
Q_PROPERTY(QString cryptoPreference READ cryptoPreference)
Q_PROPERTY(QString signaturePreference READ signaturePreference)
// ### this shouldn't be returning assembled HTML, that's a job for Grantlee
Q_PROPERTY(QString photo READ photoImgElement)
Q_PROPERTY(QString logo READ logoImgElement)
// ### those two would be unneccessary if we had a proper way for formatting dates in Grantlee
Q_PROPERTY(QString formattedBirthday READ formattedBirthday)
......@@ -52,7 +58,12 @@ class ContactGrantleeWrapper : public KContacts::Addressee
// ### this is temporary, until KContacts::Impp takes over this part
Q_PROPERTY(QVariantList imAddresses READ imAddresses)
public:
ContactGrantleeWrapper();
ContactGrantleeWrapper(const KContacts::Addressee &addr);
private:
QString addressBookLabel() const;
QString anniversaryLabel() const;
QString assistantLabel() const;
......@@ -63,6 +74,11 @@ private:
QString addressBookName() const;
int age() const;
QString cryptoPreference() const;
QString signaturePreference() const;
QString photoImgElement() const;
QString logoImgElement() const;
QString formattedBirthday() const;
QString formattedAnniversary() const;
......
......@@ -21,7 +21,6 @@
#include "grantleecontactformatter.h"
#include "grantleetheme/grantleetheme.h"
#include "grantleecontactutils.h"
#include "../contactobject/contactgrantleewrapper.h"
#include <KContacts/Addressee>
......@@ -272,7 +271,7 @@ QString GrantleeContactFormatter::toHtml(HtmlForm form) const
KColorScheme(QPalette::Active, KColorScheme::View).background().color().name());
QVariantHash mapping;
mapping.insert(QStringLiteral("contact"), QVariant::fromValue(*reinterpret_cast<ContactGrantleeWrapper*>(&rawContact)));
mapping.insert(QStringLiteral("contact"), QVariant::fromValue(ContactGrantleeWrapper(rawContact)));
mapping.insert(QStringLiteral("colors"), colorsObject);
mapping.insert(QStringLiteral("customFields"), customFields);
mapping.insert(QStringLiteral("customFieldsUrl"), customFieldsUrl);
......
......@@ -94,7 +94,6 @@ void GrantleePrintTest::shouldDisplayContactInfo_data()
QTest::addColumn<QString>("result");
QTest::newRow("name") << QStringLiteral("name") << QStringLiteral("foo1");
QTest::newRow("organization") << QStringLiteral("organization") << QStringLiteral("kde");
QTest::newRow("languages") << QStringLiteral("languages") << QStringLiteral("fr");
QTest::newRow("note") << QStringLiteral("note") << QStringLiteral("foo-note");
QTest::newRow("title") << QStringLiteral("title") << QStringLiteral("foo-title");
QTest::newRow("nickName") << QStringLiteral("nickName") << QStringLiteral("foo-nickname");
......@@ -105,9 +104,9 @@ void GrantleePrintTest::shouldDisplayContactInfo_data()
QTest::newRow("department") << QStringLiteral("department") << QStringLiteral("foo-department");
QTest::newRow("office") << QStringLiteral("office") << QStringLiteral("foo-office");
QTest::newRow("profesion") << QStringLiteral("profession") << QStringLiteral("foo-profession");
QTest::newRow("manager") << QStringLiteral("manager") << QStringLiteral("foo-managersname");
QTest::newRow("assistant") << QStringLiteral("assistant") << QStringLiteral("foo-assistantsname");
QTest::newRow("spouse") << QStringLiteral("spouse") << QStringLiteral("foo-spousesname");
QTest::newRow("manager") << QStringLiteral("managersName") << QStringLiteral("foo-managersname");
QTest::newRow("assistant") << QStringLiteral("assistantsName") << QStringLiteral("foo-assistantsname");
QTest::newRow("spouse") << QStringLiteral("spousesName") << QStringLiteral("foo-spousesname");
QTest::newRow("givenname") << QStringLiteral("givenName") << QStringLiteral("foo-givenname");
QTest::newRow("additionalname") << QStringLiteral("additionalName") << QStringLiteral("foo-additionalname");
}
......
/*
Copyright (C) 2013-2019 Montel Laurent <montel@kde.org>
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/contactgrantleeimobject.h"
#include "../contactobject/contactgrantleecryptoobject.h"
#include <KContacts/PhoneNumber>
#include <QBuffer>
#include <QVariant>
#include <grantlee/metatype.h>
#include <QLocale>
#include <QMetaProperty>
#include <QImage>
using namespace KAddressBookGrantlee;
ContactGrantleePrintObject::ContactGrantleePrintObject(const KContacts::Addressee &address, QObject *parent)
: QObject(parent)
, mAddress(address)
{
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(mListIm);
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;
}
QVariantList ContactGrantleePrintObject::emails() const
{
// ### 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("<br>"));
}
KContacts::ResourceLocatorUrl ContactGrantleePrintObject::url() const
{
return mAddress.url();
}
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();
}
QVector<KContacts::Address> ContactGrantleePrintObject::addresses() const
{
return mAddress.addresses();
}
QVector<KContacts::ResourceLocatorUrl> ContactGrantleePrintObject::urls() const
{
return mAddress.extraUrlList();
}
QVector<KContacts::PhoneNumber> ContactGrantleePrintObject::phones() const
{
return mAddress.phoneNumbers();
}
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("<img src=\"%1\" width=\"%2\" height=\"%3\"> &nbsp;")
.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("<img src=\"%1\" width=\"%2\" height=\"%3\"> &nbsp;")