File indexing completed on 2024-04-28 03:56:50

0001 // SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 #ifndef KRUNNER_ACTION_H
0004 #define KRUNNER_ACTION_H
0005 
0006 #include "krunner_export.h"
0007 
0008 #include <QMetaType>
0009 #include <QString>
0010 #include <memory>
0011 
0012 namespace KRunner
0013 {
0014 class ActionPrivate;
0015 /**
0016  * This class represents an action that will be shown next to a match.
0017  * The goal is to make it more reliable, because QIcon::fromTheme which is often needed in a QAction constructor is not thread safe.
0018  * Also, it makes the API more consistent with the org.kde.krunner1 DBus interface and forces consumers to set an icon.
0019  *
0020  * @since 6.0
0021  */
0022 class KRUNNER_EXPORT Action final
0023 {
0024     Q_GADGET
0025     /// User-visible text
0026     Q_PROPERTY(QString text READ text CONSTANT)
0027     /// Source for the icon: Name of the icon from a theme, file path or file URL
0028     Q_PROPERTY(QString iconSource READ iconSource CONSTANT)
0029 public:
0030     /**
0031      * Constructs a new action
0032      * @param id ID which identifies the action uniquely within the context of the respective runner plugin
0033      * @param iconSource name for the icon, that can be passed in to QIcon::fromTheme or file path/URL
0034      */
0035     explicit Action(const QString &id, const QString &iconSource, const QString &text);
0036 
0037     /// Empty constructor creating invalid action
0038     Action();
0039 
0040     ~Action();
0041 
0042     /**
0043      * Copy constructor
0044      * @internal
0045      */
0046     Action(const KRunner::Action &other);
0047 
0048     Action &operator=(const Action &other);
0049 
0050     /// Check if the action is valid
0051     operator bool() const
0052     {
0053         return !id().isEmpty();
0054     }
0055 
0056     bool operator==(const KRunner::Action &other) const
0057     {
0058         return id() == other.id();
0059     }
0060 
0061     QString id() const;
0062     QString text() const;
0063     QString iconSource() const;
0064 
0065 private:
0066     std::unique_ptr<ActionPrivate> d;
0067 };
0068 
0069 using Actions = QList<KRunner::Action>;
0070 }
0071 
0072 Q_DECLARE_METATYPE(KRunner::Action)
0073 #endif