[gnome-software/wip/hughsie/langpacks] Implement gs_plugin_add_langpacks to install locale data



commit 03b2f5138b28bc874a28b679d0ca4f0f31be10f4
Author: Sundeep Anand <suanand redhat com>
Date:   Mon Apr 1 20:40:30 2019 +0530

    Implement gs_plugin_add_langpacks to install locale data
    
    Can be used to add language packs to apps list as per active locale.

 contrib/gnome-software.spec.in                     |  1 +
 lib/gs-plugin-loader.c                             | 21 +++++
 lib/gs-plugin-loader.h                             |  2 +
 lib/gs-plugin-types.h                              |  2 +
 lib/gs-plugin-vfuncs.h                             | 18 ++++
 lib/gs-plugin.c                                    |  6 ++
 .../fedora-langpacks/gs-plugin-fedora-langpacks.c  | 98 ++++++++++++++++++++++
 plugins/fedora-langpacks/gs-self-test.c            | 81 ++++++++++++++++++
 plugins/fedora-langpacks/meson.build               | 40 +++++++++
 plugins/meson.build                                |  1 +
 src/gs-update-monitor.c                            | 82 ++++++++++++++++++
 11 files changed, 352 insertions(+)
---
diff --git a/contrib/gnome-software.spec.in b/contrib/gnome-software.spec.in
index aeb9b552..3cfa147e 100644
--- a/contrib/gnome-software.spec.in
+++ b/contrib/gnome-software.spec.in
@@ -165,6 +165,7 @@ desktop-file-validate %{buildroot}%{_datadir}/applications/*.desktop
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_dpkg.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_dummy.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_epiphany.so
+%{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_fedora_langpacks.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_fedora-pkgdb-collections.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_flatpak.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_fwupd.so
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index 937986fd..40fd0cb5 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -162,6 +162,12 @@ typedef gboolean    (*GsPluginUpdateFunc)          (GsPlugin       *plugin,
                                                         GError         **error);
 typedef void            (*GsPluginAdoptAppFunc)        (GsPlugin       *plugin,
                                                         GsApp          *app);
+typedef gboolean        (*GsPluginGetLangPacksFunc)    (GsPlugin       *plugin,
+                                                        GsAppList      *list,
+                                                        const gchar    *locale,
+                                                        GCancellable   *cancellable,
+                                                        GError         **error);
+
 
 /* async helper */
 typedef struct {
@@ -686,6 +692,14 @@ gs_plugin_loader_call_vfunc (GsPluginLoaderHelper *helper,
                                           cancellable, &error_local);
                }
                break;
+       case GS_PLUGIN_ACTION_GET_LANGPACKS:
+               {
+                       GsPluginGetLangPacksFunc plugin_func = func;
+                       ret = plugin_func (plugin, list,
+                                          gs_plugin_job_get_search (helper->plugin_job),
+                                          cancellable, &error_local);
+               }
+               break;
        default:
                g_critical ("no handler for %s", helper->function_name);
                break;
@@ -3669,3 +3683,10 @@ gs_plugin_loader_set_max_parallel_ops (GsPluginLoader *plugin_loader,
                g_warning ("Failed to set the maximum number of ops in parallel: %s",
                           error->message);
 }
