1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimperrordialog.c
5  * Copyright (C) 2004  Sven Neumann <sven@gimp.org>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include <string.h>
24 
25 #include <gegl.h>
26 #include <gtk/gtk.h>
27 
28 #include "libgimpwidgets/gimpwidgets.h"
29 
30 #include "widgets-types.h"
31 
32 #include "gimperrordialog.h"
33 #include "gimpmessagebox.h"
34 
35 #include "gimp-intl.h"
36 
37 #define GIMP_ERROR_DIALOG_MAX_MESSAGES 3
38 
39 
40 typedef struct
41 {
42   GtkWidget *box;
43   gchar     *domain;
44   gchar     *message;
45 } GimpErrorDialogMessage;
46 
47 static void   gimp_error_dialog_finalize        (GObject   *object);
48 static void   gimp_error_dialog_response        (GtkDialog *dialog,
49                                                  gint       response_id);
50 
51 static void   gimp_error_dialog_message_destroy (gpointer   data);
52 
G_DEFINE_TYPE(GimpErrorDialog,gimp_error_dialog,GIMP_TYPE_DIALOG)53 G_DEFINE_TYPE (GimpErrorDialog, gimp_error_dialog, GIMP_TYPE_DIALOG)
54 
55 #define parent_class gimp_error_dialog_parent_class
56 
57 
58 static void
59 gimp_error_dialog_class_init (GimpErrorDialogClass *klass)
60 {
61   GObjectClass   *object_class = G_OBJECT_CLASS (klass);
62   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
63 
64   object_class->finalize = gimp_error_dialog_finalize;
65 
66   dialog_class->response = gimp_error_dialog_response;
67 }
68 
69 static void
gimp_error_dialog_init(GimpErrorDialog * dialog)70 gimp_error_dialog_init (GimpErrorDialog *dialog)
71 {
72   gtk_window_set_role (GTK_WINDOW (dialog), "gimp-message");
73 
74   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
75 
76                           _("_OK"), GTK_RESPONSE_CLOSE,
77 
78                           NULL);
79 
80   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
81 
82   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
83 
84   dialog->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
85   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
86                       dialog->vbox, TRUE, TRUE, 0);
87   gtk_widget_show (dialog->vbox);
88 
89   dialog->messages = NULL;
90   dialog->overflow = FALSE;
91 }
92 
93 static void
gimp_error_dialog_finalize(GObject * object)94 gimp_error_dialog_finalize (GObject *object)
95 {
96   GimpErrorDialog *dialog = GIMP_ERROR_DIALOG (object);
97 
98   g_list_free_full (dialog->messages,
99                     gimp_error_dialog_message_destroy);
100 
101   G_OBJECT_CLASS (parent_class)->finalize (object);
102 }
103 
104 static void
gimp_error_dialog_response(GtkDialog * dialog,gint response_id)105 gimp_error_dialog_response (GtkDialog *dialog,
106                             gint       response_id)
107 {
108   gtk_widget_destroy (GTK_WIDGET (dialog));
109 }
110 
111 static void
gimp_error_dialog_message_destroy(gpointer data)112 gimp_error_dialog_message_destroy (gpointer data)
113 {
114   GimpErrorDialogMessage *item = (GimpErrorDialogMessage *) data;
115 
116   g_free (item->domain);
117   g_free (item->message);
118   g_free (item);
119 }
120 
121 
122 /*  public functions  */
123 
124 GtkWidget *
gimp_error_dialog_new(const gchar * title)125 gimp_error_dialog_new (const gchar *title)
126 {
127   g_return_val_if_fail (title != NULL, NULL);
128 
129   return g_object_new (GIMP_TYPE_ERROR_DIALOG,
130                        "title", title,
131                        NULL);
132 }
133 
134 void
gimp_error_dialog_add(GimpErrorDialog * dialog,const gchar * icon_name,const gchar * domain,const gchar * message)135 gimp_error_dialog_add (GimpErrorDialog *dialog,
136                        const gchar     *icon_name,
137                        const gchar     *domain,
138                        const gchar     *message)
139 {
140   GimpErrorDialogMessage *item;
141   gboolean                overflow = FALSE;
142 
143   g_return_if_fail (GIMP_IS_ERROR_DIALOG (dialog));
144   g_return_if_fail (domain != NULL);
145   g_return_if_fail (message != NULL);
146 
147   if (dialog->messages)
148     {
149       GList *iter = dialog->messages;
150 
151       for (; iter; iter = iter->next)
152         {
153           item = iter->data;
154           if (strcmp (item->domain, domain)   == 0 &&
155               strcmp (item->message, message) == 0)
156             {
157               if (gimp_message_box_repeat (GIMP_MESSAGE_BOX (item->box)))
158                 return;
159             }
160         }
161     }
162 
163   if (g_list_length (dialog->messages) >= GIMP_ERROR_DIALOG_MAX_MESSAGES)
164     {
165       g_printerr ("%s: %s\n\n", domain, message);
166 
167       overflow  = TRUE;
168       icon_name = GIMP_ICON_WILBER_EEK;
169       domain    = _("Too many error messages!");
170       message   = _("Messages are redirected to stderr.");
171 
172       if (dialog->overflow)
173         {
174           /* We were already overflowing. */
175           return;
176         }
177       dialog->overflow = TRUE;
178     }
179 
180   item = g_new0 (GimpErrorDialogMessage, 1);
181   item->box = g_object_new (GIMP_TYPE_MESSAGE_BOX,
182                             "icon-name", icon_name,
183                             NULL);
184   item->domain  = g_strdup (domain);
185   item->message = g_strdup (message);
186 
187   if (overflow)
188     gimp_message_box_set_primary_text (GIMP_MESSAGE_BOX (item->box),
189                                        "%s", domain);
190   else
191     gimp_message_box_set_primary_text (GIMP_MESSAGE_BOX (item->box),
192                                        /* %s is a message domain,
193                                         * like "GIMP Message" or
194                                         * "PNG Message"
195                                         */
196                                        _("%s Message"), domain);
197 
198   gimp_message_box_set_text (GIMP_MESSAGE_BOX (item->box), "%s", message);
199 
200   gtk_box_pack_start (GTK_BOX (dialog->vbox), item->box, TRUE, TRUE, 0);
201   gtk_widget_show (item->box);
202 
203   dialog->messages = g_list_prepend (dialog->messages, item);
204 }
205