Merge commit 'origin/stable'
[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/str.h>
28 #include <libpspp/msg-locator.h>
29 #include "message-dialog.h"
30 #include "progname.h"
31
32
33 #include <gtk/gtk.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 GtkBuilder *message_xml;
52 static GtkWidget *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 = builder_new ("message-dialog.ui");
63   message_dialog = get_widget_assert (message_xml, "message-dialog");
64 }
65
66 void
67 message_dialog_done (void)
68 {
69   msg_done ();
70   g_queue_free (early_queue);
71   dropped_messages = 0;
72   g_queue_free (late_queue);
73   gtk_widget_destroy (message_dialog);
74   g_object_unref (message_xml);
75 }
76
77 static void
78 format_message (struct msg *m, struct string *msg)
79 {
80   const char *label;
81
82   if (m->where.file_name)
83     ds_put_format (msg, "%s:", m->where.file_name);
84   if (m->where.line_number != -1)
85     ds_put_format (msg, "%d:", m->where.line_number);
86   if (m->where.file_name || m->where.line_number != -1)
87     ds_put_char (msg, ' ');
88
89   switch (m->severity)
90     {
91     case MSG_ERROR:
92       switch (m->category)
93         {
94         case MSG_SYNTAX:
95           label = _("syntax error");
96           break;
97
98         case MSG_DATA:
99           label = _("data file error");
100           break;
101
102         case MSG_GENERAL:
103         default:
104           label = _("PSPP error");
105           break;
106         }
107       break;
108     case MSG_WARNING:
109       switch (m->category)
110         {
111         case MSG_SYNTAX:
112           label = _("syntax warning");
113           break;
114
115         case MSG_DATA:
116           label = _("data file warning");
117           break;
118
119         case MSG_GENERAL:
120         default:
121           label = _("PSPP warning");
122           break;
123         }
124       break;
125     case MSG_NOTE:
126     default:
127       switch (m->category)
128         {
129         case MSG_SYNTAX:
130           label = _("syntax information");
131           break;
132
133         case MSG_DATA:
134           label = _("data file information");
135           break;
136
137         case MSG_GENERAL:
138         default:
139           label = _("PSPP information");
140           break;
141         }
142       break;
143     }
144   ds_put_format (msg, "%s: %s\n", label, m->text);
145   msg_destroy (m);
146 }
147
148 static void
149 enqueue_msg (const struct msg *msg)
150 {
151   struct msg *m = msg_dup (msg);
152
153   switch (m->severity)
154     {
155     case MSG_ERROR:
156       error_cnt++;
157       break;
158     case MSG_WARNING:
159       warning_cnt++;
160       break;
161     case MSG_NOTE:
162       note_cnt++;
163       break;
164     }
165
166   if (g_queue_get_length (early_queue) < MAX_EARLY_MESSAGES)
167     {
168       if (g_queue_is_empty (early_queue))
169         g_idle_add (popup_messages, NULL);
170       g_queue_push_tail (early_queue, m);
171     }
172   else
173     {
174       if (g_queue_get_length (late_queue) >= MAX_LATE_MESSAGES)
175         {
176           struct msg *m = g_queue_pop_head (late_queue);
177           msg_destroy (m);
178           dropped_messages++;
179         }
180       g_queue_push_tail (late_queue, m);
181     }
182 }
183
184 static gboolean
185 popup_messages (gpointer unused UNUSED)
186 {
187   GtkTextBuffer *text_buffer;
188   GtkTextIter end;
189   GtkTextView *text_view;
190   GtkLabel *label;
191   struct string lead = DS_EMPTY_INITIALIZER;
192   struct string msg = DS_EMPTY_INITIALIZER;
193   int message_cnt;
194
195   /* Set up the dialog. */
196   if (message_xml == NULL || message_dialog == NULL)
197     goto use_fallback;
198
199   /* If a pointer grab is in effect, then the combination of that, and
200      a modal dialog box, will cause an impossible situation.
201      So don't pop it up just yet.
202   */
203   if ( gdk_display_pointer_is_grabbed (gtk_widget_get_display (message_dialog)))
204     {
205       ds_destroy (&lead);
206       ds_destroy (&msg);
207       return TRUE;
208     }
209
210   /* Compose the lead-in. */
211   message_cnt = error_cnt + warning_cnt + note_cnt;
212   if (dropped_messages == 0)
213     ds_put_format (
214       &lead,
215       ngettext ("The PSPP processing engine reported the following message:",
216                 "The PSPP processing engine reported the following messages:",
217                 message_cnt));
218   else
219     {
220       ds_put_format (
221         &lead,
222         ngettext ("The PSPP processing engine reported %d message.",
223                   "The PSPP processing engine reported %d messages.",
224                   message_cnt),
225         message_cnt);
226       ds_put_cstr (&lead, "  ");
227       ds_put_format (
228         &lead,
229         ngettext ("%d of these messages are displayed below.",
230                   "%d of these messages are displayed below.",
231                   MAX_EARLY_MESSAGES + MAX_LATE_MESSAGES),
232         MAX_EARLY_MESSAGES + MAX_LATE_MESSAGES);
233     }
234
235
236   /* Compose the messages. */
237   while (!g_queue_is_empty (early_queue))
238     format_message (g_queue_pop_head (early_queue), &msg);
239   if (dropped_messages)
240     {
241       ds_put_format (&msg, "...\nOmitting %lu messages\n...\n",
242                      dropped_messages);
243       dropped_messages = 0;
244     }
245   while (!g_queue_is_empty (late_queue))
246     format_message (g_queue_pop_head (late_queue), &msg);
247
248   text_buffer = gtk_text_buffer_new (NULL);
249   gtk_text_buffer_get_end_iter (text_buffer, &end);
250   gtk_text_buffer_insert (text_buffer, &end, ds_data (&msg), ds_length (&msg));
251
252   label = GTK_LABEL (get_widget_assert (message_xml, "lead-in"));
253   if (label == NULL)
254     goto use_fallback;
255   gtk_label_set_text (label, ds_cstr (&lead));
256
257   text_view = GTK_TEXT_VIEW (get_widget_assert (message_xml, "message"));
258   if (text_view == NULL)
259     goto use_fallback;
260   gtk_text_view_set_buffer (text_view, text_buffer);
261
262   gtk_widget_grab_focus (get_widget_assert (message_xml, "close-button"));
263   gtk_dialog_run ( GTK_DIALOG (message_dialog));
264   gtk_widget_hide (message_dialog);
265
266   ds_destroy (&lead);
267   ds_destroy (&msg);
268
269   return FALSE;
270
271 use_fallback:
272   g_warning ("Could not create message dialog.  "
273              "Is PSPPIRE properly installed?");
274   fputs (ds_cstr (&msg), stderr);
275   ds_destroy (&lead);
276   ds_destroy (&msg);
277   return FALSE;
278 }
279