[gnome-software/603-gnome-software-reports-unknown-error-when-there-is-a-signing-issue: 205/207] gs-common: Add gs_utils_ask_user_accepts()




commit 2d832fc410bf01af33b507f19d80664568cd579b
Author: Milan Crha <mcrha redhat com>
Date:   Mon Sep 20 14:16:48 2021 +0200

    gs-common: Add gs_utils_ask_user_accepts()
    
    Asks the user whether he/she accepts "something". It will be used
    as a response to the GsPlugin::ask-user-accepts signal.

 src/gs-common.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 src/gs-common.h |  5 ++++
 2 files changed, 80 insertions(+), 10 deletions(-)
---
diff --git a/src/gs-common.c b/src/gs-common.c
index f99c3a5e7..3406ea1a5 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -523,7 +523,9 @@ unset_focus (GtkWidget *widget, gpointer data)
  * Inserts a widget displaying the detailed message into the message dialog.
  */
 static void
-insert_details_widget (GtkMessageDialog *dialog, const gchar *details)
+insert_details_widget (GtkMessageDialog *dialog,
+                      const gchar *details,
+                      gboolean add_prefix)
 {
        GtkWidget *message_area, *sw, *label;
        GtkWidget *tv;
@@ -536,13 +538,15 @@ insert_details_widget (GtkMessageDialog *dialog, const gchar *details)
 
        gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
 
-       msg = g_string_new ("");
-       g_string_append_printf (msg, "%s\n\n%s",
-                               /* TRANSLATORS: these are show_detailed_error messages from the
-                                * package manager no mortal is supposed to understand,
-                                * but google might know what they mean */
-                               _("Detailed errors from the package manager follow:"),
-                               details);
+       if (add_prefix) {
+               msg = g_string_new ("");
+               g_string_append_printf (msg, "%s\n\n%s",
+                                       /* TRANSLATORS: these are show_detailed_error messages from the
+                                        * package manager no mortal is supposed to understand,
+                                        * but google might know what they mean */
+                                       _("Detailed errors from the package manager follow:"),
+                                       details);
+       }
 
        message_area = gtk_message_dialog_get_message_area (dialog);
        g_assert (GTK_IS_BOX (message_area));
@@ -574,7 +578,7 @@ insert_details_widget (GtkMessageDialog *dialog, const gchar *details)
        gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD);
        gtk_style_context_add_class (gtk_widget_get_style_context (tv),
                                     "update-failed-details");
-       gtk_text_buffer_set_text (buffer, msg->str, -1);
+       gtk_text_buffer_set_text (buffer, msg ? msg->str : details, -1);
        gtk_widget_set_visible (tv, TRUE);
 
        gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), tv);
@@ -609,7 +613,7 @@ gs_utils_show_error_dialog (GtkWindow *parent,
        gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                  "%s", msg);
        if (details != NULL)
-               insert_details_widget (GTK_MESSAGE_DIALOG (dialog), details);
+               insert_details_widget (GTK_MESSAGE_DIALOG (dialog), details, TRUE);
 
        g_signal_connect_swapped (dialog, "response",
                                  G_CALLBACK (gtk_window_destroy),
@@ -617,6 +621,67 @@ gs_utils_show_error_dialog (GtkWindow *parent,
        gtk_widget_show (dialog);
 }
 
+/**
+ * gs_utils_ask_user_accepts:
+ * @parent: (nullable): modal parent, or %NULL for none
+ * @title: the title for the dialog
+ * @msg: the message for the dialog
+ * @details: (nullable): the detailed error message, or %NULL for none
+ * @accept_label: (nullable): a label of the 'accept' button, or %NULL to use 'Accept'
+ *
+ * Shows a modal question dialog for displaying an accept/cancel question to the user.
+ *
+ * Returns: whether the user accepted the question
+ *
+ * Since: 42
+ **/
+gboolean
+gs_utils_ask_user_accepts (GtkWindow *parent,
+                          const gchar *title,
+                          const gchar *msg,
+                          const gchar *details,
+                          const gchar *accept_label)
+{
+       GtkWidget *dialog;
+       RunInfo run_info;
+
+       g_return_val_if_fail (title != NULL, FALSE);
+       g_return_val_if_fail (msg != NULL, FALSE);
+
+       if (accept_label == NULL || *accept_label == '\0') {
+               /* Translators: an accept button label, in a Cancel/Accept dialog */
+               accept_label = _("_Accept");
+       }
+
+       dialog = gtk_message_dialog_new_with_markup (parent,
+                                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                    GTK_MESSAGE_QUESTION,
+                                                    GTK_BUTTONS_NONE,
+                                                    "<big><b>%s</b></big>", title);
+       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+                                                 "%s", msg);
+       if (details != NULL)
+               insert_details_widget (GTK_MESSAGE_DIALOG (dialog), details, FALSE);
+       gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Cancel"), GTK_RESPONSE_CANCEL);
+       gtk_dialog_add_button (GTK_DIALOG (dialog), accept_label, GTK_RESPONSE_OK);
+
+       run_info.response_id = GTK_RESPONSE_NONE;
+       run_info.loop = g_main_loop_new (NULL, FALSE);
+
+       /* Run */
+       if (!gtk_widget_get_visible (dialog))
+               gtk_window_present (GTK_WINDOW (dialog));
+
+       g_signal_connect (dialog, "close-request", G_CALLBACK (close_requested_cb), &run_info);
+       g_signal_connect (dialog, "response", G_CALLBACK (response_cb), &run_info);
+       g_signal_connect (dialog, "unmap", G_CALLBACK (unmap_cb), &run_info);
+
+       g_main_loop_run (run_info.loop);
+       g_clear_pointer (&run_info.loop, g_main_loop_unref);
+
+       return run_info.response_id == GTK_RESPONSE_OK;
+}
+
 /**
  * gs_utils_get_error_value:
  * @error: A GError
diff --git a/src/gs-common.h b/src/gs-common.h
index 0a19daab6..0f504553a 100644
--- a/src/gs-common.h
+++ b/src/gs-common.h
@@ -42,6 +42,11 @@ void          gs_utils_show_error_dialog     (GtkWindow      *parent,
                                                 const gchar    *title,
                                                 const gchar    *msg,
                                                 const gchar    *details);
+gboolean        gs_utils_ask_user_accepts      (GtkWindow      *parent,
+                                                const gchar    *title,
+                                                const gchar    *msg,
+                                                const gchar    *details,
+                                                const gchar    *accept_label);
 gchar          *gs_utils_build_unique_id_kind  (AsComponentKind kind,
                                                 const gchar    *id);
 gboolean        gs_utils_list_has_component_fuzzy      (GsAppList      *list,


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