Commit 779da83b authored by Ahmad Samir's avatar Ahmad Samir
Browse files

Addressee: deprecate insertEmail() and add a new method instead

addEmail() takes an Email object, i.e. you create an Email, call the setter
methods to make it preferred, set the type ...etc, then add it to Addressee.

Fix unit test: the code in insertEmail() only considered the bool "preferred"
function parameter, but not the perferred parameter in the parameter map
passed to insertEmail(). Reading the code the "preferred" email should be
first in the list returned by Adressee::emails().
parent 5dc725f9
......@@ -18,7 +18,7 @@ void AddresseeTest::emptyTest()
QVERIFY(addressee.isEmpty());
KContacts::Addressee addresseeWithMail;
addresseeWithMail.insertEmail(QStringLiteral("foo@bar.org"));
addresseeWithMail.addEmail(QStringLiteral("foo@bar.org"));
QVERIFY(!addresseeWithMail.isEmpty());
}
......
......@@ -22,7 +22,7 @@ CustomIdentifierTest::~CustomIdentifierTest()
void CustomIdentifierTest::shouldHaveEmptyCustoms()
{
KContacts::Addressee addresseeWithMail;
addresseeWithMail.insertEmail(QStringLiteral("foo@bar.org"));
addresseeWithMail.addEmail(QStringLiteral("foo@bar.org"));
QVERIFY(!addresseeWithMail.isEmpty());
QVERIFY(addresseeWithMail.customs().isEmpty());
}
......
......@@ -88,8 +88,8 @@ void ImportExportVCardTest::shouldExportFullTestVcard4()
"BDAY:19531015T231000Z\r\n"
"CALADRURI;PREF=1:mailto:detective@sherlockholmes.com\r\n"
"CALURI;PREF=1:https://sherlockholmes.com/calendar/sherlockholmes\r\n"
"EMAIL;TYPE=home:sherlock.holmes@gmail.com\r\n"
"EMAIL;PREF=1;TYPE=work:detective@sherlockholmes.com\r\n"
"EMAIL;TYPE=home:sherlock.holmes@gmail.com\r\n"
"FBURL;PREF=1:https://sherlockholmes.com/busy/detective\r\n"
"FN:Sherlock Holmes\r\n"
"GENDER:M\r\n"
......
......@@ -38,11 +38,11 @@ VCardDragTest::VCardDragTest()
{
mAddressee1.setGivenName(QStringLiteral("Kevin"));
mAddressee1.setFamilyName(QStringLiteral("Krammer"));
mAddressee1.insertEmail(QStringLiteral("kevin.krammer@gmx.at"));
mAddressee1.addEmail(QStringLiteral("kevin.krammer@gmx.at"));
mAddressee2.setGivenName(QStringLiteral("Tobias"));
mAddressee2.setFamilyName(QStringLiteral("König"));
mAddressee2.insertEmail(QStringLiteral("tokoe@kde.org"));
mAddressee2.addEmail(QStringLiteral("tokoe@kde.org"));
}
void VCardDragTest::testPopulate()
......
......@@ -1582,6 +1582,31 @@ QString Addressee::fullEmail(const QString &email) const
return text;
}
void Addressee::addEmail(const Email &email)
{
const QString mailAddr = email.mail();
auto it = std::find_if(d->mEmails.begin(), d->mEmails.end(), [&mailAddr](const Email &e) {
return e.mail() == mailAddr;
});
if (it != d->mEmails.end()) { // Already exists, modify it
*it = email;
if (email.isPreferred()) {
// Move it to the beginning of mEmails
std::rotate(d->mEmails.begin(), it, it + 1);
}
return;
}
// Add it to the list
d->mEmpty = false;
if (email.isPreferred()) {
d->mEmails.prepend(email);
} else {
d->mEmails.append(email);
}
}
#if KCONTACTS_BUILD_DEPRECATED_SINCE(5, 88)
void Addressee::insertEmail(const QString &email, bool preferred, const QMap<QString, QStringList> &param)
{
if (email.simplified().isEmpty()) {
......@@ -1608,6 +1633,7 @@ void Addressee::insertEmail(const QString &email, bool preferred, const QMap<QSt
d->mEmails.append(mail);
}
}
#endif
void Addressee::removeEmail(const QString &email)
{
......
......@@ -744,6 +744,7 @@ public:
*/
QString fullEmail(const QString &email = QString()) const;
#if KCONTACTS_ENABLE_DEPRECATED_SINCE(5, 88)
/**
Insert an email address. If the email address already exists in this
addressee it is not duplicated.
......@@ -751,8 +752,22 @@ public:
@param email Email address
@param preferred Set to true, if this is the preferred email address of
the addressee.
@deprecated Since 5.88, use addEmail(const Email &) instead; you can use the Email
methods to set any parameters, e.g. Email::setPreferred(), Email::setType() ...etc
*/
KCONTACTS_DEPRECATED_VERSION(5, 88, "Use addEmail(const Email &), and the Email class setter methods instead.")
void insertEmail(const QString &email, bool preferred = false, const QMap<QString, QStringList> &param = QMap<QString, QStringList>());
#endif
/**
Adds an email address. If the email address (i.e. @p email.mail()) already
exists in this addressee it won't be duplicated, instead @p email is assigned
to it.
@since 5.88
*/
void addEmail(const Email &email);
/**
Remove email address. If the email address doesn't exist, nothing happens.
......@@ -762,8 +777,8 @@ public:
void removeEmail(const QString &email);
/**
Return preferred email address. This is the first email address or the
last one added with insertEmail() with a set preferred parameter.
Return preferred email address. This is the first email address or the last
one added with insertEmail() or addEmail() with a set preferred parameter.
*/
QString preferredEmail() const;
......
......@@ -377,7 +377,7 @@ void KContacts::evaluatePair(Addressee &a,
|| fieldname == QLatin1String("mozillasecondemail") /* mozilla */
|| fieldname == QLatin1String("othermailbox") /*TheBat!*/) {
if (a.emails().indexOf(value) == -1) {
a.insertEmail(value);
a.addEmail(value);
}
return;
}
......
......@@ -861,7 +861,7 @@ Addressee::List VCardTool::parseVCards(const QByteArray &vcard) const
const QStringList types = (*lineIt).parameters(QStringLiteral("type"));
Email mail((*lineIt).value().toString());
mail.setParams((*lineIt).parameterMap());
addr.insertEmail((*lineIt).value().toString(), types.contains(QLatin1String("PREF")), (*lineIt).parameterMap());
addr.addEmail(mail);
}
// KIND
else if (identifier == QLatin1String("kind")) {
......
......@@ -21,8 +21,11 @@ Addressee vcard1()
KContacts::ResourceLocatorUrl url;
url.setUrl(QUrl(QStringLiteral("http://home.earthlink.net/~fdawson")));
addr.setUrl(url);
addr.insertEmail(QStringLiteral("fdawson@earthlink.net"));
addr.insertEmail(QStringLiteral("Frank_Dawson@Lotus.com"), true);
addr.addEmail(Email(QStringLiteral("fdawson@earthlink.net")));
Email mail(QStringLiteral("Frank_Dawson@Lotus.com"));
mail.setPreferred(true);
addr.addEmail(mail);
addr.insertPhoneNumber(PhoneNumber(QStringLiteral("+1-919-676-9515"), PhoneNumber::Voice | PhoneNumber::Msg | PhoneNumber::Work));
addr.insertPhoneNumber(PhoneNumber(QStringLiteral("+1-919-676-9564"), PhoneNumber::Fax | PhoneNumber::Work));
Address a(Address::Work | Address::Postal | Address::Parcel);
......@@ -41,7 +44,7 @@ Addressee vcard2()
addr.setName(QStringLiteral("Tim Howes"));
addr.setOrganization(QStringLiteral("Netscape Communications Corp."));
addr.insertEmail(QStringLiteral("howes@netscape.com"));
addr.addEmail(Email(QStringLiteral("howes@netscape.com")));
addr.insertPhoneNumber(PhoneNumber(QStringLiteral("+1-415-937-3419"), //
PhoneNumber::Voice | PhoneNumber::Msg | PhoneNumber::Work));
addr.insertPhoneNumber(PhoneNumber(QStringLiteral("+1-415-528-4164"), //
......@@ -62,7 +65,7 @@ Addressee vcard3()
addr.setName(QStringLiteral("ian geiser"));
addr.setOrganization(QStringLiteral("Source eXtreme"));
addr.insertEmail(QStringLiteral("geiseri@yahoo.com"));
addr.addEmail(QStringLiteral("geiseri@yahoo.com"));
addr.setTitle(QStringLiteral("VP of Engineering"));
return addr;
}
......
......@@ -54,8 +54,13 @@ int main(int argc, char **argv)
addressee.setUrl(url);
addressee.setSecrecy(KContacts::Secrecy(KContacts::Secrecy::Confidential));
addressee.insertEmail(QLatin1String("tokoe@kde.org"), true);
addressee.insertEmail(QLatin1String("tokoe82@yahoo.de"), true);
KContacts::Email kdemail(QLatin1String("tokoe@kde.org"));
kdemail.setPreferred(true);
addressee.addEmail(kdemail);
KContacts::Email yahoomail(QLatin1String("tokoe82@yahoo.de"));
yahoomail.setPreferred(true);
addressee.addEmail(yahoomail);
KContacts::PhoneNumber phone1(QLatin1String("3541523475"), KContacts::PhoneNumber::Pref | KContacts::PhoneNumber::Home);
KContacts::PhoneNumber phone2(QLatin1String("+46745673475"), KContacts::PhoneNumber::Work);
......
Supports Markdown
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