File indexing completed on 2024-07-14 14:27:49

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2021 Ahmad Samir <a.samirh78@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef PARAMETERMAP_P_H
0009 #define PARAMETERMAP_P_H
0010 
0011 #include <QDataStream>
0012 #include <QMap>
0013 #include <QStringList>
0014 
0015 #include <algorithm>
0016 #include <vector>
0017 
0018 namespace KContacts
0019 {
0020 struct ParameterData {
0021     QString param;
0022     QStringList paramValues;
0023 };
0024 
0025 class ParameterMap : public std::vector<ParameterData>
0026 {
0027 public:
0028     inline const_iterator findParam(const QString &param) const
0029     {
0030         return std::find_if(cbegin(), cend(), [&param](const ParameterData &info) {
0031             return info.param == param;
0032         });
0033     }
0034 
0035     inline iterator findParam(const QString &param)
0036     {
0037         return std::find_if(begin(), end(), [&param](const ParameterData &info) {
0038             return info.param == param;
0039         });
0040     }
0041 
0042     /**
0043      * Inserts @p newdata and keeps the vector sorted, the latter needs to
0044      * be sorted for comparisons.
0045      */
0046     inline iterator insertParam(ParameterData &&newdata)
0047     {
0048         auto dIt = std::lower_bound(begin(), end(), newdata);
0049         return insert(dIt, newdata);
0050     }
0051 
0052     inline QString toString() const
0053     {
0054         if (empty()) {
0055             return {};
0056         }
0057 
0058         QString str;
0059         for (const auto &[p, list] : *this) {
0060             str += QStringLiteral("%1 %2").arg(p, list.join(QLatin1Char(',')));
0061         }
0062 
0063         return QLatin1String("    parameters: ") + str + QLatin1Char('\n');
0064     }
0065 
0066     static ParameterMap fromQMap(const QMap<QString, QStringList> &params)
0067     {
0068         ParameterMap vec;
0069         vec.reserve(params.size());
0070         for (auto it = params.begin(), endIt = params.cend(); it != endIt; ++it) {
0071             vec.push_back({it.key(), it.value()});
0072         }
0073         return vec;
0074     }
0075 
0076     inline QMap<QString, QStringList> toQMap() const
0077     {
0078         QMap<QString, QStringList> map;
0079         for (const auto &[name, list] : *this) {
0080             map.insert(name, list);
0081         }
0082         return map;
0083     }
0084 };
0085 
0086 inline bool operator<(const ParameterData &a, const ParameterData &b)
0087 {
0088     return a.param < b.param;
0089 }
0090 
0091 inline bool operator==(const ParameterData &a, const ParameterData &b)
0092 {
0093     return std::tie(a.param, a.paramValues) == std::tie(b.param, b.paramValues);
0094 }
0095 
0096 inline QDataStream &operator<<(QDataStream &s, const ParameterMap &map)
0097 {
0098     s << static_cast<quint32>(map.size());
0099     for (const auto &[p, list] : map) {
0100         s << p << list;
0101     }
0102     return s;
0103 }
0104 
0105 inline QDataStream &operator>>(QDataStream &s, ParameterMap &map)
0106 {
0107     map.clear();
0108     quint32 n;
0109     s >> n;
0110     for (quint32 i = 0; i < n; ++i) {
0111         QString p;
0112         QStringList lst;
0113         s >> p >> lst;
0114         if (s.status() != QDataStream::Ok) {
0115             map.clear();
0116             break;
0117         }
0118         map.push_back({p, lst});
0119     }
0120 
0121     return s;
0122 }
0123 }
0124 
0125 #endif