1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
#include "qdbusactiongroup.h"
#include <QDebug>
#include <gio/gio.h>
QDBusActionGroup::QDBusActionGroup(QObject *parent)
:QObject(parent),
m_actionGroup(NULL)
{
}
QDBusActionGroup::~QDBusActionGroup()
{
clear();
}
QAction *QDBusActionGroup::getAction(const QString &actionName)
{
Q_FOREACH(QAction *act, m_actions) {
if (act->text() == actionName) {
return act;
}
}
return NULL;
}
void QDBusActionGroup::serviceVanish(GDBusConnection *)
{
clear();
}
void QDBusActionGroup::serviceAppear(GDBusConnection *connection)
{
GDBusActionGroup *ag = g_dbus_action_group_get(connection,
busName().toLatin1(),
objectPath().toLatin1());
setActionGroup(ag);
if (ag == NULL) {
stop();
}
}
void QDBusActionGroup::start()
{
QDBusObject::connect();
}
void QDBusActionGroup::stop()
{
QDBusObject::disconnect();
}
void QDBusActionGroup::busTypeChanged(BusType)
{
busTypeChanged();
}
void QDBusActionGroup::busNameChanged(const QString &)
{
busNameChanged();
}
void QDBusActionGroup::objectPathChanged(const QString &objectPath)
{
objectPathChanged();
}
void QDBusActionGroup::statusChanged(ConnectionStatus status)
{
statusChanged();
}
void QDBusActionGroup::setIntBusType(int busType)
{
if ((busType > None) && (busType < LastBusType)) {
setBusType(static_cast<BusType>(busType));
}
}
void QDBusActionGroup::setActionGroup(GDBusActionGroup *ag)
{
if (m_actionGroup == reinterpret_cast<GActionGroup*>(ag)) {
return;
}
if (m_actionGroup) {
g_signal_handler_disconnect(m_actionGroup, m_signalActionAddId);
g_signal_handler_disconnect(m_actionGroup, m_signalActionRemovedId);
m_signalActionAddId = m_signalActionRemovedId = 0;
g_object_unref(m_actionGroup);
}
m_actionGroup = reinterpret_cast<GActionGroup*>(ag);
if (m_actionGroup) {
m_signalActionAddId = g_signal_connect(m_actionGroup,
"action-add",
G_CALLBACK(QDBusActionGroup::onActionAdded),
this);
m_signalActionRemovedId = g_signal_connect(m_actionGroup,
"action-removed",
G_CALLBACK(QDBusActionGroup::onActionRemoved),
this);
gchar **actionNames = g_action_group_list_actions(m_actionGroup);
for(int i=0; actionNames[i] != NULL; i++) {
addAction(actionNames[i]);
}
g_strfreev(actionNames);
}
}
void QDBusActionGroup::addAction(const char *actionName)
{
QAction *act = new QAction(actionName, this);
act->setEnabled(g_action_group_get_action_enabled(m_actionGroup, actionName));
const GVariantType *stateType = g_action_group_get_action_state_type(m_actionGroup, actionName);
if (stateType == G_VARIANT_TYPE_BOOLEAN) {
act->setCheckable(true);
GVariant *actState = g_action_group_get_action_state(m_actionGroup, actionName);
if (actState != NULL) {
act->setChecked(g_variant_get_boolean(actState));
g_variant_unref(actState);
}
}
// remove any older action with the same name
removeAction(actionName);
m_actions.insert(act);
}
void QDBusActionGroup::removeAction(const char *actionName)
{
Q_FOREACH(QAction *act, m_actions) {
if (act->text() == actionName) {
m_actions.remove(act);
delete act;
break;
}
}
}
void QDBusActionGroup::updateAction(const char *actionName, GVariant *state)
{
QAction *action = getAction(actionName);
if ((action != NULL) && (state != NULL)) {
const GVariantType *stateType = g_variant_get_type(state);
if (stateType == G_VARIANT_TYPE_BOOLEAN) {
action->setChecked(g_variant_get_boolean(state));
}
}
}
void QDBusActionGroup::clear()
{
Q_FOREACH(QAction *act, m_actions) {
delete act;
}
m_actions.clear();
if (m_actionGroup != NULL) {
g_object_unref(m_actionGroup);
m_actionGroup = NULL;
}
}
void QDBusActionGroup::onActionAdded(GDBusActionGroup *, gchar *actionName, gpointer data)
{
QDBusActionGroup *self = reinterpret_cast<QDBusActionGroup*>(data);
self->addAction(actionName);
}
void QDBusActionGroup::onActionRemoved(GDBusActionGroup *, gchar *actionName, gpointer data)
{
QDBusActionGroup *self = reinterpret_cast<QDBusActionGroup*>(data);
self->removeAction(actionName);
}
void QDBusActionGroup::onActionStateChanged(GDBusActionGroup *ag, gchar *actionName, GVariant *value, gpointer data)
{
QDBusActionGroup *self = reinterpret_cast<QDBusActionGroup*>(data);
self->updateAction(actionName, value);
}
|