File indexing completed on 2024-12-01 03:42:10

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 "ksycoca.h"
0011 #include "ksycoca_p.h"
0012 
0013 #include <QDataStream>
0014 #include <QVariant>
0015 
0016 #include <KConfigGroup>
0017 
0018 class KServiceActionPrivate : public QSharedData
0019 {
0020 public:
0021     KServiceActionPrivate(const QString &name, const QString &text, const QString &icon, const QString &exec, bool noDisplay)
0022         : m_name(name)
0023         , m_text(text)
0024         , m_icon(icon)
0025         , m_exec(exec)
0026         , m_noDisplay(noDisplay)
0027     {
0028     }
0029     QString m_name;
0030     QString m_text;
0031     QString m_icon;
0032     QString m_exec;
0033     QVariant m_data;
0034     bool m_noDisplay;
0035     KServicePtr m_service;
0036     // warning keep QDataStream operators in sync if adding data here
0037 };
0038 
0039 KServiceAction::KServiceAction()
0040     : d(new KServiceActionPrivate(QString(), QString(), QString(), QString(), false))
0041 {
0042 }
0043 
0044 KServiceAction::KServiceAction(const QString &name, const QString &text, const QString &icon, const QString &exec, bool noDisplay, const KServicePtr &service)
0045     : d(new KServiceActionPrivate(name, text, icon, exec, noDisplay))
0046 {
0047     d->m_service = service;
0048 }
0049 
0050 KServiceAction::~KServiceAction()
0051 {
0052 }
0053 
0054 KServiceAction::KServiceAction(const KServiceAction &other)
0055     : d(other.d)
0056 {
0057 }
0058 
0059 KServiceAction &KServiceAction::operator=(const KServiceAction &other)
0060 {
0061     d = other.d;
0062     return *this;
0063 }
0064 
0065 QVariant KServiceAction::data() const
0066 {
0067     return d->m_data;
0068 }
0069 
0070 void KServiceAction::setData(const QVariant &data)
0071 {
0072     d->m_data = data;
0073 }
0074 
0075 QString KServiceAction::name() const
0076 {
0077     return d->m_name;
0078 }
0079 
0080 QString KServiceAction::text() const
0081 {
0082     return d->m_text;
0083 }
0084 
0085 QString KServiceAction::icon() const
0086 {
0087     return d->m_icon;
0088 }
0089 
0090 QString KServiceAction::exec() const
0091 {
0092     return d->m_exec;
0093 }
0094 
0095 bool KServiceAction::noDisplay() const
0096 {
0097     return d->m_noDisplay;
0098 }
0099 
0100 bool KServiceAction::isSeparator() const
0101 {
0102     return d->m_name == QLatin1String("_SEPARATOR_");
0103 }
0104 
0105 KServicePtr KServiceAction::service() const
0106 {
0107     return d->m_service;
0108 }
0109 
0110 void KServiceAction::setService(const KServicePtr &service)
0111 {
0112     d->m_service = service;
0113 }
0114 
0115 QDataStream &operator>>(QDataStream &str, KServiceAction &act)
0116 {
0117     KServiceActionPrivate *d = act.d;
0118     str >> d->m_name;
0119     str >> d->m_text;
0120     str >> d->m_icon;
0121     str >> d->m_exec;
0122     str >> d->m_data;
0123     str >> d->m_noDisplay;
0124     return str;
0125 }
0126 
0127 QDataStream &operator<<(QDataStream &str, const KServiceAction &act)
0128 {
0129     const KServiceActionPrivate *d = act.d;
0130     str << d->m_name;
0131     str << d->m_text;
0132     str << d->m_icon;
0133     str << d->m_exec;
0134     str << d->m_data;
0135     str << d->m_noDisplay;
0136     return str;
0137 }
0138 
0139 QVariant KServiceAction::property(const QString &_name, QMetaType::Type type) const
0140 {
0141     const auto dataMap = d->m_data.toMap();
0142     auto it = dataMap.constFind(_name);
0143     if (it == dataMap.cend() || !it.value().isValid()) {
0144         return QVariant(); // No property set.
0145     }
0146 
0147     if (type == QMetaType::QString) {
0148         return it.value(); // no conversion necessary
0149     } else {
0150         // All others
0151         // For instance properties defined as StringList, like MimeTypes.
0152         // XXX This API is accessible only through a friend declaration.
0153         return KConfigGroup::convertToQVariant(_name.toUtf8().constData(), it.value().toString().toUtf8(), QVariant(QMetaType(type)));
0154     }
0155 }