~geoubuntu/gnome-radio/development

« back to all changes in this revision

Viewing changes to src/missing_plugins.c

  • Committer: Pojar Gheorghe–Ioan
  • Date: 2021-11-20 05:23:36 UTC
  • Revision ID: geoubuntu@gmail.com-20211120052336-r88illfotfxoimlq
* Initial release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2021  Pojar Gheorghe–Ioan  <geoubuntu@gmail.com>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; either version 2 of
 
7
 * the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 
16
 */
 
17
 
 
18
#include <config.h>
 
19
 
 
20
#include <gio/gdesktopappinfo.h>
 
21
#include <glib/gi18n.h>
 
22
#include <gst/gst.h>
 
23
#include <gst/pbutils/install-plugins.h>
 
24
#include <gst/pbutils/pbutils.h>
 
25
#include <gtk/gtk.h>
 
26
 
 
27
#ifdef GDK_WINDOWING_X11
 
28
  #include <gdk/gdkx.h>
 
29
#endif
 
30
 
 
31
#include <string.h>
 
32
 
 
33
#include "missing_plugins.h"
 
34
 
 
35
static GList *blocked_plugins = NULL;
 
36
 
 
37
static gpointer parent_window = NULL;
 
38
static gboolean retry;
 
39
 
 
40
typedef struct
 
41
{
 
42
    GtkWidget *widget;
 
43
    gchar **descriptions;
 
44
    gchar **details;
 
45
} CodecInstallContext;
 
46
 
 
47
static gboolean
 
48
codec_install_plugin_is_blocked (const gchar *detail)
 
49
{
 
50
    GList *res;
 
51
 
 
52
    res = g_list_find_custom (blocked_plugins, detail, (GCompareFunc) strcmp);
 
53
 
 
54
    return (res != NULL);
 
55
}
 
56
 
 
57
static void
 
58
codec_install_block_plugin (const gchar *detail)
 
59
{
 
60
    if (!codec_install_plugin_is_blocked (detail))
 
61
    {
 
62
        blocked_plugins = g_list_prepend (blocked_plugins, g_strdup (detail));
 
63
    }
 
64
}
 
65
 
 
66
static void
 
67
codec_install_context_free (CodecInstallContext *ctx)
 
68
{
 
69
    g_strfreev (ctx->descriptions);
 
70
    g_strfreev (ctx->details);
 
71
    g_free (ctx);
 
72
}
 
73
 
 
74
static void
 
75
on_plugin_installation_done (GstInstallPluginsReturn res,
 
76
                             gpointer                user_data)
 
