extensions-tool: Implement enable/disable commands

This replicates the most basic functionality of the existing
gnome-shell-extension-tool, albeit using a git/gio/gsettings
style command interface rather than plain options; this will
allow us to implement more complex commands that have options
on their own in the future.

https://gitlab.gnome.org/GNOME/gnome-shell/issues/1234
This commit is contained in:
Florian Müllner 2018-08-26 21:37:07 +02:00
parent d8c7cac536
commit c8c93b2a70
7 changed files with 323 additions and 2 deletions

View File

@ -0,0 +1,105 @@
/* command-disable.c
*
* Copyright 2018 Florian Müllner <fmuellner@gnome.org>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <glib/gi18n.h>
#include <gio/gio.h>
#include "commands.h"
#include "common.h"
#include "config.h"
static gboolean
disable_extension (const char *uuid)
{
g_autoptr(GSettings) settings = get_shell_settings ();
g_auto(GStrv) extensions = NULL;
g_auto(GStrv) new_value = NULL;
const char **s;
guint n_extensions;
int i;
if (settings == NULL)
return FALSE;
if (!g_settings_is_writable (settings, "enabled-extensions"))
return FALSE;
extensions = g_settings_get_strv (settings, "enabled-extensions");
if (!g_strv_contains ((const char **)extensions, uuid))
return TRUE;
n_extensions = g_strv_length (extensions);
new_value = g_new0 (char *, n_extensions);
for (i = 0, s = (const char **)extensions; i < n_extensions; i++, s++)
if (!g_str_equal (*s, uuid))
new_value[i] = g_strdup (*s);
g_settings_set_strv (settings, "enabled-extensions", (const char **)new_value);
g_settings_sync ();
return TRUE;
}
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);
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;
}

View File

@ -0,0 +1,104 @@
/* command-enable.c
*
* Copyright 2018 Florian Müllner <fmuellner@gnome.org>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <glib/gi18n.h>
#include <gio/gio.h>
#include "commands.h"
#include "common.h"
#include "config.h"
static gboolean
enable_extension (const char *uuid)
{
g_autoptr(GSettings) settings = get_shell_settings ();
g_auto(GStrv) extensions = NULL;
g_auto(GStrv) new_value = NULL;
guint n_extensions;
int i;
if (settings == NULL)
return FALSE;
if (!g_settings_is_writable (settings, "enabled-extensions"))
return FALSE;
extensions = g_settings_get_strv (settings, "enabled-extensions");
if (g_strv_contains ((const char **)extensions, uuid))
return TRUE;
n_extensions = g_strv_length (extensions);
new_value = g_new0 (char *, n_extensions + 2);
for (i = 0; i < n_extensions; i++)
new_value[i] = g_strdup (extensions[i]);
new_value[i] = g_strdup (uuid);
g_settings_set_strv (settings, "enabled-extensions", (const char **)new_value);
g_settings_sync ();
return TRUE;
}
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);
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;
}

View File

@ -0,0 +1,30 @@
/* commands.h
*
* Copyright 2018 Florian Müllner <fmuellner@gnome.org>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <glib.h>
G_BEGIN_DECLS
int handle_enable (int argc, char *argv[], gboolean do_help);
int handle_disable (int argc, char *argv[], gboolean do_help);
G_END_DECLS

View File

@ -0,0 +1,32 @@
/* common.h
*
* Copyright 2018 Florian Müllner <fmuellner@gnome.org>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <glib.h>
G_BEGIN_DECLS
void show_help (GOptionContext *context,
const char *message);
GSettings *get_shell_settings (void);
G_END_DECLS

View File

@ -23,6 +23,37 @@
#include <locale.h>
#include "config.h"
#include "commands.h"
#include "common.h"
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);
}
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);
}
static int
handle_version (int argc, char *argv[], gboolean do_help)
@ -54,6 +85,8 @@ usage (void)
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 ("\n");
g_printerr (_("Use %s to get detailed help.\n"), "“gnome-extensions help COMMAND”");
}
@ -107,6 +140,10 @@ main (int argc, char *argv[])
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
usage ();

View File

@ -1,5 +1,12 @@
sources = [
'commands.h',
'command-disable.c',
'command-enable.c',
'common.h',
'main.c'
]
executable('gnome-extensions',
'main.c',
sources,
dependencies: gio_dep,
install: true
)

View File

@ -8,8 +8,14 @@ configure_file(
configuration: config_h,
)
sources = [
'command-disable.c',
'command-enable.c',
'main.c'
]
executable('gnome-extensions',
'main.c',
sources,
dependencies: gio_dep,
install: true
)