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 5b53aeec authored by Laurent Montel's avatar Laurent Montel 😁

Use email class directly now

parent 78d2c8b2
......@@ -31,6 +31,7 @@
#include "addresseehelper.h"
#include "field.h"
#include "email.h"
#ifndef KDEPIM_NO_KRESOURCES
#include "resource.h"
#endif
......@@ -140,7 +141,7 @@ class Addressee::Private : public QSharedData
PhoneNumber::List mPhoneNumbers;
Address::List mAddresses;
Key::List mKeys;
QStringList mEmails;
Email::List mEmails;
QStringList mCategories;
QMap<QString, QString> mCustomFields;
......@@ -337,7 +338,7 @@ bool Addressee::operator==( const Addressee &addressee ) const
return false;
}
if ( !emailsEquals( d->mEmails, addressee.d->mEmails ) ) {
if ( !listEquals( d->mEmails, addressee.d->mEmails ) ) {
kDebug() << "emails differs";
return false;
}
......@@ -1228,32 +1229,44 @@ QString Addressee::fullEmail( const QString &email ) const
return text;
}
void Addressee::insertEmail( const QString &email, bool preferred )
void Addressee::insertEmail( const QString &email, bool preferred, const QMap<QString, QStringList> &param )
{
if ( email.simplified().isEmpty() )
return;
if ( d->mEmails.contains( email ) ) {
if ( !preferred || d->mEmails.first() == email )
if ( email.simplified().isEmpty() )
return;
d->mEmails.removeAll( email );
d->mEmails.prepend( email );
} else {
for (int i = 0; i < d->mEmails.size(); ++i) {
if (d->mEmails.at(i).mail() == email) {
if (!preferred || i == 0) {
return;
}
Email tempMail = d->mEmails.takeAt(i);
d->mEmails.prepend( tempMail );
return;
}
}
Email mail(email);
mail.setParameters(param);
d->mEmpty = false;
if ( preferred ) {
d->mEmails.prepend( email );
d->mEmails.prepend( mail );
} else {
d->mEmails.append( email );
d->mEmails.append( mail );
}
}
}
void Addressee::insertEmail( const QString &email, bool preferred )
{
insertEmail(email, preferred, QMap<QString, QStringList>());
}
void Addressee::removeEmail( const QString &email )
{
if ( d->mEmails.contains( email ) ) {
d->mEmails.removeAll( email );
}
for (int i = 0; i < d->mEmails.size(); ++i) {
if (d->mEmails.at(i).mail() == email) {
d->mEmails.removeAt(i);
}
}
}
QString Addressee::preferredEmail() const
......@@ -1261,18 +1274,33 @@ QString Addressee::preferredEmail() const
if ( d->mEmails.count() == 0 )
return QString();
else
return d->mEmails.first();
return d->mEmails.first().mail();
}
QStringList Addressee::emails() const
{
return d->mEmails;
QStringList list;
const int numberOfEmail(d->mEmails.size());
for (int i = 0; i < numberOfEmail; ++i) {
list << d->mEmails.at(i).mail();
}
return list;
}
Email::List Addressee::emailList() const
{
return d->mEmails;
}
void Addressee::setEmails( const QStringList& emails )
{
d->mEmails = emails;
d->mEmails.clear();
for (int i = 0; i < emails.size(); ++i) {
d->mEmails.append(Email(emails.at(i)));
}
}
void Addressee::insertPhoneNumber( const PhoneNumber &phoneNumber )
{
d->mEmpty = false;
......@@ -1472,10 +1500,10 @@ QString Addressee::toString() const
str += QString::fromLatin1( " Sound: %1\n" ).arg( sound().toString() );
str += QLatin1String( " Emails {\n" );
const QStringList e = emails();
QStringList::ConstIterator it;
for ( it = e.begin(); it != e.end(); ++it ) {
str += QString::fromLatin1( " %1\n" ).arg( *it );
const Email::List listEmail = d->mEmails;
Email::List::ConstIterator it5;
for ( it5 = listEmail.begin(); it5 != listEmail.end(); ++it5 ) {
str += ( *it5 ).toString();
}
str += QLatin1String( " }\n" );
......
......@@ -35,6 +35,7 @@
#include "kabc/picture.h"
#include "kabc/secrecy.h"
#include "kabc/sound.h"
#include "kabc/email.h"
#include "kabc/timezone.h"
#include "kabc/addresseelist.h" // for typedef QList<Addressee> List;
......@@ -976,7 +977,12 @@ class KABC_EXPORT Addressee
*/
static QString mimeType();
private:
//kf5 merge with insertEmail(...)
void insertEmail(const QString &email, bool preferred, const QMap<QString, QStringList> &param);
KABC::Email::List emailList() const;
private:
class Private;
QSharedDataPointer<Private> d;
};
......
......@@ -29,7 +29,6 @@ class Email::Private : public QSharedData
{
public:
Private()
: preferred(false)
{
}
......@@ -38,11 +37,9 @@ public:
{
parameters = other.parameters;
mail = other.mail;
preferred = other.preferred;
}
QMap<QString, QStringList> parameters;
QString mail;
bool preferred;
};
Email::Email()
......@@ -51,11 +48,15 @@ Email::Email()
}
Email::Email(const QString &mail, bool preferred)
Email::Email(const QString &mail)
: d( new Private )
{
d->mail = mail;
d->preferred = preferred;
}
Email::Email( const Email &other )
: d( other.d )
{
}
Email::~Email()
......@@ -70,7 +71,12 @@ QMap<QString, QStringList> Email::parameters() const
bool Email::operator==(const Email &other) const
{
return (d->parameters == other.parameters()) && (d->mail == other.mail()) && (d->preferred == other.preferred());
return (d->parameters == other.parameters()) && (d->mail == other.mail());
}
bool Email::operator!=(const Email &other) const
{
return !( other == *this );
}
Email &Email::operator=(const Email &other)
......@@ -87,7 +93,6 @@ QString Email::toString() const
QString str;
str += QString::fromLatin1( "Email {\n" );
str += QString::fromLatin1( " mail: %1\n" ).arg( d->mail );
str += QString::fromLatin1( " preferred: %1\n" ).arg( d->preferred ? QLatin1String("True") : QLatin1String("False") );
if (!d->parameters.isEmpty()) {
QMapIterator<QString, QStringList> i(d->parameters);
QString param;
......@@ -101,16 +106,6 @@ QString Email::toString() const
return str;
}
void Email::setPreferred(bool pref)
{
d->preferred = pref;
}
bool Email::preferred() const
{
return d->preferred;
}
void Email::setParameters(const QMap<QString, QStringList> &params)
{
d->parameters = params;
......@@ -133,12 +128,12 @@ bool Email::isValid() const
QDataStream &KABC::operator<<(QDataStream &s, const Email &email)
{
return s << email.d->parameters << email.d->mail << email.d->preferred;
return s << email.d->parameters << email.d->mail;
}
QDataStream &KABC::operator>>(QDataStream &s, Email &email)
{
s >> email.d->parameters >> email.d->mail >> email.d->preferred;
s >> email.d->parameters >> email.d->mail;
return s;
}
......@@ -42,7 +42,8 @@ public:
* Creates an empty email object.
*/
Email();
Email(const QString &mail, bool preferred);
Email(const Email &other);
Email(const QString &mail);
~Email();
......@@ -56,13 +57,12 @@ public:
void setParameters(const QMap<QString, QStringList> &params);
QMap<QString, QStringList> parameters() const;
void setPreferred(bool pref);
bool preferred() const;
bool operator==( const Email &other ) const;
bool operator!=( const Email &other ) const;
Email &operator=( const Email &other );
QString toString() const;
private:
class Private;
......
......@@ -38,22 +38,19 @@ void EmailTest::shouldHaveDefaultValue()
QVERIFY(!email.isValid());
QVERIFY(email.mail().isEmpty());
QVERIFY(email.parameters().isEmpty());
QVERIFY(!email.preferred());
}
void EmailTest::shouldAssignValue()
{
const QString mail(QLatin1String("foo@kde.org"));
const bool preferred = true;
QMap<QString, QStringList> params;
params.insert(QLatin1String("Foo1"), QStringList()<< QLatin1String("bla1") <<QLatin1String("blo1"));
params.insert(QLatin1String("Foo2"), QStringList()<< QLatin1String("bla2") <<QLatin1String("blo2"));
KABC::Email email(mail, preferred);
KABC::Email email(mail);
email.setParameters(params);
QVERIFY(email.isValid());
QVERIFY(!email.mail().isEmpty());
QCOMPARE(email.mail(), mail);
QVERIFY(email.preferred());
QVERIFY(!email.parameters().isEmpty());
QCOMPARE(email.parameters(), params);
}
......@@ -62,13 +59,10 @@ void EmailTest::shouldAssignExternal()
{
KABC::Email email;
const QString mail(QLatin1String("foo@kde.org"));
const bool preferred = true;
email.setEmail(mail);
email.setPreferred(preferred);
QVERIFY(email.isValid());
QVERIFY(!email.mail().isEmpty());
QCOMPARE(email.mail(), mail);
QVERIFY(email.preferred());
}
void EmailTest::shouldSerialized()
......@@ -76,9 +70,7 @@ void EmailTest::shouldSerialized()
KABC::Email email;
KABC::Email result;
const QString mail(QLatin1String("foo@kde.org"));
const bool preferred = true;
email.setEmail(mail);
email.setPreferred(preferred);
QMap<QString, QStringList> params;
params.insert(QLatin1String("Foo1"), QStringList()<< QLatin1String("bla1") <<QLatin1String("blo1"));
params.insert(QLatin1String("Foo2"), QStringList()<< QLatin1String("bla2") <<QLatin1String("blo2"));
......@@ -100,9 +92,7 @@ void EmailTest::shouldEqualEmail()
KABC::Email email;
KABC::Email result;
const QString mail(QLatin1String("foo@kde.org"));
const bool preferred = true;
email.setEmail(mail);
email.setPreferred(preferred);
QMap<QString, QStringList> params;
params.insert(QLatin1String("Foo1"), QStringList()<< QLatin1String("bla1") <<QLatin1String("blo1"));
params.insert(QLatin1String("Foo2"), QStringList()<< QLatin1String("bla2") <<QLatin1String("blo2"));
......
......@@ -291,7 +291,6 @@ QByteArray VCardParser::createVCards( const VCard::List &list )
}
}
}
QByteArray input, output;
bool checkMultibyte = false; // avoid splitting a multibyte character
......
......@@ -26,6 +26,7 @@
#include <QtCore/QString>
#include <QtCore/QBuffer>
#include <QDebug>
using namespace KABC;
......@@ -181,15 +182,24 @@ QByteArray VCardTool::createVCards( const Addressee::List &list,
}
// EMAIL
const QStringList emails = ( *addrIt ).emails();
const Email::List emailList = ( *addrIt ).emailList();
Email::List::ConstIterator emailIt;
Email::List::ConstIterator emailEnd( emailList.end() );
bool pref = true;
for ( strIt = emails.begin(); strIt != emails.end(); ++strIt ) {
VCardLine line( QLatin1String( "EMAIL" ), *strIt );
if ( pref == true && emails.count() > 1 ) {
line.addParameter( QLatin1String( "TYPE" ), QLatin1String( "PREF" ) );
pref = false;
}
card.addLine( line );
for ( emailIt = emailList.begin(); emailIt != emailEnd; ++emailIt ) {
VCardLine line( QLatin1String( "EMAIL" ), (*emailIt).mail() );
if ( pref == true && emailList.count() > 1 ) {
line.addParameter( QLatin1String( "TYPE" ), QLatin1String( "PREF" ) );
pref = false;
}
QMapIterator<QString, QStringList> i((*emailIt).parameters());
while (i.hasNext()) {
i.next();
qDebug()<<" i.key()"<<i.key()<<" i.value().join(QLatin1S"<<i.value().join(QLatin1String(","));
line.addParameter( i.key(), i.value().join(QLatin1String(",")) );
}
card.addLine( line );
}
// FN required for only version > 2.1
......@@ -518,7 +528,7 @@ Addressee::List VCardTool::parseVCards( const QByteArray &vcard ) const
else if ( identifier == QLatin1String( "email" ) ) {
const QStringList types = ( *lineIt ).parameters( QLatin1String( "type" ) );
addr.insertEmail( ( *lineIt ).value().toString(),
types.contains( QLatin1String( "PREF" ) ) );
types.contains( QLatin1String( "PREF" ) ), (*lineIt).parameterMap() );
}
// FN
......
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