~ubuntu-branches/debian/stretch/gnome-builder/stretch

« back to all changes in this revision

Viewing changes to src/commands/gb-command-gaction.c

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson
  • Date: 2015-10-11 12:38:45 UTC
  • Revision ID: package-import@ubuntu.com-20151011123845-a0hvkz01se0p1p5a
Tags: upstream-3.16.3
ImportĀ upstreamĀ versionĀ 3.16.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* gb-command-gaction.c
 
2
 *
 
3
 * Copyright (C) 2014 Christian Hergert <christian@hergert.me>
 
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
#define G_LOG_DOMAIN "command-gaction"
 
20
 
 
21
#include <glib/gi18n.h>
 
22
#include <gio/gio.h>
 
23
 
 
24
#include "gb-command-gaction.h"
 
25
 
 
26
struct _GbCommandGaction
 
27
{
 
28
  GbCommand     parent_instance;
 
29
 
 
30
  GActionGroup *action_group;
 
31
  gchar        *action_name;
 
32
  GVariant     *parameters;
 
33
};
 
34
 
 
35
G_DEFINE_TYPE (GbCommandGaction, gb_command_gaction, GB_TYPE_COMMAND)
 
36
 
 
37
enum {
 
38
  PROP_0,
 
39
  PROP_ACTION_GROUP,
 
40
  PROP_ACTION_NAME,
 
41
  PROP_PARAMETERS,
 
42
  LAST_PROP
 
43
};
 
44
 
 
45
static GParamSpec *gParamSpecs [LAST_PROP];
 
46
 
 
47
static void
 
48
gb_command_gaction_set_action_group (GbCommandGaction *gaction,
 
49
                                     GActionGroup     *action_group)
 
50
{
 
51
  g_return_if_fail (GB_IS_COMMAND_GACTION (gaction));
 
52
  g_return_if_fail (G_IS_ACTION_GROUP (action_group));
 
53
 
 
54
  if (gaction->action_group != action_group)
 
55
    {
 
56
      g_clear_object (&gaction->action_group);
 
57
      gaction->action_group = g_object_ref (action_group);
 
58
    }
 
59
}
 
60
 
 
61
static void
 
62
gb_command_gaction_set_action_name (GbCommandGaction *gaction,
 
63
                                    const gchar      *action_name)
 
64
{
 
65
  g_return_if_fail (GB_IS_COMMAND_GACTION (gaction));
 
66
 
 
67
  if (gaction->action_name != action_name)
 
68
    {
 
69
      g_clear_pointer (&gaction->action_name, g_free);
 
70
      gaction->action_name = g_strdup (action_name);
 
71
    }
 
72
}
 
73
 
 
74
static void
 
75
gb_command_gaction_set_parameters (GbCommandGaction *gaction,
 
76
                                   GVariant         *variant)
 
77
{
 
78
  g_return_if_fail (GB_IS_COMMAND_GACTION (gaction));
 
79
 
 
80
  if (gaction->parameters != variant)
 
81
    {
 
82
      g_clear_pointer (&gaction->parameters, g_variant_unref);
 
83
      gaction->parameters = g_variant_ref (variant);
 
84
    }
 
85
}
 
86
 
 
87
static GbCommandResult *
 
88
gb_command_gaction_execute (GbCommand *command)
 
89
{
 
90
  GbCommandGaction *self = (GbCommandGaction *)command;
 
91
 
 
92
  g_return_val_if_fail (GB_IS_COMMAND_GACTION (self), NULL);
 
93
 
 
94
  if (self->action_group &&
 
95
      self->action_name &&
 
96
      g_action_group_has_action (self->action_group,
 
97
                                 self->action_name))
 
98
    {
 
99
      g_action_group_activate_action (self->action_group,
 
100
                                      self->action_name,
 
101
                                      self->parameters);
 
102
    }
 
103
 
 
104
  return NULL;
 
105
}
 
106
 
 
107
static void
 
108
gb_command_gaction_finalize (GObject *object)
 