77
{
 
78
    CodecInstallContext *ctx = (CodecInstallContext *) user_data;
 
79
    gchar **p;
 
80
 
 
81
    switch (res)
 
82
    {
 
83
        /* treat partial success the same as success; in the worst case we'll
 
84
         *  just do another round and get NOT_FOUND as result that time */
 
85
        case GST_INSTALL_PLUGINS_PARTIAL_SUCCESS:
 
86
        case GST_INSTALL_PLUGINS_SUCCESS:
 
87
        {
 
88
            /* block installed plugins too, so that we don't get into
 
89
             *  endless installer loops in case of inconsistencies */
 
90
            for (p = ctx->details; p != NULL && *p != NULL; ++p)
 
91
            {
 
92
                codec_install_block_plugin (*p);
 
93
            }
 
94
 
 
95
            g_message ("Missing plugins installed. Updating plugin registry…");
 
96
 
 
97
            /* force GStreamer to re-read its plugin registry */
 
98
            retry = gst_update_registry ();
 
99
            if (retry)
 
100
            {
 
101
                g_message ("Plugin registry updated, trying again");
 
102
            }
 
103
            else
 
104
            {
 
105
                g_warning ("GStreamer registry update failed");
 
106
            }
 
107
 
 
108
            break;
 
109
        }
 
110
 
 
111
        case GST_INSTALL_PLUGINS_NOT_FOUND:
 
112
        {
 
113
            g_message ("No installation candidate for missing plugins found");
 
114
 
 
115
            /* NOT_FOUND should only be returned if not a single one of the
 
116
             *  requested plugins was found; if we managed to play something
 
117
             *  anyway, we should just continue playing what we have and
 
118
             *  block the requested plugins for this session; if we could not
 
119
             *  play anything we should block them as well, so the install
 
120
             *  wizard isn't called again for nothing */
 
121
            for (p = ctx->details; p != NULL && *p != NULL; ++p)
 
122
            {
 
123
                codec_install_block_plugin (*p);
 
124
            }
 
125
 
 
126
            retry = FALSE;
 
127
            break;
 
128
        }
 
129
 
 
130
        case GST_INSTALL_PLUGINS_USER_ABORT:
 
131
        {
 
132
            /* block on user abort, so we show an error next time (or just
 
133
             *  what we can) instead of calling the installer */
 
134
            for (p = ctx->details; p != NULL && *p != NULL; ++p)
 
135
            {
 
136
                codec_install_block_plugin (*p);
 
137
            }
 
138
 
 
139
            retry = FALSE;
 
140
            break;
 
141
        }
 
142
 
 
143
        case GST_INSTALL_PLUGINS_ERROR:
 
144
        case GST_INSTALL_PLUGINS_CRASHED:
 
145
        default:
 
146
        {
 
147
            g_message ("Missing plugin installation failed: %s", gst_install_plugins_return_get_name (res));
 
148
 
 
149
            retry = FALSE;
 
150
            break;
 
151
        }
 
152
 
 
153
        case GST_INSTALL_PLUGINS_STARTED_OK:
 
154
        case GST_INSTALL_PLUGINS_INTERNAL_FAILURE:
 
155
        case GST_INSTALL_PLUGINS_HELPER_MISSING:
 
156
        case GST_INSTALL_PLUGINS_INSTALL_IN_PROGRESS:
 
157
        {
 
158
            g_assert_not_reached ();
 
159
            break;
 
160
        }
 
161
    }
 
162
 
 
163
    codec_install_context_free (ctx);
 
164
}
 
165
 
 
166
static void
 
167
set_startup_notification_id (GstInstallPluginsContext *install_ctx)
 
168
{
 
169
    g_autofree gchar *startup_id = NULL;
 
170
    guint32 timestamp;
 
171
 
 
172
    timestamp = gtk_get_current_event_time ();
 
173
    startup_id = g_strdup_printf ("_TIME%u", timestamp);
 
174
    gst_install_plugins_context_set_startup_notification_id (install_ctx, startup_id);
 
175
}
 
176
 
 
177
static gboolean
 
178
start_plugin_installation (CodecInstallContext *ctx,
 
179
                           gboolean             confirm_search)
 
180
{
 
181
    GstInstallPluginsContext *install_ctx;
 
182
    GstInstallPluginsReturn status;
 
183
 
 
184
    install_ctx = gst_install_plugins_context_new ();
 
185
    gst_install_plugins_context_set_desktop_id (install_ctx, "org.gnome." PACKAGE_NAME ".desktop");
 
186
    gst_install_plugins_context_set_confirm_search (install_ctx, confirm_search);
 
187
    set_startup_notification_id (install_ctx);
 
188
 
 
189
#ifdef GDK_WINDOWING_X11
 
190
    if (parent_window != NULL && gtk_widget_get_realized (GTK_WIDGET (parent_window)))
 
191
    {
 
192
        if (GDK_IS_X11_WINDOW (gtk_widget_get_window (GTK_WIDGET (parent_window))))
 
193
        {
 
194
            gulong xid = 0;
 
195
            xid = gdk_x11_window_get_xid (gtk_widget_get_window (GTK_WIDGET (parent_window)));
 
196
            gst_install_plugins_context_set_xid (install_ctx, xid);
 
197
        }
 
198
    }
 
199
#endif
 
200
 
 
201
    status = gst_install_plugins_async ((const char * const *) ctx->details, install_ctx,
 
202
                                        on_plugin_installation_done,
 
203
                                        ctx);
 
204
 
 
205
    gst_install_plugins_context_free (install_ctx);
 
206
 
 
207
    if (status != GST_INSTALL_PLUGINS_STARTED_OK)
 
208
    {
 
209
        if (status == GST_INSTALL_PLUGINS_HELPER_MISSING)
 
210
        {
 
211
            g_message ("Automatic missing codec installation not supported (helper script missing)");
 
212
        }
 
213
        else
 
214
        {
 
215
            g_warning ("Failed to start codec installation: %s", gst_install_plugins_return_get_name (status));
 
216
        }
 
217
 
 
218
        codec_install_context_free (ctx);
 
219
        return FALSE;
 
220
    }
 
221
 
 
222
    return TRUE;
 
223
}
 
