File indexing completed on 2024-11-03 12:41:43
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "kserviceaction.h" 0009 #include "kbuildservicetypefactory_p.h" 0010 #include "kservice.h" 0011 #include "ksycoca.h" 0012 #include "ksycoca_p.h" 0013 0014 #include <QDataStream> 0015 #include <QVariant> 0016 0017 #include <KConfigGroup> 0018 0019 class KServiceActionPrivate : public QSharedData 0020 { 0021 public: 0022 KServiceActionPrivate(const QString &name, const QString &text, const QString &icon, const QString &exec, bool noDisplay) 0023 : m_name(name) 0024 , m_text(text) 0025 , m_icon(icon) 0026 , m_exec(exec) 0027 , m_noDisplay(noDisplay) 0028 { 0029 } 0030 QString m_name; 0031 QString m_text; 0032 QString m_icon; 0033 QString m_exec; 0034 QVariant m_data; 0035 bool m_noDisplay; 0036 KServicePtr m_service; 0037 // warning keep QDataStream operators in sync if adding data here 0038 }; 0039 0040 KServiceAction::KServiceAction() 0041 : d(new KServiceActionPrivate(QString(), QString(), QString(), QString(), false)) 0042 { 0043 } 0044 0045 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 69) 0046 KServiceAction::KServiceAction(const QString &name, const QString &text, const QString &icon, const QString &exec, bool noDisplay) 0047 : d(new KServiceActionPrivate(name, text, icon, exec, noDisplay)) 0048 { 0049 } 0050 #endif 0051 0052 KServiceAction::KServiceAction(const QString &name, const QString &text, const QString &icon, const QString &exec, bool noDisplay, const KServicePtr &service) 0053 : d(new KServiceActionPrivate(name, text, icon, exec, noDisplay)) 0054 { 0055 d->m_service = service; 0056 } 0057 0058 KServiceAction::~KServiceAction() 0059 { 0060 } 0061 0062 KServiceAction::KServiceAction(const KServiceAction &other) 0063 : d(other.d) 0064 { 0065 } 0066 0067 KServiceAction &KServiceAction::operator=(const KServiceAction &other) 0068 { 0069 d = other.d; 0070 return *this; 0071 } 0072 0073 QVariant KServiceAction::data() const 0074 { 0075 return d->m_data; 0076 } 0077 0078 void KServiceAction::setData(const QVariant &data) 0079 { 0080 d->m_data = data; 0081 } 0082 0083 QString KServiceAction::name() const 0084 { 0085 return d->m_name; 0086 } 0087 0088 QString KServiceAction::text() const 0089 { 0090 return d->m_text; 0091 } 0092 0093 QString KServiceAction::icon() const 0094 { 0095 return d->m_icon; 0096 } 0097 0098 QString KServiceAction::exec() const 0099 { 0100 return d->m_exec; 0101 } 0102 0103 bool KServiceAction::noDisplay() const 0104 { 0105 return d->m_noDisplay; 0106 } 0107 0108 bool KServiceAction::isSeparator() const 0109 { 0110 return d->m_name == QLatin1String("_SEPARATOR_"); 0111 } 0112 0113 KServicePtr KServiceAction::service() const 0114 { 0115 return d->m_service; 0116 } 0117 0118 void KServiceAction::setService(const KServicePtr &service) 0119 { 0120 d->m_service = service; 0121 } 0122 0123 QDataStream &operator>>(QDataStream &str, KServiceAction &act) 0124 { 0125 KServiceActionPrivate *d = act.d; 0126 str >> d->m_name; 0127 str >> d->m_text; 0128 str >> d->m_icon; 0129 str >> d->m_exec; 0130 str >> d->m_data; 0131 str >> d->m_noDisplay; 0132 return str; 0133 } 0134 0135 QDataStream &operator<<(QDataStream &str, const KServiceAction &act) 0136 { 0137 const KServiceActionPrivate *d = act.d; 0138 str << d->m_name; 0139 str << d->m_text; 0140 str << d->m_icon; 0141 str << d->m_exec; 0142 str << d->m_data; 0143 str << d->m_noDisplay; 0144 return str; 0145 } 0146 0147 QVariant KServiceAction::property(const QString &_name, QMetaType::Type type) const 0148 { 0149 if (type == QMetaType::UnknownType) { 0150 KSycoca::self()->ensureCacheValid(); 0151 type = KSycocaPrivate::self()->serviceTypeFactory()->findPropertyTypeByName(_name); 0152 if (type == QMetaType::UnknownType) { 0153 return QVariant(); // Unknown property: Invalid variant. 0154 } 0155 } 0156 0157 const auto dataMap = d->m_data.toMap(); 0158 auto it = dataMap.constFind(_name); 0159 if (it == dataMap.cend() || !it.value().isValid()) { 0160 return QVariant(); // No property set. 0161 } 0162 0163 if (type == QMetaType::QString) { 0164 return it.value(); // no conversion necessary 0165 } else { 0166 // All others 0167 // For instance properties defined as StringList, like MimeTypes. 0168 // XXX This API is accessible only through a friend declaration. 0169 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0170 return KConfigGroup::convertToQVariant(_name.toUtf8().constData(), it.value().toString().toUtf8(), QVariant(static_cast<QVariant::Type>(type))); 0171 #else 0172 return KConfigGroup::convertToQVariant(_name.toUtf8().constData(), it.value().toString().toUtf8(), QVariant(QMetaType(type))); 0173 #endif 0174 } 0175 }