1 /*
2     SPDX-License-Identifier: GPL-2.0-only
3     SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
4  */
5 
6 #include "action_data/action_data.h"
7 
8 #include "action_data/action_data_visitor.h"
9 #include "actions/actions.h"
10 #include "triggers/triggers.h"
11 
12 #include <QDebug>
13 #include <kconfiggroup.h>
14 
15 namespace KHotKeys
16 {
ActionData(ActionDataGroup * parent_P,const QString & name_P,const QString & comment_P,Trigger_list * triggers_P,Condition_list * conditions_P,ActionList * actions_P)17 ActionData::ActionData(ActionDataGroup *parent_P,
18                        const QString &name_P,
19                        const QString &comment_P,
20                        Trigger_list *triggers_P,
21                        Condition_list *conditions_P,
22                        ActionList *actions_P)
23     : ActionDataBase(parent_P, name_P, comment_P, conditions_P)
24     , _triggers(triggers_P)
25     , _actions(actions_P)
26 {
27     if (!_triggers)
28         _triggers = new Trigger_list;
29 
30     if (!_actions)
31         _actions = new ActionList;
32 }
33 
accept(ActionDataConstVisitor * visitor) const34 void ActionData::accept(ActionDataConstVisitor *visitor) const
35 {
36     visitor->visitActionData(this);
37 }
38 
~ActionData()39 ActionData::~ActionData()
40 {
41     delete _triggers;
42     _triggers = nullptr;
43     delete _actions;
44     _actions = nullptr;
45 }
46 
accept(ActionDataVisitor * visitor)47 void ActionData::accept(ActionDataVisitor *visitor)
48 {
49     visitor->visitActionData(this);
50 }
51 
doDisable()52 void ActionData::doDisable()
53 {
54     triggers()->disable();
55     update_triggers();
56 }
57 
doEnable()58 void ActionData::doEnable()
59 {
60     triggers()->enable();
61     update_triggers();
62 }
63 
triggers()64 Trigger_list *ActionData::triggers()
65 {
66     return _triggers;
67 }
68 
triggers() const69 const Trigger_list *ActionData::triggers() const
70 {
71     return _triggers;
72 }
73 
aboutToBeErased()74 void ActionData::aboutToBeErased()
75 {
76     _triggers->aboutToBeErased();
77     _actions->aboutToBeErased();
78 }
79 
actions() const80 const ActionList *ActionData::actions() const
81 {
82     return _actions;
83 }
84 
actions()85 ActionList *ActionData::actions()
86 {
87     return _actions;
88 }
89 
execute()90 void ActionData::execute()
91 {
92     for (ActionList::Iterator it = _actions->begin(); it != _actions->end(); ++it)
93         (*it)->execute();
94 }
95 
add_trigger(Trigger * trigger_P)96 void ActionData::add_trigger(Trigger *trigger_P)
97 {
98     _triggers->append(trigger_P);
99 }
100 
add_triggers(Trigger_list * triggers_P)101 void ActionData::add_triggers(Trigger_list *triggers_P)
102 {
103     while (!triggers_P->isEmpty()) {
104         _triggers->append(triggers_P->takeFirst());
105     }
106     Q_ASSERT(triggers_P->isEmpty());
107     delete triggers_P;
108 }
109 
set_triggers(Trigger_list * triggers_P)110 void ActionData::set_triggers(Trigger_list *triggers_P)
111 {
112     if (_triggers)
113         delete _triggers;
114 
115     _triggers = triggers_P;
116 }
117 
add_action(Action * action,Action * after)118 void ActionData::add_action(Action *action, Action *after)
119 {
120     if (after) {
121         int index = _actions->indexOf(after);
122         _actions->insert(index != -1 ? index + 1 : _actions->count(), action);
123     } else {
124         _actions->append(action);
125     }
126 }
127 
add_actions(ActionList * actions_P,Action * after_P)128 void ActionData::add_actions(ActionList *actions_P, Action *after_P)
129 {
130     int index = 0;
131     for (ActionList::Iterator it = _actions->begin(); it != _actions->end(); ++it) {
132         ++index;
133         if (*it == after_P)
134             break;
135     }
136 
137     while (!actions_P->empty()) {
138         // Insert the actions to _actions after removing them from actions_P
139         // to prevent their deletion upon delete actions_P below.
140         _actions->insert(++index, actions_P->takeFirst());
141     }
142     Q_ASSERT(actions_P->isEmpty());
143     delete actions_P;
144 }
145 
set_actions(ActionList * actions_P)146 void ActionData::set_actions(ActionList *actions_P)
147 {
148     if (_actions)
149         delete _actions;
150     _actions = actions_P;
151 }
152 
update_triggers()153 void ActionData::update_triggers()
154 {
155     if (!_triggers)
156         return;
157 
158     bool activate = false;
159     // Activate the triggers if the actions is enabled and the conditions
160     // match.
161     if (isEnabled() && conditions_match()) {
162         activate = true;
163     }
164 
165     for (Trigger_list::Iterator it = _triggers->begin(); it != _triggers->end(); ++it) {
166         (*it)->activate(activate);
167     }
168 }
169 
170 } // namespace KHotKeys
171