Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 955d3fcf authored by Laurent Montel's avatar Laurent Montel 😁

Start to add support for LANG

parent df8a3590
......@@ -60,6 +60,7 @@ set(kabc_LIB_SRCS
sound.cpp
timezone.cpp
ldifconverter.cpp
lang.cpp
addresseelist.cpp
vcardconverter.cpp
vcardtool.cpp
......@@ -124,6 +125,7 @@ install(FILES
field.h
geo.h
key.h
lang.h
ldifconverter.h
lock.h
locknull.h
......
/*
This file is part of libkabc.
Copyright (c) 2015 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 "lang.h"
#include <QMap>
#include <qstringlist.h>
using namespace KABC;
class Lang::Private : public QSharedData
{
public:
Private()
{
}
Private( const Private &other )
: QSharedData( other )
{
parameters = other.parameters;
language = other.language;
}
QMap<QString, QStringList> parameters;
QString language;
};
Lang::Lang()
: d(new Private)
{
}
Lang::Lang(const Lang &other)
: d( other.d )
{
}
Lang::Lang(const QString &lang)
: d(new Private)
{
d->language = lang;
}
Lang::~Lang()
{
}
void Lang::setLanguage(const QString &lang)
{
d->language = lang;
}
QString Lang::language() const
{
return d->language;
}
bool Lang::isValid() const
{
return !d->language.isEmpty();
}
void Lang::setParameters(const QMap<QString, QStringList> &params)
{
d->parameters = params;
}
QMap<QString, QStringList> Lang::parameters() const
{
return d->parameters;
}
bool Lang::operator==(const Lang &other) const
{
return (d->parameters == other.parameters()) && (d->language == other.language());
}
bool Lang::operator!=(const Lang &other) const
{
return !( other == *this );
}
Lang &Lang::operator=(const Lang &other)
{
if ( this != &other ) {
d = other.d;
}
return *this;
}
QString Lang::toString() const
{
QString str;
str += QString::fromLatin1( "Lang {\n" );
str += QString::fromLatin1( " language: %1\n" ).arg( d->language );
if (!d->parameters.isEmpty()) {
QMapIterator<QString, QStringList> i(d->parameters);
QString param;
while (i.hasNext()) {
i.next();
param += QString::fromLatin1("%1 %2").arg(i.key()).arg(i.value().join(QLatin1String(",")));
}
str += QString::fromLatin1( " parameters: %1\n" ).arg( param );
}
str += QString::fromLatin1( "}\n" );
return str;
}
QDataStream &KABC::operator<<(QDataStream &s, const Lang &lang)
{
return s << lang.d->parameters << lang.d->language;
}
QDataStream &KABC::operator>>(QDataStream &s, Lang &lang)
{
s >> lang.d->parameters >> lang.d->language;
return s;
}
/*
This file is part of libkabc.
Copyright (c) 2015 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 LANG_H
#define LANG_H
#include "kabc_export.h"
#include <QtCore/QSharedDataPointer>
#include <QtCore/QString>
#include <QtCore/QMap>
/** @short Class that holds a Language for a contact.
* @since 4.14.5
*/
namespace KABC {
class KABC_EXPORT Lang
{
friend KABC_EXPORT QDataStream &operator<<( QDataStream &, const Lang & );
friend KABC_EXPORT QDataStream &operator>>( QDataStream &, Lang & );
public:
Lang();
Lang(const Lang &other);
Lang(const QString &mail);
~Lang();
typedef QList<Lang> List;
void setLanguage(const QString &lang);
QString language() const;
bool isValid() const;
void setParameters(const QMap<QString, QStringList> &params);
QMap<QString, QStringList> parameters() const;
bool operator==( const Lang &other ) const;
bool operator!=( const Lang &other ) const;
Lang &operator=( const Lang &other );
QString toString() const;
private:
class Private;
QSharedDataPointer<Private> d;
};
KABC_EXPORT QDataStream &operator<<( QDataStream &stream, const Lang &object );
KABC_EXPORT QDataStream &operator>>( QDataStream &stream, Lang &object );
}
#endif // LANG_H
......@@ -196,3 +196,13 @@ kde4_add_unit_test(emailtest TESTNAME kabc-emailtest ${emailtest_SRCS})
target_link_libraries(emailtest ${KDE4_KDECORE_LIBS} kabc ${QT_QTTEST_LIBRARY} ${QT_QTGUI_LIBRARY})
########### next target ###############
set(langtest_SRCS langtest.cpp)
kde4_add_unit_test(langtest TESTNAME kabc-langtest ${langtest_SRCS})
target_link_libraries(langtest ${KDE4_KDECORE_LIBS} kabc ${QT_QTTEST_LIBRARY} ${QT_QTGUI_LIBRARY})
/*
This file is part of libkabc.
Copyright (c) 2015 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 "langtest.h"
#include "kabc/lang.h"
#include <qtest_kde.h>
LangTest::LangTest(QObject *parent)
: QObject(parent)
{
}
LangTest::~LangTest()
{
}
void LangTest::shouldHaveDefaultValue()
{
KABC::Lang language;
QVERIFY(!language.isValid());
QVERIFY(language.language().isEmpty());
QVERIFY(language.parameters().isEmpty());
}
void LangTest::shouldAssignValue()
{
const QString lang(QLatin1String("fr"));
QMap<QString, QStringList> params;
params.insert(QLatin1String("Foo1"), QStringList()<< QLatin1String("bla1") <<QLatin1String("blo1"));
params.insert(QLatin1String("Foo2"), QStringList()<< QLatin1String("bla2") <<QLatin1String("blo2"));
KABC::Lang language(lang);
language.setParameters(params);
QVERIFY(language.isValid());
QVERIFY(!language.language().isEmpty());
QCOMPARE(language.language(), lang);
QVERIFY(!language.parameters().isEmpty());
QCOMPARE(language.parameters(), params);
}
void LangTest::shouldAssignExternal()
{
KABC::Lang language;
const QString lang(QLatin1String("fr"));
language.setLanguage(lang);
QVERIFY(language.isValid());
QCOMPARE(language.language(), lang);
}
void LangTest::shouldSerialized()
{
KABC::Lang language;
KABC::Lang result;
const QString lang(QLatin1String("fr"));
language.setLanguage(lang);
QMap<QString, QStringList> params;
params.insert(QLatin1String("Foo1"), QStringList()<< QLatin1String("bla1") <<QLatin1String("blo1"));
params.insert(QLatin1String("Foo2"), QStringList()<< QLatin1String("bla2") <<QLatin1String("blo2"));
language.setParameters(params);
QByteArray data;
QDataStream s( &data, QIODevice::WriteOnly );
s << language;
QDataStream t( &data, QIODevice::ReadOnly );
t >> result;
QVERIFY( language == result );
}
void LangTest::shouldEqualLanguage()
{
KABC::Lang language;
KABC::Lang result;
const QString lang(QLatin1String("fr"));
language.setLanguage(lang);
QMap<QString, QStringList> params;
params.insert(QLatin1String("Foo1"), QStringList()<< QLatin1String("bla1") <<QLatin1String("blo1"));
params.insert(QLatin1String("Foo2"), QStringList()<< QLatin1String("bla2") <<QLatin1String("blo2"));
language.setParameters(params);
result = language;
QVERIFY( language == result );
}
QTEST_KDEMAIN(LangTest, NoGUI)
/*
This file is part of libkabc.
Copyright (c) 2015 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 LANGTEST_H
#define LANGTEST_H
#include <QObject>
class LangTest : public QObject
{
Q_OBJECT
public:
explicit LangTest(QObject *parent = 0);
~LangTest();
private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldAssignValue();
void shouldAssignExternal();
void shouldSerialized();
void shouldEqualLanguage();
};
#endif // LANGTEST_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