Warning, file /frameworks/kcontacts/src/email.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
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 #if KCONTACTS_BUILD_DEPRECATED_SINCE(5, 88) 0084 void Email::setParameters(const QMap<QString, QStringList> ¶ms) 0085 { 0086 d->mParamMap = ParameterMap::fromQMap(params); 0087 } 0088 #endif 0089 0090 #if KCONTACTS_BUILD_DEPRECATED_SINCE(5, 88) 0091 QMap<QString, QStringList> Email::parameters() const 0092 { 0093 return d->mParamMap.toQMap(); 0094 } 0095 #endif 0096 0097 void Email::setParams(const ParameterMap ¶ms) 0098 { 0099 d->mParamMap = params; 0100 } 0101 0102 ParameterMap Email::params() const 0103 { 0104 return d->mParamMap; 0105 } 0106 0107 void Email::setEmail(const QString &mail) 0108 { 0109 d->mail = mail; 0110 } 0111 0112 QString Email::mail() const 0113 { 0114 return d->mail; 0115 } 0116 0117 bool Email::isValid() const 0118 { 0119 return !d->mail.isEmpty(); 0120 } 0121 0122 struct email_type_name { 0123 const char *name; 0124 Email::Type type; 0125 }; 0126 0127 static const email_type_name email_type_names[] = { 0128 {"HOME", Email::Home}, 0129 {"WORK", Email::Work}, 0130 {"OTHER", Email::Other}, 0131 }; 0132 0133 Email::Type KContacts::Email::type() const 0134 { 0135 const auto it = d->mParamMap.findParam(QLatin1String("type")); 0136 if (it == d->mParamMap.end()) { 0137 return Unknown; 0138 } 0139 0140 Type type = Unknown; 0141 for (const auto &s : it->paramValues) { 0142 const auto it = std::find_if(std::begin(email_type_names), std::end(email_type_names), [&s](const email_type_name &t) { 0143 return QLatin1String(t.name) == s; 0144 }); 0145 if (it != std::end(email_type_names)) { 0146 type |= (*it).type; 0147 } 0148 } 0149 return type; 0150 } 0151 0152 void Email::setType(Type type) 0153 { 0154 const auto oldType = this->type(); 0155 0156 const QString paramName(QStringLiteral("type")); 0157 0158 ParameterMap::iterator theIt; 0159 0160 auto it = d->mParamMap.findParam(paramName); 0161 if (it != d->mParamMap.end()) { 0162 theIt = it; 0163 } else { 0164 theIt = d->mParamMap.insertParam({paramName, {}}); 0165 } 0166 0167 for (const auto &t : email_type_names) { 0168 if (((type ^ oldType) & t.type) == 0) { 0169 continue; // no change 0170 } 0171 0172 if (type & t.type) { 0173 theIt->paramValues.push_back(QLatin1String(t.name)); 0174 } else { 0175 theIt->paramValues.removeAll(QLatin1String(t.name)); 0176 } 0177 } 0178 } 0179 0180 bool Email::isPreferred() const 0181 { 0182 auto it = d->mParamMap.findParam(QLatin1String("pref")); 0183 if (it != d->mParamMap.end()) { 0184 return !it->paramValues.isEmpty() && it->paramValues.at(0) == QLatin1Char('1'); 0185 } 0186 0187 it = d->mParamMap.findParam(QLatin1String("type")); 0188 if (it != d->mParamMap.end()) { 0189 return it->paramValues.contains(QLatin1String("PREF"), Qt::CaseInsensitive); 0190 } 0191 0192 return false; 0193 } 0194 0195 void Email::setPreferred(bool preferred) 0196 { 0197 if (preferred == isPreferred()) { 0198 return; 0199 } 0200 0201 const QString paramName(QStringLiteral("type")); 0202 0203 auto typeIt = d->mParamMap.findParam(paramName); 0204 QStringList types = typeIt != d->mParamMap.end() ? typeIt->paramValues : QStringList{}; 0205 0206 if (!preferred) { 0207 auto prefIt = d->mParamMap.findParam(QLatin1String("pref")); 0208 if (prefIt != d->mParamMap.end()) { 0209 d->mParamMap.erase(prefIt); 0210 } 0211 0212 types.removeAll(QLatin1String("PREF")); 0213 } else { 0214 types.push_back(QStringLiteral("PREF")); 0215 } 0216 0217 typeIt = d->mParamMap.findParam(paramName); 0218 if (typeIt != d->mParamMap.end()) { 0219 typeIt->paramValues = types; 0220 } else { 0221 d->mParamMap.insertParam({paramName, types}); 0222 } 0223 } 0224 0225 QDataStream &KContacts::operator<<(QDataStream &s, const Email &email) 0226 { 0227 return s << email.d->mParamMap << email.d->mail; 0228 } 0229 0230 QDataStream &KContacts::operator>>(QDataStream &s, Email &email) 0231 { 0232 s >> email.d->mParamMap >> email.d->mail; 0233 return s; 0234 } 0235 0236 #include "moc_email.cpp"