+
+const gchar *
+gs_plugin_loader_get_locale (GsPluginLoader *plugin_loader)
+{
+       GsPluginLoaderPrivate *priv = gs_plugin_loader_get_instance_private (plugin_loader);
+       return priv->locale;
+}
diff --git a/lib/gs-plugin-loader.h b/lib/gs-plugin-loader.h
index e5083d9e..74cbfa53 100644
--- a/lib/gs-plugin-loader.h
+++ b/lib/gs-plugin-loader.h
@@ -89,4 +89,6 @@ GsPlugin      *gs_plugin_loader_find_plugin           (GsPluginLoader *plugin_loader,
 void            gs_plugin_loader_set_max_parallel_ops  (GsPluginLoader *plugin_loader,
                                                         guint           max_ops);
 
+const gchar    *gs_plugin_loader_get_locale            (GsPluginLoader *plugin_loader);
+
 G_END_DECLS
diff --git a/lib/gs-plugin-types.h b/lib/gs-plugin-types.h
index 394cd4b4..d6d30d52 100644
--- a/lib/gs-plugin-types.h
+++ b/lib/gs-plugin-types.h
@@ -226,6 +226,7 @@ typedef enum {
  * @GS_PLUGIN_ACTION_DESTROY:                  Destroy the plugin
  * @GS_PLUGIN_ACTION_DOWNLOAD:                 Download an application
  * @GS_PLUGIN_ACTION_GET_ALTERNATES:           Get the alternates for a specific application
+ * @GS_PLUGIN_ACTION_GET_LANGPACKS:            Get appropriate language pack
  *
  * The plugin action.
  **/
@@ -270,6 +271,7 @@ typedef enum {
        GS_PLUGIN_ACTION_DESTROY,
        GS_PLUGIN_ACTION_DOWNLOAD,
        GS_PLUGIN_ACTION_GET_ALTERNATES,
+       GS_PLUGIN_ACTION_GET_LANGPACKS,
        /*< private >*/
        GS_PLUGIN_ACTION_LAST
 } GsPluginAction;
diff --git a/lib/gs-plugin-vfuncs.h b/lib/gs-plugin-vfuncs.h
index 94f63bdb..1d406d99 100644
--- a/lib/gs-plugin-vfuncs.h
+++ b/lib/gs-plugin-vfuncs.h
@@ -939,4 +939,22 @@ gboolean    gs_plugin_update                       (GsPlugin       *plugin,
                                                         GCancellable   *cancellable,
                                                         GError         **error);
 
+/**
+ * gs_plugin_add_langpacks:
+ * @plugin: a #GsPlugin
+ * @list: a #GsAppList
+ * @locale: a #LANGUAGE_CODE or #LOCALE, e.g. "ja" or "ja_JP"
+ * @cancellable: a #GCancellable, or %NULL
+ * @error: a #GError, or %NULL
+ *
+ * Returns a list of language packs, as per input language code or locale.
+ *
+ * Returns: %TRUE for success or if not relevant
+ **/
+gboolean        gs_plugin_add_langpacks                (GsPlugin       *plugin,
+                                                        GsAppList      *list,
+                                                        const gchar    *locale,
+                                                        GCancellable   *cancellable,
+                                                        GError         **error);
+
 G_END_DECLS
diff --git a/lib/gs-plugin.c b/lib/gs-plugin.c
index b33c8cc2..4764ce7b 100644
--- a/lib/gs-plugin.c
+++ b/lib/gs-plugin.c
@@ -1583,6 +1583,8 @@ gs_plugin_action_to_function_name (GsPluginAction action)
                return "gs_plugin_destroy";
        if (action == GS_PLUGIN_ACTION_GET_ALTERNATES)
                return "gs_plugin_add_alternates";
+       if (action == GS_PLUGIN_ACTION_GET_LANGPACKS)
+               return "gs_plugin_add_langpacks";
        return NULL;
 }
 
@@ -1677,6 +1679,8 @@ gs_plugin_action_to_string (GsPluginAction action)
                return "destroy";
        if (action == GS_PLUGIN_ACTION_GET_ALTERNATES)
                return "get-alternates";
+       if (action == GS_PLUGIN_ACTION_GET_LANGPACKS)
+               return "get-langpacks";
        return NULL;
 }
 
@@ -1771,6 +1775,8 @@ gs_plugin_action_from_string (const gchar *action)
                return GS_PLUGIN_ACTION_DESTROY;
        if (g_strcmp0 (action, "get-alternates") == 0)
                return GS_PLUGIN_ACTION_GET_ALTERNATES;
+       if (g_strcmp0 (action, "get-langpacks") == 0)
+               return GS_PLUGIN_ACTION_GET_LANGPACKS;
        return GS_PLUGIN_ACTION_UNKNOWN;
 }
 
