File indexing completed on 2023-11-26 10:45:34

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 "kservice.h"
0010 #include "kservice_p.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 KServiceAction::KServiceAction(const QString &name, const QString &text, const QString &icon, const QString &exec, bool noDisplay, const KServicePtr &service)
0046     : d(new KServiceActionPrivate(name, text, icon, exec, noDisplay))
0047 {
0048     d->m_service = service;
0049 }
0050 
0051 KServiceAction::~KServiceAction()
0052 {
0053 }
0054 
0055 KServiceAction::KServiceAction(const KServiceAction &other)
0056     : d(other.d)
0057 {
0058 }
0059 
0060 KServiceAction &KServiceAction::operator=(const KServiceAction &other)
0061 {
0062     d = other.d;
0063     return *this;
0064 }
0065 
0066 QVariant KServiceAction::data() const
0067 {
0068     return d->m_data;
0069 }
0070 
0071 void KServiceAction::setData(const QVariant &data)
0072 {
0073     d->m_data = data;
0074 }
0075 
0076 QString KServiceAction::name() const
0077 {
0078     return d->m_name;
0079 }
0080 
0081 QString KServiceAction::text() const
0082 {
0083     return d->m_text;
0084 }
0085 
0086 QString KServiceAction::icon() const
0087 {
0088     return d->m_icon;
0089 }
0090 
0091 QString KServiceAction::exec() const
0092 {
0093     return d->m_exec;
0094 }
0095 
0096 bool KServiceAction::noDisplay() const
0097 {
0098     return d->m_noDisplay;
0099 }
0100 
0101 bool KServiceAction::isSeparator() const
0102 {
0103     return d->m_name == QLatin1String("_SEPARATOR_");
0104 }
0105 
0106 KServicePtr KServiceAction::service() const
0107 {
0108     return d->m_service;
0109 }
0110 
0111 void KServiceAction::setService(const KServicePtr &service)
0112 {
0113     d->m_service = service;
0114 }
0115 
0116 QDataStream &operator>>(QDataStream &str, KServiceAction &act)
0117 {
0118     KServiceActionPrivate *d = act.d;
0119     str >> d->m_name;
0120     str >> d->m_text;
0121     str >> d->m_icon;
0122     str >> d->m_exec;
0123     str >> d->m_data;
0124     str >> d->m_noDisplay;
0125     return str;
0126 }
0127 
0128 QDataStream &operator<<(QDataStream &str, const KServiceAction &act)
0129 {
0130     const KServiceActionPrivate *d = act.d;
0131     str << d->m_name;
0132     str << d->m_text;
0133     str << d->m_icon;
0134     str << d->m_exec;
0135     str << d->m_data;
0136     str << d->m_noDisplay;
0137     return str;
0138 }
0139 
0140 QVariant KServiceAction::property(const QString &_name, QMetaType::Type type) const
0141 {
0142     if (type == QMetaType::UnknownType) {
0143         KSycoca::self()->ensureCacheValid();
0144         type = KServicePrivate::typeForProperty(_name);
0145         if (type == QMetaType::UnknownType) {
0146             return QVariant(); // Unknown property: Invalid variant.
0147         }
0148     }
0149 
0150     const auto dataMap = d->m_data.toMap();
0151     auto it = dataMap.constFind(_name);
0152     if (it == dataMap.cend() || !it.value().isValid()) {
0153         return QVariant(); // No property set.
0154     }
0155 
0156     if (type == QMetaType::QString) {
0157         return it.value(); // no conversion necessary
0158     } else {
0159         // All others
0160         // For instance properties defined as StringList, like MimeTypes.
0161         // XXX This API is accessible only through a friend declaration.
0162         return KConfigGroup::convertToQVariant(_name.toUtf8().constData(), it.value().toString().toUtf8(), QVariant(QMetaType(type)));
0163     }
0164 }