File indexing completed on 2025-01-19 03:39:51
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 void Impp::setParams(const ParameterMap ¶ms) 0113 { 0114 d->mParamMap = params; 0115 } 0116 0117 ParameterMap Impp::params() const 0118 { 0119 return d->mParamMap; 0120 } 0121 0122 bool Impp::operator==(const Impp &other) const 0123 { 0124 return (d->mParamMap == other.d->mParamMap) && (d->address == other.address()); 0125 } 0126 0127 bool Impp::operator!=(const Impp &other) const 0128 { 0129 return !(other == *this); 0130 } 0131 0132 Impp &Impp::operator=(const Impp &other) 0133 { 0134 if (this != &other) { 0135 d = other.d; 0136 } 0137 0138 return *this; 0139 } 0140 0141 QString Impp::toString() const 0142 { 0143 QString str = QLatin1String("Impp {\n"); 0144 str += QStringLiteral(" type: %1\n").arg(serviceType()); 0145 str += QStringLiteral(" address: %1\n").arg(d->address.url()); 0146 str += d->mParamMap.toString(); 0147 str += QLatin1String("}\n"); 0148 return str; 0149 } 0150 0151 QDataStream &KContacts::operator<<(QDataStream &s, const Impp &impp) 0152 { 0153 return s << impp.d->mParamMap << impp.d->address << (uint32_t)(0); 0154 } 0155 0156 QDataStream &KContacts::operator>>(QDataStream &s, Impp &impp) 0157 { 0158 int i; 0159 s >> impp.d->mParamMap >> impp.d->address >> i; 0160 return s; 0161 } 0162 0163 static QString improtcolFile(const QString &serviceType) 0164 { 0165 const auto path = 0166 QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kf5/kcontacts/improtocols/") + serviceType + QStringLiteral(".desktop")); 0167 if (!path.isEmpty()) { 0168 return path; 0169 } 0170 return QStringLiteral(":/org.kde.kcontacts/improtocols/") + serviceType + QStringLiteral(".desktop"); 0171 } 0172 0173 QString Impp::serviceLabel(const QString &serviceType) 0174 { 0175 const auto path = improtcolFile(serviceType); 0176 KDesktopFile df(path); 0177 return df.readName(); 0178 } 0179 0180 QString Impp::serviceIcon(const QString &serviceType) 0181 { 0182 const auto path = improtcolFile(serviceType); 0183 KDesktopFile df(path); 0184 return df.readIcon(); 0185 } 0186 0187 QList<QString> Impp::serviceTypes() 0188 { 0189 QList<QString> types; 0190 auto paths = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kf5/kcontacts/improtocols"), QStandardPaths::LocateDirectory); 0191 paths.push_back(QStringLiteral(":/org.kde.kcontacts/improtocols/")); 0192 for (const auto &path : paths) { 0193 QDirIterator it(path, QDir::Files); 0194 while (it.hasNext()) { 0195 it.next(); 0196 const auto fi = it.fileInfo(); 0197 if (fi.suffix() == QLatin1String("desktop")) { 0198 types.push_back(fi.baseName()); 0199 } 0200 } 0201 } 0202 0203 std::sort(types.begin(), types.end()); 0204 types.erase(std::unique(types.begin(), types.end()), types.end()); 0205 return types; 0206 } 0207 0208 #include "moc_impp.cpp"