From f9d480cfe50ca1d7a0f0b5a2b8bb9932962bfbe7 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 17:07:22 +0200 Subject: Adding upstream version 3.38.6. Signed-off-by: Daniel Baumann --- subprojects/extensions-tool/src/command-create.c | 509 ++++++++++++++++++++ subprojects/extensions-tool/src/command-disable.c | 129 ++++++ subprojects/extensions-tool/src/command-enable.c | 129 ++++++ subprojects/extensions-tool/src/command-info.c | 113 +++++ subprojects/extensions-tool/src/command-install.c | 213 +++++++++ subprojects/extensions-tool/src/command-list.c | 196 ++++++++ subprojects/extensions-tool/src/command-pack.c | 516 +++++++++++++++++++++ subprojects/extensions-tool/src/command-prefs.c | 107 +++++ subprojects/extensions-tool/src/command-reset.c | 86 ++++ .../extensions-tool/src/command-uninstall.c | 114 +++++ subprojects/extensions-tool/src/commands.h | 38 ++ subprojects/extensions-tool/src/common.h | 73 +++ .../src/gnome-extensions-tool.gresource.xml | 11 + subprojects/extensions-tool/src/main.c | 408 ++++++++++++++++ subprojects/extensions-tool/src/meson.build | 37 ++ .../src/templates/00-plain.desktop.in | 5 + .../src/templates/indicator.desktop.in | 5 + .../src/templates/indicator/extension.js | 74 +++ .../src/templates/indicator/stylesheet.css | 1 + .../extensions-tool/src/templates/meson.build | 13 + .../src/templates/plain/extension.js | 34 ++ .../src/templates/plain/stylesheet.css | 1 + 22 files changed, 2812 insertions(+) create mode 100644 subprojects/extensions-tool/src/command-create.c create mode 100644 subprojects/extensions-tool/src/command-disable.c create mode 100644 subprojects/extensions-tool/src/command-enable.c create mode 100644 subprojects/extensions-tool/src/command-info.c create mode 100644 subprojects/extensions-tool/src/command-install.c create mode 100644 subprojects/extensions-tool/src/command-list.c create mode 100644 subprojects/extensions-tool/src/command-pack.c create mode 100644 subprojects/extensions-tool/src/command-prefs.c create mode 100644 subprojects/extensions-tool/src/command-reset.c create mode 100644 subprojects/extensions-tool/src/command-uninstall.c create mode 100644 subprojects/extensions-tool/src/commands.h create mode 100644 subprojects/extensions-tool/src/common.h create mode 100644 subprojects/extensions-tool/src/gnome-extensions-tool.gresource.xml create mode 100644 subprojects/extensions-tool/src/main.c create mode 100644 subprojects/extensions-tool/src/meson.build create mode 100644 subprojects/extensions-tool/src/templates/00-plain.desktop.in create mode 100644 subprojects/extensions-tool/src/templates/indicator.desktop.in create mode 100644 subprojects/extensions-tool/src/templates/indicator/extension.js create mode 100644 subprojects/extensions-tool/src/templates/indicator/stylesheet.css create mode 100644 subprojects/extensions-tool/src/templates/meson.build create mode 100644 subprojects/extensions-tool/src/templates/plain/extension.js create mode 100644 subprojects/extensions-tool/src/templates/plain/stylesheet.css (limited to 'subprojects/extensions-tool/src') diff --git a/subprojects/extensions-tool/src/command-create.c b/subprojects/extensions-tool/src/command-create.c new file mode 100644 index 0000000..b328b60 --- /dev/null +++ b/subprojects/extensions-tool/src/command-create.c @@ -0,0 +1,509 @@ +/* command-create.c + * + * Copyright 2018 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#define _GNU_SOURCE /* for strcasestr */ +#include + +#include +#include +#include +#include + +#include "commands.h" +#include "common.h" +#include "config.h" + +#define TEMPLATES_PATH "/org/gnome/extensions-tool/templates" +#define TEMPLATE_KEY "Path" +#define SORT_DATA "desktop-id" + +static char * +get_shell_version (GError **error) +{ + g_autoptr (GDBusProxy) proxy = NULL; + g_autoptr (GVariant) variant = NULL; + g_auto (GStrv) split_version = NULL; + + proxy = get_shell_proxy (error); + if (proxy == NULL) + return NULL; + + variant = g_dbus_proxy_get_cached_property (proxy, "ShellVersion"); + if (variant == NULL) + return NULL; + + split_version = g_strsplit (g_variant_get_string (variant, NULL), ".", 3); + if (g_ascii_strtoll (split_version[1], NULL, 10) % 2 == 0) + g_clear_pointer (&split_version[2], g_free); + + return g_strjoinv (".", split_version); +} + +static GDesktopAppInfo * +load_app_info_from_resource (const char *uri) +{ + g_autoptr (GFile) file = NULL; + g_autofree char *contents = NULL; + g_autoptr (GKeyFile) keyfile = NULL; + + file = g_file_new_for_uri (uri); + if (!g_file_load_contents (file, NULL, &contents, NULL, NULL, NULL)) + return NULL; + + keyfile = g_key_file_new (); + if (!g_key_file_load_from_data (keyfile, contents, -1, G_KEY_FILE_NONE, NULL)) + return NULL; + + return g_desktop_app_info_new_from_keyfile (keyfile); +} + +static int +sort_func (gconstpointer a, gconstpointer b) +{ + GObject *info1 = *((GObject **) a); + GObject *info2 = *((GObject **) b); + const char *desktop1 = g_object_get_data (info1, SORT_DATA); + const char *desktop2 = g_object_get_data (info2, SORT_DATA); + + return g_strcmp0 (desktop1, desktop2); +} + +static GPtrArray * +get_templates (void) +{ + g_auto (GStrv) children = NULL; + GPtrArray *templates = g_ptr_array_new_with_free_func (g_object_unref); + char **s; + + children = g_resources_enumerate_children (TEMPLATES_PATH, 0, NULL); + + for (s = children; *s; s++) + { + g_autofree char *uri = NULL; + GDesktopAppInfo *info; + + if (!g_str_has_suffix (*s, ".desktop")) + continue; + + uri = g_strdup_printf ("resource://" TEMPLATES_PATH "/%s", *s); + info = load_app_info_from_resource (uri); + if (!info) + continue; + + g_object_set_data_full (G_OBJECT (info), SORT_DATA, g_strdup (*s), g_free); + g_ptr_array_add (templates, info); + } + + g_ptr_array_sort (templates, sort_func); + + return templates; +} + +static char * +escape_json_string (const char *string) +{ + GString *escaped = g_string_new (string); + + for (gsize i = 0; i < escaped->len; ++i) + { + if (escaped->str[i] == '"' || escaped->str[i] == '\\') + { + g_string_insert_c (escaped, i, '\\'); + ++i; + } + } + + return g_string_free (escaped, FALSE); +} + +static gboolean +create_metadata (GFile *target_dir, + const char *uuid, + const char *name, + const char *description, + GError **error) +{ + g_autofree char *uuid_escaped = NULL; + g_autofree char *name_escaped = NULL; + g_autofree char *desc_escaped = NULL; + g_autoptr (GFile) target = NULL; + g_autoptr (GString) json = NULL; + g_autofree char *version = NULL; + + version = get_shell_version (error); + if (version == NULL) + return FALSE; + + uuid_escaped = escape_json_string (uuid); + name_escaped = escape_json_string (name); + desc_escaped = escape_json_string (description); + + json = g_string_new ("{\n"); + + g_string_append_printf (json, " \"name\": \"%s\",\n", name_escaped); + g_string_append_printf (json, " \"description\": \"%s\",\n", desc_escaped); + g_string_append_printf (json, " \"uuid\": \"%s\",\n", uuid_escaped); + g_string_append_printf (json, " \"shell-version\": [\n"); + g_string_append_printf (json, " \"%s\"\n", version); + g_string_append_printf (json, " ]\n}\n"); + + target = g_file_get_child (target_dir, "metadata.json"); + return g_file_replace_contents (target, + json->str, + json->len, + NULL, + FALSE, + 0, + NULL, + NULL, + error); +} + + +static gboolean +copy_extension_template (const char *template, GFile *target_dir, GError **error) +{ + g_auto (GStrv) templates = NULL; + g_autofree char *path = NULL; + char **s; + + path = g_strdup_printf (TEMPLATES_PATH "/%s", template); + templates = g_resources_enumerate_children (path, 0, NULL); + + if (templates == NULL) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, + "No template %s", template); + return FALSE; + } + + for (s = templates; *s; s++) + { + g_autoptr (GFile) target = NULL; + g_autoptr (GFile) source = NULL; + g_autofree char *uri = NULL; + + uri = g_strdup_printf ("resource://%s/%s", path, *s); + source = g_file_new_for_uri (uri); + target = g_file_get_child (target_dir, *s); + + if (!g_file_copy (source, target, G_FILE_COPY_TARGET_DEFAULT_PERMS, NULL, NULL, NULL, error)) + return FALSE; + } + + return TRUE; +} + +static gboolean +launch_extension_source (GFile *dir, GError **error) +{ + g_autoptr (GFile) main_source = NULL; + g_autoptr (GAppInfo) handler = NULL; + GList l; + + main_source = g_file_get_child (dir, "extension.js"); + handler = g_file_query_default_handler (main_source, NULL, NULL); + if (handler == NULL) + { + /* Translators: a file path to an extension directory */ + g_print (_("The new extension was successfully created in %s.\n"), + g_file_peek_path (dir)); + return TRUE; + } + + l.data = main_source; + l.next = l.prev = NULL; + + return g_app_info_launch (handler, &l, NULL, error); +} + +static gboolean +create_extension (const char *uuid, const char *name, const char *description, const char *template) +{ + g_autoptr (GFile) dir = NULL; + g_autoptr (GError) error = NULL; + + if (template == NULL) + template = "plain"; + + dir = g_file_new_build_filename (g_get_user_data_dir (), + "gnome-shell", + "extensions", + uuid, + NULL); + + if (!g_file_make_directory_with_parents (dir, NULL, &error)) + { + g_printerr ("%s\n", error->message); + return FALSE; + } + + if (!create_metadata (dir, uuid, name, description, &error)) + { + g_printerr ("%s\n", error->message); + return FALSE; + } + + if (!copy_extension_template (template, dir, &error)) + { + g_printerr ("%s\n", error->message); + return FALSE; + } + + if (!launch_extension_source (dir, &error)) + { + g_printerr ("%s\n", error->message); + return FALSE; + } + + return TRUE; +} + +static void +prompt_metadata (char **uuid, char **name, char **description, char **template) +{ + g_autoptr (GInputStream) stdin = NULL; + g_autoptr (GDataInputStream) istream = NULL; + + if ((uuid == NULL || *uuid != NULL) && + (name == NULL || *name != NULL) && + (description == NULL || *description != NULL) && + (template == NULL || *template != NULL)) + return; + + stdin = g_unix_input_stream_new (0, FALSE); + istream = g_data_input_stream_new (stdin); + + if (name != NULL && *name == NULL) + { + char *line = NULL; + + g_print ( + _("Name should be a very short (ideally descriptive) string.\n" + "Examples are: %s"), + "“Click To Focus”, “Adblock”, “Shell Window Shrinker”\n"); + + while (line == NULL) + { + g_print ("%s: ", _("Name")); + + line = g_data_input_stream_read_line_utf8 (istream, NULL, NULL, NULL); + } + *name = g_strdelimit (line, "\n", '\0'); + + g_print ("\n"); + } + + if (description != NULL && *description == NULL) + { + char *line = NULL; + + g_print ( + _("Description is a single-sentence explanation of what your extension does.\n" + "Examples are: %s"), + "“Make windows visible on click”, “Block advertisement popups”, “Animate windows shrinking on minimize”\n"); + + while (line == NULL) + { + g_print ("%s: ", _("Description")); + + line = g_data_input_stream_read_line_utf8 (istream, NULL, NULL, NULL); + } + *description = g_strdelimit (line, "\n", '\0'); + + g_print ("\n"); + } + + if (uuid != NULL && *uuid == NULL) + { + char *line = NULL; + + g_print ( + _("UUID is a globally-unique identifier for your extension.\n" + "This should be in the format of an email address (clicktofocus@janedoe.example.com)\n")); + + while (line == NULL) + { + g_print ("UUID: "); + + line = g_data_input_stream_read_line_utf8 (istream, NULL, NULL, NULL); + } + *uuid = g_strdelimit (line, "\n", '\0'); + + g_print ("\n"); + } + + if (template != NULL && *template == NULL) + { + g_autoptr (GPtrArray) templates = get_templates (); + + if (templates->len == 1) + { + GDesktopAppInfo *info = g_ptr_array_index (templates, 0); + *template = g_desktop_app_info_get_string (info, TEMPLATE_KEY); + } + else + { + int i; + + g_print (_("Choose one of the available templates:\n")); + for (i = 0; i < templates->len; i++) + { + GAppInfo *info = g_ptr_array_index (templates, i); + g_print ("%d) %-10s – %s\n", + i + 1, + g_app_info_get_name (info), + g_app_info_get_description (info)); + } + + while (*template == NULL) + { + g_autofree char *line = NULL; + + g_print ("%s [1-%d]: ", _("Template"), templates->len); + + line = g_data_input_stream_read_line_utf8 (istream, NULL, NULL, NULL); + + if (line == NULL) + continue; + + if (g_ascii_isdigit (*line)) + { + long i = strtol (line, NULL, 10); + + if (i > 0 && i <= templates->len) + { + GDesktopAppInfo *info; + + info = g_ptr_array_index (templates, i - 1); + *template = + g_desktop_app_info_get_string (info, TEMPLATE_KEY); + } + } + else + { + for (i = 0; i < templates->len; i++) + { + GDesktopAppInfo *info = g_ptr_array_index (templates, i); + g_autofree char *cur_template = NULL; + + cur_template = + g_desktop_app_info_get_string (info, TEMPLATE_KEY); + + if (strcasestr (cur_template, line) != NULL) + *template = g_steal_pointer (&cur_template); + } + } + } + g_print ("\n"); + } + } +} + +int +handle_create (int argc, char *argv[], gboolean do_help) +{ + g_autoptr (GOptionContext) context = NULL; + g_autoptr (GError) error = NULL; + g_autofree char *name = NULL; + g_autofree char *description = NULL; + g_autofree char *uuid = NULL; + g_autofree char *template = NULL; + gboolean interactive = FALSE; + gboolean list_templates = FALSE; + GOptionEntry entries[] = { + { .long_name = "uuid", + .arg = G_OPTION_ARG_STRING, .arg_data = &uuid, + .arg_description = "UUID", + .description = _("The unique identifier of the new extension") }, + { .long_name = "name", + .arg = G_OPTION_ARG_STRING, .arg_data = &name, + .arg_description = _("NAME"), + .description = _("The user-visible name of the new extension") }, + { .long_name = "description", + .arg_description = _("DESCRIPTION"), + .arg = G_OPTION_ARG_STRING, .arg_data = &description, + .description = _("A short description of what the extension does") }, + { .long_name = "template", + .arg = G_OPTION_ARG_STRING, .arg_data = &template, + .arg_description = _("TEMPLATE"), + .description = _("The template to use for the new extension") }, + { .long_name = "list-templates", + .arg = G_OPTION_ARG_NONE, .arg_data = &list_templates, + .flags = G_OPTION_FLAG_HIDDEN }, + { .long_name = "interactive", .short_name = 'i', + .arg = G_OPTION_ARG_NONE, .arg_data = &interactive, + .description = _("Enter extension information interactively") }, + { NULL } + }; + + g_set_prgname ("gnome-extensions create"); + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_set_summary (context, _("Create a new extension")); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, get_option_group ()); + + if (do_help) + { + show_help (context, NULL); + return 0; + } + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + show_help (context, error->message); + return 1; + } + + if (argc > 1) + { + show_help (context, _("Unknown arguments")); + return 1; + } + + if (list_templates) + { + g_autoptr (GPtrArray) templates = get_templates (); + int i; + + for (i = 0; i < templates->len; i++) + { + GDesktopAppInfo *info = g_ptr_array_index (templates, i); + g_autofree char *template = NULL; + + template = g_desktop_app_info_get_string (info, TEMPLATE_KEY); + g_print ("%s\n", template); + } + return 0; + } + + if (interactive) + prompt_metadata (&uuid, &name, &description, &template); + + if (uuid == NULL || name == NULL || description == NULL) + { + show_help (context, _("UUID, name and description are required")); + return 1; + } + + return create_extension (uuid, name, description, template) ? 0 : 2; +} diff --git a/subprojects/extensions-tool/src/command-disable.c b/subprojects/extensions-tool/src/command-disable.c new file mode 100644 index 0000000..93c316b --- /dev/null +++ b/subprojects/extensions-tool/src/command-disable.c @@ -0,0 +1,129 @@ +/* command-disable.c + * + * Copyright 2018 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +#include "commands.h" +#include "common.h" +#include "config.h" + +static gboolean +disable_extension_dbus (GDBusProxy *proxy, + const char *uuid) +{ + g_autoptr (GVariant) response = NULL; + g_autoptr (GError) error = NULL; + gboolean success = FALSE; + + response = g_dbus_proxy_call_sync (proxy, + "DisableExtension", + g_variant_new ("(s)", uuid), + 0, + -1, + NULL, + &error); + + if (response == NULL) + { + g_printerr (_("Failed to connect to GNOME Shell\n")); + return FALSE; + } + + g_variant_get (response, "(b)", &success); + + if (!success) + g_printerr (_("Extension “%s” does not exist\n"), uuid); + + return success; +} + +static gboolean +disable_extension_gsettings (const char *uuid) +{ + g_autoptr(GSettings) settings = get_shell_settings (); + + if (settings == NULL) + return FALSE; + + return settings_list_remove (settings, "enabled-extensions", uuid) && + settings_list_add (settings, "disabled-extensions", uuid); +} + +static gboolean +disable_extension (const char *uuid) +{ + g_autoptr (GDBusProxy) proxy = NULL; + g_autoptr (GError) error = NULL; + + proxy = get_shell_proxy (&error); + + if (proxy != NULL) + return disable_extension_dbus (proxy, uuid); + else + return disable_extension_gsettings (uuid); +} + +int +handle_disable (int argc, char *argv[], gboolean do_help) +{ + g_autoptr (GOptionContext) context = NULL; + g_autoptr (GError) error = NULL; + g_auto(GStrv) uuids = NULL; + GOptionEntry entries[] = { + { .long_name = G_OPTION_REMAINING, + .arg_description = "UUID", + .arg = G_OPTION_ARG_STRING_ARRAY, .arg_data = &uuids }, + { NULL } + }; + + g_set_prgname ("gnome-extensions disable"); + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_set_summary (context, _("Disable an extension")); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, get_option_group()); + + if (do_help) + { + show_help (context, NULL); + return 0; + } + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + show_help (context, error->message); + return 1; + } + + if (uuids == NULL) + { + show_help (context, _("No UUID given")); + return 1; + } + else if (g_strv_length (uuids) > 1) + { + show_help (context, _("More than one UUID given")); + return 1; + } + + return disable_extension (*uuids) ? 0 : 2; +} diff --git a/subprojects/extensions-tool/src/command-enable.c b/subprojects/extensions-tool/src/command-enable.c new file mode 100644 index 0000000..6ae3a83 --- /dev/null +++ b/subprojects/extensions-tool/src/command-enable.c @@ -0,0 +1,129 @@ +/* command-enable.c + * + * Copyright 2018 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +#include "commands.h" +#include "common.h" +#include "config.h" + +static gboolean +enable_extension_dbus (GDBusProxy *proxy, + const char *uuid) +{ + g_autoptr (GVariant) response = NULL; + g_autoptr (GError) error = NULL; + gboolean success = FALSE; + + response = g_dbus_proxy_call_sync (proxy, + "EnableExtension", + g_variant_new ("(s)", uuid), + 0, + -1, + NULL, + &error); + + if (response == NULL) + { + g_printerr (_("Failed to connect to GNOME Shell\n")); + return FALSE; + } + + g_variant_get (response, "(b)", &success); + + if (!success) + g_printerr (_("Extension “%s” does not exist\n"), uuid); + + return success; +} + +static gboolean +enable_extension_gsettings (const char *uuid) +{ + g_autoptr(GSettings) settings = get_shell_settings (); + + if (settings == NULL) + return FALSE; + + return settings_list_add (settings, "enabled-extensions", uuid) && + settings_list_remove (settings, "disabled-extensions", uuid); +} + +static gboolean +enable_extension (const char *uuid) +{ + g_autoptr (GDBusProxy) proxy = NULL; + g_autoptr (GError) error = NULL; + + proxy = get_shell_proxy (&error); + + if (proxy != NULL) + return enable_extension_dbus (proxy, uuid); + else + return enable_extension_gsettings (uuid); +} + +int +handle_enable (int argc, char *argv[], gboolean do_help) +{ + g_autoptr (GOptionContext) context = NULL; + g_autoptr (GError) error = NULL; + g_auto(GStrv) uuids = NULL; + GOptionEntry entries[] = { + { .long_name = G_OPTION_REMAINING, + .arg_description = "UUID", + .arg = G_OPTION_ARG_STRING_ARRAY, .arg_data = &uuids }, + { NULL } + }; + + g_set_prgname ("gnome-extensions enable"); + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_set_summary (context, _("Enable an extension")); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, get_option_group()); + + if (do_help) + { + show_help (context, NULL); + return 0; + } + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + show_help (context, error->message); + return 1; + } + + if (uuids == NULL) + { + show_help (context, _("No UUID given")); + return 1; + } + else if (g_strv_length (uuids) > 1) + { + show_help (context, _("More than one UUID given")); + return 1; + } + + return enable_extension (*uuids) ? 0 : 2; +} diff --git a/subprojects/extensions-tool/src/command-info.c b/subprojects/extensions-tool/src/command-info.c new file mode 100644 index 0000000..61492a5 --- /dev/null +++ b/subprojects/extensions-tool/src/command-info.c @@ -0,0 +1,113 @@ +/* commands-info.c + * + * Copyright 2018 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +#include "commands.h" +#include "common.h" +#include "config.h" + +static gboolean +show_extension_info (const char *uuid) +{ + g_autoptr (GDBusProxy) proxy = NULL; + g_autoptr (GVariant) response = NULL; + g_autoptr (GVariant) asv = NULL; + g_autoptr (GVariantDict) info = NULL; + g_autoptr (GError) error = NULL; + + proxy = get_shell_proxy (&error); + if (proxy == NULL) + return FALSE; + + response = g_dbus_proxy_call_sync (proxy, + "GetExtensionInfo", + g_variant_new ("(s)", uuid), + 0, + -1, + NULL, + &error); + if (response == NULL) + { + g_printerr (_("Failed to connect to GNOME Shell\n")); + return FALSE; + } + + asv = g_variant_get_child_value (response, 0); + info = g_variant_dict_new (asv); + + if (!g_variant_dict_contains (info, "uuid")) + { + g_printerr (_("Extension “%s” doesn't exist\n"), uuid); + return FALSE; + } + + print_extension_info (info, DISPLAY_DETAILED); + + return TRUE; +} + +int +handle_info (int argc, char *argv[], gboolean do_help) +{ + g_autoptr (GOptionContext) context = NULL; + g_autoptr (GError) error = NULL; + g_auto(GStrv) uuids = NULL; + GOptionEntry entries[] = { + { .long_name = G_OPTION_REMAINING, + .arg_description = "UUID", + .arg = G_OPTION_ARG_STRING_ARRAY, .arg_data = &uuids }, + { NULL } + }; + + g_set_prgname ("gnome-extensions info"); + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_set_summary (context, _("Show extensions info")); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, get_option_group()); + + if (do_help) + { + show_help (context, NULL); + return 0; + } + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + show_help (context, error->message); + return 1; + } + + if (uuids == NULL) + { + show_help (context, _("No UUID given")); + return 1; + } + else if (g_strv_length (uuids) > 1) + { + show_help (context, _("More than one UUID given")); + return 1; + } + + return show_extension_info (*uuids) ? 0 : 2; +} diff --git a/subprojects/extensions-tool/src/command-install.c b/subprojects/extensions-tool/src/command-install.c new file mode 100644 index 0000000..2eefaba --- /dev/null +++ b/subprojects/extensions-tool/src/command-install.c @@ -0,0 +1,213 @@ +/* command-install.c + * + * Copyright 2018 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +#include +#include + +#include "commands.h" +#include "common.h" +#include "config.h" + +static JsonObject * +load_metadata (GFile *dir, + GError **error) +{ + g_autoptr (JsonParser) parser = NULL; + g_autoptr (GInputStream) stream = NULL; + g_autoptr (GFile) file = NULL; + + file = g_file_get_child (dir, "metadata.json"); + stream = G_INPUT_STREAM (g_file_read (file, NULL, error)); + if (stream == NULL) + return NULL; + + parser = json_parser_new_immutable (); + if (!json_parser_load_from_stream (parser, stream, NULL, error)) + return NULL; + + return json_node_dup_object (json_parser_get_root (parser)); +} + +static void +on_error (AutoarExtractor *extractor, + GError *error, + gpointer data) +{ + *((GError **)data) = g_error_copy (error); +} + +static GFile * +on_decide_destination (AutoarExtractor *extractor, + GFile *dest, + GList *files, + gpointer data) +{ + g_autofree char *dest_path = NULL; + GFile *new_dest; + int copy = 1; + + dest_path = g_file_get_path (dest); + new_dest = g_object_ref (dest); + + while (g_file_query_exists (new_dest, NULL)) + { + g_autofree char *new_path = g_strdup_printf ("%s (%d)", dest_path, copy); + + g_object_unref (new_dest); + new_dest = g_file_new_for_path (new_path); + + copy++; + } + + *((GFile **)data) = g_object_ref (new_dest); + + return new_dest; +} + +static int +install_extension (const char *bundle, + gboolean force) +{ + g_autoptr (AutoarExtractor) extractor = NULL; + g_autoptr (JsonObject) metadata = NULL; + g_autoptr (GFile) cachedir = NULL; + g_autoptr (GFile) tmpdir = NULL; + g_autoptr (GFile) src = NULL; + g_autoptr (GFile) dst = NULL; + g_autoptr (GFile) dstdir = NULL; + g_autoptr (GError) error = NULL; + g_autofree char *cwd = NULL; + const char *uuid; + + cwd = g_get_current_dir (); + src = g_file_new_for_commandline_arg_and_cwd (bundle, cwd); + cachedir = g_file_new_for_path (g_get_user_cache_dir ()); + + extractor = autoar_extractor_new (src, cachedir); + + g_signal_connect (extractor, "error", G_CALLBACK (on_error), &error); + g_signal_connect (extractor, "decide-destination", G_CALLBACK (on_decide_destination), &tmpdir); + + autoar_extractor_start (extractor, NULL); + + if (error != NULL) + goto err; + + metadata = load_metadata (tmpdir, &error); + if (metadata == NULL) + goto err; + + dstdir = g_file_new_build_filename (g_get_user_data_dir (), + "gnome-shell", "extensions", NULL); + + if (!g_file_make_directory_with_parents (dstdir, NULL, &error)) + { + if (error->code == G_IO_ERROR_EXISTS) + g_clear_error (&error); + else + goto err; + } + + uuid = json_object_get_string_member (metadata, "uuid"); + dst = g_file_get_child (dstdir, uuid); + + if (g_file_query_exists (dst, NULL)) + { + if (!force) + { + g_set_error (&error, G_IO_ERROR, G_IO_ERROR_EXISTS, + "%s exists and --force was not specified", uuid); + goto err; + } + else if (!file_delete_recursively (dst, &error)) + { + goto err; + } + } + + if (!g_file_move (tmpdir, dst, G_FILE_COPY_NONE, NULL, NULL, NULL, &error)) + goto err; + + return 0; + +err: + if (error != NULL) + g_printerr ("%s\n", error->message); + + if (tmpdir != NULL) + file_delete_recursively (tmpdir, NULL); + + return 2; +} + +int +handle_install (int argc, char *argv[], gboolean do_help) +{ + g_autoptr (GOptionContext) context = NULL; + g_autoptr (GError) error = NULL; + g_auto (GStrv) filenames = NULL; + gboolean force = FALSE; + GOptionEntry entries[] = { + { .long_name = "force", .short_name = 'f', + .arg = G_OPTION_ARG_NONE, .arg_data = &force, + .description = _("Overwrite an existing extension") }, + { .long_name = G_OPTION_REMAINING, + .arg_description =_("EXTENSION_BUNDLE"), + .arg = G_OPTION_ARG_FILENAME_ARRAY, .arg_data = &filenames }, + { NULL } + }; + + g_set_prgname ("gnome-extensions install"); + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_set_summary (context, _("Install an extension bundle")); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, get_option_group()); + + if (do_help) + { + show_help (context, NULL); + return 0; + } + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + show_help (context, error->message); + return 1; + } + + if (filenames == NULL) + { + show_help (context, _("No extension bundle specified")); + return 1; + } + + if (g_strv_length (filenames) > 1) + { + show_help (context, _("More than one extension bundle specified")); + return 1; + } + + return install_extension (*filenames, force); +} diff --git a/subprojects/extensions-tool/src/command-list.c b/subprojects/extensions-tool/src/command-list.c new file mode 100644 index 0000000..62db4d9 --- /dev/null +++ b/subprojects/extensions-tool/src/command-list.c @@ -0,0 +1,196 @@ +/* command-list.c + * + * Copyright 2018 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +#include "commands.h" +#include "common.h" +#include "config.h" + + +typedef enum { + LIST_FLAGS_NONE = 0, + LIST_FLAGS_USER = 1 << 0, + LIST_FLAGS_SYSTEM = 1 << 1, + LIST_FLAGS_ENABLED = 1 << 2, + LIST_FLAGS_DISABLED = 1 << 3, + LIST_FLAGS_NO_PREFS = 1 << 4, + LIST_FLAGS_NO_UPDATES = 1 << 5, +} ListFilterFlags; + +static gboolean +list_extensions (ListFilterFlags filter, DisplayFormat format) +{ + g_autoptr (GDBusProxy) proxy = NULL; + g_autoptr (GVariant) response = NULL; + g_autoptr (GVariant) extensions = NULL; + g_autoptr (GError) error = NULL; + gboolean needs_newline = FALSE; + GVariantIter iter; + GVariant *value; + char *uuid; + + proxy = get_shell_proxy (&error); + if (proxy == NULL) + return FALSE; + + response = g_dbus_proxy_call_sync (proxy, + "ListExtensions", + NULL, + 0, + -1, + NULL, + &error); + if (response == NULL) + { + g_printerr (_("Failed to connect to GNOME Shell\n")); + return FALSE; + } + + extensions = g_variant_get_child_value (response, 0); + + g_variant_iter_init (&iter, extensions); + while (g_variant_iter_loop (&iter, "{s@a{sv}}", &uuid, &value)) + { + g_autoptr (GVariantDict) info = NULL; + double type, state; + gboolean has_prefs; + gboolean has_update; + + info = g_variant_dict_new (value); + g_variant_dict_lookup (info, "type", "d", &type); + g_variant_dict_lookup (info, "state", "d", &state); + g_variant_dict_lookup (info, "hasPrefs", "b", &has_prefs); + g_variant_dict_lookup (info, "hasUpdate", "b", &has_update); + + if (type == TYPE_USER && (filter & LIST_FLAGS_USER) == 0) + continue; + + if (type == TYPE_SYSTEM && (filter & LIST_FLAGS_SYSTEM) == 0) + continue; + + if (state == STATE_ENABLED && (filter & LIST_FLAGS_ENABLED) == 0) + continue; + + if (state != STATE_ENABLED && (filter & LIST_FLAGS_DISABLED) == 0) + continue; + + if (!has_prefs && (filter & LIST_FLAGS_NO_PREFS) == 0) + continue; + + if (!has_update && (filter & LIST_FLAGS_NO_UPDATES) == 0) + continue; + + if (needs_newline) + g_print ("\n"); + + print_extension_info (info, format); + needs_newline = (format != DISPLAY_ONELINE); + } + + return TRUE; +} + +int +handle_list (int argc, char *argv[], gboolean do_help) +{ + g_autoptr (GOptionContext) context = NULL; + g_autoptr (GError) error = NULL; + int flags = LIST_FLAGS_NONE; + gboolean details = FALSE; + gboolean user = FALSE; + gboolean system = FALSE; + gboolean enabled = FALSE; + gboolean disabled = FALSE; + gboolean has_prefs = FALSE; + gboolean has_updates = FALSE; + GOptionEntry entries[] = { + { .long_name = "user", + .arg = G_OPTION_ARG_NONE, .arg_data = &user, + .description = _("Show user-installed extensions") }, + { .long_name = "system", + .arg = G_OPTION_ARG_NONE, .arg_data = &system, + .description = _("Show system-installed extensions") }, + { .long_name = "enabled", + .arg = G_OPTION_ARG_NONE, .arg_data = &enabled, + .description = _("Show enabled extensions") }, + { .long_name = "disabled", + .arg = G_OPTION_ARG_NONE, .arg_data = &disabled, + .description = _("Show disabled extensions") }, + { .long_name = "prefs", + .arg = G_OPTION_ARG_NONE, .arg_data = &has_prefs, + .description = _("Show extensions with preferences") }, + { .long_name = "updates", + .arg = G_OPTION_ARG_NONE, .arg_data = &has_updates, + .description = _("Show extensions with updates") }, + { .long_name = "details", .short_name = 'd', + .arg = G_OPTION_ARG_NONE, .arg_data = &details, + .description = _("Print extension details") }, + { NULL } + }; + + g_set_prgname ("gnome-extensions list"); + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_set_summary (context, _("List installed extensions")); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, get_option_group()); + + if (do_help) + { + show_help (context, NULL); + return 0; + } + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + show_help (context, error->message); + return 1; + } + + if (argc > 1) + { + show_help (context, _("Unknown arguments")); + return 1; + } + + if (user || !system) + flags |= LIST_FLAGS_USER; + + if (system || !user) + flags |= LIST_FLAGS_SYSTEM; + + if (enabled || !disabled) + flags |= LIST_FLAGS_ENABLED; + + if (disabled || !enabled) + flags |= LIST_FLAGS_DISABLED; + + if (!has_prefs) + flags |= LIST_FLAGS_NO_PREFS; + + if (!has_updates) + flags |= LIST_FLAGS_NO_UPDATES; + + return list_extensions (flags, details ? DISPLAY_DETAILED + : DISPLAY_ONELINE) ? 0 : 2; +} diff --git a/subprojects/extensions-tool/src/command-pack.c b/subprojects/extensions-tool/src/command-pack.c new file mode 100644 index 0000000..c8d9950 --- /dev/null +++ b/subprojects/extensions-tool/src/command-pack.c @@ -0,0 +1,516 @@ +/* command-pack.c + * + * Copyright 2018 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +#include +#include + +#include "commands.h" +#include "common.h" +#include "config.h" + +typedef struct _ExtensionPack { + GHashTable *files; + JsonObject *metadata; + GFile *tmpdir; + char *srcdir; +} ExtensionPack; + +static void extension_pack_free (ExtensionPack *); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (ExtensionPack, extension_pack_free); + +static ExtensionPack * +extension_pack_new (const char *srcdir) +{ + ExtensionPack *pack = g_new0 (ExtensionPack, 1); + pack->srcdir = g_strdup (srcdir); + pack->files = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, g_object_unref); + return pack; +} + +static void +extension_pack_free (ExtensionPack *pack) +{ + if (pack->tmpdir) + file_delete_recursively (pack->tmpdir, NULL); + + g_clear_pointer (&pack->files, g_hash_table_destroy); + g_clear_pointer (&pack->metadata, json_object_unref); + g_clear_pointer (&pack->srcdir, g_free); + g_clear_object (&pack->tmpdir); + g_free (pack); +} + +static void +extension_pack_add_source (ExtensionPack *pack, + const char *filename) +{ + g_autoptr (GFile) file = NULL; + file = g_file_new_for_commandline_arg_and_cwd (filename, pack->srcdir); + if (g_file_query_exists (file, NULL)) + g_hash_table_insert (pack->files, + g_path_get_basename (filename), g_steal_pointer (&file)); +} + +static gboolean +extension_pack_check_required_file (ExtensionPack *pack, + const char *filename, + GError **error) +{ + if (!g_hash_table_contains (pack->files, filename)) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, + "Missing %s in extension pack", filename); + return FALSE; + } + return TRUE; +} + +static gboolean +ensure_tmpdir (ExtensionPack *pack, + GError **error) +{ + g_autofree char *path = NULL; + + if (pack->tmpdir != NULL) + return TRUE; + + path = g_dir_make_tmp ("gnome-extensions.XXXXXX", error); + if (path != NULL) + pack->tmpdir = g_file_new_for_path (path); + + return pack->tmpdir != NULL; +} + +static gboolean +ensure_metadata (ExtensionPack *pack, + GError **error) +{ + g_autoptr (JsonParser) parser = NULL; + g_autoptr (GInputStream) stream = NULL; + GFile *file = NULL; + + if (pack->metadata != NULL) + return TRUE; + + if (!extension_pack_check_required_file (pack, "metadata.json", error)) + return FALSE; + + file = g_hash_table_lookup (pack->files, "metadata.json"); + stream = G_INPUT_STREAM (g_file_read (file, NULL, error)); + + if (stream == NULL) + return FALSE; + + parser = json_parser_new_immutable (); + + if (!json_parser_load_from_stream (parser, stream, NULL, error)) + return FALSE; + + pack->metadata = json_node_dup_object (json_parser_get_root (parser)); + return TRUE; +} + +static gboolean +extension_pack_add_schemas (ExtensionPack *pack, + char **schemas, + GError **error) +{ + g_autoptr (GSubprocess) proc = NULL; + g_autoptr (GFile) dstdir = NULL; + g_autofree char *dstpath = NULL; + char **s; + + if (!ensure_tmpdir (pack, error)) + return FALSE; + + dstdir = g_file_get_child (pack->tmpdir, "schemas"); + if (!g_file_make_directory (dstdir, NULL, error)) + return FALSE; + + for (s = schemas; s && *s; s++) + { + g_autoptr (GFile) src = NULL; + g_autoptr (GFile) dst = NULL; + g_autofree char *basename = NULL; + + src = g_file_new_for_commandline_arg_and_cwd (*s, pack->srcdir); + + basename = g_file_get_basename (src); + dst = g_file_get_child (dstdir, basename); + + if (!g_file_copy (src, dst, G_FILE_COPY_NONE, NULL, NULL, NULL, error)) + return FALSE; + } + + dstpath = g_file_get_path (dstdir); + proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDERR_SILENCE, error, + "glib-compile-schemas", "--strict", dstpath, NULL); + + if (!g_subprocess_wait_check (proc, NULL, error)) + return FALSE; + + g_hash_table_insert (pack->files, + g_strdup ("schemas"), g_steal_pointer (&dstdir)); + return TRUE; +} + +static gboolean +extension_pack_add_locales (ExtensionPack *pack, + const char *podir, + const char *gettext_domain, + GError **error) +{ + g_autoptr (GFile) dstdir = NULL; + g_autoptr (GFile) srcdir = NULL; + g_autoptr (GFileEnumerator) file_enum = NULL; + g_autofree char *dstpath = NULL; + g_autofree char *moname = NULL; + GFile *child; + GFileInfo *info; + + if (!ensure_tmpdir (pack, error)) + return FALSE; + + dstdir = g_file_get_child (pack->tmpdir, "locale"); + if (!g_file_make_directory (dstdir, NULL, error)) + return FALSE; + + srcdir = g_file_new_for_commandline_arg_and_cwd (podir, pack->srcdir); + file_enum = g_file_enumerate_children (srcdir, + G_FILE_ATTRIBUTE_STANDARD_NAME, + G_FILE_QUERY_INFO_NONE, + NULL, + error); + if (file_enum == NULL) + return FALSE; + + if (gettext_domain == NULL) + { + if (!ensure_metadata (pack, error)) + return FALSE; + + if (json_object_has_member (pack->metadata, "gettext-domain")) + gettext_domain = json_object_get_string_member (pack->metadata, + "gettext-domain"); + else + gettext_domain = json_object_get_string_member (pack->metadata, + "uuid"); + } + + dstpath = g_file_get_path (dstdir); + moname = g_strdup_printf ("%s.mo", gettext_domain); + + while (TRUE) + { + g_autoptr (GSubprocess) proc = NULL; + g_autoptr (GFile) modir = NULL; + g_autofree char *popath = NULL; + g_autofree char *mopath = NULL; + g_autofree char *lang = NULL; + const char *name; + + if (!g_file_enumerator_iterate (file_enum, &info, &child, NULL, error)) + return FALSE; + + if (info == NULL) + break; + + name = g_file_info_get_name (info); + if (!g_str_has_suffix (name, ".po")) + continue; + + lang = g_strndup (name, strlen (name) - 3 /* strlen (".po") */); + modir = g_file_new_build_filename (dstpath, lang, "LC_MESSAGES", NULL); + if (!g_file_make_directory_with_parents (modir, NULL, error)) + return FALSE; + + mopath = g_build_filename (dstpath, lang, "LC_MESSAGES", moname, NULL); + popath = g_file_get_path (child); + + proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDERR_SILENCE, error, + "msgfmt", "-o", mopath, popath, NULL); + + if (!g_subprocess_wait_check (proc, NULL, error)) + return FALSE; + } + + g_hash_table_insert (pack->files, + g_strdup ("locale"), g_steal_pointer (&dstdir)); + return TRUE; +} + +static void +on_error (AutoarCompressor *compressor, + GError *error, + gpointer data) +{ + *((GError **)data) = g_error_copy (error); +} + +static gboolean +extension_pack_compress (ExtensionPack *pack, + const char *outdir, + gboolean overwrite, + GError **error) +{ + g_autoptr (AutoarCompressor) compressor = NULL; + g_autoptr (GError) err = NULL; + g_autoptr (GFile) outfile = NULL; + g_autofree char *name = NULL; + const char *uuid; + + if (!ensure_metadata (pack, error)) + return FALSE; + + uuid = json_object_get_string_member (pack->metadata, "uuid"); + name = g_strdup_printf ("%s.shell-extension.zip", uuid); + outfile = g_file_new_for_commandline_arg_and_cwd (name, outdir); + + if (g_file_query_exists (outfile, NULL)) + { + if (!overwrite) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS, + "%s exists and --force was not specified", name); + return FALSE; + } + else if (!g_file_delete (outfile, NULL, error)) + { + return FALSE; + } + } + + compressor = autoar_compressor_new (g_hash_table_get_values (pack->files), + outfile, + AUTOAR_FORMAT_ZIP, + AUTOAR_FILTER_NONE, + FALSE); + autoar_compressor_set_output_is_dest (compressor, TRUE); + + g_signal_connect (compressor, "error", G_CALLBACK (on_error), err); + + autoar_compressor_start (compressor, NULL); + + if (err != NULL) + { + g_propagate_error (error, err); + return FALSE; + } + + return TRUE; +} + +static char ** +find_schemas (const char *basepath, + GError **error) +{ + g_autoptr (GFile) basedir = NULL; + g_autoptr (GFile) schemadir = NULL; + g_autoptr (GFileEnumerator) file_enum = NULL; + g_autoptr (GPtrArray) schemas = NULL; + GFile *child; + GFileInfo *info; + + basedir = g_file_new_for_path (basepath); + schemadir = g_file_get_child (basedir, "schemas"); + file_enum = g_file_enumerate_children (schemadir, + G_FILE_ATTRIBUTE_STANDARD_NAME, + G_FILE_QUERY_INFO_NONE, + NULL, error); + + if (error && *error) + { + if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) || + g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY)) + g_clear_error (error); + return NULL; + } + + schemas = g_ptr_array_new_with_free_func (g_free); + + while (TRUE) + { + if (!g_file_enumerator_iterate (file_enum, &info, &child, NULL, error)) + return NULL; + + if (child == NULL) + break; + + if (!g_str_has_suffix (g_file_info_get_name (info), ".gschema.xml")) + continue; + + g_ptr_array_add (schemas, g_file_get_relative_path (basedir, child)); + } + g_ptr_array_add (schemas, NULL); + + return (char **)g_ptr_array_free (g_ptr_array_ref (schemas), FALSE); +} + +static int +pack_extension (char *srcdir, + char *dstdir, + gboolean force, + char **extra_sources, + char **schemas, + char *podir, + char *gettext_domain) +{ + g_autoptr (ExtensionPack) pack = NULL; + g_autoptr (GError) error = NULL; + char **s; + + pack = extension_pack_new (srcdir); + extension_pack_add_source (pack, "extension.js"); + extension_pack_add_source (pack, "metadata.json"); + extension_pack_add_source (pack, "stylesheet.css"); + extension_pack_add_source (pack, "prefs.js"); + + for (s = extra_sources; s && *s; s++) + extension_pack_add_source (pack, *s); + + if (!extension_pack_check_required_file (pack, "extension.js", &error)) + goto err; + + if (!extension_pack_check_required_file (pack, "metadata.json", &error)) + goto err; + + if (schemas == NULL) + schemas = find_schemas (srcdir, &error); + + if (schemas != NULL) + extension_pack_add_schemas (pack, schemas, &error); + + if (error) + goto err; + + if (podir == NULL) + { + g_autoptr (GFile) dir = NULL; + + dir = g_file_new_for_commandline_arg_and_cwd ("po", srcdir); + if (g_file_query_exists (dir, NULL)) + podir = (char *)"po"; + } + + if (podir != NULL) + extension_pack_add_locales (pack, podir, gettext_domain, &error); + + if (error) + goto err; + + extension_pack_compress (pack, dstdir, force, &error); + +err: + if (error) + { + g_printerr ("%s\n", error->message); + return 2; + } + + return 0; +} + +int +handle_pack (int argc, char *argv[], gboolean do_help) +{ + g_autoptr (GOptionContext) context = NULL; + g_autoptr (GError) error = NULL; + g_auto(GStrv) extra_sources = NULL; + g_auto(GStrv) schemas = NULL; + g_auto(GStrv) srcdirs = NULL; + g_autofree char *podir = NULL; + g_autofree char *srcdir = NULL; + g_autofree char *dstdir = NULL; + g_autofree char *gettext_domain = NULL; + gboolean force = FALSE; + GOptionEntry entries[] = { + { .long_name = "extra-source", + .arg = G_OPTION_ARG_FILENAME_ARRAY, .arg_data = &extra_sources, + .arg_description = _("FILE"), + .description = _("Additional source to include in the bundle") }, + { .long_name = "schema", + .arg = G_OPTION_ARG_FILENAME_ARRAY, .arg_data = &schemas, + .arg_description = _("SCHEMA"), + .description = _("A GSettings schema that should be included") }, + { .long_name = "podir", + .arg_description = _("DIRECTORY"), + .arg = G_OPTION_ARG_FILENAME, .arg_data = &podir, + .description = _("The directory where translations are found") }, + { .long_name = "gettext-domain", + .arg_description = _("DOMAIN"), + .arg = G_OPTION_ARG_STRING, .arg_data = &gettext_domain, + .description = _("The gettext domain to use for translations") }, + { .long_name = "force", .short_name = 'f', + .arg = G_OPTION_ARG_NONE, .arg_data = &force, + .description = _("Overwrite an existing pack") }, + { .long_name = "out-dir", .short_name = 'o', + .arg_description = _("DIRECTORY"), + .arg = G_OPTION_ARG_FILENAME, .arg_data = &dstdir, + .description = _("The directory where the pack should be created") }, + { .long_name = G_OPTION_REMAINING, + .arg_description =_("SOURCE_DIRECTORY"), + .arg = G_OPTION_ARG_FILENAME_ARRAY, .arg_data = &srcdirs }, + { NULL } + }; + + g_set_prgname ("gnome-extensions pack"); + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_set_summary (context, _("Create an extension bundle")); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, get_option_group()); + + if (do_help) + { + show_help (context, NULL); + return 0; + } + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + show_help (context, error->message); + return 1; + } + + if (srcdirs) + { + if (g_strv_length (srcdirs) > 1) + { + show_help (context, _("More than one source directory specified")); + return 1; + } + srcdir = g_strdup (*srcdirs); + } + else + { + srcdir = g_get_current_dir (); + } + + if (dstdir == NULL) + dstdir = g_get_current_dir (); + + return pack_extension (srcdir, dstdir, force, + extra_sources, schemas, podir, gettext_domain); +} diff --git a/subprojects/extensions-tool/src/command-prefs.c b/subprojects/extensions-tool/src/command-prefs.c new file mode 100644 index 0000000..50e4b80 --- /dev/null +++ b/subprojects/extensions-tool/src/command-prefs.c @@ -0,0 +1,107 @@ +/* commands-prefs.c + * + * Copyright 2019 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +#include "commands.h" +#include "common.h" +#include "config.h" + +static gboolean +launch_extension_prefs (const char *uuid) +{ + g_autoptr (GDBusProxy) proxy = NULL; + g_autoptr (GVariant) info = NULL; + g_autoptr (GError) error = NULL; + gboolean has_prefs; + + proxy = get_shell_proxy (&error); + if (proxy == NULL) + return FALSE; + + info = get_extension_property (proxy, uuid, "hasPrefs"); + if (info == NULL) + return FALSE; + + has_prefs = g_variant_get_boolean (info); + if (!has_prefs) + { + g_printerr (_("Extension “%s” doesn't have preferences\n"), uuid); + return FALSE; + } + + g_dbus_proxy_call_sync (proxy, + "OpenExtensionPrefs", + g_variant_new ("(ssa{sv})", uuid, "", NULL), + 0, + -1, + NULL, + &error); + + return TRUE; +} + +int +handle_prefs (int argc, char *argv[], gboolean do_help) +{ + g_autoptr (GOptionContext) context = NULL; + g_autoptr (GError) error = NULL; + g_auto(GStrv) uuids = NULL; + GOptionEntry entries[] = { + { .long_name = G_OPTION_REMAINING, + .arg_description = "UUID", + .arg = G_OPTION_ARG_STRING_ARRAY, .arg_data = &uuids }, + { NULL } + }; + + g_set_prgname ("gnome-extensions prefs"); + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_set_summary (context, _("Opens extension preferences")); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, get_option_group()); + + if (do_help) + { + show_help (context, NULL); + return 0; + } + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + show_help (context, error->message); + return 1; + } + + if (uuids == NULL) + { + show_help (context, _("No UUID given")); + return 1; + } + else if (g_strv_length (uuids) > 1) + { + show_help (context, _("More than one UUID given")); + return 1; + } + + return launch_extension_prefs (*uuids) ? 0 : 2; +} diff --git a/subprojects/extensions-tool/src/command-reset.c b/subprojects/extensions-tool/src/command-reset.c new file mode 100644 index 0000000..2615f15 --- /dev/null +++ b/subprojects/extensions-tool/src/command-reset.c @@ -0,0 +1,86 @@ +/* command-reset.c + g_option_context_add_group (context, get_option_group()); + * + * Copyright 2019 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +#include "commands.h" +#include "common.h" +#include "config.h" + +static gboolean +reset_extension (const char *uuid) +{ + g_autoptr(GSettings) settings = get_shell_settings(); + + if (settings == NULL) + return FALSE; + + return settings_list_remove (settings, "enabled-extensions", uuid) && + settings_list_remove (settings, "disabled-extensions", uuid); +} + +int +handle_reset (int argc, char *argv[], gboolean do_help) +{ + g_autoptr (GOptionContext) context = NULL; + g_autoptr (GError) error = NULL; + g_auto(GStrv) uuids = NULL; + GOptionEntry entries[] = { + { .long_name = G_OPTION_REMAINING, + .arg_description = "UUID", + .arg = G_OPTION_ARG_STRING_ARRAY, .arg_data = &uuids }, + { NULL } + }; + + g_set_prgname ("gnome-extensions reset"); + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_set_summary (context, _("Reset an extension")); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, get_option_group()); + + if (do_help) + { + show_help (context, NULL); + return 0; + } + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + show_help (context, error->message); + return 1; + } + + if (uuids == NULL) + { + show_help (context, _("No UUID given")); + return 1; + } + else if (g_strv_length (uuids) > 1) + { + show_help (context, _("More than one UUID given")); + return 1; + } + + return reset_extension (*uuids) ? 0 : 2; +} diff --git a/subprojects/extensions-tool/src/command-uninstall.c b/subprojects/extensions-tool/src/command-uninstall.c new file mode 100644 index 0000000..344b720 --- /dev/null +++ b/subprojects/extensions-tool/src/command-uninstall.c @@ -0,0 +1,114 @@ +/* commands-uninstall.c + * + * Copyright 2019 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +#include "commands.h" +#include "common.h" +#include "config.h" + +static gboolean +uninstall_extension (const char *uuid) +{ + g_autoptr (GDBusProxy) proxy = NULL; + g_autoptr (GVariant) info = NULL; + g_autoptr (GVariant) response = NULL; + g_autoptr (GError) error = NULL; + gboolean success = FALSE; + double type; + + proxy = get_shell_proxy (&error); + if (proxy == NULL) + return FALSE; + + info = get_extension_property (proxy, uuid, "type"); + if (info == NULL) + return FALSE; + + type = g_variant_get_double (info); + if (type == TYPE_SYSTEM) + { + g_printerr (_("Cannot uninstall system extensions\n")); + return FALSE; + } + + response = g_dbus_proxy_call_sync (proxy, + "UninstallExtension", + g_variant_new ("(s)", uuid), + 0, + -1, + NULL, + &error); + + g_variant_get (response, "(b)", &success); + + if (!success) + g_printerr (_("Failed to uninstall “%s”\n"), uuid); + + return success; +} + +int +handle_uninstall (int argc, char *argv[], gboolean do_help) +{ + g_autoptr (GOptionContext) context = NULL; + g_autoptr (GError) error = NULL; + g_auto(GStrv) uuids = NULL; + GOptionEntry entries[] = { + { .long_name = G_OPTION_REMAINING, + .arg_description = "UUID", + .arg = G_OPTION_ARG_STRING_ARRAY, .arg_data = &uuids }, + { NULL } + }; + + g_set_prgname ("gnome-extensions uninstall"); + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_set_summary (context, _("Uninstall an extension")); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, get_option_group()); + + if (do_help) + { + show_help (context, NULL); + return 0; + } + + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + show_help (context, error->message); + return 1; + } + + if (uuids == NULL) + { + show_help (context, _("No UUID given")); + return 1; + } + else if (g_strv_length (uuids) > 1) + { + show_help (context, _("More than one UUID given")); + return 1; + } + + return uninstall_extension (*uuids) ? 0 : 2; +} diff --git a/subprojects/extensions-tool/src/commands.h b/subprojects/extensions-tool/src/commands.h new file mode 100644 index 0000000..618e841 --- /dev/null +++ b/subprojects/extensions-tool/src/commands.h @@ -0,0 +1,38 @@ +/* commands.h + * + * Copyright 2018 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +G_BEGIN_DECLS + +int handle_enable (int argc, char *argv[], gboolean do_help); +int handle_disable (int argc, char *argv[], gboolean do_help); +int handle_reset (int argc, char *argv[], gboolean do_help); +int handle_list (int argc, char *argv[], gboolean do_help); +int handle_info (int argc, char *argv[], gboolean do_help); +int handle_prefs (int argc, char *argv[], gboolean do_help); +int handle_create (int argc, char *argv[], gboolean do_help); +int handle_pack (int argc, char *argv[], gboolean do_help); +int handle_install (int argc, char *argv[], gboolean do_help); +int handle_uninstall (int argc, char *argv[], gboolean do_help); + +G_END_DECLS diff --git a/subprojects/extensions-tool/src/common.h b/subprojects/extensions-tool/src/common.h new file mode 100644 index 0000000..2b04484 --- /dev/null +++ b/subprojects/extensions-tool/src/common.h @@ -0,0 +1,73 @@ +/* common.h + * + * Copyright 2018 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +G_BEGIN_DECLS + +typedef enum { + TYPE_SYSTEM = 1, + TYPE_USER +} ExtensionType; + +typedef enum { + STATE_ENABLED = 1, + STATE_DISABLED, + STATE_ERROR, + STATE_OUT_OF_DATE, + STATE_DOWNLOADING, + STATE_INITIALIZED, + + STATE_UNINSTALLED = 99 +} ExtensionState; + +typedef enum { + DISPLAY_ONELINE, + DISPLAY_DETAILED +} DisplayFormat; + +GOptionGroup *get_option_group (void); + +void show_help (GOptionContext *context, + const char *message); + +void print_extension_info (GVariantDict *info, + DisplayFormat format); + +GDBusProxy *get_shell_proxy (GError **error); +GVariant *get_extension_property (GDBusProxy *proxy, + const char *uuid, + const char *property); + +GSettings *get_shell_settings (void); + +gboolean settings_list_add (GSettings *settings, + const char *key, + const char *value); +gboolean settings_list_remove (GSettings *settings, + const char *key, + const char *value); + +gboolean file_delete_recursively (GFile *file, + GError **error); + +G_END_DECLS diff --git a/subprojects/extensions-tool/src/gnome-extensions-tool.gresource.xml b/subprojects/extensions-tool/src/gnome-extensions-tool.gresource.xml new file mode 100644 index 0000000..0db87c3 --- /dev/null +++ b/subprojects/extensions-tool/src/gnome-extensions-tool.gresource.xml @@ -0,0 +1,11 @@ + + + + templates/00-plain.desktop + templates/indicator.desktop + templates/indicator/extension.js + templates/indicator/stylesheet.css + templates/plain/extension.js + templates/plain/stylesheet.css + + diff --git a/subprojects/extensions-tool/src/main.c b/subprojects/extensions-tool/src/main.c new file mode 100644 index 0000000..c33058d --- /dev/null +++ b/subprojects/extensions-tool/src/main.c @@ -0,0 +1,408 @@ +/* main.c + * + * Copyright 2018 Florian Müllner + * + * 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 3 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#include + +#include "config.h" +#include "commands.h" +#include "common.h" + +static const char * +extension_state_to_string (ExtensionState state) +{ + switch (state) + { + case STATE_ENABLED: + return "ENABLED"; + case STATE_DISABLED: + return "DISABLED"; + case STATE_ERROR: + return "ERROR"; + case STATE_OUT_OF_DATE: + return "OUT OF DATE"; + case STATE_DOWNLOADING: + return "DOWNLOADING"; + case STATE_INITIALIZED: + return "INITIALIZED"; + case STATE_UNINSTALLED: + return "UNINSTALLED"; + } + return "UNKNOWN"; +} + +static void +print_nothing (const char *message) +{ +} + +static gboolean +quiet_cb (const gchar *option_name, + const gchar *value, + gpointer data, + GError **error) +{ + g_set_printerr_handler (print_nothing); + return TRUE; +} + +GOptionGroup * +get_option_group () +{ + GOptionEntry entries[] = { + { .long_name = "quiet", .short_name = 'q', + .description = _("Do not print error messages"), + .arg = G_OPTION_ARG_CALLBACK, .arg_data = &quiet_cb, + .flags = G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_IN_MAIN }, + { NULL } + }; + GOptionGroup *group; + + group = g_option_group_new ("Common", "common options", "common options", NULL, NULL); + g_option_group_add_entries (group, entries); + + return group; +} + +void +show_help (GOptionContext *context, const char *message) +{ + g_autofree char *help = NULL; + + if (message) + g_printerr ("gnome-extensions: %s\n\n", message); + + help = g_option_context_get_help (context, TRUE, NULL); + g_printerr ("%s", help); +} + +GDBusProxy * +get_shell_proxy (GError **error) +{ + return g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.gnome.Shell.Extensions", + "/org/gnome/Shell/Extensions", + "org.gnome.Shell.Extensions", + NULL, + error); +} + +GSettings * +get_shell_settings (void) +{ + g_autoptr (GSettingsSchema) schema = NULL; + GSettingsSchemaSource *schema_source; + + schema_source = g_settings_schema_source_get_default (); + schema = g_settings_schema_source_lookup (schema_source, + "org.gnome.shell", + TRUE); + + if (schema == NULL) + return NULL; + + return g_settings_new_full (schema, NULL, NULL); +} + +GVariant * +get_extension_property (GDBusProxy *proxy, + const char *uuid, + const char *property) +{ + g_autoptr (GVariant) response = NULL; + g_autoptr (GVariant) asv = NULL; + g_autoptr (GVariantDict) info = NULL; + g_autoptr (GError) error = NULL; + + response = g_dbus_proxy_call_sync (proxy, + "GetExtensionInfo", + g_variant_new ("(s)", uuid), + 0, + -1, + NULL, + &error); + if (response == NULL) + { + g_printerr (_("Failed to connect to GNOME Shell")); + return NULL; + } + + asv = g_variant_get_child_value (response, 0); + info = g_variant_dict_new (asv); + + if (!g_variant_dict_contains (info, "uuid")) + { + g_printerr (_("Extension “%s” doesn't exist\n"), uuid); + return NULL; + } + + return g_variant_dict_lookup_value (info, property, NULL); +} + +gboolean +settings_list_add (GSettings *settings, + const char *key, + const char *value) +{ + g_auto(GStrv) list = NULL; + g_auto(GStrv) new_value = NULL; + guint n_values; + int i; + + if (!g_settings_is_writable (settings, key)) + return FALSE; + + list = g_settings_get_strv (settings, key); + + if (g_strv_contains ((const char **)list, value)) + return TRUE; + + n_values = g_strv_length (list); + new_value = g_new0 (char *, n_values + 2); + for (i = 0; i < n_values; i++) + new_value[i] = g_strdup (list[i]); + new_value[i] = g_strdup (value); + + g_settings_set_strv (settings, key, (const char **)new_value); + g_settings_sync (); + + return TRUE; +} + +gboolean +settings_list_remove (GSettings *settings, + const char *key, + const char *value) +{ + g_auto(GStrv) list = NULL; + g_auto(GStrv) new_value = NULL; + const char **s; + guint n_values; + int i; + + if (!g_settings_is_writable (settings, key)) + return FALSE; + + list = g_settings_get_strv (settings, key); + + if (!g_strv_contains ((const char **)list, value)) + return TRUE; + + n_values = g_strv_length (list); + new_value = g_new0 (char *, n_values); + i = 0; + for (s = (const char **)list; *s != NULL; s++) + if (!g_str_equal (*s, value)) + new_value[i++] = g_strdup (*s); + + g_settings_set_strv (settings, key, (const char **)new_value); + g_settings_sync (); + + return TRUE; +} + +void +print_extension_info (GVariantDict *info, + DisplayFormat format) +{ + const char *uuid, *name, *desc, *path, *url, *author; + double state, version; + + g_variant_dict_lookup (info, "uuid", "&s", &uuid); + g_print ("%s\n", uuid); + + if (format == DISPLAY_ONELINE) + return; + + g_variant_dict_lookup (info, "name", "&s", &name); + g_print (" %s: %s\n", _("Name"), name); + + g_variant_dict_lookup (info, "description", "&s", &desc); + g_print (" %s: %s\n", _("Description"), desc); + + g_variant_dict_lookup (info, "path", "&s", &path); + g_print (" %s: %s\n", _("Path"), path); + + if (g_variant_dict_lookup (info, "url", "&s", &url)) + g_print (" %s: %s\n", _("URL"), url); + + if (g_variant_dict_lookup (info, "original-author", "&s", &author)) + g_print (" %s: %s\n", _("Original author"), author); + + if (g_variant_dict_lookup (info, "version", "d", &version)) + g_print (" %s: %.0f\n", _("Version"), version); + + g_variant_dict_lookup (info, "state", "d", &state); + g_print (" %s: %s\n", _("State"), extension_state_to_string (state)); +} + +gboolean +file_delete_recursively (GFile *file, + GError **error) +{ + g_autoptr (GFileEnumerator) file_enum = NULL; + GFile *child; + + file_enum = g_file_enumerate_children (file, NULL, 0, NULL, NULL); + if (file_enum) + while (TRUE) + { + if (!g_file_enumerator_iterate (file_enum, NULL, &child, NULL, error)) + return FALSE; + + if (child == NULL) + break; + + if (!file_delete_recursively (child, error)) + return FALSE; + } + + return g_file_delete (file, NULL, error); +} + + +static int +handle_version (int argc, char *argv[], gboolean do_help) +{ + if (do_help || argc > 1) + { + if (!do_help) + g_printerr ("gnome-extensions: %s\n\n", _("“version” takes no arguments")); + + g_printerr ("%s\n", _("Usage:")); + g_printerr (" gnome-extensions version\n"); + g_printerr ("\n"); + g_printerr ("%s\n", _("Print version information and exit.")); + + return do_help ? 0 : 2; + } + + g_print ("%s\n", VERSION); + + return 0; +} + +static void +usage (void) +{ + g_autofree char *help_command = NULL; + + help_command = g_strdup_printf ("gnome-extensions help %s", _("COMMAND")); + + g_printerr ("%s\n", _("Usage:")); + g_printerr (" gnome-extensions %s %s\n", _("COMMAND"), _("[ARGS…]")); + g_printerr ("\n"); + g_printerr ("%s\n", _("Commands:")); + g_printerr (" help %s\n", _("Print help")); + g_printerr (" version %s\n", _("Print version")); + g_printerr (" enable %s\n", _("Enable extension")); + g_printerr (" disable %s\n", _("Disable extension")); + g_printerr (" reset %s\n", _("Reset extension")); + g_printerr (" uninstall %s\n", _("Uninstall extension")); + g_printerr (" list %s\n", _("List extensions")); + g_printerr (" info %s\n", _("Show extension info")); + g_printerr (" show %s\n", _("Show extension info")); + g_printerr (" prefs %s\n", _("Open extension preferences")); + g_printerr (" create %s\n", _("Create extension")); + g_printerr (" pack %s\n", _("Package extension")); + g_printerr (" install %s\n", _("Install extension bundle")); + g_printerr ("\n"); + g_printerr (_("Use “%s” to get detailed help.\n"), help_command); +} + +int +main (int argc, char *argv[]) +{ + const char *command; + gboolean do_help = FALSE; + + setlocale (LC_ALL, ""); + textdomain (GETTEXT_PACKAGE); + bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); + +#ifdef HAVE_BIND_TEXTDOMAIN_CODESET + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); +#endif + + if (argc < 2) + { + usage (); + return 1; + } + + command = argv[1]; + argc--; + argv++; + + if (g_str_equal (command, "help")) + { + if (argc == 1) + { + usage (); + return 0; + } + else + { + command = argv[1]; + do_help = TRUE; + } + } + else if (g_str_equal (command, "--help")) + { + usage (); + return 0; + } + else if (g_str_equal (command, "--version")) + { + command = "version"; + } + + if (g_str_equal (command, "version")) + return handle_version (argc, argv, do_help); + else if (g_str_equal (command, "enable")) + return handle_enable (argc, argv, do_help); + else if (g_str_equal (command, "disable")) + return handle_disable (argc, argv, do_help); + else if (g_str_equal (command, "reset")) + return handle_reset (argc, argv, do_help); + else if (g_str_equal (command, "list")) + return handle_list (argc, argv, do_help); + else if (g_str_equal (command, "info")) + return handle_info (argc, argv, do_help); + else if (g_str_equal (command, "show")) + return handle_info (argc, argv, do_help); + else if (g_str_equal (command, "prefs")) + return handle_prefs (argc, argv, do_help); + else if (g_str_equal (command, "create")) + return handle_create (argc, argv, do_help); + else if (g_str_equal (command, "pack")) + return handle_pack (argc, argv, do_help); + else if (g_str_equal (command, "install")) + return handle_install (argc, argv, do_help); + else if (g_str_equal (command, "uninstall")) + return handle_uninstall (argc, argv, do_help); + else + usage (); + + return 1; +} diff --git a/subprojects/extensions-tool/src/meson.build b/subprojects/extensions-tool/src/meson.build new file mode 100644 index 0000000..a855fef --- /dev/null +++ b/subprojects/extensions-tool/src/meson.build @@ -0,0 +1,37 @@ +config_h = configuration_data() +config_h.set_quoted('GETTEXT_PACKAGE', package_name) +config_h.set_quoted('VERSION', meson.project_version()) +config_h.set_quoted('LOCALEDIR', localedir) +config_h.set('HAVE_BIND_TEXTDOMAIN_CODESET', cc.has_function('bind_textdomain_codeset')) +configure_file( + output: 'config.h', + configuration: config_h, +) + +sources = [ + 'command-create.c', + 'command-disable.c', + 'command-enable.c', + 'command-info.c', + 'command-install.c', + 'command-list.c', + 'command-pack.c', + 'command-prefs.c', + 'command-reset.c', + 'command-uninstall.c', + 'main.c' +] + +subdir('templates') + +resources = gnome.compile_resources('resources', + 'gnome-extensions-tool.gresource.xml', + source_dir: ['.', meson.current_build_dir()], + dependencies: template_deps, +) + +executable('gnome-extensions', + sources, resources, + dependencies: [gio_dep, gio_unix_dep, autoar_dep, json_dep], + install: true +) diff --git a/subprojects/extensions-tool/src/templates/00-plain.desktop.in b/subprojects/extensions-tool/src/templates/00-plain.desktop.in new file mode 100644 index 0000000..36ddf80 --- /dev/null +++ b/subprojects/extensions-tool/src/templates/00-plain.desktop.in @@ -0,0 +1,5 @@ +[Desktop Entry] +Type=Application +Name=Plain +Comment=An empty extension +Path=plain diff --git a/subprojects/extensions-tool/src/templates/indicator.desktop.in b/subprojects/extensions-tool/src/templates/indicator.desktop.in new file mode 100644 index 0000000..1718e94 --- /dev/null +++ b/subprojects/extensions-tool/src/templates/indicator.desktop.in @@ -0,0 +1,5 @@ +[Desktop Entry] +Type=Application +Name=Indicator +Comment=Add an icon to the top bar +Path=indicator diff --git a/subprojects/extensions-tool/src/templates/indicator/extension.js b/subprojects/extensions-tool/src/templates/indicator/extension.js new file mode 100644 index 0000000..fd84782 --- /dev/null +++ b/subprojects/extensions-tool/src/templates/indicator/extension.js @@ -0,0 +1,74 @@ +/* extension.js + * + * 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 . + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +/* exported init */ + +const GETTEXT_DOMAIN = 'my-indicator-extension'; + +const { GObject, St } = imports.gi; + +const Gettext = imports.gettext.domain(GETTEXT_DOMAIN); +const _ = Gettext.gettext; + +const ExtensionUtils = imports.misc.extensionUtils; +const Main = imports.ui.main; +const PanelMenu = imports.ui.panelMenu; +const PopupMenu = imports.ui.popupMenu; + +const Indicator = GObject.registerClass( +class Indicator extends PanelMenu.Button { + _init() { + super._init(0.0, _('My Shiny Indicator')); + + let box = new St.BoxLayout({ style_class: 'panel-status-menu-box' }); + box.add_child(new St.Icon({ + icon_name: 'face-smile-symbolic', + style_class: 'system-status-icon', + })); + box.add_child(PopupMenu.arrowIcon(St.Side.BOTTOM)); + this.add_child(box); + + let item = new PopupMenu.PopupMenuItem(_('Show Notification')); + item.connect('activate', () => { + Main.notify(_('Whatʼs up, folks?')); + }); + this.menu.addMenuItem(item); + } +}); + +class Extension { + constructor(uuid) { + this._uuid = uuid; + + ExtensionUtils.initTranslations(GETTEXT_DOMAIN); + } + + enable() { + this._indicator = new Indicator(); + Main.panel.addToStatusArea(this._uuid, this._indicator); + } + + disable() { + this._indicator.destroy(); + this._indicator = null; + } +} + +function init(meta) { + return new Extension(meta.uuid); +} diff --git a/subprojects/extensions-tool/src/templates/indicator/stylesheet.css b/subprojects/extensions-tool/src/templates/indicator/stylesheet.css new file mode 100644 index 0000000..37b93f2 --- /dev/null +++ b/subprojects/extensions-tool/src/templates/indicator/stylesheet.css @@ -0,0 +1 @@ +/* Add your custom extension styling here */ diff --git a/subprojects/extensions-tool/src/templates/meson.build b/subprojects/extensions-tool/src/templates/meson.build new file mode 100644 index 0000000..670e2bf --- /dev/null +++ b/subprojects/extensions-tool/src/templates/meson.build @@ -0,0 +1,13 @@ +template_metas = [ + '00-plain.desktop', + 'indicator.desktop', +] +template_deps = [] +foreach template : template_metas + template_deps += i18n.merge_file(template, + input: template + '.in', + output: template, + po_dir: po_dir, + type: 'desktop', + ) +endforeach diff --git a/subprojects/extensions-tool/src/templates/plain/extension.js b/subprojects/extensions-tool/src/templates/plain/extension.js new file mode 100644 index 0000000..64857af --- /dev/null +++ b/subprojects/extensions-tool/src/templates/plain/extension.js @@ -0,0 +1,34 @@ +/* extension.js + * + * 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 . + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +/* exported init */ + +class Extension { + constructor() { + } + + enable() { + } + + disable() { + } +} + +function init() { + return new Extension(); +} diff --git a/subprojects/extensions-tool/src/templates/plain/stylesheet.css b/subprojects/extensions-tool/src/templates/plain/stylesheet.css new file mode 100644 index 0000000..37b93f2 --- /dev/null +++ b/subprojects/extensions-tool/src/templates/plain/stylesheet.css @@ -0,0 +1 @@ +/* Add your custom extension styling here */ -- cgit v1.2.3