aboutsummaryrefslogtreecommitdiff
path: root/src/idoactionhelper.c
blob: b68423a6df6c1f17ad6cdb36be29458d26597ea3 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright 2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE.  See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
*     Lars Uebernickel <lars.uebernickel@canonical.com>
*/

#include "idoactionhelper.h"

typedef GObjectClass IdoActionHelperClass;

struct _IdoActionHelper
{
  GObject parent;

  GtkWidget *widget;
  GActionGroup *actions;
  gchar *action_name;
  GVariant *action_target;
};

G_DEFINE_TYPE (IdoActionHelper, ido_action_helper, G_TYPE_OBJECT)

enum
{
  PROP_0,
  PROP_WIDGET,
  PROP_ACTION_GROUP,
  PROP_ACTION_NAME,
  PROP_ACTION_TARGET,
  NUM_PROPERTIES
};

enum
{
  ACTION_STATE_CHANGED,
  NUM_SIGNALS
};

static GParamSpec *properties[NUM_PROPERTIES];
static guint signals[NUM_SIGNALS];

static void
ido_action_helper_action_added (GActionGroup *actions,
                                const gchar  *action_name,
                                gpointer      user_data)
{
  IdoActionHelper *helper = user_data;
  gboolean enabled;
  GVariant *state;

  if (!g_str_equal (action_name, helper->action_name))
    return;

  if (g_action_group_query_action (actions, action_name,
                                   &enabled, NULL, NULL, NULL, &state))
    {
      gtk_widget_set_sensitive (helper->widget, enabled);

      if (state)
        {
          g_signal_emit (helper, signals[ACTION_STATE_CHANGED], 0, state);
          g_variant_unref (state);
        }
    }
  else
    {
      gtk_widget_set_sensitive (helper->widget, FALSE);
    }
}

static void
ido_action_helper_action_removed (GActionGroup *action_group,
                                  gchar        *action_name,
                                  gpointer      user_data)
{
  IdoActionHelper *helper = user_data;

  if (g_str_equal (action_name, helper->action_name))
    gtk_widget_set_sensitive (helper->widget, FALSE);
}

static void
ido_action_helper_action_enabled_changed (GActionGroup *action_group,
                                          gchar        *action_name,
                                          gboolean      enabled,
                                          gpointer      user_data)
{
  IdoActionHelper *helper = user_data;

  if (g_str_equal (action_name, helper->action_name))
    gtk_widget_set_sensitive (helper->widget, enabled);
}

static void
ido_action_helper_action_state_changed (GActionGroup *action_group,
                                        gchar        *action_name,
                                        GVariant     *value,
                                        gpointer      user_data)
{
  IdoActionHelper *helper = user_data;

  if (g_str_equal (action_name, helper->action_name))
    g_signal_emit (helper, signals[ACTION_STATE_CHANGED], 0, value);
}

static gboolean
call_action_added (gpointer user_data)
{
  IdoActionHelper *helper = user_data;

  ido_action_helper_action_added (helper->actions, helper->action_name, helper);

  return G_SOURCE_REMOVE;
}

static void
ido_action_helper_constructed (GObject *object)
{
  IdoActionHelper *helper = IDO_ACTION_HELPER (object);

  g_signal_connect (helper->actions, "action-added",
                    G_CALLBACK (ido_action_helper_action_added), helper);
  g_signal_connect (helper->actions, "action-removed",
                    G_CALLBACK (ido_action_helper_action_removed), helper);
  g_signal_connect (helper->actions, "action-enabled-changed",
                    G_CALLBACK (ido_action_helper_action_enabled_changed), helper);
  g_signal_connect (helper->actions, "action-state-changed",
                    G_CALLBACK (ido_action_helper_action_state_changed), helper);

  if (g_action_group_has_action (helper->actions, helper->action_name))
    {
      /* call action_added in an idle, so that we don't fire the
       * state-changed signal during construction (nobody could have
       * connected by then).
       */
      g_idle_add (call_action_added, helper);
    }

  G_OBJECT_CLASS (ido_action_helper_parent_class)->constructed (object);
}

static void
ido_action_helper_get_property (GObject    *object,
                                guint       id,
                                GValue     *value,
                                GParamSpec *pspec)
{
  IdoActionHelper *helper = IDO_ACTION_HELPER (object);

  switch (id)
    {
    case PROP_WIDGET:
      g_value_set_object (value, helper->widget);
      break;

    case PROP_ACTION_GROUP:
      g_value_set_object (value, helper->actions);
      break;

    case PROP_ACTION_NAME:
      g_value_set_string (value, helper->action_name);
      break;

    case PROP_ACTION_TARGET:
      g_value_set_variant (value, helper->action_target);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, id, pspec);
    }
}

static void
ido_action_helper_set_property (GObject      *object,
                                guint         id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  IdoActionHelper *helper = IDO_ACTION_HELPER (object);

  switch (id)
    {
    case PROP_WIDGET: /* construct-only */
      helper->widget = g_value_dup_object (value);
      break;

    case PROP_ACTION_GROUP: /* construct-only */
      helper->actions = g_value_dup_object (value);
      break;

    case PROP_ACTION_NAME: /* construct-only */
      helper->action_name = g_value_dup_string (value);
      break;

    case PROP_ACTION_TARGET: /* construct-only */
      helper->action_target = g_value_dup_variant (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, id, pspec);
    }
}

