aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <[email protected]>2016-07-22 13:39:40 +0200
committerMontel Laurent <[email protected]>2016-07-22 13:39:40 +0200
commit50612ddd724b4623592980090920d0346cd22bd8 (patch)
tree399d8f319aef8d4746a801eed1462d6988104e01
parent461af2fea82d4615fcccdd6c5c86d1eff58d0881 (diff)
Add title class. It's not just a string in vcard4 we can have some parameters
-rw-r--r--autotests/CMakeLists.txt1
-rw-r--r--autotests/titletest.cpp246
-rw-r--r--autotests/titletest.h48
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/title.cpp139
-rw-r--r--src/title.h71
6 files changed, 507 insertions, 0 deletions
diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt
index c5988ae..0863266 100644
--- a/autotests/CMakeLists.txt
+++ b/autotests/CMakeLists.txt
@@ -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)
diff --git a/autotests/titletest.cpp b/autotests/titletest.cpp
new file mode 100644
index 0000000..65caf81
--- /dev/null
+++ b/autotests/titletest.cpp
@@ -0,0 +1,246 @@
+/*
+ This file is part of the KContacts framework.
+ Copyright (c) 2016 Laurent Montel <[email protected]>
+
+ 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"
+ "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"
+ "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("[email protected]"));
+ 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:[email protected]\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("[email protected]"));
+ 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:[email protected]\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("[email protected]"));
+ 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:[email protected]\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("[email protected]"));
+ 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:[email protected]\r\n"
+ "N:;;;;\r\n"
+ "UID:testuid\r\n"
+ "END:VCARD\r\n\r\n");
+ QCOMPARE(ba, expected);
+}
+#endif
+QTEST_MAIN(TitleTest)
diff --git a/autotests/titletest.h b/autotests/titletest.h
new file mode 100644
index 0000000..cc202a8
--- /dev/null
+++ b/autotests/titletest.h
@@ -0,0 +1,48 @@
+/*
+ This file is part of the KContacts framework.
+ Copyright (c) 2016 Laurent Montel <[email protected]>
+
+ 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
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 957b453..32fd27d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -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
diff --git a/src/title.cpp b/src/title.cpp
new file mode 100644
index 0000000..8dacd20
--- /dev/null
+++ b/src/title.cpp
@@ -0,0 +1,139 @@
+/*
+ This file is part of the KContacts framework.
+ Copyright (c) 2016 Laurent Montel <[email protected]>
+
+ 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;
+}
diff --git a/src/title.h b/src/title.h
new file mode 100644
index 0000000..f823b3c
--- /dev/null
+++ b/src/title.h
@@ -0,0 +1,71 @@
+/*
+ This file is part of the KContacts framework.
+ Copyright (c) 2016 Laurent Montel <[email protected]>
+
+ 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