File indexing completed on 2024-04-14 03:51:25

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2015-2019 Laurent Montel <montel@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "email.h"
0009 #include "parametermap_p.h"
0010 
0011 #include <QDataStream>
0012 #include <QStringList>
0013 
0014 using namespace KContacts;
0015 
0016 class Q_DECL_HIDDEN Email::Private : public QSharedData
0017 {
0018 public:
0019     Private()
0020     {
0021     }
0022 
0023     Private(const Private &other)
0024         : QSharedData(other)
0025     {
0026         mParamMap = other.mParamMap;
0027         mail = other.mail;
0028     }
0029 
0030     ParameterMap mParamMap;
0031 
0032     QString mail;
0033 };
0034 
0035 Email::Email()
0036     : d(new Private)
0037 {
0038 }
0039 
0040 Email::Email(const QString &mail)
0041     : d(new Private)
0042 {
0043     d->mail = mail;
0044 }
0045 
0046 Email::Email(const Email &other)
0047     : d(other.d)
0048 {
0049 }
0050 
0051 Email::~Email()
0052 {
0053 }
0054 
0055 bool Email::operator==(const Email &other) const
0056 {
0057     return (d->mParamMap == other.d->mParamMap) && (d->mail == other.mail());
0058 }
0059 
0060 bool Email::operator!=(const Email &other) const
0061 {
0062     return !(other == *this);
0063 }
0064 
0065 Email &Email::operator=(const Email &other)
0066 {
0067     if (this != &other) {
0068         d = other.d;
0069     }
0070 
0071     return *this;
0072 }
0073 
0074 QString Email::toString() const
0075 {
0076     QString str = QLatin1String("Email {\n");
0077     str += QStringLiteral("    mail: %1\n").arg(d->mail);
0078     str += d->mParamMap.toString();
0079     str += QLatin1String("}\n");
0080     return str;
0081 }
0082 
0083 void Email::setParams(const ParameterMap &params)
0084 {
0085     d->mParamMap = params;
0086 }
0087 
0088 ParameterMap Email::params() const
0089 {
0090     return d->mParamMap;
0091 }
0092 
0093 void Email::setEmail(const QString &mail)
0094 {
0095     d->mail = mail;
0096 }
0097 
0098 QString Email::mail() const
0099 {
0100     return d->mail;
0101 }
0102 
0103 bool Email::isValid() const
0104 {
0105     return !d->mail.isEmpty();
0106 }
0107 
0108 struct email_type_name {
0109     const char *name;
0110     Email::Type type;
0111 };
0112 
0113 static const email_type_name email_type_names[] = {
0114     {"HOME", Email::Home},
0115     {"WORK", Email::Work},
0116     {"OTHER", Email::Other},
0117 };
0118 
0119 Email::Type KContacts::Email::type() const
0120 {
0121     const auto it = d->mParamMap.findParam(QLatin1String("type"));
0122     if (it == d->mParamMap.end()) {
0123         return Unknown;
0124     }
0125 
0126     Type type = Unknown;
0127     for (const auto &s : it->paramValues) {
0128         const auto it = std::find_if(std::begin(email_type_names), std::end(email_type_names), [&s](const email_type_name &t) {
0129             return QLatin1String(t.name) == s;
0130         });
0131         if (it != std::end(email_type_names)) {
0132             type |= (*it).type;
0133         }
0134     }
0135     return type;
0136 }
0137 
0138 void Email::setType(Type type)
0139 {
0140     const auto oldType = this->type();
0141 
0142     const QString paramName(QStringLiteral("type"));
0143 
0144     ParameterMap::iterator theIt;
0145 
0146     auto it = d->mParamMap.findParam(paramName);
0147     if (it != d->mParamMap.end()) {
0148         theIt = it;
0149     } else {
0150         theIt = d->mParamMap.insertParam({paramName, {}});
0151     }
0152 
0153     for (const auto &t : email_type_names) {
0154         if (((type ^ oldType) & t.type) == 0) {
0155             continue; // no change
0156         }
0157 
0158         if (type & t.type) {
0159             theIt->paramValues.push_back(QLatin1String(t.name));
0160         } else {
0161             theIt->paramValues.removeAll(QLatin1String(t.name));
0162         }
0163     }
0164 }
0165 
0166 bool Email::isPreferred() const
0167 {
0168     auto it = d->mParamMap.findParam(QLatin1String("pref"));
0169     if (it != d->mParamMap.end()) {
0170         return !it->paramValues.isEmpty() && it->paramValues.at(0) == QLatin1Char('1');
0171     }
0172 
0173     it = d->mParamMap.findParam(QLatin1String("type"));
0174     if (it != d->mParamMap.end()) {
0175         return it->paramValues.contains(QLatin1String("PREF"), Qt::CaseInsensitive);
0176     }
0177 
0178     return false;
0179 }
0180 
0181 void Email::setPreferred(bool preferred)
0182 {
0183     if (preferred == isPreferred()) {
0184         return;
0185     }
0186 
0187     const QString paramName(QStringLiteral("type"));
0188 
0189     auto typeIt = d->mParamMap.findParam(paramName);
0190     QStringList types = typeIt != d->mParamMap.end() ? typeIt->paramValues : QStringList{};
0191 
0192     if (!preferred) {
0193         auto prefIt = d->mParamMap.findParam(QLatin1String("pref"));
0194         if (prefIt != d->mParamMap.end()) {
0195             d->mParamMap.erase(prefIt);
0196         }
0197 
0198         types.removeAll(QLatin1String("PREF"));
0199     } else {
0200         types.push_back(QStringLiteral("PREF"));
0201     }
0202 
0203     typeIt = d->mParamMap.findParam(paramName);
0204     if (typeIt != d->mParamMap.end()) {
0205         typeIt->paramValues = types;
0206     } else {
0207         d->mParamMap.insertParam({paramName, types});
0208     }
0209 }
0210 
0211 QDataStream &KContacts::operator<<(QDataStream &s, const Email &email)
0212 {
0213     return s << email.d->mParamMap << email.d->mail;
0214 }
0215 
0216 QDataStream &KContacts::operator>>(QDataStream &s, Email &email)
0217 {
0218     s >> email.d->mParamMap >> email.d->mail;
0219     return s;
0220 }
0221 
0222 #include "moc_email.cpp"