Improve APIs for finding default input sources

This commit is contained in:
Ray Strode 2023-08-26 10:17:22 -04:00
parent 6013028c77
commit fe26aecaf1

View File

@ -1,4 +1,4 @@
From 38c1d2a3c70ba3785cfb22b0baf1f5d97b63b90a Mon Sep 17 00:00:00 2001 From 9f93dd73c5ed57bb2fb1b572c9282d499d1c7fd7 Mon Sep 17 00:00:00 2001
From: Ray Strode <rstrode@redhat.com> From: Ray Strode <rstrode@redhat.com>
Date: Thu, 24 Aug 2023 19:58:17 -0400 Date: Thu, 24 Aug 2023 19:58:17 -0400
Subject: [PATCH 4/4] languages: Add functions for getting default input Subject: [PATCH 4/4] languages: Add functions for getting default input
@ -17,15 +17,15 @@ gnome_get_default_input_sources_finish
Note, these functions don't provide change notification, so there Note, these functions don't provide change notification, so there
is still some redundancy needed by callers. is still some redundancy needed by callers.
--- ---
libgnome-desktop/gnome-languages.c | 234 +++++++++++++++++++++++++++++ libgnome-desktop/gnome-languages.c | 256 +++++++++++++++++++++++++++++
libgnome-desktop/gnome-languages.h | 9 ++ libgnome-desktop/gnome-languages.h | 10 ++
2 files changed, 243 insertions(+) 2 files changed, 266 insertions(+)
diff --git a/libgnome-desktop/gnome-languages.c b/libgnome-desktop/gnome-languages.c diff --git a/libgnome-desktop/gnome-languages.c b/libgnome-desktop/gnome-languages.c
index cc9bd005..65ea2845 100644 index cc9bd005..072e1b07 100644
--- a/libgnome-desktop/gnome-languages.c --- a/libgnome-desktop/gnome-languages.c
+++ b/libgnome-desktop/gnome-languages.c +++ b/libgnome-desktop/gnome-languages.c
@@ -17,60 +17,62 @@ @@ -17,80 +17,87 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>. * along with this program; if not, see <http://www.gnu.org/licenses/>.
* *
* Written by : William Jon McCann <mccann@jhu.edu> * Written by : William Jon McCann <mccann@jhu.edu>
@ -78,6 +78,11 @@ index cc9bd005..65ea2845 100644
char *modifier; char *modifier;
} GnomeLocale; } GnomeLocale;
+typedef struct _GnomeInputSourceDefaults {
+ InputSource **input_sources;
+ char **options;
+} GnomeInputSourceDefaults;
+
static GHashTable *gnome_languages_map; static GHashTable *gnome_languages_map;
static GHashTable *gnome_territories_map; static GHashTable *gnome_territories_map;
static GHashTable *gnome_available_locales_map; static GHashTable *gnome_available_locales_map;
@ -88,7 +93,27 @@ index cc9bd005..65ea2845 100644
const char *territory, const char *territory,
const char *codeset, const char *codeset,
const char *modifier); const char *modifier);
@@ -1440,30 +1442,262 @@ gnome_get_input_source_from_locale (const char *locale,
static gboolean language_name_is_valid (const char *language_name);
static void
gnome_locale_free (GnomeLocale *locale)
{
if (locale == NULL) {
return;
}
g_free (locale->id);
g_free (locale->name);
g_free (locale->codeset);
g_free (locale->modifier);
g_free (locale->language_code);
g_free (locale->territory_code);
g_free (locale);
}
static char *
@@ -1440,30 +1447,279 @@ gnome_get_input_source_from_locale (const char *locale,
} }
/** /**
@ -172,16 +197,18 @@ index cc9bd005..65ea2845 100644
+} +}
+ +
+static void +static void
+input_sources_free (InputSource **input_sources) +gnome_input_source_defaults_free (GnomeInputSourceDefaults *defaults)
+{ +{
+ size_t i; + size_t i;
+ +
+ for (i = 0; input_sources[i] != NULL; i++) { + for (i = 0; defaults->input_sources[i] != NULL; i++) {
+ g_free (input_sources[i]->type); + g_free (defaults->input_sources[i]->type);
+ g_free (input_sources[i]->id); + g_free (defaults->input_sources[i]->id);
+ g_free (input_sources[i]); + g_free (defaults->input_sources[i]);
+ } + }
+ g_free (input_sources); + g_free (defaults->input_sources);
+ g_strfreev (defaults->options);
+ g_free (defaults);
+} +}
+ +
+static void +static void
@ -198,12 +225,15 @@ index cc9bd005..65ea2845 100644
+ g_autofree char *system_locale = NULL; + g_autofree char *system_locale = NULL;
+ g_autofree char *layouts_string = NULL; + g_autofree char *layouts_string = NULL;
+ g_autofree char *variants_string = NULL; + g_autofree char *variants_string = NULL;
+ g_autofree char *options_string = NULL;
+ g_auto(GStrv) layouts = NULL; + g_auto(GStrv) layouts = NULL;
+ size_t number_of_layouts = 0; + size_t number_of_layouts = 0;
+ g_auto(GStrv) variants = NULL; + g_auto(GStrv) variants = NULL;
+ size_t number_of_variants = 0; + size_t number_of_variants = 0;
+ g_auto(GStrv) options = NULL;
+ const char *type = NULL, *id = NULL; + const char *type = NULL, *id = NULL;
+ size_t i; + size_t i;
+ GnomeInputSourceDefaults *defaults;
+ +
+ proxy = g_task_propagate_pointer (G_TASK (result), &error); + proxy = g_task_propagate_pointer (G_TASK (result), &error);
+ +
@ -237,6 +267,7 @@ index cc9bd005..65ea2845 100644
+ +
+ layouts_string = sd_dbus_locale1_dup_x11_layout (proxy); + layouts_string = sd_dbus_locale1_dup_x11_layout (proxy);
+ variants_string = sd_dbus_locale1_dup_x11_variant (proxy); + variants_string = sd_dbus_locale1_dup_x11_variant (proxy);
+ options_string = sd_dbus_locale1_dup_x11_options (proxy);
+ +
+ layouts = g_strsplit (layouts_string, ",", -1); + layouts = g_strsplit (layouts_string, ",", -1);
+ +
@ -246,6 +277,8 @@ index cc9bd005..65ea2845 100644
+ variants = g_strdupv ((char *[]) { "", NULL }); + variants = g_strdupv ((char *[]) { "", NULL });
+ } + }
+ +
+ options = g_strsplit (options_string, ",", -1);
+
+ number_of_layouts = g_strv_length (layouts); + number_of_layouts = g_strv_length (layouts);
+ number_of_variants = g_strv_length (variants); + number_of_variants = g_strv_length (variants);
+ +
@ -279,7 +312,11 @@ index cc9bd005..65ea2845 100644
+ } + }
+ g_ptr_array_add (input_sources, NULL); + g_ptr_array_add (input_sources, NULL);
+ +
+ g_task_return_pointer (main_task, g_ptr_array_steal (input_sources, NULL), (GDestroyNotify) input_sources_free); + defaults = g_new0 (GnomeInputSourceDefaults, 1);
+ defaults->input_sources = (InputSource **) g_ptr_array_steal (input_sources, NULL);
+ defaults->options = g_steal_pointer (&options);
+
+ g_task_return_pointer (main_task, defaults, (GDestroyNotify) gnome_input_source_defaults_free);
+} +}
+ +
+/** +/**
@ -314,7 +351,8 @@ index cc9bd005..65ea2845 100644
+/** +/**
+ * gnome_get_default_input_sources_finish: + * gnome_get_default_input_sources_finish:
+ * @result: a #GAsyncResult + * @result: a #GAsyncResult
+ * @types: (transfer full): an array of types (either "xkb" or "ibus") + * @types: (out) (transfer full): an array of types (either "xkb" or "ibus")
+ * @options: (out) (transfer full): an options string to use with all input sources
+ * @error: a #GError + * @error: a #GError
+ * + *
+ * Returns: (transfer full): a list of default input sources based on locale and system + * Returns: (transfer full): a list of default input sources based on locale and system
@ -326,36 +364,40 @@ index cc9bd005..65ea2845 100644
+char ** +char **
+gnome_get_default_input_sources_finish (GAsyncResult *result, +gnome_get_default_input_sources_finish (GAsyncResult *result,
+ char ***types, + char ***types,
+ char ***options,
+ GError **error) + GError **error)
+{ +{
+ InputSource **input_sources = NULL; + GnomeInputSourceDefaults *defaults = NULL;
+ size_t i; + size_t i;
+ g_autoptr (GStrvBuilder) ids_builder = NULL; + g_autoptr (GStrvBuilder) ids_builder = NULL;
+ g_autoptr (GStrvBuilder) types_builder = NULL; + g_autoptr (GStrvBuilder) types_builder = NULL;
+ +
+ input_sources = g_task_propagate_pointer (G_TASK (result), error); + defaults = g_task_propagate_pointer (G_TASK (result), error);
+ +
+ if (input_sources == NULL) { + if (defaults == NULL) {
+ return NULL; + return NULL;
+ } + }
+ +
+ ids_builder = g_strv_builder_new (); + ids_builder = g_strv_builder_new ();
+ types_builder = g_strv_builder_new (); + types_builder = g_strv_builder_new ();
+ +
+ for (i = 0; input_sources[i] != NULL; i++) { + for (i = 0; defaults->input_sources[i] != NULL; i++) {
+ g_strv_builder_add (types_builder, input_sources[i]->type); + g_strv_builder_add (types_builder, defaults->input_sources[i]->type);
+ g_strv_builder_add (ids_builder, input_sources[i]->id); + g_strv_builder_add (ids_builder, defaults->input_sources[i]->id);
+ } + }
+ +
+ if (types != NULL) + if (types != NULL)
+ *types = g_strv_builder_end (types_builder); + *types = g_strv_builder_end (types_builder);
+
+ if (options != NULL)
+ *options = g_strdupv (defaults->options);
+ return g_strv_builder_end (ids_builder); + return g_strv_builder_end (ids_builder);
+} +}
diff --git a/libgnome-desktop/gnome-languages.h b/libgnome-desktop/gnome-languages.h diff --git a/libgnome-desktop/gnome-languages.h b/libgnome-desktop/gnome-languages.h
index ed9242e7..6876a0bb 100644 index ed9242e7..e2cffa62 100644
--- a/libgnome-desktop/gnome-languages.h --- a/libgnome-desktop/gnome-languages.h
+++ b/libgnome-desktop/gnome-languages.h +++ b/libgnome-desktop/gnome-languages.h
@@ -1,61 +1,70 @@ @@ -1,61 +1,71 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
* *
* Copyright 2008 Red Hat, Inc. * Copyright 2008 Red Hat, Inc.
@ -422,6 +464,7 @@ index ed9242e7..6876a0bb 100644
+ +
+char ** gnome_get_default_input_sources_finish (GAsyncResult *result, +char ** gnome_get_default_input_sources_finish (GAsyncResult *result,
+ char ***types, + char ***types,
+ char ***options,
+ GError **error); + GError **error);
G_END_DECLS G_END_DECLS