[gnome-software/gnome-3-20] Factor out gs_utils_show_error_dialog()



commit a030ff83608b8d7a39c80465b1165934368520f4
Author: Kalev Lember <klember redhat com>
Date:   Thu May 26 11:40:12 2016 +0200

    Factor out gs_utils_show_error_dialog()
    
    Factoring it out makes it possible to use the nicely designed error
    messages that Allan / Rafal did in other places as well, not just in
    GsUpdateMonitor.
    
    This also switches distro upgrade error messages to use the common code,
    fixing the error messages to actually show up instead of a non-working
    expander in the current error messages.
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1335414

 src/gs-update-monitor.c |  111 +-------------------------------
 src/gs-utils.c          |  160 ++++++++++++++++++++++++++++++++++++-----------
 src/gs-utils.h          |    4 +
 3 files changed, 131 insertions(+), 144 deletions(-)
---
diff --git a/src/gs-update-monitor.c b/src/gs-update-monitor.c
index d4f1dcc..7fa5e7d 100644
--- a/src/gs-update-monitor.c
+++ b/src/gs-update-monitor.c
@@ -495,104 +495,12 @@ cleanup_notifications_cb (gpointer user_data)
        return G_SOURCE_REMOVE;
 }
 
-static void
-do_not_expand (GtkWidget *child, gpointer data)
-{
-       gtk_container_child_set (GTK_CONTAINER (gtk_widget_get_parent (child)),
-                                child, "expand", FALSE, "fill", FALSE, NULL);
-}
-
-static gboolean
-unset_focus (GtkWidget *widget, GdkEvent *event, gpointer data)
-{
-       if (GTK_IS_WINDOW (widget))
-               gtk_window_set_focus(GTK_WINDOW (widget), NULL);
-       return FALSE;
-}
-
-/**
- * insert_details_widget:
- * @dialog: the message dialog where the widget will be inserted
- * @details: (allow-none): the detailed message text to display
- *
- * Inserts a widget displaying the detailed message into the message dialog.
- * Does nothing if @details is %NULL so it is safe to call this function
- * without checking if there is anything to display.
- */
-static void
-insert_details_widget (GtkMessageDialog *dialog, const gchar *details)
-{
-       GtkWidget *message_area, *sw, *label;
-       GtkWidget *box, *tv;
-       GtkTextBuffer *buffer;
-       GList *children;
-       g_autoptr(GString) msg = NULL;
-
-       if (!details)
-               return;
-       g_return_if_fail (dialog != NULL);
-       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);
-
-       message_area = gtk_message_dialog_get_message_area (dialog);
-       g_assert (GTK_IS_BOX (message_area));
-       /* make the hbox expand */
-       box = gtk_widget_get_parent (message_area);
-       gtk_container_child_set (GTK_CONTAINER (gtk_widget_get_parent (box)), box,
-                                "expand", TRUE, "fill", TRUE, NULL);
-       /* make the labels not expand */
-       gtk_container_foreach (GTK_CONTAINER (message_area), do_not_expand, NULL);
-
-       /* Find the secondary label and set its width_chars.   */
-       /* Otherwise the label will tend to expand vertically. */
-       children = gtk_container_get_children (GTK_CONTAINER (message_area));
-       if (children && children->next && GTK_IS_LABEL (children->next->data)) {
-               gtk_label_set_width_chars (GTK_LABEL (children->next->data), 40);
-       }
-
-       label = gtk_label_new (_("Details"));
-       gtk_widget_set_halign (label, GTK_ALIGN_START);
-       gtk_widget_set_visible (label, TRUE);
-       gtk_box_pack_start (GTK_BOX (message_area), label, FALSE, FALSE, 0);
-
-       sw = gtk_scrolled_window_new (NULL, NULL);
-       gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
-                                            GTK_SHADOW_IN);
-       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
-                                       GTK_POLICY_NEVER,
-                                       GTK_POLICY_AUTOMATIC);
-       gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (sw), 150);
-       gtk_widget_set_visible (sw, TRUE);
-
-       tv = gtk_text_view_new ();
-       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
-       gtk_text_view_set_editable (GTK_TEXT_VIEW (tv), FALSE);
-       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_widget_set_visible (tv, TRUE);
-
-       gtk_container_add (GTK_CONTAINER (sw), tv);
-       gtk_box_pack_end (GTK_BOX (message_area), sw, TRUE, TRUE, 0);
-
-       g_signal_connect (dialog, "map-event", G_CALLBACK (unset_focus), NULL);
-}
-
 void
 gs_update_monitor_show_error (GsUpdateMonitor *monitor, GsShell *shell)
 {
        const gchar *title;
        const gchar *msg;
        gboolean show_detailed_error;
-       GtkWidget *dialog;
 
        /* can this happen in reality? */
        if (monitor->last_offline_error == NULL)
@@ -640,21 +548,10 @@ gs_update_monitor_show_error (GsUpdateMonitor *monitor, GsShell *shell)
                break;
        }
 
