作図ソフト dia の改良版
Revisión | 74f401706c77e444171557b13bd75a6c298916b3 (tree) |
---|---|
Tiempo | 2007-03-18 03:48:31 |
Autor | Hans Breuer <hans@breu...> |
Commiter | Hans Breuer |
plugged some leaks and made the filter sensitive extension update work, by
2007-03-17 Hans Breuer <hans@breuer.org>
* app/filedlg.c : plugged some leaks and made the filter sensitive
extension update work, by relying less on what we get back from gtk+
Fixes bug #403195.
svn path=/trunk/; revision=3639
@@ -1,5 +1,9 @@ | ||
1 | 1 | 2007-03-17 Hans Breuer <hans@breuer.org> |
2 | 2 | |
3 | + * app/filedlg.c : plugged some leaks and made the filter sensitive | |
4 | + extension update work, by relying less on what we get back from gtk+ | |
5 | + Fixes bug #403195. | |
6 | + | |
3 | 7 | [some playing with valgrind : memory leaks] |
4 | 8 | * lib/text.c(text_set_line_text) : removed superfluous g_strdup() |
5 | 9 | * app/interface.c : gtk_accelerator_get_label() and g_strconcat() |
@@ -149,7 +149,7 @@ file_open_response_callback(GtkWidget *fs, | ||
149 | 149 | gint response, |
150 | 150 | gpointer user_data) |
151 | 151 | { |
152 | - const char *filename; | |
152 | + char *filename; | |
153 | 153 | Diagram *diagram = NULL; |
154 | 154 | |
155 | 155 | if (response == GTK_RESPONSE_ACCEPT) { |
@@ -160,6 +160,8 @@ file_open_response_callback(GtkWidget *fs, | ||
160 | 160 | filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fs)); |
161 | 161 | |
162 | 162 | diagram = diagram_load(filename, ifilter_by_index (index - 1, filename)); |
163 | + | |
164 | + g_free (filename); | |
163 | 165 | |
164 | 166 | if (diagram != NULL) { |
165 | 167 | diagram_update_extents(diagram); |
@@ -289,7 +291,7 @@ file_save_as_response_callback(GtkWidget *fs, | ||
289 | 291 | gint response, |
290 | 292 | gpointer user_data) |
291 | 293 | { |
292 | - const char *filename; | |
294 | + char *filename; | |
293 | 295 | Diagram *dia; |
294 | 296 | struct stat stat_struct; |
295 | 297 |
@@ -325,6 +327,7 @@ file_save_as_response_callback(GtkWidget *fs, | ||
325 | 327 | /* don't hide/destroy the dialog, but simply go back to it */ |
326 | 328 | gtk_window_present (GTK_WINDOW (fs)); |
327 | 329 | gtk_widget_destroy(dialog); |
330 | + g_free (filename); | |
328 | 331 | return; |
329 | 332 | } |
330 | 333 | gtk_widget_destroy(dialog); |
@@ -336,6 +339,8 @@ file_save_as_response_callback(GtkWidget *fs, | ||
336 | 339 | |
337 | 340 | diagram_set_filename(dia, filename); |
338 | 341 | diagram_save(dia, filename); |
342 | + | |
343 | + g_free (filename); | |
339 | 344 | } |
340 | 345 | /* if we have our own reference, drop it before destroy */ |
341 | 346 | if ((dia = gtk_object_get_user_data(GTK_OBJECT(fs))) != NULL) { |
@@ -455,49 +460,63 @@ file_save_callback(gpointer data, guint action, GtkWidget *widget) | ||
455 | 460 | } |
456 | 461 | |
457 | 462 | /** |
458 | - * Given an export filter index and optionally a filename for fallback | |
459 | - * return the export filter to use | |
463 | + * Given an export filter index return the export filter to use | |
460 | 464 | */ |
461 | 465 | static DiaExportFilter * |
462 | -efilter_by_index (int index, const char* filename) | |
466 | +efilter_by_index (int index, const gchar** ext) | |
463 | 467 | { |
464 | 468 | DiaExportFilter *efilter = NULL; |
465 | 469 | |
466 | - if (index >= 0) | |
470 | + /* the index in the selection list *is* the index of the filter, | |
471 | + * filters supporing multiple formats are multiple times in the list */ | |
472 | + if (index >= 0) { | |
467 | 473 | efilter = g_list_nth_data (filter_get_export_filters(), index); |
468 | - else if (filename) /* fallback, should not happen */ | |
469 | - efilter = filter_guess_export_filter(filename); | |
474 | + if (efilter) { | |
475 | + if (ext) | |
476 | + *ext = efilter->extensions[0]; | |
477 | + return efilter; | |
478 | + } | |
479 | + else /* getting here means invalid index */ | |
480 | + g_warning ("efilter_by_index() index=%d out of range", index); | |
481 | + } | |
470 | 482 | |
471 | 483 | return efilter; |
472 | 484 | } |
473 | 485 | |
474 | 486 | /** |
475 | - * Adapt the filename to the export filter selection | |
487 | + * Adapt the filename to the export filter index | |
476 | 488 | */ |
477 | 489 | static void |
478 | -export_set_extension(GtkWidget *widget) | |
490 | +export_adapt_extension (const gchar* name, int index) | |
479 | 491 | { |
480 | - int index = gtk_combo_box_get_active (GTK_COMBO_BOX(widget)) - 1; /* Ignore "By Extension" */ | |
481 | - DiaExportFilter *efilter = efilter_by_index (index, NULL); | |
482 | - GString *s; | |
483 | - const gchar *text = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(exportdlg)); | |
484 | - const gchar *last_dot = text ? strrchr(text, '.') : NULL; | |
485 | - gchar *basename = NULL; | |
486 | - | |
487 | - if (!efilter || last_dot == text || text[0] == '\0' || | |
488 | - efilter->extensions[0] == NULL) | |
489 | - return; | |
490 | - basename = g_path_get_basename (text); | |
491 | - last_dot = strrchr(basename, '.'); | |
492 | - s = g_string_new(basename); | |
493 | - if (last_dot) | |
494 | - g_string_truncate(s, last_dot-basename); | |
495 | - g_string_append(s, "."); | |
496 | - g_string_append(s, efilter->extensions[0]); | |
497 | - gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(exportdlg), s->str); | |
498 | - g_string_free (s, TRUE); | |
492 | + const gchar* ext = NULL; | |
493 | + DiaExportFilter *efilter = efilter_by_index (index, &ext); | |
494 | + gchar *basename = g_path_get_basename (name); | |
495 | + | |
496 | + if (!efilter || !ext) | |
497 | + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(exportdlg), basename); | |
498 | + else { | |
499 | + const gchar *last_dot = strrchr(basename, '.'); | |
500 | + GString *s = g_string_new(basename); | |
501 | + if (last_dot) | |
502 | + g_string_truncate(s, last_dot-basename); | |
503 | + g_string_append(s, "."); | |
504 | + g_string_append(s, ext); | |
505 | + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(exportdlg), s->str); | |
506 | + g_string_free (s, TRUE); | |
507 | + } | |
499 | 508 | g_free (basename); |
500 | 509 | } |
510 | +static void | |
511 | +export_adapt_extension_callback(GtkWidget *widget) | |
512 | +{ | |
513 | + int index = gtk_combo_box_get_active (GTK_COMBO_BOX(widget)); | |
514 | + gchar *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(exportdlg)); | |
515 | + | |
516 | + if (name && index > 0) /* Ignore "By Extension" */ | |
517 | + export_adapt_extension (name, index - 1); | |
518 | + g_free (name); | |
519 | +} | |
501 | 520 | |
502 | 521 | /** |
503 | 522 | * Create a new "option menu" for the export options |
@@ -522,7 +541,7 @@ create_export_menu(void) | ||
522 | 541 | g_free(filter_label); |
523 | 542 | } |
524 | 543 | g_signal_connect(GTK_OBJECT(menu), "changed", |
525 | - G_CALLBACK(export_set_extension), NULL); | |
544 | + G_CALLBACK(export_adapt_extension_callback), NULL); | |
526 | 545 | return menu; |
527 | 546 | } |
528 | 547 |
@@ -534,7 +553,7 @@ file_export_response_callback(GtkWidget *fs, | ||
534 | 553 | gint response, |
535 | 554 | gpointer user_data) |
536 | 555 | { |
537 | - const char *filename; | |
556 | + char *filename; | |
538 | 557 | Diagram *dia; |
539 | 558 | DiaExportFilter *ef; |
540 | 559 | struct stat statbuf; |
@@ -565,6 +584,7 @@ file_export_response_callback(GtkWidget *fs, | ||
565 | 584 | if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_YES) { |
566 | 585 | /* if not overwrite allow to select another filename */ |
567 | 586 | gtk_widget_destroy(dialog); |
587 | + g_free (filename); | |
568 | 588 | return; |
569 | 589 | } |
570 | 590 | gtk_widget_destroy(dialog); |
@@ -573,7 +593,7 @@ file_export_response_callback(GtkWidget *fs, | ||
573 | 593 | index = gtk_combo_box_get_active (GTK_COMBO_BOX(user_data)); |
574 | 594 | if (index >= 0) |
575 | 595 | persistence_set_integer ("export-filter", index); |
576 | - ef = efilter_by_index (index - 1, filename); | |
596 | + ef = efilter_by_index (index - 1, NULL); | |
577 | 597 | if (!ef) |
578 | 598 | ef = filter_guess_export_filter(filename); |
579 | 599 | if (ef) { |
@@ -583,6 +603,7 @@ file_export_response_callback(GtkWidget *fs, | ||
583 | 603 | } else |
584 | 604 | message_error(_("Could not determine which export filter\n" |
585 | 605 | "to use to save '%s'"), dia_message_filename(filename)); |
606 | + g_free (filename); | |
586 | 607 | } |
587 | 608 | g_object_unref (dia); /* drop our diagram reference */ |
588 | 609 | gtk_widget_destroy(exportdlg); |
@@ -672,15 +693,13 @@ file_export_callback(gpointer data, guint action, GtkWidget *widget) | ||
672 | 693 | char *basename = g_path_get_basename (fnabs); |
673 | 694 | /* can't use gtk_file_chooser_set_filename for various reasons, see e.g. bug #305850 */ |
674 | 695 | gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(exportdlg), folder); |
675 | - gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER(exportdlg), basename); | |
696 | + export_adapt_extension (basename, persistence_get_integer ("export-filter") - 1); | |
676 | 697 | g_free (folder); |
677 | 698 | g_free (basename); |
678 | 699 | } |
679 | 700 | g_free(fnabs); |
680 | 701 | g_free(filename); |
681 | 702 | } |
682 | - export_set_extension(GTK_WIDGET(g_object_get_data(G_OBJECT(exportdlg), | |
683 | - "export-menu"))); | |
684 | 703 | |
685 | 704 | gtk_widget_show(exportdlg); |
686 | 705 | } |