diff --git a/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c 
b/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c
new file mode 100644
index 00000000..f1d36bc4
--- /dev/null
+++ b/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c
@@ -0,0 +1,98 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2019 Sundeep Anand <suanand redhat com>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This plugin does following..
+ *  1. locates the active locale, say, xx
+ *  2. identifies related langpacks-xx
+ *  3. tries to add langpack-xx in app list
+ *  4. logs install information; not to try again
+ */
+
+#include <gnome-software.h>
+
+struct GsPluginData {
+       GHashTable      *locale_langpack_map;
+};
+
+void
+gs_plugin_initialize (GsPlugin *plugin)
+{
+       GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData));
+
+       /* this plugin should be fedora specific */
+       if (!gs_plugin_check_distro_id (plugin, "fedora")) {
+               gs_plugin_set_enabled (plugin, FALSE);
+               return;
+       }
+
+       gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream");
+
+       /*
+       * A few language code may have more than one language packs.
+       * Example: en {en_GB}, pt {pt_BR}, zh {zh_CN, zh_TW}
+       */
+       priv->locale_langpack_map = g_hash_table_new (g_str_hash, g_str_equal);
+       g_hash_table_insert (priv->locale_langpack_map, "en_GB", "langpacks-en_GB");
+       g_hash_table_insert (priv->locale_langpack_map, "pt_BR", "langpacks-pt_BR");
+       g_hash_table_insert (priv->locale_langpack_map, "zh_CN", "langpacks-zh_CN");
+       g_hash_table_insert (priv->locale_langpack_map, "zh_TW", "langpacks-zh_TW");
+}
+
+void
+gs_plugin_destroy (GsPlugin *plugin)
+{
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+       if (priv->locale_langpack_map != NULL)
+               g_hash_table_unref (priv->locale_langpack_map);
+}
+
+gboolean
+gs_plugin_add_langpacks (GsPlugin *plugin,
+                        GsAppList *list,
+                        const gchar *locale,
+                        GCancellable *cancellable,
+                        GError **error)
+{
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+       const gchar *language_code;
+       g_autofree gchar *cachefn = NULL;
+       g_autofree gchar *langpack_pkgname = NULL;
+       g_auto(GStrv) language_region = NULL;
+
+       if (g_strrstr (locale, "_") != NULL &&
+           !g_hash_table_lookup (priv->locale_langpack_map, locale)) {
+               /*
+                * language_code should be the langpack_source_id
+                * if input language_code is a locale and it doesn't
+                * not found in locale_langpack_map
+                */
+               language_region = g_strsplit (locale, "_", 2);
+               language_code = language_region[0];
+       } else {
+               language_code = locale;
+       }
+
+       /* per-user cache */
+       langpack_pkgname = g_strconcat ("langpacks-", language_code, NULL);
+       cachefn = gs_utils_get_cache_filename ("langpacks", langpack_pkgname,
+                                              GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                              error);
+       if (cachefn == NULL)
+               return FALSE;
+       if (!g_file_test (cachefn, G_FILE_TEST_EXISTS)) {
+               g_autoptr(GsApp) app = gs_app_new (NULL);
+               gs_app_set_metadata (app, "GnomeSoftware::Creator", gs_plugin_get_name (plugin));
+               gs_app_set_kind (app, AS_APP_KIND_LOCALIZATION);
+               gs_app_add_source (app, langpack_pkgname);
+               gs_app_list_add (list, app);
+
+               /* ensure we do not keep trying to install the langpack */
+               if (!g_file_set_contents (cachefn, language_code, -1, error))
+                       return FALSE;
+       }
+
+       return TRUE;
+}
diff --git a/plugins/fedora-langpacks/gs-self-test.c b/plugins/fedora-langpacks/gs-self-test.c
new file mode 100644
index 00000000..ebec6919
--- /dev/null
+++ b/plugins/fedora-langpacks/gs-self-test.c
@@ -0,0 +1,81 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2019 Richard Hughes <richard hughsie com>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include "config.h"
+
+#include <glib/gstdio.h>
+
+#include "gnome-software-private.h"
+
+#include "gs-test.h"
+
+static void
+gs_plugins_fedora_langpacks_func (GsPluginLoader *plugin_loader)
+{
+       g_autofree gchar *cachefn = NULL;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GsApp) app = NULL;
+       g_autoptr(GsAppList) list = NULL;
+       g_autoptr(GsPluginJob) plugin_job = NULL;
+
+       /* start with a clean slate */
+       cachefn = gs_utils_get_cache_filename ("langpacks", "langpacks-ja",
+                                              GS_UTILS_CACHE_FLAG_WRITEABLE,
+                                              &error);
+       g_assert_no_error (error);
+       g_unlink (cachefn);
+
+       /* get langpacks result based on locale */
+       plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_GET_LANGPACKS,
+                                        "search", "ja_JP",
+                                        "refine-flags", GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON,
+                                        NULL);
+       list = gs_plugin_loader_job_process (plugin_loader, plugin_job, NULL, &error);
+
+       /* check if we have just one app in the list */
+       g_assert_cmpint (gs_app_list_length (list), ==, 1);
+
+       /* check app's source and kind */
+       app = gs_app_list_index (list, 0);
+       g_assert_cmpstr (gs_app_get_source_default (app), ==, "langpacks-ja");
+       g_assert_cmpint (gs_app_get_kind (app), ==, AS_APP_KIND_LOCALIZATION);
+}
+
+int
+main (int argc, char **argv)
+{
+       gboolean ret;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GsPluginLoader) plugin_loader = NULL;
+       const gchar *whitelist[] = {
+               "fedora_langpacks",
+               NULL
+       };
+
+       g_test_init (&argc, &argv, NULL);
+       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
+
+       /* only critical and error are fatal */
+       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+
+       /* we can only load this once per process */
+       plugin_loader = gs_plugin_loader_new ();
+       gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR);
+       ret = gs_plugin_loader_setup (plugin_loader,
+                                     (gchar**) whitelist,
+                                     NULL,
+                                     NULL,
+                                     &error);
+       g_assert_no_error (error);
+       g_assert (ret);
+
+       /* plugin tests go here */
+       g_test_add_data_func ("/gnome-software/plugins/fedora-langpacks",
+                             plugin_loader,
+                             (GTestDataFunc) gs_plugins_fedora_langpacks_func);
+       return g_test_run ();
+}
diff --git a/plugins/fedora-langpacks/meson.build b/plugins/fedora-langpacks/meson.build
new file mode 100644
index 00000000..a934d17a
--- /dev/null
+++ b/plugins/fedora-langpacks/meson.build
@@ -0,0 +1,40 @@
+cargs = ['-DG_LOG_DOMAIN="GsPluginFedoraLangpacks"']
+
+shared_module(
+  'gs_plugin_fedora_langpacks',
+  sources : 'gs-plugin-fedora-langpacks.c',
+  include_directories : [
+    include_directories('../..'),
+    include_directories('../../lib'),
+  ],
+  install : true,
+  install_dir: plugin_dir,
+  c_args : cargs,
+  dependencies : plugin_libs,
+  link_with : [
+    libgnomesoftware
+  ]
+)
+
+if get_option('tests')
+  cargs += ['-DLOCALPLUGINDIR="' + meson.current_build_dir() + '"']
+  e = executable(
+    'gs-self-test-fedora-langpacks',
+    compiled_schemas,
+    sources : [
+      'gs-self-test.c',
+    ],
+    include_directories : [
+      include_directories('../..'),
+      include_directories('../../lib'),
+    ],
+    dependencies : [
+      plugin_libs,
+    ],
+    link_with : [
+      libgnomesoftware
+    ],
+    c_args : cargs,
+  )
+  test('gs-self-test-fedora-langpacks', e, env: test_env)
+endif
diff --git a/plugins/meson.build b/plugins/meson.build
index c941fef1..2eebff02 100644
--- a/plugins/meson.build
+++ b/plugins/meson.build
@@ -12,6 +12,7 @@ subdir('core')
 subdir('dpkg')
 subdir('dummy')
 subdir('epiphany')