-       dialog = gtk_message_dialog_new_with_markup (gs_shell_get_window (shell),
-                                        0,
-                                        GTK_MESSAGE_INFO,
-                                        GTK_BUTTONS_CLOSE,
-                                        "<big><b>%s</b></big>", title);
-       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
-                                                 "%s", msg);
-       if (show_detailed_error) {
-               insert_details_widget (GTK_MESSAGE_DIALOG (dialog),
-                                      monitor->last_offline_error->message);
-       }
-       g_signal_connect_swapped (dialog, "response",
-                                 G_CALLBACK (gtk_widget_destroy),
-                                 dialog);
-       gtk_widget_show (dialog);
+       gs_utils_show_error_dialog (gs_shell_get_window (shell),
+                                   title,
+                                   msg,
+                                   show_detailed_error ? monitor->last_offline_error->message : NULL);
 }
 
 static void
diff --git a/src/gs-utils.c b/src/gs-utils.c
index 956a721..3d4cbd6 100644
--- a/src/gs-utils.c
+++ b/src/gs-utils.c
@@ -159,9 +159,7 @@ gs_app_notify_failed_modal (GsApp *app,
                            GsPluginLoaderAction action,
                            const GError *error)
 {
-       GtkWidget *dialog;
        const gchar *title;
-       gboolean show_detailed_error = TRUE;
        g_autoptr(GString) msg = NULL;
 
        /* TRANSLATORS: install or removed failed */
@@ -185,42 +183,8 @@ gs_app_notify_failed_modal (GsApp *app,
                g_assert_not_reached ();
                break;
        }
-       g_string_append (msg, " ");
-
-       dialog = gtk_message_dialog_new (parent_window,
-                                        GTK_DIALOG_MODAL |
-                                        GTK_DIALOG_USE_HEADER_BAR |
-                                        GTK_DIALOG_DESTROY_WITH_PARENT,
-                                        GTK_MESSAGE_ERROR,
-                                        GTK_BUTTONS_CLOSE,
-                                        "%s", title);
-       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
-                                                 "%s", msg->str);
-
-       /* detailed error in an expander */
-       if (show_detailed_error) {
-               GtkWidget *vbox;
-               GtkWidget *expander;
-               GtkWidget *scrolled_window;
-               GtkWidget *label;
-
-               vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
-               /* TRANSLATORS: this is an expander title */
-               expander = gtk_expander_new (_("Show Details"));
-               gtk_widget_set_margin_start (expander, 36);
-               gtk_widget_set_margin_end (expander, 36);
-               scrolled_window = gtk_scrolled_window_new (NULL, NULL);
-               gtk_container_add (GTK_CONTAINER (expander), scrolled_window);
-               label = gtk_label_new (error->message);
-               gtk_container_add (GTK_CONTAINER (scrolled_window), label);
-               gtk_box_pack_end (GTK_BOX (vbox), expander, FALSE, TRUE, 4);
-               gtk_widget_show_all (expander);
 
-       }
-
-       g_signal_connect (dialog, "response",
-                         G_CALLBACK (gtk_widget_destroy), NULL);
-       gtk_window_present (GTK_WINDOW (dialog));
+       gs_utils_show_error_dialog (parent_window, title, msg->str, error->message);
 }
 
 typedef enum {
@@ -623,4 +587,126 @@ gs_utils_widget_set_custom_css (GtkWidget *widget, const gchar *css)
                                g_object_unref);
 }
 
