]> git.sur5r.net Git - glabels/blob - src/file.c
Added Sigel template
[glabels] / src / file.c
1 /*
2  *  file.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels 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  *  gLabels 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 gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "file.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <string.h>
28
29 #include "xml-label.h"
30 #include "recent.h"
31 #include "file-util.h"
32 #include "window.h"
33 #include "new-label-dialog.h"
34 #include <libglabels.h>
35
36 #include "debug.h"
37
38
39 /*===========================================*/
40 /* Private globals                           */
41 /*===========================================*/
42
43 /* Saved state for new dialog */
44 static gchar   *page_size   = NULL;
45 static gchar   *category    = NULL;
46 static gchar   *sheet_name  = NULL;
47 static gboolean rotate_flag = FALSE;
48
49 /* Saved state of file selectors */
50 static gchar *open_path = NULL;
51 static gchar *save_path = NULL;
52
53
54 /*===========================================*/
55 /* Local function prototypes.                */
56 /*===========================================*/
57 static void new_complete                     (GtkDialog         *dialog,
58                                               gpointer           user_data);
59
60 static void properties_complete              (GtkDialog         *dialog,
61                                               gpointer           user_data);
62
63 static void open_response                    (GtkDialog         *chooser,
64                                               gint               response,
65                                               glWindow          *window);
66 static void save_as_response                 (GtkDialog         *chooser,
67                                               gint               response,
68                                               glLabel           *label);
69
70
71 /*****************************************************************************/
72 /* "New" menu callback.                                                      */
73 /*****************************************************************************/
74 void
75 gl_file_new (glWindow  *window)
76 {
77         GtkWidget    *dialog;
78
79         gl_debug (DEBUG_FILE, "START");
80
81         g_return_if_fail (window && GTK_IS_WINDOW (window));
82
83         dialog = gl_new_label_dialog_new (GTK_WINDOW (window));
84         gtk_window_set_title (GTK_WINDOW (dialog), _("New Label or Card"));
85
86         g_object_set_data (G_OBJECT (dialog), "parent_window", window);
87
88         g_signal_connect (G_OBJECT(dialog), "complete", G_CALLBACK (new_complete), dialog);
89
90         if (page_size != NULL) {
91                 gl_new_label_dialog_set_filter_parameters (GL_NEW_LABEL_DIALOG (dialog),
92                                                             page_size,
93                                                             category);
94         }
95         if (sheet_name != NULL) {
96                 gl_new_label_dialog_set_template_name (GL_NEW_LABEL_DIALOG (dialog),
97                                           sheet_name);
98         }
99         gl_new_label_dialog_set_rotate_state (GL_NEW_LABEL_DIALOG (dialog), rotate_flag);
100
101         gtk_widget_show_all (GTK_WIDGET (dialog));
102
103         gl_debug (DEBUG_FILE, "END");
104 }
105
106
107 /*---------------------------------------------------------------------------*/
108 /* PRIVATE.  New "ok" button callback.                                       */
109 /*---------------------------------------------------------------------------*/
110 static void
111 new_complete (GtkDialog *dialog,
112               gpointer   user_data)
113 {
114         lglTemplate *template;
115         glLabel     *label;
116         glWindow    *window;
117         GtkWidget   *new_window;
118
119         gl_debug (DEBUG_FILE, "START");
120
121         gl_new_label_dialog_get_filter_parameters (GL_NEW_LABEL_DIALOG (dialog),
122                                                    &page_size,
123                                                    &category);
124
125         if (sheet_name != NULL)
126         {
127                 g_free (sheet_name);
128         }
129
130         sheet_name = gl_new_label_dialog_get_template_name (GL_NEW_LABEL_DIALOG (dialog));
131
132         rotate_flag = gl_new_label_dialog_get_rotate_state (GL_NEW_LABEL_DIALOG (dialog));
133
134         template = lgl_db_lookup_template_from_name (sheet_name);
135
136         label = GL_LABEL(gl_label_new ());
137         gl_label_set_template (label, template, FALSE);
138         gl_label_set_rotate_flag (label, rotate_flag, FALSE);
139
140         lgl_template_free (template);
141
142         window = GL_WINDOW (g_object_get_data (G_OBJECT (dialog), "parent_window"));
143         if ( gl_window_is_empty (window) )
144         {
145                 gl_window_set_label (window, label);
146         }
147         else
148         {
149                 new_window = gl_window_new_from_label (label);
150                 gtk_widget_show_all (new_window);
151         }
152                 
153         gl_debug (DEBUG_FILE, "END");
154 }
155
156
157 /*****************************************************************************/
158 /* "Properties" menu callback.                                               */
159 /*****************************************************************************/
160 void
161 gl_file_properties (glLabel   *label,
162                     glWindow  *window)
163 {
164         const lglTemplate *template;
165         gboolean           rotate_flag;
166         GtkWidget         *dialog;
167         gchar             *name;
168
169         gl_debug (DEBUG_FILE, "START");
170
171         g_return_if_fail (label && GL_IS_LABEL (label));
172         g_return_if_fail (window && GTK_IS_WINDOW (window));
173
174         dialog = gl_new_label_dialog_new (GTK_WINDOW (window));
175         gtk_window_set_title (GTK_WINDOW (dialog), _("Label properties"));
176
177         g_object_set_data (G_OBJECT (dialog), "label", label);
178
179         g_signal_connect (G_OBJECT(dialog), "complete", G_CALLBACK (properties_complete), dialog);
180
181         template    = gl_label_get_template (label);
182         rotate_flag = gl_label_get_rotate_flag (label);
183
184         if (template->paper_id != NULL) {
185                 gl_new_label_dialog_set_filter_parameters (GL_NEW_LABEL_DIALOG (dialog),
186                                                            template->paper_id,
187                                                            NULL);
188         }
189         name = lgl_template_get_name (template);
190         if (name != NULL) {
191                 gl_new_label_dialog_set_template_name (GL_NEW_LABEL_DIALOG (dialog), name);
192         }
193         g_free (name);
194
195         gl_new_label_dialog_set_rotate_state (GL_NEW_LABEL_DIALOG (dialog), rotate_flag);
196
197         gtk_widget_show_all (GTK_WIDGET (dialog));
198
199         gl_debug (DEBUG_FILE, "END");
200 }
201
202
203 /*---------------------------------------------------------------------------*/
204 /* PRIVATE.  Properties "ok" button callback.                                */
205 /*---------------------------------------------------------------------------*/
206 static void
207 properties_complete (GtkDialog *dialog,
208                      gpointer   user_data)
209 {
210         lglTemplate *template;
211         glLabel     *label;
212
213         gl_debug (DEBUG_FILE, "START");
214
215         gl_new_label_dialog_get_filter_parameters (GL_NEW_LABEL_DIALOG (dialog),
216                                                    &page_size,
217                                                    &category);
218
219         if (sheet_name != NULL)
220         {
221                 g_free (sheet_name);
222         }
223
224         sheet_name = gl_new_label_dialog_get_template_name (GL_NEW_LABEL_DIALOG (dialog));
225
226         rotate_flag = gl_new_label_dialog_get_rotate_state (GL_NEW_LABEL_DIALOG (dialog));
227
228         template = lgl_db_lookup_template_from_name (sheet_name);
229
230         label = GL_LABEL(g_object_get_data (G_OBJECT (dialog), "label"));
231
232         gl_label_set_template (label, template, TRUE);
233         gl_label_set_rotate_flag (label, rotate_flag, TRUE);
234
235         gl_debug (DEBUG_FILE, "END");
236 }
237
238
239 /*****************************************************************************/
240 /* "Open" menu callback.                                                     */
241 /*****************************************************************************/
242 void
243 gl_file_open (glWindow  *window)
244 {
245         GtkWidget     *chooser;
246         GtkFileFilter *filter;
247
248         gl_debug (DEBUG_FILE, "START");
249
250         g_return_if_fail (window != NULL);
251
252         chooser = gtk_file_chooser_dialog_new ("Open label",
253                                                GTK_WINDOW (window),
254                                                GTK_FILE_CHOOSER_ACTION_OPEN,
255                                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
256                                                GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
257                                                NULL);
258
259         /* Recover state of open dialog */
260         if (open_path != NULL) {
261                 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(chooser),
262                                                      open_path);
263         }
264
265         filter = gtk_file_filter_new ();
266         gtk_file_filter_add_pattern (filter, "*");
267         gtk_file_filter_set_name (filter, _("All files"));
268         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
269
270         filter = gtk_file_filter_new ();
271         gtk_file_filter_add_pattern (filter, "*.glabels");
272         gtk_file_filter_set_name (filter, _("gLabels documents"));
273         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
274
275         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser), filter);
276
277         g_signal_connect (G_OBJECT (chooser), "response",
278                           G_CALLBACK (open_response), window);
279
280         /* show the dialog */
281         gtk_widget_show (GTK_WIDGET (chooser));
282
283         gl_debug (DEBUG_FILE, "END");
284 }
285
286
287 /*---------------------------------------------------------------------------*/
288 /* PRIVATE.  Open "response" callback.                                       */
289 /*---------------------------------------------------------------------------*/
290 static void
291 open_response (GtkDialog     *chooser,
292                gint           response,
293                glWindow      *window)
294 {
295         gchar            *raw_filename;
296         gchar            *filename;
297         GtkWidget        *dialog;
298
299         gl_debug (DEBUG_FILE, "START");
300
301         g_return_if_fail (chooser && GTK_IS_FILE_CHOOSER (chooser));
302         g_return_if_fail (window && GTK_IS_WINDOW (window));
303
304         switch (response) {
305
306         case GTK_RESPONSE_ACCEPT:
307                 /* get the filename */
308                 raw_filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(chooser));
309                 filename = g_filename_to_utf8 (raw_filename, -1, NULL, NULL, NULL);
310
311                 if (!raw_filename || 
312                     !filename || 
313                     g_file_test (raw_filename, G_FILE_TEST_IS_DIR)) {
314
315                         dialog = gtk_message_dialog_new (GTK_WINDOW(chooser),
316                                                       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
317                                                       GTK_MESSAGE_WARNING,
318                                                       GTK_BUTTONS_CLOSE,
319                                                       _("Empty file name selection"));
320                         gtk_message_dialog_format_secondary_text (
321                                 GTK_MESSAGE_DIALOG (dialog),
322                                 _("Please select a file or supply a valid file name"));
323
324                         gtk_dialog_run (GTK_DIALOG (dialog));
325                         gtk_widget_destroy (dialog);
326
327                 } else {
328
329                         if (!g_file_test (raw_filename, G_FILE_TEST_IS_REGULAR)) {
330
331                                 dialog = gtk_message_dialog_new (GTK_WINDOW(chooser),
332                                                               GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
333                                                               GTK_MESSAGE_WARNING,
334                                                               GTK_BUTTONS_CLOSE,
335                                                               _("File does not exist"));
336                                 gtk_message_dialog_format_secondary_text (
337                                         GTK_MESSAGE_DIALOG (dialog),
338                                         _("Please select a file or supply a valid file name"));
339
340                                 gtk_dialog_run (GTK_DIALOG (dialog));
341                                 gtk_widget_destroy (dialog);
342
343
344                         } else {
345                 
346                                 if ( gl_file_open_real (filename, window) ) {
347                                         gtk_widget_destroy (GTK_WIDGET (chooser));
348                                 }
349
350                         }
351
352                 }
353
354                 g_free (filename);
355                 g_free (raw_filename);
356                 break;
357
358         default:
359                 gtk_widget_destroy (GTK_WIDGET (chooser));
360                 break;
361
362         }
363
364         gl_debug (DEBUG_FILE, "END");
365 }
366
367
368 /*****************************************************************************/
369 /* "Open recent" menu callback.                                              */
370 /*****************************************************************************/
371 void
372 gl_file_open_recent (const gchar     *filename,
373                      glWindow        *window)
374 {
375         gl_debug (DEBUG_FILE, "");
376
377         if (filename) {
378                 gl_debug (DEBUG_FILE, "open recent: %s", filename);
379
380                 gl_file_open_real (filename, window);
381         }
382 }
383
384
385 /*---------------------------------------------------------------------------*/
386 /* PRIVATE.  Open a file.                                                    */
387 /*---------------------------------------------------------------------------*/
388 gboolean
389 gl_file_open_real (const gchar     *filename,
390                    glWindow        *window)
391 {
392         gchar            *abs_filename;
393         glLabel          *label;
394         glXMLLabelStatus  status;
395         GtkWidget        *new_window;
396
397         gl_debug (DEBUG_FILE, "START");
398
399         abs_filename = gl_file_util_make_absolute (filename);
400         label = gl_xml_label_open (abs_filename, &status);
401         if (!label) {
402                 GtkWidget *dialog;
403
404                 gl_debug (DEBUG_FILE, "couldn't open file");
405
406                 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
407                                               GTK_DIALOG_DESTROY_WITH_PARENT,
408                                               GTK_MESSAGE_ERROR,
409                                               GTK_BUTTONS_CLOSE,
410                                               _("Could not open file \"%s\""),
411                                               filename);
412                 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
413                                                           _("Not a supported file format"));
414
415                 gtk_dialog_run (GTK_DIALOG (dialog));
416                 gtk_widget_destroy (dialog);
417
418                 g_free (abs_filename);
419
420                 gl_debug (DEBUG_FILE, "END false");
421
422                 return FALSE;
423
424         } else {
425
426                 if ( gl_window_is_empty (GL_WINDOW(window)) ) {
427                         gl_window_set_label (GL_WINDOW(window), label);
428                 } else {
429                         new_window = gl_window_new_from_label (label);
430                         gtk_widget_show_all (new_window);
431                 }
432
433                 gl_recent_add_utf8_filename (abs_filename);
434
435                 if (open_path != NULL)
436                         g_free (open_path);
437                 open_path = g_path_get_dirname (abs_filename);
438                 g_free (abs_filename);
439
440                 gl_debug (DEBUG_FILE, "END true");
441
442                 return TRUE;
443
444         }
445 }
446
447
448 /*****************************************************************************/
449 /* "Save" menu callback.                                                     */
450 /*****************************************************************************/
451 gboolean
452 gl_file_save (glLabel   *label,
453               glWindow  *window)
454 {
455         glXMLLabelStatus  status;
456         gchar            *filename = NULL;
457
458         gl_debug (DEBUG_FILE, "");
459
460         g_return_val_if_fail (label != NULL, FALSE);
461         
462         if (gl_label_is_untitled (label))
463         {
464                 gl_debug (DEBUG_FILE, "Untitled");
465
466                 return gl_file_save_as (label, window);
467         }
468
469         if (!gl_label_is_modified (label))      
470         {
471                 gl_debug (DEBUG_FILE, "Not modified");
472
473                 return TRUE;
474         }
475         
476         filename = gl_label_get_filename (label);
477         g_return_val_if_fail (filename != NULL, FALSE);
478         
479         gl_xml_label_save (label, filename, &status);
480
481         if (status != XML_LABEL_OK)
482         {
483                 GtkWidget *dialog;
484
485                 gl_debug (DEBUG_FILE, "FAILED");
486
487                 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
488                                               GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
489                                               GTK_MESSAGE_ERROR,
490                                               GTK_BUTTONS_CLOSE,
491                                               _("Could not save file \"%s\""),
492                                               filename);
493                 gtk_message_dialog_format_secondary_text (
494                         GTK_MESSAGE_DIALOG (dialog),
495                         _("Error encountered during save.  The file is still not saved."));
496
497                 gtk_dialog_run (GTK_DIALOG (dialog));
498                 gtk_widget_destroy (dialog);
499
500                 g_free (filename);
501
502                 return FALSE;
503         }       
504         else
505         {
506                 gl_debug (DEBUG_FILE, "OK");
507
508                 gl_recent_add_utf8_filename (filename);
509
510                 g_free (filename);
511
512                 return TRUE;
513         }
514 }
515
516
517 /*****************************************************************************/
518 /* "Save As" menu callback.                                                  */
519 /*****************************************************************************/
520 gboolean
521 gl_file_save_as (glLabel   *label,
522                  glWindow  *window)
523 {
524         GtkWidget        *chooser;
525         GtkFileFilter    *filter;
526         gboolean          saved_flag = FALSE;
527         gchar            *name, *title;
528
529         gl_debug (DEBUG_FILE, "START");
530
531         g_return_val_if_fail (label && GL_IS_LABEL(label), FALSE);
532         g_return_val_if_fail (window && GL_IS_WINDOW(window), FALSE);
533
534         name = gl_label_get_short_name (label);
535         title = g_strdup_printf (_("Save \"%s\" as"), name);
536         g_free (name);
537
538         chooser = gtk_file_chooser_dialog_new (title,
539                                                GTK_WINDOW (window),
540                                                GTK_FILE_CHOOSER_ACTION_SAVE,
541                                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
542                                                GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
543                                                NULL);
544
545         gtk_window_set_modal (GTK_WINDOW (chooser), TRUE);
546
547         g_free (title);
548
549         /* Recover proper state of save-as dialog */
550         if (save_path != NULL) {
551                 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(chooser),
552                                                      save_path);
553         }
554
555         filter = gtk_file_filter_new ();
556         gtk_file_filter_add_pattern (filter, "*");
557         gtk_file_filter_set_name (filter, _("All files"));
558         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
559
560         filter = gtk_file_filter_new ();
561         gtk_file_filter_add_pattern (filter, "*.glabels");
562         gtk_file_filter_set_name (filter, _("gLabels documents"));
563         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
564
565         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser), filter);
566
567         g_signal_connect (G_OBJECT (chooser), "response",
568                           G_CALLBACK (save_as_response), label);
569
570         g_object_set_data (G_OBJECT (chooser), "saved_flag", &saved_flag);
571
572         /* show the dialog */
573         gtk_widget_show (GTK_WIDGET (chooser));
574
575         /* Hold here and process events until we are done with this dialog. */
576         /* This is so we can return a boolean result of our save attempt.   */
577         gtk_main ();
578
579         gl_debug (DEBUG_FILE, "END");
580
581         /* Return flag as set by one of the above callbacks, TRUE = saved */
582         return saved_flag;
583 }
584
585
586 /*---------------------------------------------------------------------------*/
587 /* PRIVATE.  "Save As" ok button callback.                                   */
588 /*---------------------------------------------------------------------------*/
589 static void
590 save_as_response (GtkDialog     *chooser,
591                   gint           response,
592                   glLabel       *label)
593 {
594         gchar            *raw_filename, *filename, *full_filename;
595         GtkWidget        *dialog;
596         glXMLLabelStatus  status;
597         gboolean         *saved_flag;
598         gboolean          cancel_flag = FALSE;
599
600         gl_debug (DEBUG_FILE, "START");
601
602         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
603
604         saved_flag = g_object_get_data (G_OBJECT(chooser), "saved_flag");
605
606         switch (response) {
607
608         case GTK_RESPONSE_ACCEPT:
609                 /* get the filename */
610                 raw_filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(chooser));
611
612                 gl_debug (DEBUG_FILE, "raw_filename = \"%s\"", raw_filename);
613
614                 if (!raw_filename || g_file_test (raw_filename, G_FILE_TEST_IS_DIR)) {
615
616                         dialog = gtk_message_dialog_new (GTK_WINDOW(chooser),
617                                                       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
618                                                       GTK_MESSAGE_WARNING,
619                                                       GTK_BUTTONS_CLOSE,
620                                                       _("Empty file name selection"));
621                         gtk_message_dialog_format_secondary_text (
622                                 GTK_MESSAGE_DIALOG (dialog),
623                                 _("Please supply a valid file name"));
624
625                         gtk_dialog_run (GTK_DIALOG (dialog));
626                         gtk_widget_destroy (dialog);
627
628                 } else {
629
630                         full_filename = gl_file_util_add_extension (raw_filename);
631
632                         filename = g_filename_to_utf8 (full_filename, -1,
633                                                        NULL, NULL, NULL);
634
635                         gl_debug (DEBUG_FILE, "filename = \"%s\"", filename);
636
637                         if (g_file_test (full_filename, G_FILE_TEST_IS_REGULAR)) {
638                                 gint ret;
639
640                                 dialog = gtk_message_dialog_new (GTK_WINDOW(chooser),
641                                                               GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
642                                                               GTK_MESSAGE_QUESTION,
643                                                               GTK_BUTTONS_YES_NO,
644                                                               _("Overwrite file \"%s\"?"),
645                                                                filename);
646                                 gtk_message_dialog_format_secondary_text (
647                                         GTK_MESSAGE_DIALOG (dialog),
648                                         _("File already exists."));
649
650                                 ret = gtk_dialog_run (GTK_DIALOG (dialog));
651                                 if ( ret == GTK_RESPONSE_NO ) {
652                                         cancel_flag = TRUE;
653                                 }
654                                 gtk_widget_destroy (dialog);
655                         }
656
657                         if (!cancel_flag) {
658
659                                 gl_xml_label_save (label, filename, &status);
660
661                                 gl_debug (DEBUG_FILE, "status of save = %d", status);
662
663                                 if ( status != XML_LABEL_OK ) {
664
665                                         dialog = gtk_message_dialog_new (
666                                                 GTK_WINDOW(chooser),
667                                                 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
668                                                 GTK_MESSAGE_ERROR,
669                                                 GTK_BUTTONS_CLOSE,
670                                                 _("Could not save file \"%s\""),
671                                                 filename);
672                                         gtk_message_dialog_format_secondary_text (
673                                                 GTK_MESSAGE_DIALOG (dialog),
674                                                 _("Error encountered during save.  The file is still not saved."));
675
676                                         gtk_dialog_run (GTK_DIALOG (dialog));
677                                         gtk_widget_destroy (dialog);
678
679                                 } else {
680
681                                         *saved_flag = TRUE;
682
683                                         gl_recent_add_utf8_filename (filename);
684
685                                         if (save_path != NULL)
686                                                 g_free (save_path);
687                                         save_path = g_path_get_dirname (filename);
688
689                                         gtk_widget_destroy (GTK_WIDGET (chooser));
690                                         gtk_main_quit ();
691                                 }
692
693                         }
694
695                         g_free (filename);
696                         g_free (full_filename);
697                 }
698
699                 g_free (raw_filename);
700                 break;
701
702         default:
703                 *saved_flag = FALSE;
704                 gtk_widget_destroy (GTK_WIDGET (chooser));
705                 gtk_main_quit ();
706                 break;
707
708         }
709
710         gl_debug (DEBUG_FILE, "END");
711 }
712
713
714 /*****************************************************************************/
715 /* "Close" menu callback.                                                    */
716 /*****************************************************************************/
717 gboolean
718 gl_file_close (glWindow *window)
719 {
720         glView  *view;
721         glLabel *label;
722         gboolean close = TRUE;
723
724         gl_debug (DEBUG_FILE, "START");
725
726         g_return_val_if_fail (window && GL_IS_WINDOW(window), TRUE);
727
728         if ( !gl_window_is_empty (window) ) {
729
730                 view = GL_VIEW(window->view);
731                 label = view->label;
732
733                 if (gl_label_is_modified (label))       {
734                         GtkWidget *dialog;
735                         gchar *fname = NULL;
736                         gint ret;
737
738                         fname = gl_label_get_short_name (label);
739                         
740                         dialog = gtk_message_dialog_new (GTK_WINDOW(window),
741                                                          GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
742                                                          GTK_MESSAGE_WARNING,
743                                                          GTK_BUTTONS_NONE,
744                                                          _("Save changes to document \"%s\" before closing?"),
745                                                          fname);
746                         gtk_message_dialog_format_secondary_text (
747                                 GTK_MESSAGE_DIALOG (dialog),
748                                 _("Your changes will be lost if you don't save them."));
749
750                         gtk_dialog_add_button (GTK_DIALOG (dialog),
751                                                _("Close without saving"),
752                                                GTK_RESPONSE_NO);
753
754                         gtk_dialog_add_button (GTK_DIALOG (dialog),
755                                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
756
757                         gtk_dialog_add_button (GTK_DIALOG (dialog),
758                                                GTK_STOCK_SAVE, GTK_RESPONSE_YES);
759
760                         gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_YES);
761
762                         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
763
764                         ret = gtk_dialog_run (GTK_DIALOG (dialog));
765                 
766                         gtk_widget_destroy (dialog);
767
768                         g_free (fname);
769                 
770                         switch (ret)
771                         {
772                         case GTK_RESPONSE_YES:
773                                 close = gl_file_save (label, window);
774                                 break;
775                         case GTK_RESPONSE_NO:
776                                 close = TRUE;
777                                 break;
778                         default:
779                                 close = FALSE;
780                         }
781
782                         gl_debug (DEBUG_FILE, "CLOSE: %s", close ? "TRUE" : "FALSE");
783                 }
784
785         }
786
787         if (close) {
788                 gtk_widget_destroy (GTK_WIDGET(window));
789
790                 if ( gl_window_get_window_list () == NULL ) {
791                         
792                         gl_debug (DEBUG_FILE, "All windows closed.");
793         
794                         gtk_main_quit ();
795                 }
796
797         }
798
799         gl_debug (DEBUG_FILE, "END");
800
801         return close;
802 }
803
804
805 /*****************************************************************************/
806 /* "Exit" menu callback.                                                     */
807 /*****************************************************************************/
808 void
809 gl_file_exit (void)
810 {
811         const GList *window_list;
812         GList       *p, *p_next;
813
814         gl_debug (DEBUG_FILE, "START");
815
816         window_list = gl_window_get_window_list ();
817
818         for (p=(GList *)window_list; p != NULL; p=p_next) {
819                 p_next = p->next;
820
821                 gl_file_close (GL_WINDOW(p->data));
822         }
823
824         gl_debug (DEBUG_FILE, "END");
825 }
826
827
828
829 /*
830  * Local Variables:       -- emacs
831  * mode: C                -- emacs
832  * c-basic-offset: 8      -- emacs
833  * tab-width: 8           -- emacs
834  * indent-tabs-mode: nil  -- emacs
835  * End:                   -- emacs
836  */