3
* Copyright (C) 2013-2014 Stefano Karapetsas
5
* This file is part of MATE Applets.
7
* MATE Applets is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
10
* (at your option) any later version.
12
* MATE Applets is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with MATE Applets. If not, see <http://www.gnu.org/licenses/>.
21
* Stefano Karapetsas <stefano@karapetsas.com>
27
#include <glib/gi18n.h>
31
#include <mate-panel-applet.h>
32
#include <mate-panel-applet-gsettings.h>
34
/* Applet constants */
35
#define APPLET_ICON "terminal"
36
#define ERROR_OUTPUT "#"
38
/* GSettings constants */
39
#define COMMAND_SCHEMA "org.mate.panel.applet.command"
40
#define COMMAND_KEY "command"
41
#define INTERVAL_KEY "interval"
42
#define SHOW_ICON_KEY "show-icon"
44
/* GKeyFile constants */
45
#define GK_COMMAND_GROUP "Command"
46
#define GK_COMMAND_OUTPUT "Output"
47
#define GK_COMMAND_ICON "Icon"
49
/* Max output lenght accepted from commands */
50
#define MAX_OUTPUT_LENGTH 30
54
MatePanelApplet *applet;
68
static void command_about_callback (GtkAction *action, CommandApplet *command_applet);
69
static void command_settings_callback (GtkAction *action, CommandApplet *command_applet);
70
static gboolean command_execute (CommandApplet *command_applet);
72
static const GtkActionEntry applet_menu_actions [] = {
73
{ "Preferences", GTK_STOCK_PROPERTIES, N_("_Preferences"), NULL, NULL, G_CALLBACK (command_settings_callback) },
74
{ "About", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL, G_CALLBACK (command_about_callback) }
77
static char *ui = "<menuitem name='Item 1' action='Preferences' />"
78
"<menuitem name='Item 2' action='About' />";
81
command_applet_destroy (MatePanelApplet *applet_widget, CommandApplet *command_applet)
83
g_assert (command_applet);
85
if (command_applet->timeout_id != 0)
87
g_source_remove(command_applet->timeout_id);
88
command_applet->timeout_id = 0;
91
if (command_applet->command != NULL)
93
g_free (command_applet->command);
94
command_applet->command = NULL;
97
g_object_unref (command_applet->settings);
100
/* Show the about dialog */
102
command_about_callback (GtkAction *action, CommandApplet *command_applet)
104
const char* authors[] = { "Stefano Karapetsas <stefano@karapetsas.com>", NULL };
106
gtk_show_about_dialog(NULL,
108
"copyright", "Copyright Ā© 2013-2014 Stefano Karapetsas",
110
"comments", _("Shows the output of a command"),
111
"translator-credits", _("translator-credits"),
112
"logo-icon-name", APPLET_ICON,
116
/* Show the preferences dialog */
118
command_settings_callback (GtkAction *action, CommandApplet *command_applet)
127
dialog = GTK_DIALOG (gtk_dialog_new_with_buttons(_("Command Applet Preferences"),
133
table = gtk_table_new (3, 2, FALSE);
134
gtk_table_set_row_spacings (table, 12);
135
gtk_table_set_col_spacings (table, 12);
137
gtk_window_set_default_size (GTK_WINDOW (dialog), 350, 150);
139
widget = gtk_label_new (_("Command:"));
140
gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
141
gtk_table_attach (table, widget, 1, 2, 0, 1,
145
command = gtk_entry_new ();
146
gtk_table_attach (table, command, 2, 3, 0, 1,
147
GTK_EXPAND | GTK_FILL | GTK_SHRINK, GTK_FILL,
150
widget = gtk_label_new (_("Interval (seconds):"));
151
gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
152
gtk_table_attach (table, widget, 1, 2, 1, 2,
156
interval = gtk_spin_button_new_with_range (1.0, 600.0, 1.0);
157
gtk_table_attach (table, interval, 2, 3, 1, 2,
158
GTK_EXPAND | GTK_FILL | GTK_SHRINK, GTK_FILL,
161
showicon = gtk_check_button_new_with_label (_("Show icon"));
162
gtk_table_attach (table, showicon, 2, 3, 3, 4,
163
GTK_EXPAND | GTK_FILL | GTK_SHRINK, GTK_FILL,
166
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (dialog)), table, TRUE, TRUE, 0);
168
g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog);
170
/* use g_settings_bind to manage settings */
171
g_settings_bind (command_applet->settings, COMMAND_KEY, command, "text", G_SETTINGS_BIND_DEFAULT);
172
g_settings_bind (command_applet->settings, INTERVAL_KEY, interval, "value", G_SETTINGS_BIND_DEFAULT);
173
g_settings_bind (command_applet->settings, SHOW_ICON_KEY, showicon, "active", G_SETTINGS_BIND_DEFAULT);
175
gtk_widget_show_all (GTK_WIDGET (dialog));
178
/* GSettings signal callbacks */
180
settings_command_changed (GSettings *settings, gchar *key, CommandApplet *command_applet)
184
command = g_settings_get_string (command_applet->settings, COMMAND_KEY);
186
if (command_applet->command)
187
g_free (command_applet->command);
189
if (command != NULL && command[0] != 0)
190
command_applet->command = command;
192
command_applet->command = g_strdup ("");
196
settings_interval_changed (GSettings *settings, gchar *key, CommandApplet *command_applet)
200
interval = g_settings_get_int (command_applet->settings, INTERVAL_KEY);
202
/* minimum interval */
206
command_applet->interval = interval;
208
/* stop current timer */
209
if (command_applet->timeout_id != 0)
211
g_source_remove(command_applet->timeout_id);
212
command_applet->timeout_id = 0;
215
/* execute command to start new timer */
216
command_execute (command_applet);
220
command_execute (CommandApplet *command_applet)
222
GError *error = NULL;
223
gchar *output = NULL;
226
if (g_spawn_command_line_sync (command_applet->command, &output, NULL, &ret, &error))
228
gtk_widget_set_tooltip_text (GTK_WIDGET (command_applet->label), command_applet->command);
230
if ((output != NULL) && (output[0] != 0))
232
/* check if output is a custom GKeyFile */
233
if (g_str_has_prefix (output, "[Command]"))
235
GKeyFile *file = g_key_file_new ();
236
if (g_key_file_load_from_data (file, output, -1, G_KEY_FILE_NONE, NULL))
238
gchar *goutput = g_key_file_get_string (file, GK_COMMAND_GROUP, GK_COMMAND_OUTPUT, NULL);
239
gchar *icon = g_key_file_get_string (file, GK_COMMAND_GROUP, GK_COMMAND_ICON, NULL);
243
gtk_label_set_use_markup (command_applet->label, TRUE);
244
gtk_label_set_markup (command_applet->label, goutput);
247
gtk_image_set_from_icon_name (command_applet->image, icon, 24);
253
gtk_label_set_text (command_applet->label, ERROR_OUTPUT);
254
g_key_file_free (file);
258
/* check output length */
259
if (strlen(output) > MAX_OUTPUT_LENGTH)
261
GString *strip_output;
262
strip_output = g_string_new_len (output, MAX_OUTPUT_LENGTH);
264
output = strip_output->str;
265
g_string_free (strip_output, FALSE);
267
/* remove last char if it is '\n' to avoid aligment problems */
268
if (g_str_has_suffix (output, "\n"))
270
output[strlen(output) - 1] = 0;
273
gtk_label_set_text (command_applet->label, output);
277
gtk_label_set_text (command_applet->label, ERROR_OUTPUT);
280
gtk_label_set_text (command_applet->label, ERROR_OUTPUT);
284
/* start timer for next execution */
285
command_applet->timeout_id = g_timeout_add_seconds (command_applet->interval,
286
(GSourceFunc) command_execute,
293
command_applet_fill (MatePanelApplet* applet)
295
CommandApplet *command_applet;
297
g_set_application_name (_("Command Applet"));
298
gtk_window_set_default_icon_name (APPLET_ICON);
300
mate_panel_applet_set_flags (applet, MATE_PANEL_APPLET_EXPAND_MINOR);
301
mate_panel_applet_set_background_widget (applet, GTK_WIDGET (applet));
303
command_applet = g_malloc0(sizeof(CommandApplet));
304
command_applet->applet = applet;
305
command_applet->settings = mate_panel_applet_settings_new (applet, COMMAND_SCHEMA);
307
command_applet->interval = g_settings_get_int (command_applet->settings, INTERVAL_KEY);
308
command_applet->command = g_settings_get_string (command_applet->settings, COMMAND_KEY);
310
command_applet->hbox = gtk_hbox_new (FALSE, 0);
311
command_applet->image = gtk_image_new_from_icon_name (APPLET_ICON, 24);
312
command_applet->label = gtk_label_new (ERROR_OUTPUT);
313
command_applet->timeout_id = 0;
315
/* we add the Gtk label into the applet */
316
gtk_box_pack_start (GTK_BOX (command_applet->hbox),
317
GTK_WIDGET (command_applet->image),
319
gtk_box_pack_start (GTK_BOX (command_applet->hbox),
320
GTK_WIDGET (command_applet->label),
323
gtk_container_add (GTK_CONTAINER (applet),
324
GTK_WIDGET (command_applet->hbox));
326
gtk_widget_show_all (GTK_WIDGET (command_applet->applet));
328
g_signal_connect(G_OBJECT (command_applet->applet), "destroy",
329
G_CALLBACK (command_applet_destroy),
332
/* GSettings signals */
333
g_signal_connect(command_applet->settings,
334
"changed::" COMMAND_KEY,
335
G_CALLBACK (settings_command_changed),
337
g_signal_connect(command_applet->settings,
338
"changed::" INTERVAL_KEY,
339
G_CALLBACK (settings_interval_changed),
341
g_settings_bind (command_applet->settings,
343
command_applet->image,
345
G_SETTINGS_BIND_DEFAULT);
347
/* set up context menu */
348
GtkActionGroup *action_group = gtk_action_group_new ("Command Applet Actions");
349
gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
350
gtk_action_group_add_actions (action_group, applet_menu_actions,
351
G_N_ELEMENTS (applet_menu_actions), command_applet);
352
mate_panel_applet_setup_menu (command_applet->applet, ui, action_group);
354
/* first command execution */
355
command_execute (command_applet);
360
/* this function, called by mate-panel, will create the applet */
362
command_factory (MatePanelApplet* applet, const char* iid, gpointer data)
364
gboolean retval = FALSE;
366
if (!g_strcmp0 (iid, "CommandApplet"))
367
retval = command_applet_fill (applet);
372
/* needed by mate-panel applet library */
373
MATE_PANEL_APPLET_OUT_PROCESS_FACTORY("CommandAppletFactory",