summaryrefslogtreecommitdiff
path: root/garcon/garcon-menu-item-action.c
blob: b4168954b654ea5d566efa5063ccbd2df8a620ca (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
/* vi:set et ai sw=2 sts=2 ts=2: */
/*-
 * Copyright (c) 2015 Danila Poyarkov <dannotemail@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gio/gio.h>
#include <libxfce4util/libxfce4util.h>

#include <garcon/garcon-environment.h>
#include <garcon/garcon-menu-item-action.h>
#include <garcon/garcon-private.h>

/* Property identifiers */
enum
{
  PROP_0,
  PROP_NAME,
  PROP_COMMAND,
};

static void garcon_menu_item_action_finalize     (GObject       *object);
static void garcon_menu_item_action_get_property (GObject       *object,
                                                  guint          prop_id,
                                                  GValue        *value,
                                                  GParamSpec    *pspec);
static void garcon_menu_item_action_set_property (GObject       *object,
                                                  guint          prop_id,
                                                  const GValue  *value,
                                                  GParamSpec    *pspec);

struct _GarconMenuItemActionPrivate
{
  /* Name to be displayed for the action */
  gchar *name;

  /* Command to be executed when the action is clicked */
  gchar *command;
};

G_DEFINE_TYPE (GarconMenuItemAction, garcon_menu_item_action, G_TYPE_OBJECT)

static void
garcon_menu_item_action_class_init (GarconMenuItemActionClass *klass)
{
  GObjectClass *gobject_class;

  g_type_class_add_private (klass, sizeof (GarconMenuItemActionPrivate));

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = garcon_menu_item_action_finalize;
  gobject_class->get_property = garcon_menu_item_action_get_property;
  gobject_class->set_property = garcon_menu_item_action_set_property;

  /**
   * GarconMenuItemAction:name:
   *
   * Name of the application action (will be displayed in menus etc.).
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_NAME,
                                   g_param_spec_string ("name",
                                                        "Name",
                                                        "Name of the action",
                                                        NULL,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_STRINGS));

  /**
   * GarconMenuItemAction:command:
   *
   * Command to be executed when the application action is clicked.
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_COMMAND,
                                   g_param_spec_string ("command",
                                                        "Command",
                                                        "Application command",
                                                        NULL,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_STRINGS));
}

static void
garcon_menu_item_action_init (GarconMenuItemAction *action)
{
  action->priv = G_TYPE_INSTANCE_GET_PRIVATE (action, GARCON_TYPE_MENU_ITEM_ACTION, GarconMenuItemActionPrivate);
}



static void
garcon_menu_item_action_finalize (GObject *object)
{
  GarconMenuItemAction *action = GARCON_MENU_ITEM_ACTION (object);

  g_free (action->priv->name);
  g_free (action->priv->command);
  
  (*G_OBJECT_CLASS (garcon_menu_item_action_parent_class)->finalize) (object);
}



static void
garcon_menu_item_action_get_property (GObject    *object,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
  GarconMenuItemAction *action = GARCON_MENU_ITEM_ACTION (object);

  switch (prop_id)
    {
    case PROP_NAME:
      g_value_set_string (value, garcon_menu_item_action_get_name (action));
      break;

    case PROP_COMMAND:
      g_value_set_string (value, garcon_menu_item_action_get_command (action));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
garcon_menu_item_action_set_property (GObject      *object,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
  GarconMenuItemAction *action = GARCON_MENU_ITEM_ACTION (object);

  switch (prop_id)
    {
    case PROP_NAME:
      garcon_menu_item_action_set_name (action, g_value_get_string (value));
      break;

    case PROP_COMMAND:
      garcon_menu_item_action_set_command (action, g_value_get_string (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}


const gchar*
garcon_menu_item_action_get_name (GarconMenuItemAction *action)
{
  g_return_val_if_fail (GARCON_IS_MENU_ITEM_ACTION (action), NULL);
  return action->priv->name;
}



void
garcon_menu_item_action_set_name (GarconMenuItemAction *action,
                                  const gchar          *name)
{
  g_return_if_fail (GARCON_IS_MENU_ITEM_ACTION (action));
  g_return_if_fail (g_utf8_validate (name, -1, NULL));

  /* Abort if old and new name are equal */
  if (g_strcmp0 (action->priv->name, name) == 0)
    return;

  /* Assign new name */
  g_free (action->priv->name);
  action->priv->name = g_strdup (name);

  /* Notify listeners */
  g_object_notify (G_OBJECT (action), "name");
}


const gchar*
garcon_menu_item_action_get_command (GarconMenuItemAction *action)
{
  g_return_val_if_fail (GARCON_IS_MENU_ITEM_ACTION (action), NULL);
  return action->priv->command;
}



void
garcon_menu_item_action_set_command (GarconMenuItemAction *action,
                                     const gchar          *command)
{
  g_return_if_fail (GARCON_IS_MENU_ITEM_ACTION (action));
  g_return_if_fail (command != NULL);

  /* Abort if old and new command are equal */
  if (g_strcmp0 (action->priv->command, command) == 0)
    return;

  /* Assign new command */
  g_free (action->priv->command);
  action->priv->command = g_strdup (command);

  /* Notify listeners */
  g_object_notify (G_OBJECT (action), "command");
}

void
garcon_menu_item_action_ref (GarconMenuItemAction *action)
{
  g_return_if_fail (GARCON_IS_MENU_ITEM_ACTION (action));

  /* Grab a reference on the object */
  g_object_ref (G_OBJECT (action));
}



void
garcon_menu_item_action_unref (GarconMenuItemAction *action)
{
  g_return_if_fail (GARCON_IS_MENU_ITEM_ACTION (action));

  /* Decrement the reference counter */
  g_object_unref (G_OBJECT (action));
}