| 1 | /* |
| 2 | This file is part of the KDE project |
| 3 | SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #ifndef KSERVICEACTION_H |
| 9 | #define KSERVICEACTION_H |
| 10 | |
| 11 | #include <QSharedDataPointer> |
| 12 | #include <QVariant> |
| 13 | #include <kservice_export.h> |
| 14 | #include <kserviceconversioncheck_p.h> |
| 15 | class QVariant; |
| 16 | class KServiceActionPrivate; |
| 17 | class KService; |
| 18 | |
| 19 | // we can't include kservice.h, it includes this header... |
| 20 | typedef QExplicitlySharedDataPointer<KService> KServicePtr; |
| 21 | |
| 22 | /*! |
| 23 | * \class KServiceAction |
| 24 | * \inmodule KService |
| 25 | * |
| 26 | * \brief Represents an action in a .desktop file. |
| 27 | * |
| 28 | * Actions are defined with the config key Actions in the [Desktop Entry] |
| 29 | * group, followed by one group per action, as per the desktop entry standard. |
| 30 | * \sa KService::actions |
| 31 | */ |
| 32 | class KSERVICE_EXPORT KServiceAction |
| 33 | { |
| 34 | public: |
| 35 | /*! |
| 36 | * Creates a KServiceAction. |
| 37 | * |
| 38 | * Normally you don't have to do this, KService creates the actions |
| 39 | * when parsing the .desktop file. |
| 40 | * |
| 41 | * \since 5.69 |
| 42 | */ |
| 43 | KServiceAction(const QString &name, const QString &text, const QString &icon, const QString &exec, bool noDisplay, const KServicePtr &service); |
| 44 | /*! |
| 45 | * \internal |
| 46 | * Needed for operator>> |
| 47 | */ |
| 48 | KServiceAction(); |
| 49 | |
| 50 | ~KServiceAction(); |
| 51 | |
| 52 | KServiceAction(const KServiceAction &other); |
| 53 | |
| 54 | KServiceAction &operator=(const KServiceAction &other); |
| 55 | |
| 56 | /*! |
| 57 | * Sets the action's internal data to the given \a userData. |
| 58 | */ |
| 59 | void setData(const QVariant &userData); |
| 60 | |
| 61 | /*! |
| 62 | * Returns the action's internal data. |
| 63 | */ |
| 64 | QVariant data() const; |
| 65 | |
| 66 | /*! |
| 67 | * Returns the action's internal name |
| 68 | * |
| 69 | * For instance Actions=Setup;... and the group [Desktop Action Setup] |
| 70 | * define an action with the name "Setup". |
| 71 | */ |
| 72 | QString name() const; |
| 73 | |
| 74 | /*! |
| 75 | * Returns the action's text, as defined by the Name key in the desktop action group |
| 76 | */ |
| 77 | QString text() const; |
| 78 | |
| 79 | /*! |
| 80 | * Returns the action's icon, as defined by the Icon key in the desktop action group |
| 81 | */ |
| 82 | QString icon() const; |
| 83 | |
| 84 | /*! |
| 85 | * Returns the action's exec command, as defined by the Exec key in the desktop action group |
| 86 | */ |
| 87 | QString exec() const; |
| 88 | |
| 89 | /*! |
| 90 | * Returns whether the action should be suppressed in menus. |
| 91 | * |
| 92 | * This is useful for having actions with a known name that the code |
| 93 | * looks for explicitly, like Setup and Root for kscreensaver actions, |
| 94 | * and which should not appear in popup menus. |
| 95 | */ |
| 96 | bool noDisplay() const; |
| 97 | |
| 98 | /*! |
| 99 | * Returns whether the action is a separator. |
| 100 | * |
| 101 | * This is true when the Actions key contains "_SEPARATOR_". |
| 102 | */ |
| 103 | bool isSeparator() const; |
| 104 | |
| 105 | /*! |
| 106 | * Returns the service that this action comes from |
| 107 | * \since 5.69 |
| 108 | */ |
| 109 | KServicePtr service() const; |
| 110 | |
| 111 | /*! |
| 112 | * Returns the requested property. |
| 113 | * |
| 114 | * T the type of the requested property |
| 115 | * |
| 116 | * \a name the name of the requested property |
| 117 | * |
| 118 | * \since 6.0 |
| 119 | */ |
| 120 | template<typename T> |
| 121 | T property(const QString &name) const |
| 122 | { |
| 123 | KServiceConversionCheck::to_QVariant<T>(); |
| 124 | return property(name: name, type: static_cast<QMetaType::Type>(qMetaTypeId<T>())).value<T>(); |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | QSharedDataPointer<KServiceActionPrivate> d; |
| 129 | friend KSERVICE_EXPORT QDataStream &operator>>(QDataStream &str, KServiceAction &act); |
| 130 | friend KSERVICE_EXPORT QDataStream &operator<<(QDataStream &str, const KServiceAction &act); |
| 131 | friend class KService; |
| 132 | KSERVICE_NO_EXPORT void setService(const KServicePtr &service); |
| 133 | |
| 134 | QVariant property(const QString &_name, QMetaType::Type type) const; |
| 135 | }; |
| 136 | |
| 137 | KSERVICE_EXPORT QDataStream &operator>>(QDataStream &str, KServiceAction &act); |
| 138 | KSERVICE_EXPORT QDataStream &operator<<(QDataStream &str, const KServiceAction &act); |
| 139 | |
| 140 | #endif /* KSERVICEACTION_H */ |
| 141 | |