Diagram * diagram_load(const char *filename, DiaImportFilter *ifilter) { Diagram *diagram = NULL; GList *diagrams; gboolean was_default = FALSE; for (diagrams = open_diagrams; diagrams != NULL; diagrams = g_list_next(diagrams)) { Diagram *old_diagram = (Diagram*)diagrams->data; if (old_diagram->is_default) { diagram = old_diagram; was_default = TRUE; break; } } /* TODO: Make diagram not be initialized twice */ if (diagram == NULL) { diagram = new_diagram(filename); } if (diagram == NULL) return NULL; if ( !diagram_init(diagram, filename) || !diagram_load_into (diagram, filename, ifilter)) { if (!was_default) /* don't kill the default diagram on import failure */ diagram_destroy(diagram); diagram = NULL; } else { /* not modifying 'diagram->unsaved' the state depends on the import filter used */ diagram_set_modified(diagram, FALSE); if (app_is_interactive()) { recent_file_history_add(filename); if (was_default) /* replacing it is like first remove than add */ dia_diagram_remove(diagram); dia_diagram_add(diagram); } } if (diagram != NULL && was_default && app_is_interactive()) { diagram_update_for_filename(diagram); diagram->is_default = FALSE; if ( g_slist_length(diagram->displays) == 1 ) display_set_active (diagram->displays->data); } return diagram; }
static void are_you_sure_close_dialog_respond(GtkWidget *widget, /* the dialog */ gint response_id, gpointer user_data) /* the display */ { DDisplay *ddisp = (DDisplay *)user_data; gboolean close_ddisp = TRUE; switch (response_id) { case GTK_RESPONSE_YES : /* save changes */ if (ddisp->diagram->unsaved) { /* we have to open the file dlg, close this one first */ gtk_widget_destroy(widget); if (file_save_as(ddisp->diagram, ddisp)) ddisp_destroy (ddisp); /* no way back */ return; } else { DiaContext *ctx = dia_context_new (_("Save")); if (!diagram_save(ddisp->diagram, ddisp->diagram->filename, ctx)) close_ddisp = FALSE; dia_context_release (ctx); } if (close_ddisp) /* saving succeeded */ recent_file_history_add(ddisp->diagram->filename); if (ddisp->update_id && close_ddisp) { g_source_remove (ddisp->update_id); ddisp->update_id = 0; } /* fall through */ case GTK_RESPONSE_NO : if (close_ddisp) ddisp_destroy (ddisp); /* fall through */ case GTK_RESPONSE_CANCEL : case GTK_RESPONSE_NONE : case GTK_RESPONSE_DELETE_EVENT : /* closing via window manager */ gtk_widget_destroy(widget); break; default : g_assert_not_reached(); } }
static GtkWidget * file_save_as_dialog_prepare (Diagram *dia, DDisplay *ddisp) { gchar *filename = NULL; if (!savedlg) { GtkWidget *compressbutton; savedlg = gtk_file_chooser_dialog_new(_("Save Diagram"), GTK_WINDOW(ddisp->shell), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); /* vfs saving is as easy - if you see 'bad file descriptor' there is * something wrong with the permissions of the share ;) */ gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER(savedlg), FALSE); gtk_dialog_set_default_response(GTK_DIALOG(savedlg), GTK_RESPONSE_ACCEPT); gtk_window_set_role(GTK_WINDOW(savedlg), "save_diagram"); /* Need better way to make it a reasonable size. Isn't there some*/ /* standard look for them (or is that just Gnome?)*/ compressbutton = gtk_check_button_new_with_label(_("Compress diagram files")); gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(savedlg), compressbutton); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compressbutton), dia->data->is_compressed); g_signal_connect(G_OBJECT(compressbutton), "toggled", G_CALLBACK(toggle_compress_callback), NULL); gtk_widget_show(compressbutton); gtk_widget_set_tooltip_text (compressbutton, _("Compression reduces file size to less than 1/10th " "size and speeds up loading and saving. Some text " "programs cannot manipulate compressed files.")); g_signal_connect (GTK_FILE_CHOOSER(savedlg), "response", G_CALLBACK(file_save_as_response_callback), compressbutton); g_signal_connect(G_OBJECT(savedlg), "destroy", G_CALLBACK(gtk_widget_destroyed), &savedlg); } else { GtkWidget *compressbutton = gtk_file_chooser_get_extra_widget(GTK_FILE_CHOOSER(savedlg)); gtk_widget_set_sensitive(savedlg, TRUE); g_signal_handlers_block_by_func(G_OBJECT(compressbutton), toggle_compress_callback, NULL); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compressbutton), dia->data->is_compressed); g_signal_handlers_unblock_by_func(G_OBJECT(compressbutton), toggle_compress_callback, NULL); if (g_object_get_data (G_OBJECT (savedlg), "user_data") != NULL) g_object_unref (g_object_get_data (G_OBJECT (savedlg), "user_data")); #if GTK_CHECK_VERSION(2,20,0) if (gtk_widget_get_visible(savedlg)) { #else if (GTK_WIDGET_VISIBLE(savedlg)) { #endif /* keep a refernce to the diagram */ g_object_ref(dia); g_object_set_data (G_OBJECT (savedlg), "user_data", dia); gtk_window_present (GTK_WINDOW(savedlg)); return savedlg; } } if (dia && dia->filename) filename = g_filename_from_utf8(dia->filename, -1, NULL, NULL, NULL); if (filename != NULL) { char* fnabs = dia_get_absolute_filename (filename); if (fnabs) { gchar *base = g_path_get_basename(dia->filename); gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(savedlg), fnabs); /* FileChooser api insist on exiting files for set_filename */ /* ... and does not use filename encoding on this one. */ gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(savedlg), base); g_free(base); } g_free(fnabs); g_free(filename); } g_object_ref(dia); g_object_set_data (G_OBJECT (savedlg), "user_data", dia); return savedlg; } /** * Respond to the File/Save menu entry. * * Delegates to Save As if there is no filename set yet. */ void file_save_callback(GtkAction *action) { Diagram *diagram; diagram = ddisplay_active_diagram(); if (!diagram) return; if (diagram->unsaved) { file_save_as_callback(action); } else { gchar *filename = g_filename_from_utf8(diagram->filename, -1, NULL, NULL, NULL); DiaContext *ctx = dia_context_new (_("Save")); diagram_update_extents(diagram); if (diagram_save(diagram, filename, ctx)) recent_file_history_add(filename); g_free (filename); dia_context_release (ctx); } }
/** * Respond to a button press (also destroy) in the save as dialog. */ static void file_save_as_response_callback(GtkWidget *fs, gint response, gpointer user_data) { char *filename; Diagram *dia; struct stat stat_struct; if (response == GTK_RESPONSE_ACCEPT) { dia = g_object_get_data (G_OBJECT(fs), "user_data"); filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fs)); if (!filename) { /* Not getting a filename looks like a contract violation in Gtk+ to me. * Still Dia would be crashing (bug #651949) - instead simply go back to the dialog. */ gtk_window_present (GTK_WINDOW (fs)); return; } if (g_stat(filename, &stat_struct) == 0) { GtkWidget *dialog = NULL; char *utf8filename = NULL; if (!g_utf8_validate(filename, -1, NULL)) { utf8filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL); if (utf8filename == NULL) { message_warning(_("Some characters in the filename are neither UTF-8\n" "nor your local encoding.\nSome things will break.")); } } if (utf8filename == NULL) utf8filename = g_strdup(filename); dialog = gtk_message_dialog_new (GTK_WINDOW(fs), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, _("File already exists")); gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), _("The file '%s' already exists.\n" "Do you want to overwrite it?"), utf8filename); g_free(utf8filename); gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_YES); if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_YES) { /* don't hide/destroy the dialog, but simply go back to it */ gtk_window_present (GTK_WINDOW (fs)); gtk_widget_destroy(dialog); g_free (filename); return; } gtk_widget_destroy(dialog); } dia->data->is_compressed = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(user_data)); diagram_update_extents(dia); { DiaContext *ctx = dia_context_new (_("Save as")); diagram_set_filename(dia, filename); dia_context_set_filename (ctx, filename); if (diagram_save(dia, filename, ctx)) recent_file_history_add(filename); dia_context_release (ctx); } g_free (filename); } /* if we have our own reference, drop it before destroy */ if ((dia = g_object_get_data (G_OBJECT(fs), "user_data")) != NULL) { g_object_set_data (G_OBJECT(fs), "user_data", NULL); g_object_unref (dia); } /* if we destroy it gtk_dialog_run wont give the response */ if (!g_object_get_data (G_OBJECT(fs), "dont-destroy")) gtk_widget_destroy(GTK_WIDGET(fs)); }