1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2017 - 2018 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 **************************************************************************/
19
20 #include "ActionExecutor.h"
21
22 namespace Otter
23 {
24
Object()25 ActionExecutor::Object::Object() : m_executor(nullptr)
26 {
27 }
28
Object(QObject * object,ActionExecutor * executor)29 ActionExecutor::Object::Object(QObject *object, ActionExecutor *executor) : m_object(object), m_executor(executor)
30 {
31 }
32
Object(const Object & other)33 ActionExecutor::Object::Object(const Object &other) : m_object(other.m_object), m_executor(other.m_executor)
34 {
35 }
36
connectSignals(const QObject * receiver,const QMetaMethod * actionsStateChangedMethod,const QMetaMethod * arbitraryActionsStateChangedMethod,const QMetaMethod * categorizedActionsStateChangedMethod)37 void ActionExecutor::Object::connectSignals(const QObject *receiver, const QMetaMethod *actionsStateChangedMethod, const QMetaMethod *arbitraryActionsStateChangedMethod, const QMetaMethod *categorizedActionsStateChangedMethod)
38 {
39 if (receiver && m_object)
40 {
41 const QMetaObject *metaObject(m_object.data()->metaObject());
42 const QMetaMethod actionsStateChangedSignal(metaObject->method(metaObject->indexOfSignal("actionsStateChanged()")));
43 const QMetaMethod arbitraryActionsStateChangedSignal(metaObject->method(metaObject->indexOfSignal("arbitraryActionsStateChanged(QVector<int>)")));
44 const QMetaMethod categorizedActionsStateChangedSignal(metaObject->method(metaObject->indexOfSignal("categorizedActionsStateChanged(QVector<int>)")));
45
46 if (actionsStateChangedSignal.isValid() && actionsStateChangedMethod)
47 {
48 QObject::connect(m_object.data(), actionsStateChangedSignal, receiver, *(actionsStateChangedMethod));
49 }
50
51 if (arbitraryActionsStateChangedSignal.isValid() && arbitraryActionsStateChangedMethod)
52 {
53 QObject::connect(m_object.data(), arbitraryActionsStateChangedSignal, receiver, *(arbitraryActionsStateChangedMethod));
54 }
55
56 if (categorizedActionsStateChangedSignal.isValid() && categorizedActionsStateChangedMethod)
57 {
58 QObject::connect(m_object.data(), categorizedActionsStateChangedSignal, receiver, *(categorizedActionsStateChangedMethod));
59 }
60 }
61 }
62
disconnectSignals(const QObject * receiver,const QMetaMethod * actionsStateChangedMethod,const QMetaMethod * arbitraryActionsStateChangedMethod,const QMetaMethod * categorizedActionsStateChangedMethod)63 void ActionExecutor::Object::disconnectSignals(const QObject *receiver, const QMetaMethod *actionsStateChangedMethod, const QMetaMethod *arbitraryActionsStateChangedMethod, const QMetaMethod *categorizedActionsStateChangedMethod)
64 {
65 if (receiver && m_object)
66 {
67 const QMetaObject *metaObject(m_object.data()->metaObject());
68 const QMetaMethod actionsStateChangedSignal(metaObject->method(metaObject->indexOfSignal("actionsStateChanged()")));
69 const QMetaMethod arbitraryActionsStateChangedSignal(metaObject->method(metaObject->indexOfSignal("arbitraryActionsStateChanged(QVector<int>)")));
70 const QMetaMethod categorizedActionsStateChangedSignal(metaObject->method(metaObject->indexOfSignal("categorizedActionsStateChanged(QVector<int>)")));
71
72 if (actionsStateChangedSignal.isValid() && actionsStateChangedMethod)
73 {
74 QObject::disconnect(m_object.data(), actionsStateChangedSignal, receiver, *(actionsStateChangedMethod));
75 }
76
77 if (arbitraryActionsStateChangedSignal.isValid() && arbitraryActionsStateChangedMethod)
78 {
79 QObject::disconnect(m_object.data(), arbitraryActionsStateChangedSignal, receiver, *(arbitraryActionsStateChangedMethod));
80 }
81
82 if (categorizedActionsStateChangedSignal.isValid() && categorizedActionsStateChangedMethod)
83 {
84 QObject::disconnect(m_object.data(), categorizedActionsStateChangedSignal, receiver, *(categorizedActionsStateChangedMethod));
85 }
86 }
87 }
88
triggerAction(int identifier,const QVariantMap & parameters,ActionsManager::TriggerType trigger)89 void ActionExecutor::Object::triggerAction(int identifier, const QVariantMap ¶meters, ActionsManager::TriggerType trigger)
90 {
91 if (!m_object.isNull())
92 {
93 m_executor->triggerAction(identifier, parameters, trigger);
94 }
95 }
96
getObject() const97 QObject* ActionExecutor::Object::getObject() const
98 {
99 return m_object.data();
100 }
101
operator =(const Object & other)102 ActionExecutor::Object& ActionExecutor::Object::operator=(const Object &other)
103 {
104 if (this != &other)
105 {
106 m_object = other.m_object;
107 m_executor = other.m_executor;
108 }
109
110 return *this;
111 }
112
getActionState(int identifier,const QVariantMap & parameters) const113 ActionsManager::ActionDefinition::State ActionExecutor::Object::getActionState(int identifier, const QVariantMap ¶meters) const
114 {
115 if (!m_object.isNull())
116 {
117 return m_executor->getActionState(identifier, parameters);
118 }
119
120 ActionsManager::ActionDefinition::State state(ActionsManager::getActionDefinition(identifier).getDefaultState());
121 state.isEnabled = false;
122
123 return state;
124 }
125
isValid() const126 bool ActionExecutor::Object::isValid() const
127 {
128 return (!m_object.isNull() && !m_executor->isAboutToClose());
129 }
130
ActionExecutor()131 ActionExecutor::ActionExecutor()
132 {
133 }
134
~ActionExecutor()135 ActionExecutor::~ActionExecutor()
136 {
137 }
138
isAboutToClose() const139 bool ActionExecutor::isAboutToClose() const
140 {
141 return false;
142 }
143
144 }
145