Consolidate multiple messages into single message dialog. Patch
[pspp-builds.git] / src / ui / gui / message-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 #include <stdio.h>
19 #include <stdarg.h>
20
21 #include <config.h>
22 #include <gettext.h>
23 #define _(msgid) gettext (msgid)
24 #define N_(msgid) msgid
25
26 #include <libpspp/message.h>
27 #include <libpspp/msg-locator.h>
28 #include "message-dialog.h"
29 #include "progname.h"
30
31
32 #include <gtk/gtk.h>
33 #include <glade/glade.h>
34 #include <glib.h>
35
36 #include "helper.h"
37
38 static void enqueue_msg (const struct msg *m);
39 static gboolean popup_messages (gpointer);
40
41 #define MAX_EARLY_MESSAGES 100
42 static GQueue *early_queue;
43
44 static unsigned long dropped_messages;
45
46 #define MAX_LATE_MESSAGES 10
47 static GQueue *late_queue;
48
49 static int error_cnt, warning_cnt, note_cnt;
50
51 static GladeXML *message_xml;
52 static GtkDialog *message_dialog;
53
54 void
55 message_dialog_init (struct source_stream *ss)
56 {
57   early_queue = g_queue_new ();
58   dropped_messages = 0;
59   late_queue = g_queue_new ();
60   error_cnt = warning_cnt = note_cnt = 0;
61   msg_init (ss, enqueue_msg);
62   message_xml = XML_NEW ("message-dialog.glade");
63   message_dialog = GTK_DIALOG (get_widget_assert (message_xml,
64                                                   "message-dialog"));
65 }
66
67 void
68 message_dialog_done (void)
69 {
70   msg_done ();
71   g_queue_free (early_queue);
72   dropped_messages = 0;
73   g_queue_free (late_queue);
74   gtk_widget_destroy (GTK_WIDGET (message_dialog));
75   g_object_unref (message_xml);
76 }
77
78 static void
79 format_message (struct msg *m, struct string *msg)
80 {
81   const char *label;
82
83   if (m->where.file_name)
84     ds_put_format (msg, "%s:", m->where.file_name);
85   if (m->where.line_number != -1)
86     ds_put_format (msg, "%d:", m->where.line_number);
87   if (m->where.file_name || m->where.line_number != -1)
88     ds_put_char (msg, ' ');
89
90   switch (m->severity)
91     {
92     case MSG_ERROR:
93       switch (m->category)
94         {
95         case MSG_SYNTAX:
96           label = _("syntax error");
97           break;
98
99         case MSG_DATA:
100           label = _("data file error");
101           break;
102
103         case MSG_GENERAL:
104         default:
105           label = _("PSPP error");
106           break;
107         }
108       break;
109     case MSG_WARNING:
110       switch (m->category)
111         {
112         case MSG_SYNTAX:
113           label = _("syntax warning");
114           break;
115
116         case MSG_DATA:
117           label = _("data file warning");
118           break;
119
120         case MSG_GENERAL:
121         default:
122           label = _("PSPP warning");
123           break;
124         }
125       break;
126     case MSG_NOTE:
127     default:
128       switch (m->category)
129         {
130         case MSG_SYNTAX:
131           label = _("syntax information");
132           break;
133
134         case MSG_DATA:
135           label = _("data file information");
136           break;
137
138         case MSG_GENERAL:
139         default:
140           label = _("PSPP information");
141           break;
142         }
143       break;
144     }
145   ds_put_format (msg, "%s: %s\n", label, m->text);
146   msg_destroy (m);
147 }
148
149 static void
150 enqueue_msg (const struct msg *msg)
151 {
152   struct msg *m = msg_dup (msg);
153
154   switch (m->severity)
155     {
156     case MSG_ERROR:
157       error_cnt++;
158       break;
159     case MSG_WARNING:
160       warning_cnt++;
161       break;
162     case MSG_NOTE:
163       note_cnt++;
164       break;
165     }
166
167   if (g_queue_get_length (early_queue) < MAX_EARLY_MESSAGES)
168     {
169       if (g_queue_is_empty (early_queue))
170         g_idle_add (popup_messages, NULL);
171       g_queue_push_tail (early_queue, m);
172     }
173   else
174     {
175       if (g_queue_get_length (late_queue) >= MAX_LATE_MESSAGES)
176         {
177           struct msg *m = g_queue_pop_head (late_queue);
178           msg_destroy (m);
179           dropped_messages++;
180         }
181       g_queue_push_tail (late_queue, m);
182     }
183 }
184
185 gboolean
186 popup_messages (gpointer unused UNUSED)
187 {
188   GtkTextBuffer *text_buffer;
189   GtkTextIter end;
190   GtkTextView *text_view;
191   GtkLabel *label;
192   struct string lead, msg;
193   int message_cnt;
194
195   /* If a pointer grab is in effect, then the combination of that, and
196      a modal dialog box, will cause an impossible situation.
197      So don't pop it up just yet.
198   */
199   if ( gdk_pointer_is_grabbed ())
200     return TRUE;
201
202   /* Compose the lead-in. */
203   message_cnt = error_cnt + warning_cnt + note_cnt;
204   ds_init_empty (&lead);
205   if (dropped_messages == 0)
206     ds_put_format (
207       &lead,
208       ngettext ("The PSPP processing engine reported the following message:",
209                 "The PSPP processing engine reported the following messages:",
210                 message_cnt));
211   else
212     {
213       ds_put_format (
214         &lead,
215         ngettext ("The PSPP processing engine reported %d message.",
216                   "The PSPP processing engine reported %d messages.",
217                   message_cnt),
218         message_cnt);
219       ds_put_cstr (&lead, "  ");
220       ds_put_format (
221         &lead,
222         ngettext ("%d of these messages are displayed below.",
223                   "%d of these messages are displayed below.",
224                   MAX_EARLY_MESSAGES + MAX_LATE_MESSAGES),
225         MAX_EARLY_MESSAGES + MAX_LATE_MESSAGES);
226     }
227
228
229   /* Compose the messages. */
230   ds_init_empty (&msg);
231   while (!g_queue_is_empty (early_queue))
232     format_message (g_queue_pop_head (early_queue), &msg);
233   if (dropped_messages)
234     {
235       ds_put_format (&msg, "...\nOmitting %lu messages\n...\n",
236                      dropped_messages);
237       dropped_messages = 0;
238     }
239   while (!g_queue_is_empty (late_queue))
240     format_message (g_queue_pop_head (late_queue), &msg);
241
242   /* Set up the dialog. */
243   if (message_xml == NULL || message_dialog == NULL)
244     goto use_fallback;
245
246   text_buffer = gtk_text_buffer_new (NULL);
247   gtk_text_buffer_get_end_iter (text_buffer, &end);
248   gtk_text_buffer_insert (text_buffer, &end, ds_data (&msg), ds_length (&msg));
249   ds_destroy (&msg);
250
251   label = GTK_LABEL (get_widget_assert (message_xml, "lead-in"));
252   if (label == NULL)
253     goto use_fallback;
254   gtk_label_set_text (label, ds_cstr (&lead));
255
256   text_view = GTK_TEXT_VIEW (get_widget_assert (message_xml, "message"));
257   if (text_view == NULL)
258     goto use_fallback;
259   gtk_text_view_set_buffer (text_view, text_buffer);
260
261   gtk_dialog_run (message_dialog);
262   gtk_widget_hide (GTK_WIDGET (message_dialog));
263
264   return FALSE;
265
266 use_fallback:
267   g_warning ("Could not create message dialog.  "
268              "Is PSPPIRE properly installed?");
269   fputs (ds_cstr (&msg), stderr);
270   ds_destroy (&lead);
271   ds_destroy (&msg);
272   return FALSE;
273 }
274