+subdir('fedora-langpacks')
 subdir('fedora-pkgdb-collections')
 
 if get_option('eos_updater')
diff --git a/src/gs-update-monitor.c b/src/gs-update-monitor.c
index ef6082a6..b0d4246a 100644
--- a/src/gs-update-monitor.c
+++ b/src/gs-update-monitor.c
@@ -52,6 +52,21 @@ download_updates_data_free (DownloadUpdatesData *data)
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(DownloadUpdatesData, download_updates_data_free);
 
+typedef struct {
+       GsUpdateMonitor         *monitor;
+       GsApp                   *app;
+} LanguagePackData;
+
+static void
+language_pack_data_free (LanguagePackData *data)
+{
+       g_clear_object (&data->monitor);
+       g_clear_object (&data->app);
+       g_slice_free (LanguagePackData, data);
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(LanguagePackData, language_pack_data_free);
+
 static gboolean
 reenable_offline_update_notification (gpointer data)
 {
@@ -651,6 +666,70 @@ typedef enum {
        UP_DEVICE_LEVEL_LAST
 } UpDeviceLevel;
 
+static void
+install_language_pack_cb (GObject *object, GAsyncResult *res, gpointer data)
+{
+       g_autoptr(GError) error = NULL;
+       g_autoptr(LanguagePackData) language_pack_data = data;
+
+       if (!gs_plugin_loader_job_action_finish (GS_PLUGIN_LOADER (object), res, &error)) {
+               if (!g_error_matches (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_CANCELLED))
+                       g_debug ("failed to install language pack: %s", error->message);
+               return;
+       } else {
+               g_debug ("language pack for %s installed",
+                        gs_app_get_name (language_pack_data->app));
+       }
+}
+
+/*
+ * determines active locale and looks for langpacks
+ * installs located language pack, if not already
+ */
+static void
+check_language_pack (GsUpdateMonitor *monitor) {
+
+       const gchar *locale;
+       GsApp *app;
+       g_autoptr(GsPluginJob) plugin_job = NULL;
+       g_autoptr(GsAppList) app_list = NULL;
+       g_autoptr(GError) error = NULL;
+
+       locale = gs_plugin_loader_get_locale (monitor->plugin_loader);
+       plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_GET_LANGPACKS,
+                                        "search", locale,
+                                        "refine-flags", GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON,
+                                        NULL);
+       app_list = gs_plugin_loader_job_process (monitor->plugin_loader,
+                                                plugin_job,
+                                                NULL,
+                                                &error);
+       if (app_list == NULL) {
+               g_debug ("failed to get language pack: %s", error->message);
+               return;
+       }
+
+       /* there should be one langpack for a given locale */
+       app = g_object_ref (gs_app_list_index (app_list, 0));
+       if (!gs_app_is_installed (app)) {
+               g_autoptr(LanguagePackData) language_pack_data = NULL;
+               g_autoptr(GsPluginJob) plugin_install_job = NULL;
+
+               language_pack_data = g_slice_new0 (LanguagePackData);
+               language_pack_data->monitor = g_object_ref (monitor);
+               language_pack_data->app = g_object_ref (app);
+
+               plugin_install_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_INSTALL,
+                                                        "app", app,
+                                                        NULL);
+               gs_plugin_loader_job_process_async (monitor->plugin_loader,
+                                                   plugin_install_job,
+                                                   monitor->cancellable,
+                                                   install_language_pack_cb,
+                                                   g_steal_pointer (&language_pack_data));
+       }
+}
+
 static void
 check_updates (GsUpdateMonitor *monitor)
 {
@@ -663,6 +742,9 @@ check_updates (GsUpdateMonitor *monitor)
        if (!gs_plugin_loader_get_network_available (monitor->plugin_loader))
                return;
 
+       /* check for language pack */
+       check_language_pack (monitor);
+
        refresh_on_metered = g_settings_get_boolean (monitor->settings,
                                                     "refresh-when-metered");
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]