Commit c12a131e authored by Laurent Montel's avatar Laurent Montel 😁

Nickname can have several parameters in vcard4

parent 812b4c11
......@@ -35,5 +35,6 @@ ecm_add_tests(birthdaytest.cpp
fieldgrouptest.cpp
importexportvcardtest.cpp
titletest.cpp
nicknametest.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 "nicknametest.h"
#include "nickname.h"
#include "vcardtool.h"
#include <qtest.h>
NickNameTest::NickNameTest(QObject *parent)
: QObject(parent)
{
}
NickNameTest::~NickNameTest()
{
}
void NickNameTest::shouldHaveDefaultValue()
{
KContacts::NickName nickname;
QVERIFY(!nickname.isValid());
QVERIFY(nickname.nickname().isEmpty());
QVERIFY(nickname.parameters().isEmpty());
}
void NickNameTest::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::NickName nickname(lang);
nickname.setParameters(params);
QVERIFY(nickname.isValid());
QVERIFY(!nickname.nickname().isEmpty());
QCOMPARE(nickname.nickname(), lang);
QVERIFY(!nickname.parameters().isEmpty());
QCOMPARE(nickname.parameters(), params);
}
void NickNameTest::shouldAssignExternal()
{
KContacts::NickName nickname;
const QString lang(QStringLiteral("fr"));
nickname.setNickName(lang);
QVERIFY(nickname.isValid());
QCOMPARE(nickname.nickname(), lang);
}
void NickNameTest::shouldSerialized()
{
KContacts::NickName nickname;
KContacts::NickName result;
const QString lang(QStringLiteral("fr"));
nickname.setNickName(lang);
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
nickname.setParameters(params);
QByteArray data;
QDataStream s(&data, QIODevice::WriteOnly);
s << nickname;
QDataStream t(&data, QIODevice::ReadOnly);
t >> result;
QVERIFY(nickname == result);
}
void NickNameTest::shouldEqualNickName()
{
KContacts::NickName nickname;
KContacts::NickName result;
const QString lang(QStringLiteral("fr"));
nickname.setNickName(lang);
QMap<QString, QStringList> params;
params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
nickname.setParameters(params);
result = nickname;
QVERIFY(nickname == result);
}
QTEST_MAIN(NickNameTest)
/*
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 NICKNAMETEST_H
#define NICKNAMETEST_H
#include <QObject>
class NickNameTest : public QObject
{
Q_OBJECT
public:
explicit NickNameTest(QObject *parent = Q_NULLPTR);
~NickNameTest();
private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldAssignValue();
void shouldAssignExternal();
void shouldSerialized();
void shouldEqualNickName();
//TODO add parsing and co
};
#endif // NICKNAMETEST_H
......@@ -33,6 +33,7 @@ set(kcontacts_SRCS
vcardtool.cpp
fieldgroup.cpp
title.cpp
nickname.cpp
${vcardparser_SRCS}
)
......@@ -96,6 +97,7 @@ ecm_generate_headers(KContacts_CamelCase_HEADERS
Sound
TimeZone
Title
NickName
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 "nickname.h"
#include <QMap>
#include <qstringlist.h>
#include <QDataStream>
using namespace KContacts;
class Q_DECL_HIDDEN NickName::Private : public QSharedData
{
public:
Private()
{
}
Private(const Private &other)
: QSharedData(other)
{
parameters = other.parameters;
nickname = other.nickname;
}
QMap<QString, QStringList> parameters;
QString nickname;
};
NickName::NickName()
: d(new Private)
{
}
NickName::NickName(const NickName &other)
: d(other.d)
{
}
NickName::NickName(const QString &nickname)
: d(new Private)
{
d->nickname = nickname;
}
NickName::~NickName()
{
}
void NickName::setNickName(const QString &nickname)
{
d->nickname = nickname;
}
QString NickName::nickname() const
{
return d->nickname;
}
bool NickName::isValid() const
{
return !d->nickname.isEmpty();
}
void NickName::setParameters(const QMap<QString, QStringList> &params)
{
d->parameters = params;
}
QMap<QString, QStringList> NickName::parameters() const
{
return d->parameters;
}
bool NickName::operator==(const NickName &other) const
{
return (d->parameters == other.parameters()) && (d->nickname == other.nickname());
}
bool NickName::operator!=(const NickName &other) const
{
return !(other == *this);
}
NickName &NickName::operator=(const NickName &other)
{
if (this != &other) {
d = other.d;
}
return *this;
}
QString NickName::toString() const
{
QString str;
str += QLatin1String("NickName {\n");
str += QStringLiteral(" nickname: %1\n").arg(d->nickname);
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 NickName &nickname)
{
return s << nickname.d->parameters << nickname.d->nickname;
}
QDataStream &KContacts::operator>>(QDataStream &s, NickName &nickname)
{
s >> nickname.d->parameters >> nickname.d->nickname;
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 NICKNAME_H
#define NICKNAME_H
#include "kcontacts_export.h"
#include <QtCore/QSharedDataPointer>
#include <QtCore/QString>
#include <QtCore/QMap>
/** @short Class that holds a NickName for a contact.
* @since 5.3
*/
namespace KContacts
{
class KCONTACTS_EXPORT NickName
{
friend KCONTACTS_EXPORT QDataStream &operator<<(QDataStream &, const NickName &);
friend KCONTACTS_EXPORT QDataStream &operator>>(QDataStream &, NickName &);
public:
NickName();
NickName(const NickName &other);
NickName(const QString &nickname);
~NickName();
typedef QVector<NickName> List;
void setNickName(const QString &nickname);
QString nickname() const;
bool isValid() const;
void setParameters(const QMap<QString, QStringList> &params);
QMap<QString, QStringList> parameters() const;
bool operator==(const NickName &other) const;
bool operator!=(const NickName &other) const;
NickName &operator=(const NickName &other);
QString toString() const;
private:
class Private;
QSharedDataPointer<Private> d;
};
KCONTACTS_EXPORT QDataStream &operator<<(QDataStream &stream, const NickName &object);
KCONTACTS_EXPORT QDataStream &operator>>(QDataStream &stream, NickName &object);
}
Q_DECLARE_TYPEINFO(KContacts::NickName, Q_MOVABLE_TYPE);
#endif // NICKNAME_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