Commit 50612ddd authored by Laurent Montel's avatar Laurent Montel 😁

Add title class. It's not just a string in vcard4 we can have some parameters

parent 461af2fe
......@@ -34,5 +34,6 @@ ecm_add_tests(birthdaytest.cpp
vcardlinetest.cpp
fieldgrouptest.cpp
importexportvcardtest.cpp
titletest.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 "titletest.h"
#include "title.h"
#include "vcardtool.h"
#include <qtest.h>
TitleTest::TitleTest(QObject *parent)
: QObject(parent)
{
}
TitleTest::~TitleTest()
{
}
void TitleTest::shouldHaveDefaultValue()
{
KContacts::Title title;
QVERIFY(!title.isValid());
QVERIFY(title.title().isEmpty());
QVERIFY(title.parameters().isEmpty());
}
void TitleTest::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::Title title(lang);
title.setParameters(params);
QVERIFY(title.isValid());
QVERIFY(!title.title().isEmpty());
QCOMPARE(title.title(), lang);
QVERIFY(!title.parameters().isEmpty());
QCOMPARE(title.parameters(), params);
}
void TitleTest::shouldAssignExternal()
{
KContacts::Title title;
const QString lang(QStringLiteral("fr"));
title.setTitle(lang);
QVERIFY(title.isValid());
QCOMPARE(title.title(), lang);
}
void TitleTest::shouldSerialized()
{
KContacts::Title title;
KContacts::Title result;
const QString lang(QStringLiteral("fr"));
title.setTitle(lang);
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
title.setParameters(params);
QByteArray data;
QDataStream s(&data, QIODevice::WriteOnly);
s << title;
QDataStream t(&data, QIODevice::ReadOnly);
t >> result;
QVERIFY(title == result);
}
void TitleTest::shouldEqualTitle()
{
KContacts::Title title;
KContacts::Title result;
const QString lang(QStringLiteral("fr"));
title.setTitle(lang);
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
title.setParameters(params);
result = title;
QVERIFY(title == result);
}
#if 0
void TitleTest::shouldParseLanguage()
{
QByteArray vcarddata("BEGIN:VCARD\n"
"VERSION:3.0\n"
"N:LastName;FirstName;;;\n"
"UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
"LANG:fr"
"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).langs().count(), 1);
}
void TitleTest::shouldParseWithoutLanguage()
{
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).langs().count(), 0);
}
void TitleTest::shouldCreateVCard()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::Lang::List lstLang;
KContacts::Lang lang(QStringLiteral("fr"));
lstLang << lang;
addr.setLangs(lstLang);
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"
"LANG:fr\r\n"
"N:;;;;\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
void TitleTest::shouldCreateVCardWithTwoLang()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::Lang::List lstLang;
KContacts::Lang lang(QStringLiteral("fr"));
KContacts::Lang lang2(QStringLiteral("fr2"));
lstLang << lang << lang2;
addr.setLangs(lstLang);
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"
"LANG:fr\r\n"
"LANG:fr2\r\n"
"N:;;;;\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
void TitleTest::shouldCreateVCardWithParameters()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::Lang::List lstLang;
KContacts::Lang 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"));
lang.setParameters(params);
lstLang << lang;
addr.setLangs(lstLang);
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"
"LANG;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
"N:;;;;\r\n"
"UID:testuid\r\n"
"END:VCARD\r\n\r\n");
QCOMPARE(ba, expected);
}
void TitleTest::shouldNotGenerateLangForVCard3()
{
KContacts::AddresseeList lst;
KContacts::Addressee addr;
addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
addr.setUid(QStringLiteral("testuid"));
KContacts::Lang::List lstLang;
KContacts::Lang 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"));
lang.setParameters(params);
lstLang << lang;
addr.setLangs(lstLang);
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);
}
#endif
QTEST_MAIN(TitleTest)
/*
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 TITLETEST_H
#define TITLETEST_H
#include <QObject>
class TitleTest : public QObject
{
Q_OBJECT
public:
explicit TitleTest(QObject *parent = Q_NULLPTR);
~TitleTest();
private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldAssignValue();
void shouldAssignExternal();
void shouldSerialized();
void shouldEqualTitle();
#if 0
void shouldParseLanguage();
void shouldParseWithoutLanguage();
void shouldCreateVCard();
void shouldCreateVCardWithTwoLang();
void shouldCreateVCardWithParameters();
void shouldNotGenerateLangForVCard3();
#endif
};
#endif // TITLETEST_H
......@@ -32,6 +32,7 @@ set(kcontacts_SRCS
vcarddrag.cpp
vcardtool.cpp
fieldgroup.cpp
title.cpp
${vcardparser_SRCS}
)
......@@ -94,6 +95,7 @@ ecm_generate_headers(KContacts_CamelCase_HEADERS
SortMode
Sound
TimeZone
Title
VCardDrag
FieldGroup
PREFIX KContacts
......
/*
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 "title.h"
#include <QMap>
#include <qstringlist.h>
#include <QDataStream>
using namespace KContacts;
class Q_DECL_HIDDEN Title::Private : public QSharedData
{
public:
Private()
{
}
Private(const Private &other)
: QSharedData(other)
{
parameters = other.parameters;
title = other.title;
}
QMap<QString, QStringList> parameters;
QString title;
};
Title::Title()
: d(new Private)
{
}
Title::Title(const Title &other)
: d(other.d)
{
}
Title::Title(const QString &language)
: d(new Private)
{
d->title = language;
}
Title::~Title()
{
}
void Title::setTitle(const QString &title)
{
d->title = title;
}
QString Title::title() const
{
return d->title;
}
bool Title::isValid() const
{
return !d->title.isEmpty();
}
void Title::setParameters(const QMap<QString, QStringList> &params)
{
d->parameters = params;
}
QMap<QString, QStringList> Title::parameters() const
{
return d->parameters;
}
bool Title::operator==(const Title &other) const
{
return (d->parameters == other.parameters()) && (d->title == other.title());
}
bool Title::operator!=(const Title &other) const
{
return !(other == *this);
}
Title &Title::operator=(const Title &other)
{
if (this != &other) {
d = other.d;
}
return *this;
}
QString Title::toString() const
{
QString str;
str += QLatin1String("Title {\n");
str += QStringLiteral(" title: %1\n").arg(d->title);
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 Title &title)
{
return s << title.d->parameters << title.d->title;
}
QDataStream &KContacts::operator>>(QDataStream &s, Title &title)
{
s >> title.d->parameters >> title.d->title;
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 TITLE_H
#define TITLE_H
#include "kcontacts_export.h"
#include <QtCore/QSharedDataPointer>
#include <QtCore/QString>
#include <QtCore/QMap>
/** @short Class that holds a Title for a contact.
* @since 5.3
*/
namespace KContacts
{
class KCONTACTS_EXPORT Title
{
friend KCONTACTS_EXPORT QDataStream &operator<<(QDataStream &, const Title &);
friend KCONTACTS_EXPORT QDataStream &operator>>(QDataStream &, Title &);
public:
Title();
Title(const Title &other);
Title(const QString &titleuage);
~Title();
typedef QVector<Title> List;
void setTitle(const QString &title);
QString title() const;
bool isValid() const;
void setParameters(const QMap<QString, QStringList> &params);
QMap<QString, QStringList> parameters() const;
bool operator==(const Title &other) const;
bool operator!=(const Title &other) const;
Title &operator=(const Title &other);
QString toString() const;
private:
class Private;
QSharedDataPointer<Private> d;
};
KCONTACTS_EXPORT QDataStream &operator<<(QDataStream &stream, const Title &object);
KCONTACTS_EXPORT QDataStream &operator>>(QDataStream &stream, Title &object);
}
Q_DECLARE_TYPEINFO(KContacts::Title, Q_MOVABLE_TYPE);
#endif // TITLE_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