app: keep track of all messages displayed by GimpErrorDialog.

This way, you can increment repeated messages even when not the last
one and you don't overflow the error dialog needelessly when 2 errors
repeat one after another.
This commit is contained in:
Jehan
2017-06-07 15:09:43 +02:00
parent 20fdb8dfa3
commit 6c2658ea4d
2 changed files with 58 additions and 49 deletions

View File

@ -37,10 +37,18 @@
#define GIMP_ERROR_DIALOG_MAX_MESSAGES 3 #define GIMP_ERROR_DIALOG_MAX_MESSAGES 3
typedef struct
{
GtkWidget *box;
gchar *domain;
gchar *message;
} GimpErrorDialogMessage;
static void gimp_error_dialog_finalize (GObject *object); static void gimp_error_dialog_finalize (GObject *object);
static void gimp_error_dialog_response (GtkDialog *dialog, static void gimp_error_dialog_response (GtkDialog *dialog,
gint response_id); gint response_id);
static void gimp_error_dialog_message_destroy (gpointer data);
G_DEFINE_TYPE (GimpErrorDialog, gimp_error_dialog, GIMP_TYPE_DIALOG) G_DEFINE_TYPE (GimpErrorDialog, gimp_error_dialog, GIMP_TYPE_DIALOG)
@ -78,10 +86,8 @@ gimp_error_dialog_init (GimpErrorDialog *dialog)
dialog->vbox, TRUE, TRUE, 0); dialog->vbox, TRUE, TRUE, 0);
gtk_widget_show (dialog->vbox); gtk_widget_show (dialog->vbox);
dialog->last_box = NULL; dialog->messages = NULL;
dialog->last_domain = NULL; dialog->overflow = FALSE;
dialog->last_message = NULL;
dialog->num_messages = 0;
} }
static void static void
@ -89,17 +95,8 @@ gimp_error_dialog_finalize (GObject *object)
{ {
GimpErrorDialog *dialog = GIMP_ERROR_DIALOG (object); GimpErrorDialog *dialog = GIMP_ERROR_DIALOG (object);
if (dialog->last_domain) g_list_free_full (dialog->messages,
{ gimp_error_dialog_message_destroy);
g_free (dialog->last_domain);
dialog->last_domain = NULL;
}
if (dialog->last_message)
{
g_free (dialog->last_message);
dialog->last_message = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object); G_OBJECT_CLASS (parent_class)->finalize (object);
} }
@ -110,6 +107,15 @@ gimp_error_dialog_response (GtkDialog *dialog,
gtk_widget_destroy (GTK_WIDGET (dialog)); gtk_widget_destroy (GTK_WIDGET (dialog));
} }
static void
gimp_error_dialog_message_destroy (gpointer data)
{
GimpErrorDialogMessage *item = (GimpErrorDialogMessage *) data;
g_free (item->domain);
g_free (item->message);
g_free (item);
}
/* public functions */ /* public functions */
@ -129,22 +135,30 @@ gimp_error_dialog_add (GimpErrorDialog *dialog,
const gchar *domain, const gchar *domain,
const gchar *message) const gchar *message)
{ {
GtkWidget *box; GimpErrorDialogMessage *item;
gboolean overflow = FALSE; gboolean overflow = FALSE;
g_return_if_fail (GIMP_IS_ERROR_DIALOG (dialog)); g_return_if_fail (GIMP_IS_ERROR_DIALOG (dialog));
g_return_if_fail (domain != NULL); g_return_if_fail (domain != NULL);
g_return_if_fail (message != NULL); g_return_if_fail (message != NULL);
if (dialog->last_box && if (dialog->messages)
dialog->last_domain && strcmp (dialog->last_domain, domain) == 0 &&
dialog->last_message && strcmp (dialog->last_message, message) == 0)
{ {
if (gimp_message_box_repeat (GIMP_MESSAGE_BOX (dialog->last_box))) GList *iter = dialog->messages;
for (; iter; iter = iter->next)
{
item = iter->data;
if (strcmp (item->domain, domain) == 0 &&
strcmp (item->message, message) == 0)
{
if (gimp_message_box_repeat (GIMP_MESSAGE_BOX (item->box)))
return; return;
} }
}
}
if (dialog->num_messages >= GIMP_ERROR_DIALOG_MAX_MESSAGES) if (g_list_length (dialog->messages) >= GIMP_ERROR_DIALOG_MAX_MESSAGES)
{ {
g_printerr ("%s: %s\n\n", domain, message); g_printerr ("%s: %s\n\n", domain, message);
@ -153,39 +167,36 @@ gimp_error_dialog_add (GimpErrorDialog *dialog,
domain = _("Too many error messages!"); domain = _("Too many error messages!");
message = _("Messages are redirected to stderr."); message = _("Messages are redirected to stderr.");
if (dialog->last_domain && strcmp (dialog->last_domain, domain) == 0 && if (dialog->overflow)
dialog->last_message && strcmp (dialog->last_message, message) == 0)
{ {
/* We were already overflowing. */
return; return;
} }
dialog->overflow = TRUE;
} }
box = g_object_new (GIMP_TYPE_MESSAGE_BOX, item = g_new0 (GimpErrorDialogMessage, 1);
item->box = g_object_new (GIMP_TYPE_MESSAGE_BOX,
"icon-name", icon_name, "icon-name", icon_name,
NULL); NULL);
item->domain = g_strdup (domain);
dialog->num_messages++; item->message = g_strdup (message);
if (overflow) if (overflow)
gimp_message_box_set_primary_text (GIMP_MESSAGE_BOX (box), "%s", domain); gimp_message_box_set_primary_text (GIMP_MESSAGE_BOX (item->box),
"%s", domain);
else else
gimp_message_box_set_primary_text (GIMP_MESSAGE_BOX (box), gimp_message_box_set_primary_text (GIMP_MESSAGE_BOX (item->box),
/* %s is a message domain, /* %s is a message domain,
* like "GIMP Message" or * like "GIMP Message" or
* "PNG Message" * "PNG Message"
*/ */
_("%s Message"), domain); _("%s Message"), domain);
gimp_message_box_set_text (GIMP_MESSAGE_BOX (box), "%s", message); gimp_message_box_set_text (GIMP_MESSAGE_BOX (item->box), "%s", message);
gtk_box_pack_start (GTK_BOX (dialog->vbox), box, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (dialog->vbox), item->box, TRUE, TRUE, 0);
gtk_widget_show (box); gtk_widget_show (item->box);
dialog->last_box = box; dialog->messages = g_list_prepend (dialog->messages, item);
g_free (dialog->last_domain);
dialog->last_domain = g_strdup (domain);
g_free (dialog->last_message);
dialog->last_message = g_strdup (message);
} }

View File

@ -40,10 +40,8 @@ struct _GimpErrorDialog
GtkWidget *vbox; GtkWidget *vbox;
GtkWidget *last_box; GList *messages;
gchar *last_domain; gboolean overflow;
gchar *last_message;
gint num_messages;
}; };
struct _GimpErrorDialogClass struct _GimpErrorDialogClass