1
From: Tim Lunn <tim@feathertop.org>
2
Date: Fri, 11 Sep 2015 13:43:40 +1000
3
Subject: Provide an api for the installation of language packs
5
This adds cc_common_language_maybe_install() which uses
6
PackageKit WhatProvides locale() to locate and install
7
language packs for the selected language
10
diff --git a/panels/common/cc-common-language.c b/panels/common/cc-common-language.c
11
index f447698..a5b3e6c 100644
12
--- a/panels/common/cc-common-language.c
13
+++ b/panels/common/cc-common-language.c
14
@@ -425,3 +425,235 @@ cc_common_language_add_user_languages (GtkTreeModel *model)
15
g_hash_table_destroy (user_langs);
21
+ GDBusProxy *pk_proxy, *pk_transaction_proxy;
26
+on_pk_what_provides_ready (GObject *source,
28
+ PkTransactionData *pk_data)
34
+ result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error);
37
+ g_debug ("Error getting PackageKit updates list: %s", error->message);
38
+ g_error_free (error);
44
+cc_common_language_install (guint xid, gchar **packages) {
46
+ GDBusProxy *proxy = NULL;
47
+ GError *error = NULL;
50
+ /* get a session bus proxy */
51
+ proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
52
+ G_DBUS_PROXY_FLAGS_NONE, NULL,
53
+ "org.freedesktop.PackageKit",
54
+ "/org/freedesktop/PackageKit",
55
+ "org.freedesktop.PackageKit.Modify",
57
+ if (proxy == NULL) {
58
+ g_debug ("failed: %s", error->message);
59
+ g_error_free (error);
63
+ /* issue the sync request */
64
+ retval = g_dbus_proxy_call_sync (proxy,
65
+ "InstallPackageNames",
66
+ g_variant_new ("(u^a&ss)",
70
+ G_DBUS_CALL_FLAGS_NONE,
72
+ NULL, /* cancellable */
74
+ if (retval == NULL) {
75
+ g_debug ("failed: %s", error->message);
76
+ g_error_free (error);
79
+ g_object_unref (retval);
83
+ g_object_unref (proxy);
88
+on_pk_transaction_signal (GDBusProxy *proxy,
91
+ GVariant *parameters,
92
+ PkTransactionData *pk_data)
94
+ if (g_strcmp0 (signal_name, "Package") == 0) {
95
+ gchar *package, *unused;
99
+ g_variant_get (parameters, "(u&s&s)", &status, &package, &unused);
101
+ if (status == 2) { /*PK_INFO_ENUM_AVAILABLE*/
102
+ split = g_strsplit(package, ";", -1);
103
+ g_ptr_array_add (pk_data->array, g_strdup (split[0]));
107
+ } else if (!g_strcmp0 (signal_name, "Finished")) {
110
+ g_ptr_array_add (pk_data->array, NULL);
112
+ lang = (char **) g_ptr_array_free (pk_data->array, FALSE);
113
+ /* Now install all packages returned by the previous call */
114
+ if (lang[0] != NULL)
115
+ cc_common_language_install(pk_data->xid, lang);
119
+ } else if (g_strcmp0 (signal_name, "Destroy") == 0) {
120
+ g_free (pk_data->lang);
121
+ g_clear_object (&pk_data->pk_transaction_proxy);
122
+ g_clear_object (&pk_data->pk_proxy);
127
+on_pk_get_tid_ready (GObject *source,
129
+ PkTransactionData *pk_data)
136
+ const gchar * provides_args[] = { g_strdup_printf ("locale(%s)",pk_data->lang), NULL };
138
+ result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error);
139
+ if (result == NULL)
141
+ if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN) == FALSE)
142
+ g_debug ("Error getting PackageKit transaction ID: %s", error->message);
143
+ g_error_free (error);
147
+ g_variant_get (result, "(o)", &tid);
149
+ pk_data->pk_transaction_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
150
+ G_DBUS_PROXY_FLAGS_NONE,
152
+ "org.freedesktop.PackageKit",
154
+ "org.freedesktop.PackageKit.Transaction",
158
+ g_variant_unref (result);
160
+ if (pk_data->pk_transaction_proxy == NULL)
162
+ g_debug ("Unable to get PackageKit transaction proxy object");
166
+ g_signal_connect (pk_data->pk_transaction_proxy,
168
+ G_CALLBACK (on_pk_transaction_signal),
171
+ g_dbus_proxy_call (pk_data->pk_transaction_proxy,
173
+ /* TODO need to get enums from libpackagekit-glib2 */
174
+ g_variant_new ("(tu^a&s)",
175
+ 1, /*PK_FILTER_ENUM_NONE*/
176
+ 11, /*PK_PROVIDES_ENUM_LANGUAGE_SUPPORT*/
178
+ G_DBUS_CALL_FLAGS_NONE,
181
+ (GAsyncReadyCallback) on_pk_what_provides_ready,
186
+cc_common_language_maybe_install (guint xid, const gchar *lang)
188
+ gchar *language_code, *territory_code, *territory_lang = NULL;
190
+ gboolean result = FALSE;
192
+ PkTransactionData *pk_data = NULL;
194
+ gnome_parse_locale (lang, &language_code, &territory_code, NULL, NULL);
196
+ /* If the language is already available, do nothing */
197
+ territory_lang = g_strdup_printf ("%s_%s", language_code, territory_code);
199
+ langs = cc_common_language_get_installed_languages();
200
+ for (i = 0; langs[i]; i++) {
201
+ if (g_strcmp0 (langs[i], territory_lang) ==0 ) {
202
+ g_warning ("Language is already installed");
208
+ g_warning ("Language %s not installed, trying to intall it", lang);
210
+ pk_data = g_new0 (PkTransactionData, 1);
211
+ pk_data->lang = g_strdup (lang);
212
+ pk_data->array = g_ptr_array_new ();
213
+ pk_data->xid = xid;
215
+ /* Now try to retrieve the list of packages needed to install this language */
216
+ pk_data->pk_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
217
+ G_DBUS_PROXY_FLAGS_NONE,
219
+ "org.freedesktop.PackageKit",
220
+ "/org/freedesktop/PackageKit",
221
+ "org.freedesktop.PackageKit",
224
+ if (pk_data->pk_proxy == NULL) {
225
+ /* if there's a PK error, ignore and assume the lang is available */
226
+ g_debug ("PackageKit not available, not installing language");
232
+ /* Retrieve PK transaction */
233
+ g_dbus_proxy_call (pk_data->pk_proxy,
234
+ "CreateTransaction",
236
+ G_DBUS_CALL_FLAGS_NONE,
239
+ (GAsyncReadyCallback) on_pk_get_tid_ready,
243
+ g_free (language_code);
244
+ g_free (territory_code);
245
+ g_free (territory_lang);
246
+ g_strfreev (langs);
250
diff --git a/panels/common/cc-common-language.h b/panels/common/cc-common-language.h
251
index 8ebd723..31ed19d 100644
252
--- a/panels/common/cc-common-language.h
253
+++ b/panels/common/cc-common-language.h
254
@@ -51,6 +51,8 @@ GHashTable *cc_common_language_get_initial_languages (void);
255
GHashTable *cc_common_language_get_user_languages (void);
256
GHashTable *cc_common_language_get_initial_regions (const gchar *lang);
258
+gboolean cc_common_language_maybe_install (guint32 xid, const gchar *lang);
260
void cc_common_language_setup_list (GtkWidget *treeview,
262
GHashTable *initial);