File indexing completed on 2024-04-14 14:19:02

0001 /*
0002     This file is part of libkabc.
0003     SPDX-FileCopyrightText: 2015-2019 Laurent Montel <montel@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "impp.h"
0009 #include "kcontacts_debug.h"
0010 #include "parametermap_p.h"
0011 
0012 #include <KDesktopFile>
0013 
0014 #include <QDataStream>
0015 #include <QDirIterator>
0016 #include <QStandardPaths>
0017 #include <QStringList>
0018 #include <QUrl>
0019 
0020 using namespace KContacts;
0021 
0022 class Q_DECL_HIDDEN Impp::Private : public QSharedData
0023 {
0024 public:
0025     Private() = default;
0026     Private(const Private &other)
0027         : QSharedData(other)
0028     {
0029         mParamMap = other.mParamMap;
0030     }
0031 
0032     ParameterMap mParamMap;
0033     QUrl address;
0034 };
0035 
0036 Impp::Impp()
0037     : d(new Private)
0038 {
0039 }
0040 
0041 Impp::Impp(const Impp &other)
0042     : d(other.d)
0043 {
0044 }
0045 
0046 Impp::Impp(const QUrl &address)
0047     : d(new Private)
0048 {
0049     d->address = address;
0050 }
0051 
0052 Impp::~Impp()
0053 {
0054 }
0055 
0056 bool Impp::isValid() const
0057 {
0058     return !d->address.isEmpty() && !d->address.scheme().isEmpty();
0059 }
0060 
0061 void Impp::setAddress(const QUrl &address)
0062 {
0063     d->address = address;
0064 }
0065 
0066 QUrl Impp::address() const
0067 {
0068     return d->address;
0069 }
0070 
0071 QString Impp::serviceType() const
0072 {
0073     return d->address.scheme();
0074 }
0075 
0076 QString Impp::serviceLabel() const
0077 {
0078     return serviceLabel(serviceType());
0079 }
0080 
0081 QString Impp::serviceIcon() const
0082 {
0083     return serviceIcon(serviceType());
0084 }
0085 
0086 bool Impp::isPreferred() const
0087 {
0088     const auto it = d->mParamMap.findParam(QLatin1String("pref"));
0089     if (it != d->mParamMap.cend()) {
0090         return !it->paramValues.isEmpty() && it->paramValues.at(0) == QLatin1Char('1');
0091     }
0092     return false;
0093 }
0094 
0095 void Impp::setPreferred(bool preferred)
0096 {
0097     if (!preferred) {
0098         auto paramIt = d->mParamMap.findParam(QStringLiteral("pref"));
0099         if (paramIt != d->mParamMap.end()) {
0100             d->mParamMap.erase(paramIt);
0101         }
0102     } else {
0103         auto paramIt = d->mParamMap.findParam(QStringLiteral("pref"));
0104         if (paramIt != d->mParamMap.end()) {
0105             paramIt->paramValues = QStringList{QStringLiteral("1")};
0106         } else {
0107             d->mParamMap.insertParam({QStringLiteral("pref"), {QStringLiteral("1")}});
0108         }
0109     }
0110 }
0111 
0112 #if KCONTACTS_BUILD_DEPRECATED_SINCE(5, 88)
0113 void Impp::setParameters(const QMap<QString, QStringList> &params)
0114 {
0115     d->mParamMap = ParameterMap::fromQMap(params);
0116 }
0117 #endif
0118 
0119 #if KCONTACTS_BUILD_DEPRECATED_SINCE(5, 88)
0120 QMap<QString, QStringList> Impp::parameters() const
0121 {
0122     return d->mParamMap.toQMap();
0123 }
0124 #endif
0125 
0126 void Impp::setParams(const ParameterMap &params)
0127 {
0128     d->mParamMap = params;
0129 }
0130 
0131 ParameterMap Impp::params() const
0132 {
0133     return d->mParamMap;
0134 }
0135 
0136 bool Impp::operator==(const Impp &other) const
0137 {
0138     return (d->mParamMap == other.d->mParamMap) && (d->address == other.address());
0139 }
0140 
0141 bool Impp::operator!=(const Impp &other) const
0142 {
0143     return !(other == *this);
0144 }
0145 
0146 Impp &Impp::operator=(const Impp &other)
0147 {
0148     if (this != &other) {
0149         d = other.d;
0150     }
0151 
0152     return *this;
0153 }
0154 
0155 QString Impp::toString() const
0156 {
0157     QString str = QLatin1String("Impp {\n");
0158     str += QStringLiteral("    type: %1\n").arg(serviceType());
0159     str += QStringLiteral("    address: %1\n").arg(d->address.url());
0160     str += d->mParamMap.toString();
0161     str += QLatin1String("}\n");
0162     return str;
0163 }
0164 
0165 QDataStream &KContacts::operator<<(QDataStream &s, const Impp &impp)
0166 {
0167     return s << impp.d->mParamMap << impp.d->address << (uint32_t)(0);
0168 }
0169 
0170 QDataStream &KContacts::operator>>(QDataStream &s, Impp &impp)
0171 {
0172     int i;
0173     s >> impp.d->mParamMap >> impp.d->address >> i;
0174     return s;
0175 }
0176 
0177 static QString improtcolFile(const QString &serviceType)
0178 {
0179     const auto path =
0180         QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kf5/kcontacts/improtocols/") + serviceType + QStringLiteral(".desktop"));
0181     if (!path.isEmpty()) {
0182         return path;
0183     }
0184     return QStringLiteral(":/org.kde.kcontacts/improtocols/") + serviceType + QStringLiteral(".desktop");
0185 }
0186 
0187 QString Impp::serviceLabel(const QString &serviceType)
0188 {
0189     const auto path = improtcolFile(serviceType);
0190     KDesktopFile df(path);
0191     return df.readName();
0192 }
0193 
0194 QString Impp::serviceIcon(const QString &serviceType)
0195 {
0196     const auto path = improtcolFile(serviceType);
0197     KDesktopFile df(path);
0198     return df.readIcon();
0199 }
0200 
0201 QVector<QString> Impp::serviceTypes()
0202 {
0203     QVector<QString> types;
0204     auto paths = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kf5/kcontacts/improtocols"), QStandardPaths::LocateDirectory);
0205     paths.push_back(QStringLiteral(":/org.kde.kcontacts/improtocols/"));
0206     for (const auto &path : paths) {
0207         QDirIterator it(path, QDir::Files);
0208         while (it.hasNext()) {
0209             it.next();
0210             const auto fi = it.fileInfo();
0211             if (fi.suffix() == QLatin1String("desktop")) {
0212                 types.push_back(fi.baseName());
0213             }
0214         }
0215     }
0216 
0217     std::sort(types.begin(), types.end());
0218     types.erase(std::unique(types.begin(), types.end()), types.end());
0219     return types;
0220 }
0221 
0222 #include "moc_impp.cpp"