Commit 390166ad authored by Laurent Montel's avatar Laurent Montel 😁

Add support for fieldgroup

CCBUG: 365192
parent c404c304
......@@ -32,5 +32,6 @@ ecm_add_tests(birthdaytest.cpp
resourcelocatorurltest.cpp
customidentifiertest.cpp
vcardlinetest.cpp
fieldgrouptest.cpp
NAME_PREFIX "kcontacts-" LINK_LIBRARIES KF5Contacts Qt5::Test KF5::ConfigCore Qt5::Gui)
/*
This file is part of the KContacts framework.
Copyright (c) 2016 Laurent Montel <montel@kde.org>
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 "fieldgrouptest.h"
#include "fieldgroup.h"
#include "vcardtool.h"
#include <qtest.h>
FieldGroupTest::FieldGroupTest(QObject *parent)
: QObject(parent)
{
}
FieldGroupTest::~FieldGroupTest()
{
}
void FieldGroupTest::shouldHaveDefaultValue()
{
KContacts::FieldGroup fieldgroup;
QVERIFY(!fieldgroup.isValid());
QVERIFY(fieldgroup.fieldGroupName().isEmpty());
QVERIFY(fieldgroup.parameters().isEmpty());
QVERIFY(fieldgroup.value().isEmpty());
}
void FieldGroupTest::shouldAssignValue()
{
const QString fielgroundname(QStringLiteral("fr"));
const QString value(QStringLiteral("bla"));
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
KContacts::FieldGroup fieldGroup(fielgroundname);
fieldGroup.setParameters(params);
fieldGroup.setValue(value);
QVERIFY(fieldGroup.isValid());
QVERIFY(!fieldGroup.fieldGroupName().isEmpty());
QCOMPARE(fieldGroup.fieldGroupName(), fielgroundname);
QVERIFY(!fieldGroup.parameters().isEmpty());
QCOMPARE(fieldGroup.parameters(), params);
QCOMPARE(fieldGroup.value(), value);
}
void FieldGroupTest::shouldAssignExternal()
{
KContacts::FieldGroup fieldgroup;
const QString fieldgroundname(QStringLiteral("fr"));
const QString value(QStringLiteral("bla"));
fieldgroup.setValue(value);
fieldgroup.setFieldGroupName(fieldgroundname);
QVERIFY(fieldgroup.isValid());
QCOMPARE(fieldgroup.fieldGroupName(), fieldgroundname);
QCOMPARE(fieldgroup.value(), value);
}
void FieldGroupTest::shouldSerialized()
{
KContacts::FieldGroup fieldGroup;
KContacts::FieldGroup result;
const QString lang(QStringLiteral("fr"));
fieldGroup.setFieldGroupName(lang);
const QString value(QStringLiteral("bla"));
fieldGroup.setValue(value);
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
fieldGroup.setParameters(params);
QByteArray data;
QDataStream s(&data, QIODevice::WriteOnly);
s << fieldGroup;
QDataStream t(&data, QIODevice::ReadOnly);
t >> result;
QVERIFY(fieldGroup == result);
}
void FieldGroupTest::shouldEqualFieldGroup()
{
KContacts::FieldGroup fieldGroup;
KContacts::FieldGroup result;
const QString lang(QStringLiteral("fr"));
const QString value(QStringLiteral("bla"));
fieldGroup.setValue(value);
fieldGroup.setFieldGroupName(lang);
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
fieldGroup.setParameters(params);
result = fieldGroup;
QVERIFY(fieldGroup == result);
}
void FieldGroupTest::shouldParseFieldGroup()
{
QByteArray vcarddata("BEGIN:VCARD\n"
"VERSION:3.0\n"
"N:LastName;FirstName;;;\n"
"UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
"REV:2015-03-14T09:24:45+00:00\n"
"FN:FirstName LastName\n"
"END:VCARD\n");
KContacts::VCardTool vcard;
const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
QCOMPARE(lst.count(), 1);
QCOMPARE(lst.at(0).fieldGroupList().count(), 1);
}
void FieldGroupTest::shouldParseWithoutFieldGroup()
{
QByteArray vcarddata("BEGIN:VCARD\n"
"VERSION:3.0\n"
"N:LastName;FirstName;;;\n"
"UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
"REV:2015-03-14T09:24:45+00:00\n"
"FN:FirstName LastName\n"
"END:VCARD\n");
KContacts::VCardTool vcard;
const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
QCOMPARE(lst.count(), 1);
QCOMPARE(lst.at(0).fieldGroupList().count(), 0);
}
void FieldGroupTest::shouldCreateVCard()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::FieldGroup::List lstFieldGroup;
KContacts::FieldGroup fieldGroup(QStringLiteral("fr"));
const QString value(QStringLiteral("bla"));
fieldGroup.setValue(value);
lstFieldGroup << fieldGroup;
addr.setFieldGroupList(lstFieldGroup);
lst << addr;
KContacts::VCardTool vcard;
const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
QByteArray expected("BEGIN:VCARD\r\n"
"VERSION:4.0\r\n"
"EMAIL:foo@kde.org\r\n"
"N:;;;;\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
void FieldGroupTest::shouldCreateVCardWithTwoLang()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::FieldGroup::List lstFieldGroup;
KContacts::FieldGroup fieldGroup1(QStringLiteral("fr"));
const QString value(QStringLiteral("bla"));
fieldGroup1.setValue(value);
KContacts::FieldGroup fieldGroup2(QStringLiteral("fr2"));
fieldGroup2.setValue(value);
lstFieldGroup << fieldGroup1 << fieldGroup2;
addr.setFieldGroupList(lstFieldGroup);
lst << addr;
KContacts::VCardTool vcard;
const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
QByteArray expected("BEGIN:VCARD\r\n"
"VERSION:4.0\r\n"
"EMAIL:foo@kde.org\r\n"
"N:;;;;\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
void FieldGroupTest::shouldCreateVCardWithParameters()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
const QString value(QStringLiteral("bla"));
KContacts::FieldGroup::List lstFieldGroup;
KContacts::FieldGroup fieldGroup(QStringLiteral("fr"));
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
fieldGroup.setParameters(params);
fieldGroup.setValue(value);
lstFieldGroup << fieldGroup;
addr.setFieldGroupList(lstFieldGroup);
lst << addr;
KContacts::VCardTool vcard;
const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
QByteArray expected("BEGIN:VCARD\r\n"
"VERSION:4.0\r\n"
"EMAIL:foo@kde.org\r\n"
"N:;;;;\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
void FieldGroupTest::shouldNotGenerateFieldGroupForVCard3()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::FieldGroup::List lstFieldGroup;
KContacts::FieldGroup fieldGroup(QStringLiteral("fr"));
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
fieldGroup.setParameters(params);
const QString value(QStringLiteral("bla"));
fieldGroup.setValue(value);
lstFieldGroup << fieldGroup;
addr.setFieldGroupList(lstFieldGroup);
lst << addr;
KContacts::VCardTool vcard;
const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
QByteArray expected("BEGIN:VCARD\r\n"
"VERSION:3.0\r\n"
"EMAIL:foo@kde.org\r\n"
"N:;;;;\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
QTEST_MAIN(FieldGroupTest)
/*
This file is part of the KContacts framework.
Copyright (c) 2016 Laurent Montel <montel@kde.org>
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.
*/
#ifndef FieldGroupTest_H
#define FieldGroupTest_H
#include <QObject>
class FieldGroupTest : public QObject
{
Q_OBJECT
public:
explicit FieldGroupTest(QObject *parent = Q_NULLPTR);
~FieldGroupTest();
private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldAssignValue();
void shouldAssignExternal();
void shouldSerialized();
void shouldEqualFieldGroup();
void shouldParseFieldGroup();
void shouldParseWithoutFieldGroup();
void shouldCreateVCard();
void shouldCreateVCardWithTwoLang();
void shouldCreateVCardWithParameters();
void shouldNotGenerateFieldGroupForVCard3();
};
#endif // FieldGroupTest_H
......@@ -31,6 +31,7 @@ set(kcontacts_SRCS
timezone.cpp
vcarddrag.cpp
vcardtool.cpp
fieldgroup.cpp
${vcardparser_SRCS}
)
......@@ -94,6 +95,7 @@ ecm_generate_headers(KContacts_CamelCase_HEADERS
Sound
TimeZone
VCardDrag
FieldGroup
PREFIX KContacts
REQUIRED_HEADERS KContacts_HEADERS
)
......
......@@ -151,6 +151,7 @@ public:
QVector<QUrl> mSources;
QStringList mMembers;
Related::List mRelationShips;
FieldGroup::List mFieldGroupList;
bool mEmpty : 1;
bool mChanged : 1;
......@@ -397,6 +398,11 @@ bool Addressee::operator==(const Addressee &addressee) const
return false;
}
if (!listEquals(d->mFieldGroupList, addressee.d->mFieldGroupList)) {
qCDebug(KCONTACTS_LOG) << "Field Groups differs";
return false;
}
return true;
}
......@@ -552,6 +558,27 @@ QVector<QUrl> Addressee::sourcesUrlList() const
return d->mSources;
}
FieldGroup::List Addressee::fieldGroupList() const
{
return d->mFieldGroupList;
}
void Addressee::setFieldGroupList(const FieldGroup::List &fieldGroupList)
{
d->mEmpty = false;
d->mFieldGroupList = fieldGroupList;
}
void Addressee::insertFieldGroup(const FieldGroup &fieldGroup)
{
if (fieldGroup.isValid()) {
d->mEmpty = false;
//TODO don't duplicate ?
d->mFieldGroupList.append(fieldGroup);
}
}
void Addressee::insertImpp(const Impp &impp)
{
if (impp.isValid()) {
......@@ -2276,6 +2303,7 @@ QDataStream &KContacts::operator<<(QDataStream &s, const Addressee &a)
s << a.d->mRelationShips;
s << a.d->mSources;
s << a.d->mImpps;
s << a.d->mFieldGroupList;
return s;
}
......@@ -2328,6 +2356,7 @@ QDataStream &KContacts::operator>>(QDataStream &s, Addressee &a)
s >> a.d->mRelationShips;
s >> a.d->mSources;
s >> a.d->mImpps;
s >> a.d->mFieldGroupList;
a.d->mEmpty = false;
return s;
......
......@@ -44,6 +44,7 @@
#include "related.h"
#include "addresseelist.h"
#include "resourcelocatorurl.h"
#include "fieldgroup.h"
namespace KContacts
{
......@@ -1038,6 +1039,12 @@ public:
void setImppList(const Impp::List &imppList);
void insertImpp(const Impp &impp);
//FieldGroup
FieldGroup::List fieldGroupList() const;
void setFieldGroupList(const FieldGroup::List &fieldGroupList);
void insertFieldGroup(const FieldGroup &fieldGroup);
private:
class Private;
QSharedDataPointer<Private> d;
......
/*
This file is part of the KContacts framework.
Copyright (c) 2016 Laurent Montel <montel@kde.org>
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 "fieldgroup.h"
#include <QMap>
#include <qstringlist.h>
#include <QDataStream>
using namespace KContacts;
class Q_DECL_HIDDEN FieldGroup::Private : public QSharedData
{
public:
Private()
{
}
Private(const Private &other)
: QSharedData(other)
{
parameters = other.parameters;
fieldGroupName = other.fieldGroupName;
value = other.value;
}
QMap<QString, QStringList> parameters;
QString fieldGroupName;
QString value;
};
FieldGroup::FieldGroup()
: d(new Private)
{
}
FieldGroup::FieldGroup(const FieldGroup &other)
: d(other.d)
{
}
FieldGroup::FieldGroup(const QString &FieldGroupName)
: d(new Private)
{
d->fieldGroupName = FieldGroupName;
}
FieldGroup::~FieldGroup()
{
}
void FieldGroup::setFieldGroupName(const QString &fieldGroup)
{
d->fieldGroupName = fieldGroup;
}
QString FieldGroup::fieldGroupName() const
{
return d->fieldGroupName;
}
bool FieldGroup::isValid() const
{
return !d->fieldGroupName.isEmpty();
}
void FieldGroup::setValue(const QString &value)
{
d->value = value;
}
QString FieldGroup::value() const
{
return d->value;
}
void FieldGroup::setParameters(const QMap<QString, QStringList> &params)
{
d->parameters = params;
}
QMap<QString, QStringList> FieldGroup::parameters() const
{
return d->parameters;
}
bool FieldGroup::operator==(const FieldGroup &other) const
{
return (d->parameters == other.parameters()) && (d->fieldGroupName == other.fieldGroupName()) && (d->value == other.value());
}
bool FieldGroup::operator!=(const FieldGroup &other) const
{
return !(other == *this);
}
FieldGroup &FieldGroup::operator=(const FieldGroup &other)
{
if (this != &other) {
d = other.d;
}
return *this;
}
QString FieldGroup::toString() const
{
QString str;
str += QLatin1String("FieldGroup {\n");
str += QStringLiteral(" FieldGroupName: %1 Value %2\n").arg(d->fieldGroupName).arg(d->value);
if (!d->parameters.isEmpty()) {
QMapIterator<QString, QStringList> i(d->parameters);
QString param;
while (i.hasNext()) {
i.next();
param += QStringLiteral("%1 %2").arg(i.key(), i.value().join(QStringLiteral(",")));
}
str += QStringLiteral(" parameters: %1\n").arg(param);
}
str += QLatin1String("}\n");
return str;
}
QDataStream &KContacts::operator<<(QDataStream &s, const FieldGroup &fieldGroup)
{
return s << fieldGroup.d->parameters << fieldGroup.d->fieldGroupName << fieldGroup.d->value;
}
QDataStream &KContacts::operator>>(QDataStream &s, FieldGroup &fieldGroup)
{
s >> fieldGroup.d->parameters >> fieldGroup.d->fieldGroupName >> fieldGroup.d->value;
return s;
}
/*
This file is part of the KContacts framework.
Copyright (c) 2016 Laurent Montel <montel@kde.org>
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.
*/
#ifndef FIELDGROUP_H
#define FIELDGROUP_H
#include "kcontacts_export.h"
#include <QtCore/QSharedDataPointer>
#include <QtCore/QString>
#include <QtCore/QMap>
/** @short Class that holds a FieldGroup for a contact.
* @since 5.3
*/
namespace KContacts
{
class KCONTACTS_EXPORT FieldGroup
{
friend KCONTACTS_EXPORT QDataStream &operator<<(QDataStream &, const FieldGroup &);
friend KCONTACTS_EXPORT QDataStream &operator>>(QDataStream &, FieldGroup &);
public:
FieldGroup();
FieldGroup(const FieldGroup &other);
FieldGroup(const QString &fieldGroupName);
~FieldGroup();
typedef QVector<FieldGroup> List;
void setFieldGroupName(const QString &fieldGroup);
QString fieldGroupName() const;
bool isValid() const;
void setValue(const QString &value);
QString value() const;
void setParameters(const QMap<QString, QStringList> &params);
QMap<QString, QStringList> parameters() const;
bool operator==(const FieldGroup &other) const;
bool operator!=(const FieldGroup &other) const;
FieldGroup &operator=(const FieldGroup &other);
QString toString() const;
private:
class Private;
QSharedDataPointer<Private> d;
};
KCONTACTS_EXPORT QDataStream &operator<<(QDataStream &stream, const FieldGroup &object);
KCONTACTS_EXPORT QDataStream &operator>>(QDataStream &stream, FieldGroup &object);
}
Q_DECLARE_TYPEINFO(KContacts::FieldGroup, Q_MOVABLE_TYPE);
#endif // FIELDGROUP_H