GTK+2 panel 

GTK+2 panel Git Source Tree

Root/libgpwidgets/gpw-shell-command.c

1/* Gtk2Panel - A bleeding edge Gtk+2 panel
2 * Copyright (C) 2009 Nicola Fontana <ntd at entidi.it>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20
21/**
22 * SECTION:gpw-shell-command
23 * @title: GpwShellCommand
24 * @short_description: A #GtkAction providing spawning of shell processes
25 *
26 * GpwShellCommand is a GtkAction with the embedded feature of spawning
27 * the shell command specified by its #GpwShellCommand:command-line property
28 * whenever the #GpwShellCommand::activate signal is emitted.
29 **/
30
31/**
32 * GpwShellCommand:
33 *
34 * All fields are private and should not be used directly.
35 * Use its public methods instead.
36 **/
37
38
39#include "gpw-shell-command.h"
40#include "gpw-shell-command-private.h"
41#include "gpw-intl.h"
42
43
44enum {
45 PROP_0,
46 PROP_COMMAND_LINE
47};
48
49
50static void get_property (GObject *object,
51 guint prop_id,
52 GValue *value,
53 GParamSpec *pspec);
54static void set_property (GObject *object,
55 guint prop_id,
56 const GValue *value,
57 GParamSpec *pspec);
58static void finalize (GObject *object);
59static void set_command_line (GpwShellCommand*shell_command,
60 const gchar *command_line);
61static void activate (GtkAction *action);
62
63
64
65
66G_DEFINE_TYPE(GpwShellCommand, gpw_shell_command, GTK_TYPE_ACTION);
67
68
69static void
70gpw_shell_command_class_init(GpwShellCommandClass *klass)
71{
72 GObjectClass *gobject_class;
73 GtkActionClass *action_class;
74 GParamSpec *param;
75
76 gobject_class = (GObjectClass *) klass;
77 action_class = (GtkActionClass *) klass;
78
79 g_type_class_add_private(klass, sizeof(GpwShellCommandPrivate));
80
81 gobject_class->get_property = get_property;
82 gobject_class->set_property = set_property;
83 gobject_class->finalize = finalize;
84
85 action_class->activate = activate;
86
87 param = g_param_spec_string("command-line",
88 P_("Command Line"),
89 P_("The command line arg to pass to g_spawn_command_line_async() when this action is activated"),
90 NULL,
91 G_PARAM_READWRITE);
92 g_object_class_install_property(gobject_class, PROP_COMMAND_LINE, param);
93}
94
95static void
96gpw_shell_command_init(GpwShellCommand *shell_command)
97{
98 GpwShellCommandPrivate *data;
99
100 data = G_TYPE_INSTANCE_GET_PRIVATE(shell_command, GPW_TYPE_SHELL_COMMAND,
101 GpwShellCommandPrivate);
102
103 data->command_line = NULL;
104
105 shell_command->data = data;
106}
107
108static void
109get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
110{
111 GpwShellCommandPrivate *data = GPW_SHELL_COMMAND_GET_DATA(object);
112
113 switch (prop_id) {
114 case PROP_COMMAND_LINE:
115 g_value_set_string(value, data->command_line);
116 break;
117 default:
118 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
119 break;
120 }
121}
122
123static void
124set_property(GObject *object,
125 guint prop_id, const GValue *value, GParamSpec *pspec)
126{
127 GpwShellCommand *shell_command = (GpwShellCommand *) object;
128
129 switch (prop_id) {
130 case PROP_COMMAND_LINE:
131 set_command_line(shell_command, g_value_get_string(value));
132 break;
133 default:
134 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
135 break;
136 }
137}
138
139static void
140finalize(GObject *object)
141{
142 GpwShellCommand *shell_command = (GpwShellCommand *) object;
143
144 set_command_line(shell_command, NULL);
145
146 G_OBJECT_CLASS(gpw_shell_command_parent_class)->finalize(object);
147}
148
149static void
150set_command_line(GpwShellCommand *shell_command, const gchar *command_line)
151{
152 GpwShellCommandPrivate *data = GPW_SHELL_COMMAND_GET_DATA(shell_command);
153
154 /* Check for a "clear" request */
155 if (command_line == NULL) {
156 g_free(data->command_line);
157 data->command_line = NULL;
158 return;
159 }
160
161 /* Check if something changed */
162 if (data->command_line != NULL &&
163 strcmp(command_line, data->command_line) == 0)
164 return;
165
166 g_free(data->command_line);
167 data->command_line = g_strdup(command_line);
168}
169
170static void
171activate(GtkAction *action)
172{
173 GpwShellCommandPrivate *data;
174 const gchar *command_line;
175 GError *error;
176
177 data = GPW_SHELL_COMMAND_GET_DATA(action);
178 command_line = data->command_line;
179 error = NULL;
180
181 if (!g_spawn_command_line_async(command_line, &error)) {
182 g_assert(error != NULL && error->message != NULL);
183 g_warning(error->message);
184 g_error_free(error);
185 }
186}

Archive Download this file

Branches

Tags