+static void
+do_not_expand (GtkWidget *child, gpointer data)
+{
+       gtk_container_child_set (GTK_CONTAINER (gtk_widget_get_parent (child)),
+                                child, "expand", FALSE, "fill", FALSE, NULL);
+}
+
+static gboolean
+unset_focus (GtkWidget *widget, GdkEvent *event, gpointer data)
+{
+       if (GTK_IS_WINDOW (widget))
+               gtk_window_set_focus (GTK_WINDOW (widget), NULL);
+       return FALSE;
+}
+
+/**
+ * insert_details_widget:
+ * @dialog: the message dialog where the widget will be inserted
+ * @details: the detailed message text to display
+ *
+ * Inserts a widget displaying the detailed message into the message dialog.
+ */
+static void
+insert_details_widget (GtkMessageDialog *dialog, const gchar *details)
+{
+       GtkWidget *message_area, *sw, *label;
+       GtkWidget *box, *tv;
+       GtkTextBuffer *buffer;
+       GList *children;
+       g_autoptr(GString) msg = NULL;
+
+       g_assert (GTK_IS_MESSAGE_DIALOG (dialog));
+       g_assert (details != NULL);
+
+       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);
+
+       message_area = gtk_message_dialog_get_message_area (dialog);
+       g_assert (GTK_IS_BOX (message_area));
+       /* make the hbox expand */
+       box = gtk_widget_get_parent (message_area);
+       gtk_container_child_set (GTK_CONTAINER (gtk_widget_get_parent (box)), box,
+                                "expand", TRUE, "fill", TRUE, NULL);
+       /* make the labels not expand */
+       gtk_container_foreach (GTK_CONTAINER (message_area), do_not_expand, NULL);
+
+       /* Find the secondary label and set its width_chars.   */
+       /* Otherwise the label will tend to expand vertically. */
+       children = gtk_container_get_children (GTK_CONTAINER (message_area));
+       if (children && children->next && GTK_IS_LABEL (children->next->data)) {
+               gtk_label_set_width_chars (GTK_LABEL (children->next->data), 40);
+       }
+
+       label = gtk_label_new (_("Details"));
+       gtk_widget_set_halign (label, GTK_ALIGN_START);
+       gtk_widget_set_visible (label, TRUE);
+       gtk_box_pack_start (GTK_BOX (message_area), label, FALSE, FALSE, 0);
+
+       sw = gtk_scrolled_window_new (NULL, NULL);
+       gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
+                                            GTK_SHADOW_IN);
+       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
+                                       GTK_POLICY_NEVER,
+                                       GTK_POLICY_AUTOMATIC);
+       gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (sw), 150);
+       gtk_widget_set_visible (sw, TRUE);
+
+       tv = gtk_text_view_new ();
+       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
+       gtk_text_view_set_editable (GTK_TEXT_VIEW (tv), FALSE);
+       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_widget_set_visible (tv, TRUE);
+
+       gtk_container_add (GTK_CONTAINER (sw), tv);
+       gtk_box_pack_end (GTK_BOX (message_area), sw, TRUE, TRUE, 0);
+
+       g_signal_connect (dialog, "map-event", G_CALLBACK (unset_focus), NULL);
+}
+
+/**
+ * gs_utils_show_error_dialog:
+ * @parent: transient parent, or NULL for none
+ * @title: the title for the dialog
+ * @msg: the message for the dialog
+ * @details: (allow-none): the detailed error message, or NULL for none
+ *
+ * Shows a message dialog for displaying error messages.
+ */
+void
+gs_utils_show_error_dialog (GtkWindow *parent,
+                            const gchar *title,
+                            const gchar *msg,
+                            const gchar *details)
+{
+       GtkWidget *dialog;
+
+       dialog = gtk_message_dialog_new_with_markup (parent,
+                                                    0,
+                                                    GTK_MESSAGE_INFO,
+                                                    GTK_BUTTONS_CLOSE,
+                                                    "<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);
+
+       g_signal_connect_swapped (dialog, "response",
+                                 G_CALLBACK (gtk_widget_destroy),
+                                 dialog);
+       gtk_widget_show (dialog);
+}
+
 /* vim: set noexpandtab: */
diff --git a/src/gs-utils.h b/src/gs-utils.h
index 89d9abf..c28e125 100644
--- a/src/gs-utils.h
+++ b/src/gs-utils.h
@@ -69,6 +69,10 @@ gboolean      gs_utils_is_current_desktop    (const gchar    *name);
 gboolean        gs_utils_is_current_desktop    (const gchar    *name);
 void            gs_utils_widget_set_custom_css (GtkWidget      *widget,
                                                 const gchar    *css);
+void            gs_utils_show_error_dialog     (GtkWindow      *parent,
+                                                const gchar    *title,
+                                                const gchar    *msg,
+                                                const gchar    *details);
 
 G_END_DECLS
 


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