224
 
 
225
static void
 
226
on_codec_confirmation_dialog_response (GtkDialog *dialog,
 
227
                                       gint       response_id,
 
228
                                       gpointer   user_data)
 
229
{
 
230
    CodecInstallContext *ctx = (CodecInstallContext *) user_data;
 
231
 
 
232
    switch (response_id)
 
233
    {
 
234
        case GTK_RESPONSE_ACCEPT:
 
235
        {
 
236
            start_plugin_installation (ctx, FALSE);
 
237
            break;
 
238
        }
 
239
 
 
240
        case GTK_RESPONSE_CANCEL:
 
241
        case GTK_RESPONSE_DELETE_EVENT:
 
242
        {
 
243
            break;
 
244
        }
 
245
 
 
246
        default:
 
247
        {
 
248
            g_assert_not_reached ();
 
249
        }
 
250
    }
 
251
 
 
252
    gtk_widget_destroy (GTK_WIDGET (dialog));
 
253
}
 
254
 
 
255
static void
 
256
show_codec_confirmation_dialog (CodecInstallContext *ctx,
 
257
                                const gchar         *install_helper_display_name)
 
258
{
 
259
    GtkWidget *dialog;
 
260
    GtkWidget *button;
 
261
    g_autofree gchar *button_text = NULL;
 
262
    g_autofree gchar *descriptions_text = NULL;
 
263
    g_autofree gchar *message_text = NULL;
 
264
    guint num;
 
265
 
 
266
    dialog = gtk_message_dialog_new (GTK_WINDOW (ctx->widget),
 
267
                                     GTK_DIALOG_MODAL |
 
268
                                     GTK_DIALOG_DESTROY_WITH_PARENT,
 
269
                                     GTK_MESSAGE_ERROR,
 
270
                                     GTK_BUTTONS_CANCEL,
 
271
                                     _("An error occurred while trying to start recording"));
 
272
 
 
273
    num = g_strv_length (ctx->descriptions);
 
274
    descriptions_text = g_strjoinv ("\n", ctx->descriptions);
 
275
 
 
276
    message_text = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
 
277
                                                 "Additional “%s” plugin required to record in the selected format, but is not installed.",
 
278
                                                 "Additional plugins required to record in the selected format, but are not installed:\n%s", num),
 
279
                                    (num == 1) ? *ctx->descriptions : descriptions_text);
 
280
 
 
281
    gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", message_text);
 
282
 
 
283
    /* Translators: This is a button to launch a codec installer.
 
284
     *  %s will be replaced with the software installer's name, e.g.
 
285
     *  “Software” in case of gnome-software. */
 
286
    button_text = g_strdup_printf (_("_Find in %s"), install_helper_display_name);
 
287
    button = gtk_dialog_add_button (GTK_DIALOG (dialog), button_text, GTK_RESPONSE_ACCEPT);
 
288
    gtk_style_context_add_class (gtk_widget_get_style_context (button), "suggested-action");
 
289
    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
 
290
    gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT, gst_install_plugins_supported ());
 
291
 
 
292
    g_signal_connect (dialog, "response",
 
293
                      G_CALLBACK (on_codec_confirmation_dialog_response),
 
294
                      ctx);
 
