From 1ee4ea5cc7e554b481da4e052e1c7b46d03b6b5b Mon Sep 17 00:00:00 2001 From: rbuj Date: Sat, 1 Aug 2020 13:50:38 +0200 Subject: command: Use common subdirs - src, data --- command/Makefile.am | 61 +-- command/command-preferences.ui | 191 -------- command/command-resources.gresource.xml | 6 - command/command.c | 494 --------------------- command/data/Makefile.am | 42 ++ command/data/command-preferences.ui | 191 ++++++++ command/data/command-resources.gresource.xml | 6 + ...s.CommandApplet.mate-panel-applet.desktop.in.in | 11 + ...te.panel.applet.CommandAppletFactory.service.in | 3 + .../org.mate.panel.applet.command.gschema.xml.in | 24 + command/ma-command.c | 417 ----------------- command/ma-command.h | 43 -- ...s.CommandApplet.mate-panel-applet.desktop.in.in | 11 - ...te.panel.applet.CommandAppletFactory.service.in | 3 - .../org.mate.panel.applet.command.gschema.xml.in | 24 - command/src/Makefile.am | 44 ++ command/src/command.c | 494 +++++++++++++++++++++ command/src/ma-command.c | 417 +++++++++++++++++ command/src/ma-command.h | 43 ++ configure.ac | 4 +- po/POTFILES.in | 8 +- 21 files changed, 1283 insertions(+), 1254 deletions(-) delete mode 100644 command/command-preferences.ui delete mode 100644 command/command-resources.gresource.xml delete mode 100644 command/command.c create mode 100644 command/data/Makefile.am create mode 100644 command/data/command-preferences.ui create mode 100644 command/data/command-resources.gresource.xml create mode 100644 command/data/org.mate.applets.CommandApplet.mate-panel-applet.desktop.in.in create mode 100644 command/data/org.mate.panel.applet.CommandAppletFactory.service.in create mode 100644 command/data/org.mate.panel.applet.command.gschema.xml.in delete mode 100644 command/ma-command.c delete mode 100644 command/ma-command.h delete mode 100644 command/org.mate.applets.CommandApplet.mate-panel-applet.desktop.in.in delete mode 100644 command/org.mate.panel.applet.CommandAppletFactory.service.in delete mode 100644 command/org.mate.panel.applet.command.gschema.xml.in create mode 100644 command/src/Makefile.am create mode 100644 command/src/command.c create mode 100644 command/src/ma-command.c create mode 100644 command/src/ma-command.h diff --git a/command/Makefile.am b/command/Makefile.am index 81debe28..5d55ad08 100644 --- a/command/Makefile.am +++ b/command/Makefile.am @@ -1,62 +1,3 @@ -AM_CPPFLAGS = \ - $(MATE_APPLETS4_CFLAGS) \ - -I$(srcdir) \ - $(DISABLE_DEPRECATED_CFLAGS) - -APPLET_LOCATION = $(libexecdir)/command-applet - -libexec_PROGRAMS = command-applet - -BUILT_SOURCES = command-resources.c command-resources.h -nodist_command_applet_SOURCES = $(BUILT_SOURCES) -command_applet_SOURCES = command.c ma-command.c ma-command.h -command_applet_LDADD = $(MATE_APPLETS4_LIBS) -command_applet_CFLAGS = $(WARN_CFLAGS) - -command-resources.c: command-resources.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(srcdir) --generate-dependencies $(srcdir)/command-resources.gresource.xml) - $(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir) --generate --c-name command $< - -command-resources.h: command-resources.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(srcdir) --generate-dependencies $(srcdir)/command-resources.gresource.xml) - $(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir) --generate --c-name command $< - -appletsdir = $(datadir)/mate-panel/applets -applets_in_files = org.mate.applets.CommandApplet.mate-panel-applet.desktop.in -applets_DATA = $(applets_in_files:.mate-panel-applet.desktop.in=.mate-panel-applet) - -$(applets_in_files): $(applets_in_files).in Makefile - $(AM_V_GEN)sed \ - -e "s|\@LOCATION\@|$(APPLET_LOCATION)|" \ - $< > $@ - -$(applets_DATA): $(applets_in_files) Makefile - $(AM_V_GEN) $(MSGFMT) --desktop --keyword=Name --keyword=Description --template $< -d $(top_srcdir)/po -o $@ - -servicedir = $(datadir)/dbus-1/services -service_in_files = org.mate.panel.applet.CommandAppletFactory.service.in -service_DATA = $(service_in_files:.service.in=.service) - -org.mate.panel.applet.CommandAppletFactory.service: $(service_in_files) - $(AM_V_GEN)sed \ - -e "s|\@LOCATION\@|$(APPLET_LOCATION)|" \ - $< > $@ - -command_gschema_in_files = org.mate.panel.applet.command.gschema.xml.in -gsettings_SCHEMAS = $(command_gschema_in_files:.xml.in=.xml) -@GSETTINGS_RULES@ - -EXTRA_DIST = \ - $(applets_in_files).in \ - $(service_in_files) \ - $(command_gschema_in_files) \ - command-preferences.ui \ - command-resources.gresource.xml - -CLEANFILES = \ - $(BUILT_SOURCES) \ - $(applets_DATA) \ - $(applets_in_files) \ - $(service_DATA) \ - $(gsettings_SCHEMAS) \ - *.gschema.valid +SUBDIRS = data src -include $(top_srcdir)/git.mk diff --git a/command/command-preferences.ui b/command/command-preferences.ui deleted file mode 100644 index 9a2d5435..00000000 --- a/command/command-preferences.ui +++ /dev/null @@ -1,191 +0,0 @@ - - - - - - True - False - window-close - - - 1 - 86400 - 1 - 10 - - - 1 - 100 - 1 - 10 - - - False - 12 - Command Applet Preferences - utilities-terminal - dialog - - - False - vertical - 2 - - - False - end - - - _Close - True - True - True - image1 - True - True - - - True - True - 0 - - - - - False - False - 0 - - - - - True - False - vertical - 6 - - - True - False - 6 - 12 - - - True - False - start - C_ommand: - True - command_entry - - - 0 - 0 - - - - - True - False - start - _Interval (seconds): - True - interval_spinbutton - - - 0 - 1 - - - - - True - False - start - Maximum _width (chars): - True - width_spinbutton - - - 0 - 2 - - - - - True - True - True - - - - 1 - 0 - - - - - True - True - True - interval_adjustment - - - - 1 - 1 - - - - - True - True - True - width_adjustment - - - - 1 - 2 - - - - - False - True - 0 - - - - - _Show icon - True - True - False - start - True - True - - - False - True - 1 - - - - - False - True - 1 - - - - - - button1 - - - - - - diff --git a/command/command-resources.gresource.xml b/command/command-resources.gresource.xml deleted file mode 100644 index 72f8ac4c..00000000 --- a/command/command-resources.gresource.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - command-preferences.ui - - diff --git a/command/command.c b/command/command.c deleted file mode 100644 index f53bef43..00000000 --- a/command/command.c +++ /dev/null @@ -1,494 +0,0 @@ -/* command.c: - * - * Copyright (C) 2013-2014 Stefano Karapetsas - * - * This file is part of MATE Applets. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * Authors: - * Stefano Karapetsas - */ - -#include - -#include -#include -#include -#include -#include - -#include -#include -#include "ma-command.h" - -/* Applet constants */ -#define APPLET_ICON "utilities-terminal" -#define ERROR_OUTPUT "#" - -/* GSettings constants */ -#define COMMAND_SCHEMA "org.mate.panel.applet.command" -#define COMMAND_KEY "command" -#define INTERVAL_KEY "interval" -#define SHOW_ICON_KEY "show-icon" -#define WIDTH_KEY "width" - -/* GKeyFile constants */ -#define GK_COMMAND_GROUP "Command" -#define GK_COMMAND_OUTPUT "Output" -#define GK_COMMAND_ICON "Icon" - -#define GET_WIDGET(x) (GTK_WIDGET (gtk_builder_get_object (builder, (x)))) -#define GET_DIALOG(x) (GTK_DIALOG (gtk_builder_get_object (builder, (x)))) - -typedef struct -{ - MatePanelApplet *applet; - - GSettings *settings; - - GtkLabel *label; - GtkImage *image; - GtkBox *box; - MaCommand *command; - GCancellable *cancellable; - gboolean running; - - gchar *cmdline; - gint interval; - gint width; - - guint timeout_id; -} CommandApplet; - -static void command_about_callback (GtkAction *action, CommandApplet *command_applet); -static void command_settings_callback (GtkAction *action, CommandApplet *command_applet); -static gboolean command_execute (CommandApplet *command_applet); -static gboolean command_text_changed (GtkWidget *widget, GdkEvent *event, gpointer user_data); -static void interval_value_changed (GtkSpinButton *spin_button, gpointer user_data); -static void width_value_changed (GtkSpinButton *spin_button, gpointer user_data); -static void command_async_ready_callback (GObject *source_object, GAsyncResult *res, gpointer user_data); -static gboolean timeout_callback (CommandApplet *command_applet); - -static const GtkActionEntry applet_menu_actions [] = { - { "Preferences", "document-properties", N_("_Preferences"), NULL, NULL, G_CALLBACK (command_settings_callback) }, - { "About", "help-about", N_("_About"), NULL, NULL, G_CALLBACK (command_about_callback) } -}; - -static char *ui = "" - ""; - -static void -command_applet_destroy (MatePanelApplet *applet_widget, CommandApplet *command_applet) -{ - g_assert (command_applet); - - if (command_applet->timeout_id != 0) - { - g_source_remove (command_applet->timeout_id); - command_applet->timeout_id = 0; - } - - if (command_applet->cmdline != NULL) - { - g_free (command_applet->cmdline); - command_applet->cmdline = NULL; - } - - if (command_applet->command != NULL) - { - g_object_unref (command_applet->command); - } - - g_object_unref (command_applet->settings); -} - -/* Show the about dialog */ -static void -command_about_callback (GtkAction *action, CommandApplet *command_applet) -{ - const char* authors[] = { "Stefano Karapetsas ", NULL }; - - gtk_show_about_dialog(NULL, - "title", _("About Command Applet"), - "version", VERSION, - "copyright", _("Copyright \xc2\xa9 2013-2014 Stefano Karapetsas\n" - "Copyright \xc2\xa9 2015-2020 MATE developers"), - "authors", authors, - "comments", _("Shows the output of a command"), - "translator-credits", _("translator-credits"), - "logo-icon-name", APPLET_ICON, - NULL ); -} - -static gboolean -command_text_changed (GtkWidget *widget, GdkEvent *event, gpointer user_data) -{ - const gchar *text; - CommandApplet *command_applet; - - command_applet = (CommandApplet*) user_data; - text = gtk_entry_get_text (GTK_ENTRY(widget)); - if (g_strcmp0(command_applet->cmdline, text) == 0) { - return TRUE; - } - - if (strlen (text) == 0) { - gtk_label_set_text (command_applet->label, ERROR_OUTPUT); - return TRUE; - } - - g_settings_set_string (command_applet->settings, COMMAND_KEY, text); - return TRUE; -} - -static void interval_value_changed (GtkSpinButton *spin_button, gpointer user_data) -{ - gint value; - CommandApplet *command_applet; - - command_applet = (CommandApplet*) user_data; - value = gtk_spin_button_get_value_as_int (spin_button); - if (command_applet->interval == value) { - return; - } - - g_settings_set_int (command_applet->settings, INTERVAL_KEY, value); -} - -static void width_value_changed (GtkSpinButton *spin_button, gpointer user_data) -{ - gint value; - CommandApplet *command_applet; - - command_applet = (CommandApplet*) user_data; - value = gtk_spin_button_get_value_as_int (spin_button); - if (command_applet->width == value) { - return; - } - - g_settings_set_int (command_applet->settings, WIDTH_KEY, value); -} - -/* Show the preferences dialog */ -static void -command_settings_callback (GtkAction *action, CommandApplet *command_applet) -{ - GtkDialog *dialog; - GtkBuilder *builder; - - builder = gtk_builder_new_from_resource ("/org/mate/mate-applets/command/command-preferences.ui"); - - dialog = GET_DIALOG ("preferences_dialog"); - - g_settings_bind (command_applet->settings, COMMAND_KEY, GET_WIDGET ("command_entry"), "text", G_SETTINGS_BIND_GET_NO_CHANGES); - g_settings_bind (command_applet->settings, INTERVAL_KEY, GET_WIDGET ("interval_spinbutton"), "value", G_SETTINGS_BIND_GET_NO_CHANGES); - g_settings_bind (command_applet->settings, WIDTH_KEY, GET_WIDGET ("width_spinbutton"), "value", G_SETTINGS_BIND_GET_NO_CHANGES); - g_settings_bind (command_applet->settings, SHOW_ICON_KEY, GET_WIDGET ("show_icon_check"), "active", G_SETTINGS_BIND_DEFAULT); - - /* signals */ - gtk_builder_add_callback_symbols (builder, - "on_command_entry_focus_out_event", G_CALLBACK (command_text_changed), - "on_interval_spinbutton_value_changed", G_CALLBACK (interval_value_changed), - "on_width_spinbutton_value_changed", G_CALLBACK (width_value_changed), - NULL); - gtk_builder_connect_signals (builder, command_applet); - g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog); - - g_object_unref (builder); - - gtk_widget_show_all (GTK_WIDGET (dialog)); -} - -/* GSettings signal callbacks */ -static void -settings_command_changed (GSettings *settings, gchar *key, CommandApplet *command_applet) -{ - GError *error = NULL; - gchar *cmdline; - gchar **argv; - - cmdline = g_settings_get_string (command_applet->settings, COMMAND_KEY); - if (strlen (cmdline) == 0 || g_strcmp0(command_applet->cmdline, cmdline) == 0) - return; - - if (!g_shell_parse_argv (cmdline, NULL, &argv, &error)) - { - gtk_label_set_text (command_applet->label, ERROR_OUTPUT); - g_clear_error (&error); - return; - } - g_strfreev(argv); - - if (command_applet->cmdline) - g_free (command_applet->cmdline); - command_applet->cmdline = cmdline; - - command_execute (command_applet); -} - -static void -settings_width_changed (GSettings *settings, gchar *key, CommandApplet *command_applet) -{ - gint width; - - width = g_settings_get_int (command_applet->settings, WIDTH_KEY); - - if (command_applet->width != width) { - command_applet->width = width; - } -} - -static void -settings_interval_changed (GSettings *settings, gchar *key, CommandApplet *command_applet) -{ - gint interval; - - interval = g_settings_get_int (command_applet->settings, INTERVAL_KEY); - - /* minimum interval */ - if (interval < 1) - interval = 1; - - if (command_applet->interval == interval) { - return; - } - command_applet->interval = interval; - - command_execute (command_applet); -} - -static void -process_command_output (CommandApplet *command_applet, gchar *output) -{ - gtk_widget_set_tooltip_text (GTK_WIDGET (command_applet->label), command_applet->cmdline); - - if ((output == NULL) || (output[0] == '\0')) - { - gtk_label_set_text (command_applet->label, ERROR_OUTPUT); - return; - } - - /* check if output is a custom GKeyFile */ - if (g_str_has_prefix (output, "[Command]")) - { - GKeyFile *file = g_key_file_new (); - if (g_key_file_load_from_data (file, output, -1, G_KEY_FILE_NONE, NULL)) - { - gchar *goutput = g_key_file_get_string (file, GK_COMMAND_GROUP, GK_COMMAND_OUTPUT, NULL); - gchar *icon = g_key_file_get_string (file, GK_COMMAND_GROUP, GK_COMMAND_ICON, NULL); - - if (goutput) - { - gtk_label_set_use_markup (command_applet->label, TRUE); - gtk_label_set_markup (command_applet->label, goutput); - } - - if (icon) - gtk_image_set_from_icon_name (command_applet->image, icon, GTK_ICON_SIZE_LARGE_TOOLBAR); - - g_free (goutput); - g_free (icon); - } - else - gtk_label_set_text (command_applet->label, ERROR_OUTPUT); - - g_key_file_free (file); - } - else - { - /* Remove leading and trailing whitespace */ - g_strstrip (output); - - /* check output length */ - if (g_utf8_strlen (output, strlen(output)) > command_applet->width) - { - *g_utf8_offset_to_pointer(output, command_applet->width) = '\0'; - } - - gtk_label_set_text (command_applet->label, output); - } -} - -static void command_async_ready_callback (GObject *source_object, GAsyncResult *res, gpointer user_data) -{ - gchar *output; - GError *error = NULL; - CommandApplet *command_applet; - - command_applet = (CommandApplet*) user_data; - - output = ma_command_run_finish (command_applet->command, res, &error); - if (error == NULL) { - process_command_output (command_applet, output); - } else { - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED)) { - gtk_label_set_text (command_applet->label, ERROR_OUTPUT); - } - g_error_free (error); - } - g_free (output); - command_applet->running = FALSE; -} - -static gboolean timeout_callback (CommandApplet *command_applet) -{ - /* command is empty, wait for next timer execution */ - if (strlen (command_applet->cmdline) == 0) { - return G_SOURCE_CONTINUE; - } - - /* command running, wait for next timer execution */ - if (command_applet->running) { - return G_SOURCE_CONTINUE; - } else { - gchar **argv; - GError *error = NULL; - if (!g_shell_parse_argv (command_applet->cmdline, NULL, &argv, &error)) { - gtk_label_set_text (command_applet->label, ERROR_OUTPUT); - g_clear_error (&error); - return G_SOURCE_CONTINUE; - } - g_strfreev(argv); - command_execute (command_applet); - return G_SOURCE_REMOVE; - } -} - -static gboolean -command_execute (CommandApplet *command_applet) -{ - /* stop current timer */ - if (command_applet->timeout_id != 0) - { - g_source_remove (command_applet->timeout_id); - command_applet->timeout_id = 0; - } - - if (command_applet->running) { - g_cancellable_cancel (command_applet->cancellable); - } - - g_object_set (G_OBJECT(command_applet->command), "command", command_applet->cmdline, NULL); - ma_command_run_async (command_applet->command, - command_applet->cancellable, - command_async_ready_callback, - command_applet); - if (!command_applet->running) { - command_applet->running = TRUE; - } - - if (g_cancellable_is_cancelled (command_applet->cancellable)) { - g_cancellable_reset (command_applet->cancellable); - } - - command_applet->timeout_id = g_timeout_add_seconds (command_applet->interval, - (GSourceFunc) timeout_callback, - command_applet); - return G_SOURCE_CONTINUE; -} - -static gboolean -command_applet_fill (MatePanelApplet* applet) -{ - CommandApplet *command_applet; - - g_set_application_name (_("Command Applet")); - gtk_window_set_default_icon_name (APPLET_ICON); - - mate_panel_applet_set_flags (applet, MATE_PANEL_APPLET_EXPAND_MINOR); - mate_panel_applet_set_background_widget (applet, GTK_WIDGET (applet)); - - command_applet = g_malloc0(sizeof(CommandApplet)); - command_applet->applet = applet; - command_applet->settings = mate_panel_applet_settings_new (applet, COMMAND_SCHEMA); - - command_applet->interval = g_settings_get_int (command_applet->settings, INTERVAL_KEY); - command_applet->cmdline = g_settings_get_string (command_applet->settings, COMMAND_KEY); - command_applet->width = g_settings_get_int (command_applet->settings, WIDTH_KEY); - command_applet->command = ma_command_new(command_applet->cmdline, NULL); - command_applet->cancellable = g_cancellable_new (); - - command_applet->box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0)); - command_applet->image = GTK_IMAGE (gtk_image_new_from_icon_name (APPLET_ICON, GTK_ICON_SIZE_LARGE_TOOLBAR)); - command_applet->label = GTK_LABEL (gtk_label_new (ERROR_OUTPUT)); - command_applet->timeout_id = 0; - - /* we add the Gtk label into the applet */ - gtk_box_pack_start (command_applet->box, - GTK_WIDGET (command_applet->image), - TRUE, TRUE, 0); - gtk_box_pack_start (command_applet->box, - GTK_WIDGET (command_applet->label), - TRUE, TRUE, 0); - - gtk_container_add (GTK_CONTAINER (applet), - GTK_WIDGET (command_applet->box)); - - gtk_widget_show_all (GTK_WIDGET (command_applet->applet)); - - g_signal_connect(G_OBJECT (command_applet->applet), "destroy", - G_CALLBACK (command_applet_destroy), - command_applet); - - /* GSettings signals */ - g_signal_connect(command_applet->settings, - "changed::" COMMAND_KEY, - G_CALLBACK (settings_command_changed), - command_applet); - g_signal_connect(command_applet->settings, - "changed::" INTERVAL_KEY, - G_CALLBACK (settings_interval_changed), - command_applet); - g_signal_connect(command_applet->settings, - "changed::" WIDTH_KEY, - G_CALLBACK (settings_width_changed), - command_applet); - g_settings_bind (command_applet->settings, - SHOW_ICON_KEY, - command_applet->image, - "visible", - G_SETTINGS_BIND_DEFAULT); - - /* set up context menu */ - GtkActionGroup *action_group = gtk_action_group_new ("Command Applet Actions"); - gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE); - gtk_action_group_add_actions (action_group, applet_menu_actions, - G_N_ELEMENTS (applet_menu_actions), command_applet); - mate_panel_applet_setup_menu (command_applet->applet, ui, action_group); - - /* first command execution */ - command_execute (command_applet); - return TRUE; -} - -/* this function, called by mate-panel, will create the applet */ -static gboolean -command_factory (MatePanelApplet* applet, const char* iid, gpointer data) -{ - gboolean retval = FALSE; - - if (!g_strcmp0 (iid, "CommandApplet")) - retval = command_applet_fill (applet); - - return retval; -} - -/* needed by mate-panel applet library */ -MATE_PANEL_APPLET_OUT_PROCESS_FACTORY("CommandAppletFactory", - PANEL_TYPE_APPLET, - "Command applet", - command_factory, - NULL) diff --git a/command/data/Makefile.am b/command/data/Makefile.am new file mode 100644 index 00000000..b94e9523 --- /dev/null +++ b/command/data/Makefile.am @@ -0,0 +1,42 @@ +APPLET_LOCATION = $(libexecdir)/command-applet + +appletsdir = $(datadir)/mate-panel/applets +applets_in_files = org.mate.applets.CommandApplet.mate-panel-applet.desktop.in +applets_DATA = $(applets_in_files:.mate-panel-applet.desktop.in=.mate-panel-applet) + +$(applets_in_files): $(applets_in_files).in Makefile + $(AM_V_GEN)sed \ + -e "s|\@LOCATION\@|$(APPLET_LOCATION)|" \ + $< > $@ + +$(applets_DATA): $(applets_in_files) Makefile + $(AM_V_GEN) $(MSGFMT) --desktop --keyword=Name --keyword=Description --template $< -d $(top_srcdir)/po -o $@ + +servicedir = $(datadir)/dbus-1/services +service_in_files = org.mate.panel.applet.CommandAppletFactory.service.in +service_DATA = $(service_in_files:.service.in=.service) + +org.mate.panel.applet.CommandAppletFactory.service: $(service_in_files) + $(AM_V_GEN)sed \ + -e "s|\@LOCATION\@|$(APPLET_LOCATION)|" \ + $< > $@ + +command_gschema_in_files = org.mate.panel.applet.command.gschema.xml.in +gsettings_SCHEMAS = $(command_gschema_in_files:.xml.in=.xml) +@GSETTINGS_RULES@ + +EXTRA_DIST = \ + $(applets_in_files).in \ + $(service_in_files) \ + $(command_gschema_in_files) \ + command-preferences.ui \ + command-resources.gresource.xml + +CLEANFILES = \ + $(applets_DATA) \ + $(applets_in_files) \ + $(service_DATA) \ + $(gsettings_SCHEMAS) \ + *.gschema.valid + +-include $(top_srcdir)/git.mk diff --git a/command/data/command-preferences.ui b/command/data/command-preferences.ui new file mode 100644 index 00000000..9a2d5435 --- /dev/null +++ b/command/data/command-preferences.ui @@ -0,0 +1,191 @@ + + + + + + True + False + window-close + + + 1 + 86400 + 1 + 10 + + + 1 + 100 + 1 + 10 + + + False + 12 + Command Applet Preferences + utilities-terminal + dialog + + + False + vertical + 2 + + + False + end + + + _Close + True + True + True + image1 + True + True + + + True + True + 0 + + + + + False + False + 0 + + + + + True + False + vertical + 6 + + + True + False + 6 + 12 + + + True + False + start + C_ommand: + True + command_entry + + + 0 + 0 + + + + + True + False + start + _Interval (seconds): + True + interval_spinbutton + + + 0 + 1 + + + + + True + False + start + Maximum _width (chars): + True + width_spinbutton + + + 0 + 2 + + + + + True + True + True + + + + 1 + 0 + + + + + True + True + True + interval_adjustment + + + + 1 + 1 + + + + + True + True + True + width_adjustment + + + + 1 + 2 + + + + + False + True + 0 + + + + + _Show icon + True + True + False + start + True + True + + + False + True + 1 + + + + + False + True + 1 + + + + + + button1 + + + + + + diff --git a/command/data/command-resources.gresource.xml b/command/data/command-resources.gresource.xml new file mode 100644 index 00000000..72f8ac4c --- /dev/null +++ b/command/data/command-resources.gresource.xml @@ -0,0 +1,6 @@ + + + + command-preferences.ui + + diff --git a/command/data/org.mate.applets.CommandApplet.mate-panel-applet.desktop.in.in b/command/data/org.mate.applets.CommandApplet.mate-panel-applet.desktop.in.in new file mode 100644 index 00000000..04f76f4a --- /dev/null +++ b/command/data/org.mate.applets.CommandApplet.mate-panel-applet.desktop.in.in @@ -0,0 +1,11 @@ +[Applet Factory] +Id=CommandAppletFactory +Location=@LOCATION@ +Name=Command Factory +Description=Command Factory + +[CommandApplet] +Name=Command +Description=Shows the output of a command +# Translators: Do NOT translate or transliterate this text (this is an icon file name)! +Icon=utilities-terminal diff --git a/command/data/org.mate.panel.applet.CommandAppletFactory.service.in b/command/data/org.mate.panel.applet.CommandAppletFactory.service.in new file mode 100644 index 00000000..a4e04846 --- /dev/null +++ b/command/data/org.mate.panel.applet.CommandAppletFactory.service.in @@ -0,0 +1,3 @@ +[D-BUS Service] +Name=org.mate.panel.applet.CommandAppletFactory +Exec=@LOCATION@ diff --git a/command/data/org.mate.panel.applet.command.gschema.xml.in b/command/data/org.mate.panel.applet.command.gschema.xml.in new file mode 100644 index 00000000..b352a21b --- /dev/null +++ b/command/data/org.mate.panel.applet.command.gschema.xml.in @@ -0,0 +1,24 @@ + + + + 'date +%T' + Command to execute + Command/script to execute to get the output + + + 1 + Interval for the command + Interval to execute the command (in seconds) + + + 60 + Width of output + Number of characters to display + + + false + Show icon + If applet icon is shown or not + + + diff --git a/command/ma-command.c b/command/ma-command.c deleted file mode 100644 index da0e8795..00000000 --- a/command/ma-command.c +++ /dev/null @@ -1,417 +0,0 @@ -/* - * Copyright (C) 2018 Alberts Muktupāvels - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 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 . - */ - -#include -#include "ma-command.h" - -#define BUFFER_SIZE 64 - -struct _MaCommand -{ - GObject parent; - - gchar *command; - gchar **argv; -}; - -typedef struct -{ - GPid pid; - - GIOChannel *channel; - - GString *input; - - guint io_watch_id; - guint child_watch_id; -} CommandData; - -enum -{ - PROP_0, - - PROP_COMMAND, - - LAST_PROP -}; - -static GParamSpec *command_properties[LAST_PROP] = { NULL }; - -static void initable_iface_init (GInitableIface *iface); - -G_DEFINE_TYPE_WITH_CODE (MaCommand, ma_command, G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, - initable_iface_init)) - -static gboolean -read_cb (GIOChannel *source, - GIOCondition condition, - gpointer user_data) -{ - GTask *task; - CommandData *data; - gchar buffer[BUFFER_SIZE]; - gsize bytes_read; - GError *error; - GIOStatus status; - - task = (GTask *) user_data; - data = g_task_get_task_data (task); - - if (g_task_return_error_if_cancelled (task)) - { - g_object_unref (task); - - data->io_watch_id = 0; - - return G_SOURCE_REMOVE; - } - - error = NULL; - status = g_io_channel_read_chars (source, buffer, BUFFER_SIZE, - &bytes_read, &error); - - if (status == G_IO_STATUS_AGAIN) - { - g_clear_error (&error); - - return G_SOURCE_CONTINUE; - } - else if (status != G_IO_STATUS_NORMAL) - { - if (error != NULL) - { - g_task_return_error (task, error); - g_object_unref (task); - } - - data->io_watch_id = 0; - - return G_SOURCE_REMOVE; - } - - g_string_append_len (data->input, buffer, bytes_read); - - return G_SOURCE_CONTINUE; -} - -static void -child_watch_cb (GPid pid, - gint status, - gpointer user_data) -{ - GTask *task; - CommandData *data; - - task = (GTask *) user_data; - data = g_task_get_task_data (task); - - g_task_return_pointer (task, g_strdup (data->input->str), g_free); - g_object_unref (task); -} - -static void -cancelled_cb (GCancellable *cancellable, - gpointer user_data) -{ - GTask *task; - - task = G_TASK (user_data); - - g_object_unref (task); -} - -static void -command_data_free (gpointer user_data) -{ - CommandData *data; - - data = (CommandData *) user_data; - - if (data->pid != 0) - { - g_spawn_close_pid (data->pid); - data->pid = 0; - } - - if (data->channel != NULL) - { - g_io_channel_unref (data->channel); - data->channel = NULL; - } - - if (data->input != NULL) - { - g_string_free (data->input, TRUE); - data->input = NULL; - } - - if (data->io_watch_id != 0) - { - g_source_remove (data->io_watch_id); - data->io_watch_id = 0; - } - - if (data->child_watch_id != 0) - { - g_source_remove (data->child_watch_id); - data->child_watch_id = 0; - } - - g_free (data); -} - -static gboolean -ma_command_initable_init (GInitable *initable, - GCancellable *cancellable, - GError **error) -{ - MaCommand *command; - - command = MA_COMMAND (initable); - - if (command->command == NULL || *command->command == '\0') - { - g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, - "Empty command"); - - return FALSE; - } - - return TRUE; -} - -static void -initable_iface_init (GInitableIface *iface) -{ - iface->init = ma_command_initable_init; -} - -static void -ma_command_finalize (GObject *object) -{ - MaCommand *command; - - command = MA_COMMAND (object); - - g_clear_pointer (&command->command, g_free); - g_clear_pointer (&command->argv, g_strfreev); - - G_OBJECT_CLASS (ma_command_parent_class)->finalize (object); -} - -static void -ma_command_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) -{ - MaCommand *command; - - command = MA_COMMAND (object); - - switch (property_id) - { - case PROP_COMMAND: - //g_assert (command->command == NULL); - command->command = g_value_dup_string (value); - if (command->argv && *command->argv != NULL) { - g_strfreev(command->argv); - } - g_shell_parse_argv (command->command, NULL, &command->argv, NULL); - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - break; - } -} - -static void -ma_command_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) -{ - MaCommand *command; - - command = MA_COMMAND (object); - - switch (prop_id) - { - case PROP_COMMAND: - g_value_set_string (value, command->command); - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -install_properties (GObjectClass *object_class) -{ - command_properties[PROP_COMMAND] = - g_param_spec_string ("command", "command", "command", - NULL, - G_PARAM_READWRITE); - - g_object_class_install_properties (object_class, LAST_PROP, - command_properties); -} - -static void -ma_command_class_init (MaCommandClass *command_class) -{ - GObjectClass *object_class; - - object_class = G_OBJECT_CLASS (command_class); - - object_class->finalize = ma_command_finalize; - object_class->set_property = ma_command_set_property; - object_class->get_property = ma_command_get_property; - - install_properties (object_class); -} - -static void -ma_command_init (MaCommand *command) -{ -} - -/** - * ma_command_new: - * @command: a command - * @error: (nullable): return location for an error, or %NULL - * - * Creates a new #MaCommand. - * - * Returns: (nullable): a newly allocated #MaCommand - */ -MaCommand * -ma_command_new (const gchar *command, - GError **error) -{ - return g_initable_new (MA_TYPE_COMMAND, NULL, error, - "command", command, - NULL); -} - -/** - * ma_command_run_async: - * @command: a #MaCommand - * @cancellable: (nullable): a #GCancellable or %NULL - * @callback: a #GAsyncReadyCallback to call when the request is satisfied - * @user_data: the data to pass to @callback - * - * Request an asynchronous read of output from command that was passed - * to ma_command_new(). - */ -void -ma_command_run_async (MaCommand *command, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data) -{ - GTask *task; - CommandData *data; - GSpawnFlags spawn_flags; - gint command_stdout; - GError *error; - GIOChannel *channel; - GIOStatus status; - GIOCondition condition; - - g_return_if_fail (MA_IS_COMMAND (command)); - g_return_if_fail (callback != NULL); - - task = g_task_new (command, cancellable, callback, user_data); - g_task_set_source_tag (task, ma_command_run_async); - - if (cancellable) - { - g_signal_connect_object (cancellable, "cancelled", - G_CALLBACK (cancelled_cb), task, - G_CONNECT_AFTER); - } - - data = g_new0 (CommandData, 1); - g_task_set_task_data (task, data, command_data_free); - - spawn_flags = G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD; - error = NULL; - - if (!g_spawn_async_with_pipes (NULL, command->argv, NULL, spawn_flags, - NULL, NULL, &data->pid, NULL, &command_stdout, - NULL, &error)) - { - g_task_return_error (task, error); - g_object_unref (task); - - return; - } - - channel = data->channel = g_io_channel_unix_new (command_stdout); - g_io_channel_set_close_on_unref (channel, TRUE); - - g_assert (error == NULL); - status = g_io_channel_set_encoding (channel, NULL, &error); - - if (status != G_IO_STATUS_NORMAL) - { - g_task_return_error (task, error); - g_object_unref (task); - - return; - } - - g_assert (error == NULL); - status = g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, &error); - - if (status != G_IO_STATUS_NORMAL) - { - g_task_return_error (task, error); - g_object_unref (task); - - return; - } - - data->input = g_string_new (NULL); - - condition = G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP; - data->io_watch_id = g_io_add_watch (channel, condition, read_cb, task); - - data->child_watch_id = g_child_watch_add (data->pid, child_watch_cb, task); -} - -/** - * ma_command_run_finish: - * @command: a #MaCommand - * @result: a #GAsyncResult - * @error: (nullable): return location for an error, or %NULL - * - * Finishes an operation started with ma_command_run_async(). - * - * Returns: %NULL if @error is set, otherwise output from command - */ -gchar * -ma_command_run_finish (MaCommand *command, - GAsyncResult *result, - GError **error) -{ - g_return_val_if_fail (MA_IS_COMMAND (command), NULL); - g_return_val_if_fail (g_task_is_valid (result, command), NULL); - g_return_val_if_fail (error == NULL || *error == NULL, NULL); - - return g_task_propagate_pointer (G_TASK (result), error); -} diff --git a/command/ma-command.h b/command/ma-command.h deleted file mode 100644 index 8fb51ab3..00000000 --- a/command/ma-command.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2018 Alberts Muktupāvels - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 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 . - */ - -#ifndef MA_COMMAND_H -#define MA_COMMAND_H - -#include -#include - -G_BEGIN_DECLS - -#define MA_TYPE_COMMAND (ma_command_get_type ()) -G_DECLARE_FINAL_TYPE (MaCommand, ma_command, MA, COMMAND, GObject) - -MaCommand *ma_command_new (const gchar *command, - GError **error); - -void ma_command_run_async (MaCommand *command, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gchar *ma_command_run_finish (MaCommand *command, - GAsyncResult *result, - GError **error); - -G_END_DECLS - -#endif diff --git a/command/org.mate.applets.CommandApplet.mate-panel-applet.desktop.in.in b/command/org.mate.applets.CommandApplet.mate-panel-applet.desktop.in.in deleted file mode 100644 index 04f76f4a..00000000 --- a/command/org.mate.applets.CommandApplet.mate-panel-applet.desktop.in.in +++ /dev/null @@ -1,11 +0,0 @@ -[Applet Factory] -Id=CommandAppletFactory -Location=@LOCATION@ -Name=Command Factory -Description=Command Factory - -[CommandApplet] -Name=Command -Description=Shows the output of a command -# Translators: Do NOT translate or transliterate this text (this is an icon file name)! -Icon=utilities-terminal diff --git a/command/org.mate.panel.applet.CommandAppletFactory.service.in b/command/org.mate.panel.applet.CommandAppletFactory.service.in deleted file mode 100644 index a4e04846..00000000 --- a/command/org.mate.panel.applet.CommandAppletFactory.service.in +++ /dev/null @@ -1,3 +0,0 @@ -[D-BUS Service] -Name=org.mate.panel.applet.CommandAppletFactory -Exec=@LOCATION@ diff --git a/command/org.mate.panel.applet.command.gschema.xml.in b/command/org.mate.panel.applet.command.gschema.xml.in deleted file mode 100644 index b352a21b..00000000 --- a/command/org.mate.panel.applet.command.gschema.xml.in +++ /dev/null @@ -1,24 +0,0 @@ - - - - 'date +%T' - Command to execute - Command/script to execute to get the output - - - 1 - Interval for the command - Interval to execute the command (in seconds) - - - 60 - Width of output - Number of characters to display - - - false - Show icon - If applet icon is shown or not - - - diff --git a/command/src/Makefile.am b/command/src/Makefile.am new file mode 100644 index 00000000..d0d2065a --- /dev/null +++ b/command/src/Makefile.am @@ -0,0 +1,44 @@ +NULL = + +AM_CPPFLAGS = \ + $(MATE_APPLETS4_CFLAGS) \ + -I$(srcdir) \ + $(DISABLE_DEPRECATED_CFLAGS) \ + $(NULL) + +libexec_PROGRAMS = command-applet + +BUILT_SOURCES = \ + command-resources.c \ + command-resources.h \ + $(NULL) + +nodist_command_applet_SOURCES = \ + $(BUILT_SOURCES) \ + $(NULL) + +command_applet_SOURCES = \ + command.c \ + ma-command.c \ + ma-command.h \ + $(NULL) + +command_applet_LDADD = \ + $(MATE_APPLETS4_LIBS) \ + $(NULL) + +command_applet_CFLAGS = \ + $(WARN_CFLAGS) \ + $(NULL) + +command-resources.c: $(srcdir)/../data/command-resources.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(srcdir)/../data --generate-dependencies $(srcdir)/../data/command-resources.gresource.xml) + $(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir)/../data --generate --c-name command $< + +command-resources.h: $(srcdir)/../data/command-resources.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(srcdir)/../data --generate-dependencies $(srcdir)/../data/command-resources.gresource.xml) + $(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir)/../data --generate --c-name command $< + +CLEANFILES = \ + $(BUILT_SOURCES) \ + $(NULL) + +-include $(top_srcdir)/git.mk diff --git a/command/src/command.c b/command/src/command.c new file mode 100644 index 00000000..f53bef43 --- /dev/null +++ b/command/src/command.c @@ -0,0 +1,494 @@ +/* command.c: + * + * Copyright (C) 2013-2014 Stefano Karapetsas + * + * This file is part of MATE Applets. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Stefano Karapetsas + */ + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include "ma-command.h" + +/* Applet constants */ +#define APPLET_ICON "utilities-terminal" +#define ERROR_OUTPUT "#" + +/* GSettings constants */ +#define COMMAND_SCHEMA "org.mate.panel.applet.command" +#define COMMAND_KEY "command" +#define INTERVAL_KEY "interval" +#define SHOW_ICON_KEY "show-icon" +#define WIDTH_KEY "width" + +/* GKeyFile constants */ +#define GK_COMMAND_GROUP "Command" +#define GK_COMMAND_OUTPUT "Output" +#define GK_COMMAND_ICON "Icon" + +#define GET_WIDGET(x) (GTK_WIDGET (gtk_builder_get_object (builder, (x)))) +#define GET_DIALOG(x) (GTK_DIALOG (gtk_builder_get_object (builder, (x)))) + +typedef struct +{ + MatePanelApplet *applet; + + GSettings *settings; + + GtkLabel *label; + GtkImage *image; + GtkBox *box; + MaCommand *command; + GCancellable *cancellable; + gboolean running; + + gchar *cmdline; + gint interval; + gint width; + + guint timeout_id; +} CommandApplet; + +static void command_about_callback (GtkAction *action, CommandApplet *command_applet); +static void command_settings_callback (GtkAction *action, CommandApplet *command_applet); +static gboolean command_execute (CommandApplet *command_applet); +static gboolean command_text_changed (GtkWidget *widget, GdkEvent *event, gpointer user_data); +static void interval_value_changed (GtkSpinButton *spin_button, gpointer user_data); +static void width_value_changed (GtkSpinButton *spin_button, gpointer user_data); +static void command_async_ready_callback (GObject *source_object, GAsyncResult *res, gpointer user_data); +static gboolean timeout_callback (CommandApplet *command_applet); + +static const GtkActionEntry applet_menu_actions [] = { + { "Preferences", "document-properties", N_("_Preferences"), NULL, NULL, G_CALLBACK (command_settings_callback) }, + { "About", "help-about", N_("_About"), NULL, NULL, G_CALLBACK (command_about_callback) } +}; + +static char *ui = "" + ""; + +static void +command_applet_destroy (MatePanelApplet *applet_widget, CommandApplet *command_applet) +{ + g_assert (command_applet); + + if (command_applet->timeout_id != 0) + { + g_source_remove (command_applet->timeout_id); + command_applet->timeout_id = 0; + } + + if (command_applet->cmdline != NULL) + { + g_free (command_applet->cmdline); + command_applet->cmdline = NULL; + } + + if (command_applet->command != NULL) + { + g_object_unref (command_applet->command); + } + + g_object_unref (command_applet->settings); +} + +/* Show the about dialog */ +static void +command_about_callback (GtkAction *action, CommandApplet *command_applet) +{ + const char* authors[] = { "Stefano Karapetsas ", NULL }; + + gtk_show_about_dialog(NULL, + "title", _("About Command Applet"), + "version", VERSION, + "copyright", _("Copyright \xc2\xa9 2013-2014 Stefano Karapetsas\n" + "Copyright \xc2\xa9 2015-2020 MATE developers"), + "authors", authors, + "comments", _("Shows the output of a command"), + "translator-credits", _("translator-credits"), + "logo-icon-name", APPLET_ICON, + NULL ); +} + +static gboolean +command_text_changed (GtkWidget *widget, GdkEvent *event, gpointer user_data) +{ + const gchar *text; + CommandApplet *command_applet; + + command_applet = (CommandApplet*) user_data; + text = gtk_entry_get_text (GTK_ENTRY(widget)); + if (g_strcmp0(command_applet->cmdline, text) == 0) { + return TRUE; + } + + if (strlen (text) == 0) { + gtk_label_set_text (command_applet->label, ERROR_OUTPUT); + return TRUE; + } + + g_settings_set_string (command_applet->settings, COMMAND_KEY, text); + return TRUE; +} + +static void interval_value_changed (GtkSpinButton *spin_button, gpointer user_data) +{ + gint value; + CommandApplet *command_applet; + + command_applet = (CommandApplet*) user_data; + value = gtk_spin_button_get_value_as_int (spin_button); + if (command_applet->interval == value) { + return; + } + + g_settings_set_int (command_applet->settings, INTERVAL_KEY, value); +} + +static void width_value_changed (GtkSpinButton *spin_button, gpointer user_data) +{ + gint value; + CommandApplet *command_applet; + + command_applet = (CommandApplet*) user_data; + value = gtk_spin_button_get_value_as_int (spin_button); + if (command_applet->width == value) { + return; + } + + g_settings_set_int (command_applet->settings, WIDTH_KEY, value); +} + +/* Show the preferences dialog */ +static void +command_settings_callback (GtkAction *action, CommandApplet *command_applet) +{ + GtkDialog *dialog; + GtkBuilder *builder; + + builder = gtk_builder_new_from_resource ("/org/mate/mate-applets/command/command-preferences.ui"); + + dialog = GET_DIALOG ("preferences_dialog"); + + g_settings_bind (command_applet->settings, COMMAND_KEY, GET_WIDGET ("command_entry"), "text", G_SETTINGS_BIND_GET_NO_CHANGES); + g_settings_bind (command_applet->settings, INTERVAL_KEY, GET_WIDGET ("interval_spinbutton"), "value", G_SETTINGS_BIND_GET_NO_CHANGES); + g_settings_bind (command_applet->settings, WIDTH_KEY, GET_WIDGET ("width_spinbutton"), "value", G_SETTINGS_BIND_GET_NO_CHANGES); + g_settings_bind (command_applet->settings, SHOW_ICON_KEY, GET_WIDGET ("show_icon_check"), "active", G_SETTINGS_BIND_DEFAULT); + + /* signals */ + gtk_builder_add_callback_symbols (builder, + "on_command_entry_focus_out_event", G_CALLBACK (command_text_changed), + "on_interval_spinbutton_value_changed", G_CALLBACK (interval_value_changed), + "on_width_spinbutton_value_changed", G_CALLBACK (width_value_changed), + NULL); + gtk_builder_connect_signals (builder, command_applet); + g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog); + + g_object_unref (builder); + + gtk_widget_show_all (GTK_WIDGET (dialog)); +} + +/* GSettings signal callbacks */ +static void +settings_command_changed (GSettings *settings, gchar *key, CommandApplet *command_applet) +{ + GError *error = NULL; + gchar *cmdline; + gchar **argv; + + cmdline = g_settings_get_string (command_applet->settings, COMMAND_KEY); + if (strlen (cmdline) == 0 || g_strcmp0(command_applet->cmdline, cmdline) == 0) + return; + + if (!g_shell_parse_argv (cmdline, NULL, &argv, &error)) + { + gtk_label_set_text (command_applet->label, ERROR_OUTPUT); + g_clear_error (&error); + return; + } + g_strfreev(argv); + + if (command_applet->cmdline) + g_free (command_applet->cmdline); + command_applet->cmdline = cmdline; + + command_execute (command_applet); +} + +static void +settings_width_changed (GSettings *settings, gchar *key, CommandApplet *command_applet) +{ + gint width; + + width = g_settings_get_int (command_applet->settings, WIDTH_KEY); + + if (command_applet->width != width) { + command_applet->width = width; + } +} + +static void +settings_interval_changed (GSettings *settings, gchar *key, CommandApplet *command_applet) +{ + gint interval; + + interval = g_settings_get_int (command_applet->settings, INTERVAL_KEY); + + /* minimum interval */ + if (interval < 1) + interval = 1; + + if (command_applet->interval == interval) { + return; + } + command_applet->interval = interval; + + command_execute (command_applet); +} + +static void +process_command_output (CommandApplet *command_applet, gchar *output) +{ + gtk_widget_set_tooltip_text (GTK_WIDGET (command_applet->label), command_applet->cmdline); + + if ((output == NULL) || (output[0] == '\0')) + { + gtk_label_set_text (command_applet->label, ERROR_OUTPUT); + return; + } + + /* check if output is a custom GKeyFile */ + if (g_str_has_prefix (output, "[Command]")) + { + GKeyFile *file = g_key_file_new (); + if (g_key_file_load_from_data (file, output, -1, G_KEY_FILE_NONE, NULL)) + { + gchar *goutput = g_key_file_get_string (file, GK_COMMAND_GROUP, GK_COMMAND_OUTPUT, NULL); + gchar *icon = g_key_file_get_string (file, GK_COMMAND_GROUP, GK_COMMAND_ICON, NULL); + + if (goutput) + { + gtk_label_set_use_markup (command_applet->label, TRUE); + gtk_label_set_markup (command_applet->label, goutput); + } + + if (icon) + gtk_image_set_from_icon_name (command_applet->image, icon, GTK_ICON_SIZE_LARGE_TOOLBAR); + + g_free (goutput); + g_free (icon); + } + else + gtk_label_set_text (command_applet->label, ERROR_OUTPUT); + + g_key_file_free (file); + } + else + { + /* Remove leading and trailing whitespace */ + g_strstrip (output); + + /* check output length */ + if (g_utf8_strlen (output, strlen(output)) > command_applet->width) + { + *g_utf8_offset_to_pointer(output, command_applet->width) = '\0'; + } + + gtk_label_set_text (command_applet->label, output); + } +} + +static void command_async_ready_callback (GObject *source_object, GAsyncResult *res, gpointer user_data) +{ + gchar *output; + GError *error = NULL; + CommandApplet *command_applet; + + command_applet = (CommandApplet*) user_data; + + output = ma_command_run_finish (command_applet->command, res, &error); + if (error == NULL) { + process_command_output (command_applet, output); + } else { + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED)) { + gtk_label_set_text (command_applet->label, ERROR_OUTPUT); + } + g_error_free (error); + } + g_free (output); + command_applet->running = FALSE; +} + +static gboolean timeout_callback (CommandApplet *command_applet) +{ + /* command is empty, wait for next timer execution */ + if (strlen (command_applet->cmdline) == 0) { + return G_SOURCE_CONTINUE; + } + + /* command running, wait for next timer execution */ + if (command_applet->running) { + return G_SOURCE_CONTINUE; + } else { + gchar **argv; + GError *error = NULL; + if (!g_shell_parse_argv (command_applet->cmdline, NULL, &argv, &error)) { + gtk_label_set_text (command_applet->label, ERROR_OUTPUT); + g_clear_error (&error); + return G_SOURCE_CONTINUE; + } + g_strfreev(argv); + command_execute (command_applet); + return G_SOURCE_REMOVE; + } +} + +static gboolean +command_execute (CommandApplet *command_applet) +{ + /* stop current timer */ + if (command_applet->timeout_id != 0) + { + g_source_remove (command_applet->timeout_id); + command_applet->timeout_id = 0; + } + + if (command_applet->running) { + g_cancellable_cancel (command_applet->cancellable); + } + + g_object_set (G_OBJECT(command_applet->command), "command", command_applet->cmdline, NULL); + ma_command_run_async (command_applet->command, + command_applet->cancellable, + command_async_ready_callback, + command_applet); + if (!command_applet->running) { + command_applet->running = TRUE; + } + + if (g_cancellable_is_cancelled (command_applet->cancellable)) { + g_cancellable_reset (command_applet->cancellable); + } + + command_applet->timeout_id = g_timeout_add_seconds (command_applet->interval, + (GSourceFunc) timeout_callback, + command_applet); + return G_SOURCE_CONTINUE; +} + +static gboolean +command_applet_fill (MatePanelApplet* applet) +{ + CommandApplet *command_applet; + + g_set_application_name (_("Command Applet")); + gtk_window_set_default_icon_name (APPLET_ICON); + + mate_panel_applet_set_flags (applet, MATE_PANEL_APPLET_EXPAND_MINOR); + mate_panel_applet_set_background_widget (applet, GTK_WIDGET (applet)); + + command_applet = g_malloc0(sizeof(CommandApplet)); + command_applet->applet = applet; + command_applet->settings = mate_panel_applet_settings_new (applet, COMMAND_SCHEMA); + + command_applet->interval = g_settings_get_int (command_applet->settings, INTERVAL_KEY); + command_applet->cmdline = g_settings_get_string (command_applet->settings, COMMAND_KEY); + command_applet->width = g_settings_get_int (command_applet->settings, WIDTH_KEY); + command_applet->command = ma_command_new(command_applet->cmdline, NULL); + command_applet->cancellable = g_cancellable_new (); + + command_applet->box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0)); + command_applet->image = GTK_IMAGE (gtk_image_new_from_icon_name (APPLET_ICON, GTK_ICON_SIZE_LARGE_TOOLBAR)); + command_applet->label = GTK_LABEL (gtk_label_new (ERROR_OUTPUT)); + command_applet->timeout_id = 0; + + /* we add the Gtk label into the applet */ + gtk_box_pack_start (command_applet->box, + GTK_WIDGET (command_applet->image), + TRUE, TRUE, 0); + gtk_box_pack_start (command_applet->box, + GTK_WIDGET (command_applet->label), + TRUE, TRUE, 0); + + gtk_container_add (GTK_CONTAINER (applet), + GTK_WIDGET (command_applet->box)); + + gtk_widget_show_all (GTK_WIDGET (command_applet->applet)); + + g_signal_connect(G_OBJECT (command_applet->applet), "destroy", + G_CALLBACK (command_applet_destroy), + command_applet); + + /* GSettings signals */ + g_signal_connect(command_applet->settings, + "changed::" COMMAND_KEY, + G_CALLBACK (settings_command_changed), + command_applet); + g_signal_connect(command_applet->settings, + "changed::" INTERVAL_KEY, + G_CALLBACK (settings_interval_changed), + command_applet); + g_signal_connect(command_applet->settings, + "changed::" WIDTH_KEY, + G_CALLBACK (settings_width_changed), + command_applet); + g_settings_bind (command_applet->settings, + SHOW_ICON_KEY, + command_applet->image, + "visible", + G_SETTINGS_BIND_DEFAULT); + + /* set up context menu */ + GtkActionGroup *action_group = gtk_action_group_new ("Command Applet Actions"); + gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE); + gtk_action_group_add_actions (action_group, applet_menu_actions, + G_N_ELEMENTS (applet_menu_actions), command_applet); + mate_panel_applet_setup_menu (command_applet->applet, ui, action_group); + + /* first command execution */ + command_execute (command_applet); + return TRUE; +} + +/* this function, called by mate-panel, will create the applet */ +static gboolean +command_factory (MatePanelApplet* applet, const char* iid, gpointer data) +{ + gboolean retval = FALSE; + + if (!g_strcmp0 (iid, "CommandApplet")) + retval = command_applet_fill (applet); + + return retval; +} + +/* needed by mate-panel applet library */ +MATE_PANEL_APPLET_OUT_PROCESS_FACTORY("CommandAppletFactory", + PANEL_TYPE_APPLET, + "Command applet", + command_factory, + NULL) diff --git a/command/src/ma-command.c b/command/src/ma-command.c new file mode 100644 index 00000000..da0e8795 --- /dev/null +++ b/command/src/ma-command.c @@ -0,0 +1,417 @@ +/* + * Copyright (C) 2018 Alberts Muktupāvels + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 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 . + */ + +#include +#include "ma-command.h" + +#define BUFFER_SIZE 64 + +struct _MaCommand +{ + GObject parent; + + gchar *command; + gchar **argv; +}; + +typedef struct +{ + GPid pid; + + GIOChannel *channel; + + GString *input; + + guint io_watch_id; + guint child_watch_id; +} CommandData; + +enum +{ + PROP_0, + + PROP_COMMAND, + + LAST_PROP +}; + +static GParamSpec *command_properties[LAST_PROP] = { NULL }; + +static void initable_iface_init (GInitableIface *iface); + +G_DEFINE_TYPE_WITH_CODE (MaCommand, ma_command, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, + initable_iface_init)) + +static gboolean +read_cb (GIOChannel *source, + GIOCondition condition, + gpointer user_data) +{ + GTask *task; + CommandData *data; + gchar buffer[BUFFER_SIZE]; + gsize bytes_read; + GError *error; + GIOStatus status; + + task = (GTask *) user_data; + data = g_task_get_task_data (task); + + if (g_task_return_error_if_cancelled (task)) + { + g_object_unref (task); + + data->io_watch_id = 0; + + return G_SOURCE_REMOVE; + } + + error = NULL; + status = g_io_channel_read_chars (source, buffer, BUFFER_SIZE, + &bytes_read, &error); + + if (status == G_IO_STATUS_AGAIN) + { + g_clear_error (&error); + + return G_SOURCE_CONTINUE; + } + else if (status != G_IO_STATUS_NORMAL) + { + if (error != NULL) + { + g_task_return_error (task, error); + g_object_unref (task); + } + + data->io_watch_id = 0; + + return G_SOURCE_REMOVE; + } + + g_string_append_len (data->input, buffer, bytes_read); + + return G_SOURCE_CONTINUE; +} + +static void +child_watch_cb (GPid pid, + gint status, + gpointer user_data) +{ + GTask *task; + CommandData *data; + + task = (GTask *) user_data; + data = g_task_get_task_data (task); + + g_task_return_pointer (task, g_strdup (data->input->str), g_free); + g_object_unref (task); +} + +static void +cancelled_cb (GCancellable *cancellable, + gpointer user_data) +{ + GTask *task; + + task = G_TASK (user_data); + + g_object_unref (task); +} + +static void +command_data_free (gpointer user_data) +{ + CommandData *data; + + data = (CommandData *) user_data; + + if (data->pid != 0) + { + g_spawn_close_pid (data->pid); + data->pid = 0; + } + + if (data->channel != NULL) + { + g_io_channel_unref (data->channel); + data->channel = NULL; + } + + if (data->input != NULL) + { + g_string_free (data->input, TRUE); + data->input = NULL; + } + + if (data->io_watch_id != 0) + { + g_source_remove (data->io_watch_id); + data->io_watch_id = 0; + } + + if (data->child_watch_id != 0) + { + g_source_remove (data->child_watch_id); + data->child_watch_id = 0; + } + + g_free (data); +} + +static gboolean +ma_command_initable_init (GInitable *initable, + GCancellable *cancellable, + GError **error) +{ + MaCommand *command; + + command = MA_COMMAND (initable); + + if (command->command == NULL || *command->command == '\0') + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, + "Empty command"); + + return FALSE; + } + + return TRUE; +} + +static void +initable_iface_init (GInitableIface *iface) +{ + iface->init = ma_command_initable_init; +} + +static void +ma_command_finalize (GObject *object) +{ + MaCommand *command; + + command = MA_COMMAND (object); + + g_clear_pointer (&command->command, g_free); + g_clear_pointer (&command->argv, g_strfreev); + + G_OBJECT_CLASS (ma_command_parent_class)->finalize (object); +} + +static void +ma_command_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + MaCommand *command; + + command = MA_COMMAND (object); + + switch (property_id) + { + case PROP_COMMAND: + //g_assert (command->command == NULL); + command->command = g_value_dup_string (value); + if (command->argv && *command->argv != NULL) { + g_strfreev(command->argv); + } + g_shell_parse_argv (command->command, NULL, &command->argv, NULL); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +ma_command_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + MaCommand *command; + + command = MA_COMMAND (object); + + switch (prop_id) + { + case PROP_COMMAND: + g_value_set_string (value, command->command); + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +install_properties (GObjectClass *object_class) +{ + command_properties[PROP_COMMAND] = + g_param_spec_string ("command", "command", "command", + NULL, + G_PARAM_READWRITE); + + g_object_class_install_properties (object_class, LAST_PROP, + command_properties); +} + +static void +ma_command_class_init (MaCommandClass *command_class) +{ + GObjectClass *object_class; + + object_class = G_OBJECT_CLASS (command_class); + + object_class->finalize = ma_command_finalize; + object_class->set_property = ma_command_set_property; + object_class->get_property = ma_command_get_property; + + install_properties (object_class); +} + +static void +ma_command_init (MaCommand *command) +{ +} + +/** + * ma_command_new: + * @command: a command + * @error: (nullable): return location for an error, or %NULL + * + * Creates a new #MaCommand. + * + * Returns: (nullable): a newly allocated #MaCommand + */ +MaCommand * +ma_command_new (const gchar *command, + GError **error) +{ + return g_initable_new (MA_TYPE_COMMAND, NULL, error, + "command", command, + NULL); +} + +/** + * ma_command_run_async: + * @command: a #MaCommand + * @cancellable: (nullable): a #GCancellable or %NULL + * @callback: a #GAsyncReadyCallback to call when the request is satisfied + * @user_data: the data to pass to @callback + * + * Request an asynchronous read of output from command that was passed + * to ma_command_new(). + */ +void +ma_command_run_async (MaCommand *command, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GTask *task; + CommandData *data; + GSpawnFlags spawn_flags; + gint command_stdout; + GError *error; + GIOChannel *channel; + GIOStatus status; + GIOCondition condition; + + g_return_if_fail (MA_IS_COMMAND (command)); + g_return_if_fail (callback != NULL); + + task = g_task_new (command, cancellable, callback, user_data); + g_task_set_source_tag (task, ma_command_run_async); + + if (cancellable) + { + g_signal_connect_object (cancellable, "cancelled", + G_CALLBACK (cancelled_cb), task, + G_CONNECT_AFTER); + } + + data = g_new0 (CommandData, 1); + g_task_set_task_data (task, data, command_data_free); + + spawn_flags = G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD; + error = NULL; + + if (!g_spawn_async_with_pipes (NULL, command->argv, NULL, spawn_flags, + NULL, NULL, &data->pid, NULL, &command_stdout, + NULL, &error)) + { + g_task_return_error (task, error); + g_object_unref (task); + + return; + } + + channel = data->channel = g_io_channel_unix_new (command_stdout); + g_io_channel_set_close_on_unref (channel, TRUE); + + g_assert (error == NULL); + status = g_io_channel_set_encoding (channel, NULL, &error); + + if (status != G_IO_STATUS_NORMAL) + { + g_task_return_error (task, error); + g_object_unref (task); + + return; + } + + g_assert (error == NULL); + status = g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, &error); + + if (status != G_IO_STATUS_NORMAL) + { + g_task_return_error (task, error); + g_object_unref (task); + + return; + } + + data->input = g_string_new (NULL); + + condition = G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP; + data->io_watch_id = g_io_add_watch (channel, condition, read_cb, task); + + data->child_watch_id = g_child_watch_add (data->pid, child_watch_cb, task); +} + +/** + * ma_command_run_finish: + * @command: a #MaCommand + * @result: a #GAsyncResult + * @error: (nullable): return location for an error, or %NULL + * + * Finishes an operation started with ma_command_run_async(). + * + * Returns: %NULL if @error is set, otherwise output from command + */ +gchar * +ma_command_run_finish (MaCommand *command, + GAsyncResult *result, + GError **error) +{ + g_return_val_if_fail (MA_IS_COMMAND (command), NULL); + g_return_val_if_fail (g_task_is_valid (result, command), NULL); + g_return_val_if_fail (error == NULL || *error == NULL, NULL); + + return g_task_propagate_pointer (G_TASK (result), error); +} diff --git a/command/src/ma-command.h b/command/src/ma-command.h new file mode 100644 index 00000000..8fb51ab3 --- /dev/null +++ b/command/src/ma-command.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018 Alberts Muktupāvels + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 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 . + */ + +#ifndef MA_COMMAND_H +#define MA_COMMAND_H + +#include +#include + +G_BEGIN_DECLS + +#define MA_TYPE_COMMAND (ma_command_get_type ()) +G_DECLARE_FINAL_TYPE (MaCommand, ma_command, MA, COMMAND, GObject) + +MaCommand *ma_command_new (const gchar *command, + GError **error); + +void ma_command_run_async (MaCommand *command, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); + +gchar *ma_command_run_finish (MaCommand *command, + GAsyncResult *result, + GError **error); + +G_END_DECLS + +#endif diff --git a/configure.ac b/configure.ac index 19bf2be0..1b1d1134 100644 --- a/configure.ac +++ b/configure.ac @@ -603,7 +603,9 @@ cpufreq/help/Makefile timerapplet/Makefile timerapplet/org.mate.panel.applet.timer.gschema.xml command/Makefile -command/org.mate.panel.applet.command.gschema.xml +command/data/Makefile +command/data/org.mate.panel.applet.command.gschema.xml +command/src/Makefile netspeed/Makefile netspeed/data/Makefile netspeed/data/org.mate.panel.applet.netspeed.gschema.xml diff --git a/po/POTFILES.in b/po/POTFILES.in index ea0cb3e1..748cedc3 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -13,10 +13,10 @@ charpick/charpick.c charpick/org.mate.applets.CharpickerApplet.mate-panel-applet.desktop.in.in charpick/org.mate.panel.applet.charpick.gschema.xml.in charpick/properties.c -command/command.c -command/command-preferences.ui -command/org.mate.applets.CommandApplet.mate-panel-applet.desktop.in.in -command/org.mate.panel.applet.command.gschema.xml.in +command/data/command-preferences.ui +command/data/org.mate.applets.CommandApplet.mate-panel-applet.desktop.in.in +command/data/org.mate.panel.applet.command.gschema.xml.in +command/src/command.c cpufreq/cpufreq-preferences.ui cpufreq/org.mate.applets.CPUFreqApplet.mate-panel-applet.desktop.in.in cpufreq/org.mate.panel.applet.cpufreq.gschema.xml.in -- cgit v1.2.1