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




commit 6cb168ce9e079654c80014ee45e3a0ef430bd660
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 | 75 +++++++++++++++++++++++++++++++++++++++++++++++++--------
 src/gs-common.h |  5 ++++
 2 files changed, 70 insertions(+), 10 deletions(-)
---
diff --git a/src/gs-common.c b/src/gs-common.c
index 583c8c1f6..dd357829d 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -473,7 +473,9 @@ unset_focus (GtkWidget *widget, GdkEvent *event, 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 *box, *tv;
@@ -486,13 +488,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));
@@ -530,7 +534,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_container_add (GTK_CONTAINER (sw), tv);
@@ -566,7 +570,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_widget_destroy),
@@ -574,6 +578,57 @@ 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;
+       gint response;
+
+       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 label caption, 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);
+
+       response = gtk_dialog_run (GTK_DIALOG (dialog));
+
+       gtk_widget_destroy (dialog);
+
+       return response == GTK_RESPONSE_OK;
+}
+
 /**
  * gs_utils_get_error_value:
  * @error: A GError
diff --git a/src/gs-common.h b/src/gs-common.h
index cc27dbcb6..eacc5bbc2 100644
--- a/src/gs-common.h
+++ b/src/gs-common.h
@@ -38,6 +38,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]