Commit 335fc3bb authored by Laurent Montel's avatar Laurent Montel 😁

in vcard4 we can have several role

parent 801426de
...@@ -36,5 +36,6 @@ ecm_add_tests(birthdaytest.cpp ...@@ -36,5 +36,6 @@ ecm_add_tests(birthdaytest.cpp
importexportvcardtest.cpp importexportvcardtest.cpp
titletest.cpp titletest.cpp
nicknametest.cpp nicknametest.cpp
roletest.cpp
NAME_PREFIX "kcontacts-" LINK_LIBRARIES KF5Contacts Qt5::Test KF5::ConfigCore Qt5::Gui) 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 "roletest.h"
#include "role.h"
#include "vcardtool.h"
#include <qtest.h>
RoleTest::RoleTest(QObject *parent)
: QObject(parent)
{
}
RoleTest::~RoleTest()
{
}
void RoleTest::shouldHaveDefaultValue()
{
KContacts::Role role;
QVERIFY(!role.isValid());
QVERIFY(role.role().isEmpty());
QVERIFY(role.parameters().isEmpty());
}
void RoleTest::shouldAssignValue()
{
const QString lang(QStringLiteral("fr"));
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
KContacts::Role role(lang);
role.setParameters(params);
QVERIFY(role.isValid());
QVERIFY(!role.role().isEmpty());
QCOMPARE(role.role(), lang);
QVERIFY(!role.parameters().isEmpty());
QCOMPARE(role.parameters(), params);
}
void RoleTest::shouldAssignExternal()
{
KContacts::Role role;
const QString lang(QStringLiteral("fr"));
role.setRole(lang);
QVERIFY(role.isValid());
QCOMPARE(role.role(), lang);
}
void RoleTest::shouldSerialized()
{
KContacts::Role role;
KContacts::Role result;
const QString lang(QStringLiteral("fr"));
role.setRole(lang);
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
role.setParameters(params);
QByteArray data;
QDataStream s(&data, QIODevice::WriteOnly);
s << role;
QDataStream t(&data, QIODevice::ReadOnly);
t >> result;
QVERIFY(role == result);
}
void RoleTest::shouldEqualRole()
{
KContacts::Role role;
KContacts::Role result;
const QString lang(QStringLiteral("fr"));
role.setRole(lang);
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
role.setParameters(params);
result = role;
QVERIFY(role == result);
}
#if 0
void RoleTest::shouldParseRole()
{
QByteArray vcarddata("BEGIN:VCARD\n"
"VERSION:3.0\n"
"N:LastName;FirstName;;;\n"
"UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
"Role:boo\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).extraRoleList().count(), 1);
QCOMPARE(lst.at(0).role(), QStringLiteral("boo"));
}
void RoleTest::shouldParseWithoutRole()
{
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).extraRoleList().count(), 0);
}
void RoleTest::shouldCreateVCard()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::Role::List lstRole;
KContacts::Role role(QStringLiteral("fr"));
lstRole << role;
addr.setExtraRoleList(lstRole);
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"
"ROLE:fr\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
void RoleTest::shouldCreateVCardWithTwoRole()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::Role::List lstRole;
KContacts::Role role(QStringLiteral("fr"));
KContacts::Role role2(QStringLiteral("fr2"));
lstRole << role << role2;
addr.setExtraRoleList(lstRole);
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"
"ROLE:fr\r\n"
"ROLE:fr2\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
void RoleTest::shouldCreateVCardWithParameters()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::Role::List lstRole;
KContacts::Role role(QStringLiteral("fr"));
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
role.setParameters(params);
lstRole << role;
addr.setExtraRoleList(lstRole);
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"
"ROLE;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
void RoleTest::shouldGenerateRoleForVCard3()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::Role::List lstRole;
KContacts::Role role(QStringLiteral("fr"));
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
role.setParameters(params);
lstRole << role;
addr.setExtraRoleList(lstRole);
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"
"ROLE;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
#endif
QTEST_MAIN(RoleTest)
/*
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 ROLETEST_H
#define ROLETEST_H
#include <QObject>
class RoleTest : public QObject
{
Q_OBJECT
public:
explicit RoleTest(QObject *parent = Q_NULLPTR);
~RoleTest();
private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldAssignValue();
void shouldAssignExternal();
void shouldSerialized();
void shouldEqualRole();
#if 0
void shouldParseRole();
void shouldParseWithoutRole();
void shouldCreateVCard();
void shouldCreateVCardWithTwoRole();
void shouldCreateVCardWithParameters();
void shouldGenerateRoleForVCard3();
#endif
};
#endif // ROLETEST_H
...@@ -34,6 +34,7 @@ set(kcontacts_SRCS ...@@ -34,6 +34,7 @@ set(kcontacts_SRCS
fieldgroup.cpp fieldgroup.cpp
title.cpp title.cpp
nickname.cpp nickname.cpp
role.cpp
${vcardparser_SRCS} ${vcardparser_SRCS}
) )
...@@ -97,6 +98,7 @@ ecm_generate_headers(KContacts_CamelCase_HEADERS ...@@ -97,6 +98,7 @@ ecm_generate_headers(KContacts_CamelCase_HEADERS
Sound Sound
TimeZone TimeZone
Title Title
Role
NickName NickName
VCardDrag VCardDrag
FieldGroup FieldGroup
......
/*
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 "role.h"
#include <QMap>
#include <qstringlist.h>
#include <QDataStream>
using namespace KContacts;
class Q_DECL_HIDDEN Role::Private : public QSharedData
{
public:
Private()
{
}
Private(const Private &other)
: QSharedData(other)
{
parameters = other.parameters;
role = other.role;
}
QMap<QString, QStringList> parameters;
QString role;
};
Role::Role()
: d(new Private)
{
}
Role::Role(const Role &other)
: d(other.d)
{
}
Role::Role(const QString &role)
: d(new Private)
{
d->role = role;
}
Role::~Role()
{
}
void Role::setRole(const QString &role)
{
d->role = role;
}
QString Role::role() const
{
return d->role;
}
bool Role::isValid() const
{
return !d->role.isEmpty();
}
void Role::setParameters(const QMap<QString, QStringList> &params)
{
d->parameters = params;
}
QMap<QString, QStringList> Role::parameters() const
{
return d->parameters;
}
bool Role::operator==(const Role &other) const
{
return (d->parameters == other.parameters()) && (d->role == other.role());
}
bool Role::operator!=(const Role &other) const
{
return !(other == *this);
}
Role &Role::operator=(const Role &other)
{
if (this != &other) {
d = other.d;
}
return *this;
}
QString Role::toString() const
{
QString str;
str += QLatin1String("Role {\n");
str += QStringLiteral(" role: %1\n").arg(d->role);
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(QLatin1Char(',')));
}
str += QStringLiteral(" parameters: %1\n").arg(param);
}
str += QLatin1String("}\n");
return str;
}
QDataStream &KContacts::operator<<(QDataStream &s, const Role &role)
{
return s << role.d->parameters << role.d->role;
}
QDataStream &KContacts::operator>>(QDataStream &s, Role &role)
{
s >> role.d->parameters >> role.d->role;
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 ROLE_H
#define ROLE_H
#include "kcontacts_export.h"
#include <QtCore/QSharedDataPointer>
#include <QtCore/QString>
#include <QtCore/QMap>
/** @short Class that holds a Role for a contact.
* @since 5.3
*/
namespace KContacts
{
class KCONTACTS_EXPORT Role
{
friend KCONTACTS_EXPORT QDataStream &operator<<(QDataStream &, const Role &);
friend KCONTACTS_EXPORT QDataStream &operator>>(QDataStream &, Role &);
public:
Role();
Role(const Role &other);
Role(const QString &role);
~Role();
typedef QVector<Role> List;
void setRole(const QString &role);
QString role() const;
bool isValid() const;
void setParameters(const QMap<QString, QStringList> &params);
QMap<QString, QStringList> parameters() const;
bool operator==(const Role &other) const;
bool operator!=(const Role &other) const;
Role &operator=(const Role &other);
QString toString() const;
private:
class Private;
QSharedDataPointer<Private> d;
};
KCONTACTS_EXPORT QDataStream &operator<<(QDataStream &stream, const Role &object);
KCONTACTS_EXPORT QDataStream &operator>>(QDataStream &stream, Role &object);
}
Q_DECLARE_TYPEINFO(KContacts::Role, Q_MOVABLE_TYPE);
#endif // ROLE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment