File indexing completed on 2024-12-01 13:34:28

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-only
0003     SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
0004  */
0005 
0006 #ifndef _ACTIONS_H_
0007 #define _ACTIONS_H_
0008 
0009 #include <QList>
0010 #include <QTimer>
0011 
0012 #include <KService>
0013 
0014 class KConfig;
0015 class KConfigGroup;
0016 
0017 #include "khotkeysglobal.h"
0018 
0019 namespace KHotKeys
0020 {
0021 class ActionData;
0022 class Windowdef_list;
0023 
0024 class ActionVisitor
0025 {
0026 public:
0027     virtual ~ActionVisitor() = 0;
0028 };
0029 
0030 // this one is a base for all "real" resulting actions, e.g. running a command,
0031 // ActionData instances usually contain at least one Action
0032 class Q_DECL_EXPORT Action
0033 {
0034     Q_DISABLE_COPY(Action)
0035 
0036 public:
0037     /**
0038      * A enum for all possible action types.
0039      *
0040      * @see type()
0041      */
0042     enum ActionType {
0043         ActivateWindowActionType = 0x01, //!< @see ActivateWindowAction
0044         CommandUrlActionType = 0x02, //!< @see CommandUrlAction
0045         DBusActionType = 0x04, //!< @see DBusAction
0046         KeyboardInputActionType = 0x08, //!< @see KeyboardInputAction
0047         MenuEntryActionType = 0x10, //!< @see MenuEntryAction
0048         ActionListType = 0x11, //!< @see ActionList
0049         AllTypes = 0xEF, //!< All types. For convenience
0050     };
0051 
0052     Q_DECLARE_FLAGS(ActionTypes, ActionType)
0053 
0054     /**
0055      * Create a action
0056      */
0057     Action(ActionData *data_P);
0058 
0059     virtual ~Action();
0060 
0061     /**
0062      * Acyclic visitor pattern
0063      */
0064     virtual void accept(ActionVisitor &) = 0;
0065 
0066     /**
0067      * Execute the action.
0068      */
0069     virtual void execute() = 0;
0070 
0071     /**
0072      * Have a look what's inside.
0073      *
0074      * @return the type of the action.
0075      */
0076     virtual ActionType type() = 0;
0077 
0078     /**
0079      * Returns a short descriptions of the action.
0080      *
0081      * The description is generated and can't be set.
0082      *
0083      * @return a description for this action
0084      */
0085     virtual const QString description() const = 0;
0086 
0087     /**
0088      * Write the actions configuration to @p cfg_P.
0089      */
0090     virtual void cfg_write(KConfigGroup &cfg_P) const;
0091 
0092     /**
0093      * Return a copy of the action.
0094      *
0095      * This is a real deep copy.
0096      */
0097     virtual Action *copy(ActionData *data_P) const = 0;
0098 
0099     /**
0100      * The action is about to be erased permanently
0101      */
0102     virtual void aboutToBeErased();
0103 
0104 protected:
0105     /**
0106      * The action data corresponding to this action.
0107      */
0108     ActionData *const data;
0109 };
0110 
0111 Q_DECLARE_OPERATORS_FOR_FLAGS(Action::ActionTypes)
0112 
0113 class Q_DECL_EXPORT ActionList : public QList<Action *>
0114 {
0115     Q_DISABLE_COPY(ActionList)
0116 
0117 public:
0118     ActionList(const QString &comment = QString());
0119 
0120     ~ActionList();
0121     void cfg_write(KConfigGroup &cfg_P) const;
0122     //! Some convenience typedef
0123     typedef QList<Action *>::Iterator Iterator;
0124     typedef QList<Action *>::ConstIterator ConstIterator;
0125     const QString &comment() const;
0126 
0127     /**
0128      * @reimp
0129      */
0130     void aboutToBeErased();
0131 
0132 private:
0133     QString _comment;
0134 };
0135 
0136 class CommandUrlAction;
0137 class CommandUrlActionVisitor
0138 {
0139 public:
0140     virtual ~CommandUrlActionVisitor();
0141     virtual void visit(CommandUrlAction &) = 0;
0142 };
0143 
0144 class Q_DECL_EXPORT CommandUrlAction : public Action
0145 {
0146     typedef Action base;
0147 
0148 public:
0149     CommandUrlAction(ActionData *data_P, const QString &command_url_P = QString());
0150     void cfg_write(KConfigGroup &cfg_P) const override;
0151     void execute() override;
0152     const QString description() const override;
0153 
0154     //! The command url to trigger
0155     void set_command_url(const QString &command_url);
0156     QString command_url() const;
0157 
0158     ActionType type() override
0159     {
0160         return CommandUrlActionType;
0161     }
0162     Action *copy(ActionData *data_P) const override;
0163 
0164     /**
0165      * Acyclic visitor pattern
0166      */
0167     void accept(ActionVisitor &) override;
0168 
0169 private:
0170     QString _command_url;
0171 };
0172 
0173 class MenuEntryAction;
0174 class MenuEntryActionVisitor
0175 {
0176 public:
0177     virtual ~MenuEntryActionVisitor();
0178     virtual void visit(MenuEntryAction &) = 0;
0179 };
0180 
0181 class Q_DECL_EXPORT MenuEntryAction : public CommandUrlAction
0182 {
0183     typedef CommandUrlAction base;
0184 
0185 public:
0186     MenuEntryAction(ActionData *data_P, const QString &menuentry_P = QString());
0187     void cfg_write(KConfigGroup &cfg_P) const override;
0188     void execute() override;
0189 
0190     // The service we trigger
0191     KService::Ptr service() const;
0192     void set_service(KService::Ptr);
0193 
0194     const QString description() const override;
0195     Action *copy(ActionData *data_P) const override;
0196     ActionType type() override
0197     {
0198         return Action::MenuEntryActionType;
0199     }
0200 
0201     /**
0202      * Acyclic visitor pattern
0203      */
0204     void accept(ActionVisitor &) override;
0205 
0206 private:
0207     KService::Ptr _service;
0208 };
0209 
0210 class DBusAction;
0211 class DBusActionVisitor
0212 {
0213 public:
0214     virtual ~DBusActionVisitor();
0215     virtual void visit(DBusAction &) = 0;
0216 };
0217 
0218 class Q_DECL_EXPORT DBusAction : public Action
0219 {
0220     typedef Action base;
0221 
0222 public:
0223     DBusAction(ActionData *data_P,
0224                const QString &app_P = QString(),
0225                const QString &obj_P = QString(),
0226                const QString &call_P = QString(),
0227                const QString &args_P = QString());
0228 
0229     void cfg_write(KConfigGroup &cfg_P) const override;
0230     void execute() override;
0231     const QString remote_application() const;
0232     const QString remote_object() const;
0233     const QString called_function() const;
0234     const QString arguments() const;
0235 
0236     void set_remote_application(const QString &application);
0237     void set_remote_object(const QString &object);
0238     void set_called_function(const QString &function);
0239     void set_arguments(const QString &args);
0240 
0241     const QString description() const override;
0242     Action *copy(ActionData *data_P) const override;
0243     ActionType type() override
0244     {
0245         return DBusActionType;
0246     }
0247 
0248     /**
0249      * Acyclic visitor pattern
0250      */
0251     void accept(ActionVisitor &) override;
0252 
0253 private:
0254     QString _application; // CHECKME QCString ?
0255     QString _object;
0256     QString _function;
0257     QString _arguments;
0258 };
0259 
0260 class KeyboardInputAction;
0261 class KeyboardInputActionVisitor
0262 {
0263 public:
0264     virtual ~KeyboardInputActionVisitor();
0265     virtual void visit(KeyboardInputAction &) = 0;
0266 };
0267 
0268 class Q_DECL_EXPORT KeyboardInputAction : public Action
0269 {
0270     typedef Action base;
0271 
0272 public:
0273     /**
0274      * Where should we send the data too
0275      */
0276     enum DestinationWindow {
0277         ActiveWindow,
0278         SpecificWindow,
0279         ActionWindow,
0280     };
0281 
0282     KeyboardInputAction(ActionData *data_P, const QString &input_P = QString(), Windowdef_list *dest_window_P = nullptr, bool active_window_P = true);
0283 
0284     ~KeyboardInputAction() override;
0285     void cfg_write(KConfigGroup &cfg_P) const override;
0286     void execute() override;
0287 
0288     const QString &input() const;
0289     void setInput(const QString &input);
0290 
0291     // send to specific window: dest_window != nullptr
0292     // send to active window: dest_window == nullptr && activeWindow() == true
0293     // send to action window: dest_window == nullptr && activeWindow() == false
0294     //
0295 
0296     DestinationWindow destination() const;
0297     void setDestination(const DestinationWindow &dest);
0298 
0299     const Windowdef_list *dest_window() const;
0300     Windowdef_list *dest_window();
0301     void setDestinationWindowRules(Windowdef_list *list);
0302 
0303     bool activeWindow() const;
0304     const QString description() const override;
0305     Action *copy(ActionData *data_P) const override;
0306     ActionType type() override
0307     {
0308         return KeyboardInputActionType;
0309     }
0310 
0311     /**
0312      * Acyclic visitor pattern
0313      */
0314     void accept(ActionVisitor &) override;
0315 
0316 private:
0317     QString _input;
0318     Windowdef_list *_dest_window;
0319 
0320     //! Which window should get the input
0321     DestinationWindow _destination;
0322 };
0323 
0324 class ActivateWindowAction;
0325 class ActivateWindowActionVisitor
0326 {
0327 public:
0328     virtual ~ActivateWindowActionVisitor();
0329     virtual void visit(ActivateWindowAction &) = 0;
0330 };
0331 
0332 class Q_DECL_EXPORT ActivateWindowAction : public Action
0333 {
0334     typedef Action base;
0335 
0336 public:
0337     ActivateWindowAction(ActionData *data_P, const Windowdef_list *window = nullptr);
0338 
0339     ~ActivateWindowAction() override;
0340     void cfg_write(KConfigGroup &cfg_P) const override;
0341     void execute() override;
0342 
0343     const Windowdef_list *window() const;
0344     void set_window_list(Windowdef_list *list);
0345 
0346     const QString description() const override;
0347     Action *copy(ActionData *data_P) const override;
0348     ActionType type() override
0349     {
0350         return ActivateWindowActionType;
0351     }
0352 
0353     /**
0354      * Acyclic visitor pattern
0355      */
0356     void accept(ActionVisitor &) override;
0357 
0358 private:
0359     const Windowdef_list *_window;
0360 };
0361 
0362 } // namespace KHotKeys
0363 
0364 #endif