109
{
 
110
  GbCommandGaction *self = GB_COMMAND_GACTION (object);
 
111
 
 
112
  g_clear_object (&self->action_group);
 
113
  g_clear_pointer (&self->action_name, g_free);
 
114
  g_clear_pointer (&self->parameters, g_variant_unref);
 
115
 
 
116
  G_OBJECT_CLASS (gb_command_gaction_parent_class)->finalize (object);
 
117
}
 
118
 
 
119
static void
 
120
gb_command_gaction_get_property (GObject    *object,
 
121
                                 guint       prop_id,
 
122
                                 GValue     *value,
 
123
                                 GParamSpec *pspec)
 
124
{
 
125
  GbCommandGaction *self = GB_COMMAND_GACTION (object);
 
126
 
 
127
  switch (prop_id)
 
128
    {
 
129
    case PROP_ACTION_GROUP:
 
130
      g_value_set_object (value, self->action_group);
 
131
      break;
 
132
 
 
133
    case PROP_ACTION_NAME:
 
134
      g_value_set_string (value, self->action_name);
 
135
      break;
 
136
 
 
137
    case PROP_PARAMETERS:
 
138
      g_value_set_variant (value, self->parameters);
 
139
      break;
 
140
 
 
141
    default:
 
142
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
143
    }
 
144
}
 
145
 
 
146
static void
 
147
gb_command_gaction_set_property (GObject      *object,
 
148
                                 guint         prop_id,
 
149
                                 const GValue *value,
 
150
                                 GParamSpec   *pspec)
 
151
{
 
152
  GbCommandGaction *self = GB_COMMAND_GACTION (object);
 
153
 
 
154
  switch (prop_id)
 
155
    {
 
156
    case PROP_ACTION_GROUP:
 
157
      gb_command_gaction_set_action_group (self, g_value_get_object (value));
 
158
      break;
 
159
 
 
160
    case PROP_ACTION_NAME:
 
161
      gb_command_gaction_set_action_name (self, g_value_get_string (value));
 
162
      break;
 
163
 
 
164
    case PROP_PARAMETERS:
 
165
      gb_command_gaction_set_parameters (self, g_value_get_variant (value));
 
166
      break;
 
167
 
 
168
    default:
 
169
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
170
    }
 
171
}
 
172
 
 
173
static void
 
174
gb_command_gaction_class_init (GbCommandGactionClass *klass)
 
175
{
 
176
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
177
  GbCommandClass *command_class = GB_COMMAND_CLASS (klass);
 
178
 
 
179
  object_class->finalize = gb_command_gaction_finalize;
 
180
  object_class->get_property = gb_command_gaction_get_property;
 
181
  object_class->set_property = gb_command_gaction_set_property;
 
182
 
 
183
  command_class->execute = gb_command_gaction_execute;
 
184
 
 
185
  gParamSpecs [PROP_ACTION_GROUP] =
 
186
    g_param_spec_object ("action-group",
 
187
                         _("Action Group"),
 
188
                         _("The GActionGroup containing the action."),
 
189
                         G_TYPE_ACTION_GROUP,
 
190
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
191
 
 
192
  gParamSpecs [PROP_ACTION_NAME] =
 
193
    g_param_spec_string ("action-name",
 
194
                         _("Action Name"),
 
195
                         _("The name of the action to execute."),
 
196
                         NULL,
 
197
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
198
 
 
199
  gParamSpecs [PROP_PARAMETERS] =
 
200
    g_param_spec_variant ("parameters",
 
201
                          _("Parameters"),
 
202
                          _("The parameters for the action."),
 
203
                          G_VARIANT_TYPE_ANY,
 
204
                          NULL,
 
205
                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
206
 
 
207
  g_object_class_install_properties (object_class, LAST_PROP, gParamSpecs);
 
208
}
 
209
 
 
210
static void
 
211
gb_command_gaction_init (GbCommandGaction *self)
 
212
{
 
213
}