295
 
 
296
    gtk_window_present (GTK_WINDOW (dialog));
 
297
}
 
298
 
 
299
static void
 
300
on_codec_confirmation_infobar_response (GtkInfoBar *infobar,
 
301
                                        gint        response_id,
 
302
                                        gpointer    user_data)
 
303
{
 
304
    CodecInstallContext *ctx = (CodecInstallContext *) user_data;
 
305
 
 
306
    switch (response_id)
 
307
    {
 
308
        case GTK_RESPONSE_ACCEPT:
 
309
        {
 
310
            start_plugin_installation (ctx, FALSE);
 
311
            break;
 
312
        }
 
313
 
 
314
        case GTK_RESPONSE_CLOSE:
 
315
        {
 
316
            break;
 
317
        }
 
318
 
 
319
        default:
 
320
        {
 
321
            g_assert_not_reached ();
 
322
        }
 
323
    }
 
324
 
 
325
    gtk_info_bar_set_revealed (infobar, FALSE);
 
326
}
 
327
 
 
328
static void
 
329
show_codec_confirmation_infobar (CodecInstallContext *ctx,
 
330
                                 const gchar         *install_helper_display_name)
 
331
{
 
332
    GtkWidget *infobar;
 
333
    GtkWidget *label;
 
334
    GtkWidget *button;
 
335
    g_autofree gchar *markup = NULL;
 
336
    g_autofree gchar *button_text = NULL;
 
337
 
 
338
    infobar = gtk_info_bar_new ();
 
339
    gtk_container_add (GTK_CONTAINER (ctx->widget), infobar);
 
340
 
 
341
    gtk_info_bar_set_message_type (GTK_INFO_BAR (infobar), GTK_MESSAGE_OTHER);
 
342
    gtk_info_bar_set_show_close_button (GTK_INFO_BAR (infobar), TRUE);
 
343
 
 
344
    markup = g_markup_printf_escaped ("<span style='italic'>\%s</span>", _("Additional software required to use the selected file format."));
 
345
    label = gtk_label_new (NULL);
 
346
    gtk_label_set_markup (GTK_LABEL (label), markup);
 
347
    gtk_container_add (GTK_CONTAINER (gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar))), label);
 
348
 
 
349
    /* Translators: This is a button to launch a codec installer.
 
350
     *  %s will be replaced with the software installer's name, e.g.
 
351
     *  “Software” in case of gnome-software. */
 
352
    button_text = g_strdup_printf (_("_Find in %s"), install_helper_display_name);
 
353
    button = gtk_info_bar_add_button (GTK_INFO_BAR (infobar), button_text, GTK_RESPONSE_ACCEPT);
 
354
    gtk_style_context_add_class (gtk_widget_get_style_context (button), "suggested-action");
 
355
    gtk_info_bar_set_default_response (GTK_INFO_BAR (infobar), GTK_RESPONSE_ACCEPT);
 
356
    gtk_info_bar_set_response_sensitive (GTK_INFO_BAR (infobar), GTK_RESPONSE_ACCEPT, gst_install_plugins_supported ());
 
357
 
 
358
    g_signal_connect (infobar, "response",
 
359
                      G_CALLBACK (on_codec_confirmation_infobar_response),
 
360
                      ctx);
 
361
 
 
362
    gtk_widget_show_all (infobar);
 
363
 
 
364
    gtk_info_bar_set_revealed (GTK_INFO_BAR (infobar), TRUE);
 
365
    gtk_widget_grab_focus (button);
 
366
}
 
367
 
 
368
static void
 
369
on_packagekit_proxy_ready (GObject      *source_object,
 
370
                           GAsyncResult *result,
 
371
                           gpointer      user_data)
 