static void
ido_action_helper_finalize (GObject *object)
{
  IdoActionHelper *helper = IDO_ACTION_HELPER (object);

  g_object_unref (helper->widget);

  g_signal_handlers_disconnect_by_data (helper->actions, helper);
  g_object_unref (helper->actions);

  g_free (helper->action_name);

  if (helper->action_target)
    g_variant_unref (helper->action_target);

  G_OBJECT_CLASS (ido_action_helper_parent_class)->finalize (object);
}

static void
ido_action_helper_class_init (IdoActionHelperClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->constructed = ido_action_helper_constructed;
  object_class->get_property = ido_action_helper_get_property;
  object_class->set_property = ido_action_helper_set_property;
  object_class->finalize = ido_action_helper_finalize;

  signals[ACTION_STATE_CHANGED] = g_signal_new ("action-state-changed",
                                                IDO_TYPE_ACTION_HELPER,
                                                G_SIGNAL_RUN_FIRST,
                                                0, NULL, NULL,
                                                g_cclosure_marshal_VOID__VARIANT,
                                                G_TYPE_NONE, 1, G_TYPE_VARIANT);

  properties[PROP_WIDGET] = g_param_spec_object ("widget", "", "",
                                                 GTK_TYPE_WIDGET,
                                                 G_PARAM_CONSTRUCT_ONLY |
                                                 G_PARAM_READWRITE |
                                                 G_PARAM_STATIC_STRINGS);

  properties[PROP_ACTION_GROUP] = g_param_spec_object ("action-group", "", "",
                                                       G_TYPE_ACTION_GROUP,
                                                       G_PARAM_CONSTRUCT_ONLY |
                                                       G_PARAM_READWRITE |
                                                       G_PARAM_STATIC_STRINGS);

  properties[PROP_ACTION_NAME] = g_param_spec_string ("action-name", "", "", NULL,
                                                      G_PARAM_CONSTRUCT_ONLY |
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_STATIC_STRINGS);

  properties[PROP_ACTION_TARGET] = g_param_spec_variant ("action-target", "", "",
                                                         G_VARIANT_TYPE_ANY, NULL,
                                                         G_PARAM_CONSTRUCT_ONLY |
                                                         G_PARAM_READWRITE |
                                                         G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
}

static void
ido_action_helper_init (IdoActionHelper *helper)
{
}

/**
 * ido_action_helper_new:
 * @widget: a #GtkWidget
 * @action_group: a #GActionGroup
 * @action_name: the name of an action in @action_group
 * @target: the target of the action
 *
 * Creates a new #IdoActionHelper. This helper ties @widget to an action
 * (and a target), and performs some common tasks:
 *
 * @widget will be set to insensitive whenever @action_group does not
 * contain an action with the name @action_name, or the action with that
 * name is disabled.
 *
 * Also, the helper emits the "action-state-changed" signal whenever the
 * widget must be updated from the action's state. This includes once
 * when the action was added, and every time the action changes its
 * state.
 *
 * Returns: (transfer full): a new #IdoActionHelper
 */
IdoActionHelper *
ido_action_helper_new (GtkWidget    *widget,
                       GActionGroup *action_group,
                       const gchar  *action_name,
                       GVariant     *target)
{
  g_return_val_if_fail (widget != NULL, NULL);
  g_return_val_if_fail (action_group != NULL, NULL);
  g_return_val_if_fail (action_name != NULL, NULL);

  return g_object_new (IDO_TYPE_ACTION_HELPER,
                       "widget", widget,
                       "action-group", action_group,
                       "action-name", action_name,
                       "action-target", target,
                       NULL);
}

/**
 * ido_action_helper_get_widget:
 * @helper: an #IdoActionHelper
 *
 * Returns: (transfer none): the #GtkWidget associated with @helper
 */
GtkWidget *
ido_action_helper_get_widget (IdoActionHelper *helper)
{
  g_return_val_if_fail (IDO_IS_ACTION_HELPER (helper), NULL);

  return helper->widget;
}

/**
 * ido_action_helper_get_action_target:
 * @helper: an #IdoActionHelper
 *
 * Returns: (transfer none): the action target that was set in
 * ido_action_helper_new() as a #GVariant
 */
GVariant *
ido_action_helper_get_action_target (IdoActionHelper *helper)
{
  g_return_val_if_fail (IDO_IS_ACTION_HELPER (helper), NULL);

  return helper->action_target;
}

/**
 * ido_action_helper_activate:
 * @helper: an #IdoActionHelper
 *
 * Activates the action that is associated with this helper.
 */
void
ido_action_helper_activate (IdoActionHelper *helper)
{
  g_return_if_fail (IDO_IS_ACTION_HELPER (helper));

  if (helper->actions && helper->action_name)
    g_action_group_activate_action (helper->actions, helper->action_name, helper->action_target);
}