File indexing completed on 2024-06-16 06:57:50

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 #ifndef KSERVICEACTION_H
0009 #define KSERVICEACTION_H
0010 
0011 #include <QSharedDataPointer>
0012 #include <QVariant>
0013 #include <kservice_export.h>
0014 #include <kserviceconversioncheck_p.h>
0015 class QVariant;
0016 class KServiceActionPrivate;
0017 class KService;
0018 
0019 // we can't include kservice.h, it includes this header...
0020 typedef QExplicitlySharedDataPointer<KService> KServicePtr;
0021 
0022 /**
0023  * @class KServiceAction kserviceaction.h <KServiceAction>
0024  *
0025  * Represents an action in a .desktop file
0026  * Actions are defined with the config key Actions in the [Desktop Entry]
0027  * group, followed by one group per action, as per the desktop entry standard.
0028  * @see KService::actions
0029  */
0030 class KSERVICE_EXPORT KServiceAction
0031 {
0032 public:
0033     /**
0034      * Creates a KServiceAction.
0035      * Normally you don't have to do this, KService creates the actions
0036      * when parsing the .desktop file.
0037      * @since 5.69
0038      */
0039     KServiceAction(const QString &name, const QString &text, const QString &icon, const QString &exec, bool noDisplay, const KServicePtr &service);
0040     /**
0041      * @internal
0042      * Needed for operator>>
0043      */
0044     KServiceAction();
0045     /**
0046      * Destroys a KServiceAction.
0047      */
0048     ~KServiceAction();
0049 
0050     /**
0051      * Copy constructor
0052      */
0053     KServiceAction(const KServiceAction &other);
0054     /**
0055      * Assignment operator
0056      */
0057     KServiceAction &operator=(const KServiceAction &other);
0058 
0059     /**
0060      * Sets the action's internal data to the given @p userData.
0061      */
0062     void setData(const QVariant &userData);
0063     /**
0064      * @return the action's internal data.
0065      */
0066     QVariant data() const;
0067 
0068     /**
0069      * @return the action's internal name
0070      * For instance Actions=Setup;... and the group [Desktop Action Setup]
0071      * define an action with the name "Setup".
0072      */
0073     QString name() const;
0074 
0075     /**
0076      * @return the action's text, as defined by the Name key in the desktop action group
0077      */
0078     QString text() const;
0079 
0080     /**
0081      * @return the action's icon, as defined by the Icon key in the desktop action group
0082      */
0083     QString icon() const;
0084 
0085     /**
0086      * @return the action's exec command, as defined by the Exec key in the desktop action group
0087      */
0088     QString exec() const;
0089 
0090     /**
0091      * Returns whether the action should be suppressed in menus.
0092      * This is useful for having actions with a known name that the code
0093      * looks for explicitly, like Setup and Root for kscreensaver actions,
0094      * and which should not appear in popup menus.
0095      * @return true to suppress this service
0096      */
0097     bool noDisplay() const;
0098 
0099     /**
0100      * Returns whether the action is a separator.
0101      * This is true when the Actions key contains "_SEPARATOR_".
0102      */
0103     bool isSeparator() const;
0104 
0105     /**
0106      * Returns the service that this action comes from
0107      * @since 5.69
0108      */
0109     KServicePtr service() const;
0110 
0111     /**
0112      * Returns the requested property.
0113      *
0114      * @tparam T the type of the requested property
0115      * @param name the name of the requested property
0116      * @return the property
0117      * @since 6.0
0118      */
0119     template<typename T>
0120     T property(const QString &name) const
0121     {
0122         KServiceConversionCheck::to_QVariant<T>();
0123         return property(name, static_cast<QMetaType::Type>(qMetaTypeId<T>())).value<T>();
0124     }
0125 
0126 private:
0127     QSharedDataPointer<KServiceActionPrivate> d;
0128     friend KSERVICE_EXPORT QDataStream &operator>>(QDataStream &str, KServiceAction &act);
0129     friend KSERVICE_EXPORT QDataStream &operator<<(QDataStream &str, const KServiceAction &act);
0130     friend class KService;
0131     KSERVICE_NO_EXPORT void setService(const KServicePtr &service);
0132 
0133     QVariant property(const QString &_name, QMetaType::Type type) const;
0134 };
0135 
0136 KSERVICE_EXPORT QDataStream &operator>>(QDataStream &str, KServiceAction &act);
0137 KSERVICE_EXPORT QDataStream &operator<<(QDataStream &str, const KServiceAction &act);
0138 
0139 #endif /* KSERVICEACTION_H */