372
{
 
373
    CodecInstallContext *ctx = (CodecInstallContext *) user_data;
 
374
    g_autoptr (GDBusProxy) packagekit_proxy = NULL;
 
375
    g_autoptr (GVariant) property = NULL;
 
376
    g_autoptr (GError) error = NULL;
 
377
 
 
378
    packagekit_proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
 
379
    if (packagekit_proxy == NULL && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
 
380
    {
 
381
        return;
 
382
    }
 
383
 
 
384
    if (packagekit_proxy != NULL)
 
385
    {
 
386
        property = g_dbus_proxy_get_cached_property (packagekit_proxy, "DisplayName");
 
387
        if (property != NULL)
 
388
        {
 
389
            const gchar *display_name;
 
390
 
 
391
            display_name = g_variant_get_string (property, NULL);
 
392
            if (display_name != NULL && *display_name != '\0')
 
393
            {
 
394
                if (GTK_IS_WINDOW (ctx->widget))
 
395
                {
 
396
                    show_codec_confirmation_dialog (ctx, display_name);
 
397
                }
 
398
                else
 
399
                {
 
400
                    show_codec_confirmation_infobar (ctx, display_name);
 
401
                }
 
402
 
 
403
                return;
 
404
            }
 
405
        }
 
406
    }
 
407
 
 
408
    /* If the above failed, fall back to immediately starting the codec installation */
 
409
    start_plugin_installation (ctx, TRUE);
 
410
}
 
411
 
 
412
gboolean
 
413
on_missing_plugins_event (GtkWidget  *widget,
 
414
                          gchar     **details,
 
415
                          gchar     **descriptions,
 
416
                          gboolean    ignore_block,
 
417
                          gpointer    user_data)
 
418
{
 
419
    CodecInstallContext *ctx;
 
420
    guint i, num;
 
421
 
 
422
    num = g_strv_length (details);
 
423
    g_return_val_if_fail (num > 0 && g_strv_length (descriptions) == num, FALSE);
 
424
 
 
425
    ctx = g_new0 (CodecInstallContext, 1);
 
426
    ctx->descriptions = g_strdupv (descriptions);
 
427
    ctx->details = g_strdupv (details);
 
428
    ctx->widget = widget;
 
429
 
 
430
    for (i = 0; i < num; ++i)
 
431
    {
 
432
        if (ignore_block == FALSE && codec_install_plugin_is_blocked (ctx->details[i]))
 
433
        {
 
434
            g_message ("Missing plugin: %s (ignoring)", ctx->details[i]);
 
435
            g_free (ctx->details[i]);
 
436
            g_free (ctx->descriptions[i]);
 
437
            ctx->details[i] = ctx->details[num - 1];
 
438
            ctx->descriptions[i] = ctx->descriptions[num - 1];
 
439
            ctx->details[num - 1] = NULL;
 
440
            ctx->descriptions[num - 1] = NULL;
 
441
            --num;
 
442
            --i;
 
443
        }
 
444
        else
 
445
        {
 
446
            g_message ("Missing plugin: %s (%s)", ctx->details[i], ctx->descriptions[i]);
 
447
        }
 
448
    }
 
449
 
 
450
    if (num == 0)
 
451
    {
 
452
        g_message ("All missing plugins are blocked, doing nothing");
 
453
        codec_install_context_free (ctx);
 
454
        return FALSE;
 
455
    }
 
456
 
 
457
    /* Get the PackageKit session interface proxy and continue with the
 
458
     *  codec installation in the callback */
 
459
    g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
 
460
                              G_DBUS_PROXY_FLAGS_NONE,
 
461
                              NULL,
 
462
                              "org.freedesktop.PackageKit",
 
463
                              "/org/freedesktop/PackageKit",
 
464
                              "org.freedesktop.PackageKit.Modify2",
 
465
                              NULL,
 
466
                              on_packagekit_proxy_ready,
 
467
                              ctx);
 
468
 
 
469
    return TRUE;
 
470
}
 
471
 
 
472
void
 
473
missing_plugins_init (GtkWindow *window)
 
474
{
 
475
    parent_window = window;
 
476
    g_object_add_weak_pointer (G_OBJECT (parent_window), &parent_window);
 
477
 
 
478
    gst_pb_utils_init ();
 
479
}