File indexing completed on 2024-05-05 17:34:12

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-only
0003     SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
0004  */
0005 
0006 #include "action_data/action_data.h"
0007 
0008 #include "action_data/action_data_visitor.h"
0009 #include "actions/actions.h"
0010 #include "triggers/triggers.h"
0011 
0012 #include <QDebug>
0013 #include <kconfiggroup.h>
0014 
0015 namespace KHotKeys
0016 {
0017 ActionData::ActionData(ActionDataGroup *parent_P,
0018                        const QString &name_P,
0019                        const QString &comment_P,
0020                        Trigger_list *triggers_P,
0021                        Condition_list *conditions_P,
0022                        ActionList *actions_P)
0023     : ActionDataBase(parent_P, name_P, comment_P, conditions_P)
0024     , _triggers(triggers_P)
0025     , _actions(actions_P)
0026 {
0027     if (!_triggers)
0028         _triggers = new Trigger_list;
0029 
0030     if (!_actions)
0031         _actions = new ActionList;
0032 }
0033 
0034 void ActionData::accept(ActionDataConstVisitor *visitor) const
0035 {
0036     visitor->visitActionData(this);
0037 }
0038 
0039 ActionData::~ActionData()
0040 {
0041     delete _triggers;
0042     _triggers = nullptr;
0043     delete _actions;
0044     _actions = nullptr;
0045 }
0046 
0047 void ActionData::accept(ActionDataVisitor *visitor)
0048 {
0049     visitor->visitActionData(this);
0050 }
0051 
0052 void ActionData::doDisable()
0053 {
0054     triggers()->disable();
0055     update_triggers();
0056 }
0057 
0058 void ActionData::doEnable()
0059 {
0060     triggers()->enable();
0061     update_triggers();
0062 }
0063 
0064 Trigger_list *ActionData::triggers()
0065 {
0066     return _triggers;
0067 }
0068 
0069 const Trigger_list *ActionData::triggers() const
0070 {
0071     return _triggers;
0072 }
0073 
0074 void ActionData::aboutToBeErased()
0075 {
0076     _triggers->aboutToBeErased();
0077     _actions->aboutToBeErased();
0078 }
0079 
0080 const ActionList *ActionData::actions() const
0081 {
0082     return _actions;
0083 }
0084 
0085 ActionList *ActionData::actions()
0086 {
0087     return _actions;
0088 }
0089 
0090 void ActionData::execute()
0091 {
0092     for (ActionList::Iterator it = _actions->begin(); it != _actions->end(); ++it)
0093         (*it)->execute();
0094 }
0095 
0096 void ActionData::add_trigger(Trigger *trigger_P)
0097 {
0098     _triggers->append(trigger_P);
0099 }
0100 
0101 void ActionData::add_triggers(Trigger_list *triggers_P)
0102 {
0103     while (!triggers_P->isEmpty()) {
0104         _triggers->append(triggers_P->takeFirst());
0105     }
0106     Q_ASSERT(triggers_P->isEmpty());
0107     delete triggers_P;
0108 }
0109 
0110 void ActionData::set_triggers(Trigger_list *triggers_P)
0111 {
0112     if (_triggers)
0113         delete _triggers;
0114 
0115     _triggers = triggers_P;
0116 }
0117 
0118 void ActionData::add_action(Action *action, Action *after)
0119 {
0120     if (after) {
0121         int index = _actions->indexOf(after);
0122         _actions->insert(index != -1 ? index + 1 : _actions->count(), action);
0123     } else {
0124         _actions->append(action);
0125     }
0126 }
0127 
0128 void ActionData::add_actions(ActionList *actions_P, Action *after_P)
0129 {
0130     int index = 0;
0131     for (ActionList::Iterator it = _actions->begin(); it != _actions->end(); ++it) {
0132         ++index;
0133         if (*it == after_P)
0134             break;
0135     }
0136 
0137     while (!actions_P->empty()) {
0138         // Insert the actions to _actions after removing them from actions_P
0139         // to prevent their deletion upon delete actions_P below.
0140         _actions->insert(++index, actions_P->takeFirst());
0141     }
0142     Q_ASSERT(actions_P->isEmpty());
0143     delete actions_P;
0144 }
0145 
0146 void ActionData::set_actions(ActionList *actions_P)
0147 {
0148     if (_actions)
0149         delete _actions;
0150     _actions = actions_P;
0151 }
0152 
0153 void ActionData::update_triggers()
0154 {
0155     if (!_triggers)
0156         return;
0157 
0158     bool activate = false;
0159     // Activate the triggers if the actions is enabled and the conditions
0160     // match.
0161     if (isEnabled() && conditions_match()) {
0162         activate = true;
0163     }
0164 
0165     for (Trigger_list::Iterator it = _triggers->begin(); it != _triggers->end(); ++it) {
0166         (*it)->activate(activate);
0167     }
0168 }
0169 
0170 } // namespace KHotKeys