From: John Darrington Date: Thu, 5 Mar 2009 05:35:25 +0000 (+0900) Subject: Refactor Data and Syntax Loading X-Git-Tag: v0.7.3~255 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d112adc6bdbab5593d533fdc8853e80a2cb358e7;p=pspp-builds.git Refactor Data and Syntax Loading --- diff --git a/src/ui/gui/psppire-data-window.c b/src/ui/gui/psppire-data-window.c index 5849df4a..16db8131 100644 --- a/src/ui/gui/psppire-data-window.c +++ b/src/ui/gui/psppire-data-window.c @@ -161,6 +161,7 @@ extern PsppireVarStore *the_var_store; extern struct dataset *the_dataset; extern PsppireDataStore *the_data_store ; +extern GtkRecentManager *the_recent_mgr; static void set_paste_menuitem_sensitivity (PsppireDataWindow *de, gboolean x) @@ -343,9 +344,8 @@ dump_rm (GtkRecentManager *rm) #endif -void -psppire_data_window_load_file (PsppireDataWindow *de, - const gchar *file_name) +static gboolean +load_file (PsppireWindow *de, const gchar *file_name) { struct getl_interface *sss; struct string filename; @@ -355,26 +355,23 @@ psppire_data_window_load_file (PsppireDataWindow *de, sss = create_syntax_string_source ("GET FILE=%s.", ds_cstr (&filename)); + ds_destroy (&filename); if (execute_syntax (sss) ) { - psppire_window_set_filename (PSPPIRE_WINDOW (de), file_name); - add_most_recent (file_name, the_recent_mgr); - } - else - { - delete_recent (file_name, the_recent_mgr); + psppire_window_set_filename (de, file_name); + return TRUE; } - psppire_window_set_unsaved (PSPPIRE_WINDOW (de), FALSE); + return FALSE; } /* Callback for the data_open action. Prompts for a filename and opens it */ static void -open_data_dialog (GtkAction *action, PsppireDataWindow *de) +open_data_dialog (GtkAction *action, PsppireWindow *de) { GtkWidget *dialog = gtk_file_chooser_dialog_new (_("Open"), @@ -420,7 +417,7 @@ open_data_dialog (GtkAction *action, PsppireDataWindow *de) gchar *name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); - psppire_data_window_load_file (de, name); + psppire_window_load (de, name); g_free (name); } @@ -898,10 +895,9 @@ create_var_sheet_variable_popup_menu (PsppireDataWindow *de) static void on_recent_data_select (GtkMenuShell *menushell, - gpointer user_data) + PsppireWindow *window) { gchar *file; - PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (user_data); gchar *uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (menushell)); @@ -910,7 +906,7 @@ on_recent_data_select (GtkMenuShell *menushell, g_free (uri); - psppire_data_window_load_file (de, file); + psppire_window_load (window, file); g_free (file); } @@ -931,8 +927,10 @@ on_recent_files_select (GtkMenuShell *menushell, gpointer user_data) se = psppire_syntax_window_new (); - psppire_syntax_window_load_from_file (PSPPIRE_SYNTAX_WINDOW (se), file, NULL); - gtk_widget_show (se); + if ( psppire_window_load (PSPPIRE_WINDOW (se), file) ) + gtk_widget_show (se); + else + gtk_widget_destroy (se); g_free (file); } @@ -1796,4 +1794,5 @@ static void psppire_data_window_iface_init (PsppireWindowIface *iface) { iface->save = data_save; + iface->load = load_file; } diff --git a/src/ui/gui/psppire-data-window.h b/src/ui/gui/psppire-data-window.h index bd9f516e..1b6deab1 100644 --- a/src/ui/gui/psppire-data-window.h +++ b/src/ui/gui/psppire-data-window.h @@ -74,8 +74,6 @@ struct _PsppireDataWindowClass GType psppire_data_window_get_type (void); GtkWidget* psppire_data_window_new (void); -void psppire_data_window_load_file (PsppireDataWindow *, const gchar *); - G_END_DECLS diff --git a/src/ui/gui/psppire-syntax-window.c b/src/ui/gui/psppire-syntax-window.c index e5998cc6..86d87bb9 100644 --- a/src/ui/gui/psppire-syntax-window.c +++ b/src/ui/gui/psppire-syntax-window.c @@ -395,16 +395,10 @@ open_syntax_window (GtkMenuItem *menuitem, gpointer parent) GtkWidget *se = psppire_syntax_window_new (); - if ( psppire_syntax_window_load_from_file (PSPPIRE_SYNTAX_WINDOW (se), file_name, NULL) ) - { - add_most_recent (file_name, the_recent_mgr); - } + if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) ) + gtk_widget_show (se); else - { - delete_recent (file_name, the_recent_mgr); - } - - gtk_widget_show (se); + gtk_widget_destroy (se); } gtk_widget_destroy (dialog); @@ -562,34 +556,34 @@ psppire_syntax_window_new (void) /* Loads the buffer from the file called FILENAME */ -gboolean -psppire_syntax_window_load_from_file (PsppireSyntaxWindow *se, - const gchar *filename, - GError **err) +static gboolean +syntax_load (PsppireWindow *window, const gchar *filename) { gchar *text; GtkTextIter iter; + PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window); - gchar *glibfilename = g_filename_from_utf8 (filename, -1, 0, 0, err); + gchar *glibfilename = g_filename_from_utf8 (filename, -1, 0, 0, NULL); if ( ! glibfilename ) return FALSE; /* FIXME: What if it's a very big file ? */ - if ( ! g_file_get_contents (glibfilename, &text, NULL, err) ) + if ( ! g_file_get_contents (glibfilename, &text, NULL, NULL) ) { g_free (glibfilename); return FALSE; } + g_free (glibfilename); - gtk_text_buffer_get_iter_at_line (se->buffer, &iter, 0); + gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0); - gtk_text_buffer_insert (se->buffer, &iter, text, -1); + gtk_text_buffer_insert (sw->buffer, &iter, text, -1); - psppire_window_set_filename (PSPPIRE_WINDOW (se), filename); + psppire_window_set_filename (window, filename); - gtk_text_buffer_set_modified (se->buffer, FALSE); + gtk_text_buffer_set_modified (sw->buffer, FALSE); return TRUE; } @@ -600,4 +594,5 @@ static void psppire_syntax_window_iface_init (PsppireWindowIface *iface) { iface->save = syntax_save; + iface->load = syntax_load; } diff --git a/src/ui/gui/psppire-syntax-window.h b/src/ui/gui/psppire-syntax-window.h index 2144b86f..c863a92f 100644 --- a/src/ui/gui/psppire-syntax-window.h +++ b/src/ui/gui/psppire-syntax-window.h @@ -62,10 +62,6 @@ struct _PsppireSyntaxWindowClass GType psppire_syntax_window_get_type (void); GtkWidget* psppire_syntax_window_new (void); -gboolean psppire_syntax_window_load_from_file (PsppireSyntaxWindow *se, - const gchar *filename, - GError **err); - void open_syntax_window (GtkMenuItem *menuitem, gpointer parent); void create_syntax_window (void); diff --git a/src/ui/gui/psppire-window.c b/src/ui/gui/psppire-window.c index 6bd6603b..f0d6d916 100644 --- a/src/ui/gui/psppire-window.c +++ b/src/ui/gui/psppire-window.c @@ -16,6 +16,7 @@ #include + #include #include #include @@ -577,11 +578,40 @@ psppire_window_save (PsppireWindow *w) i->save (w); } +extern GtkRecentManager *the_recent_mgr; + +static void add_most_recent (const char *file_name, GtkRecentManager *rm); +static void delete_recent (const char *file_name, GtkRecentManager *rm); + +gboolean +psppire_window_load (PsppireWindow *w, const gchar *file) +{ + gboolean ok; + PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w); + + g_assert (PSPPIRE_IS_WINDOW_MODEL (w)); + + g_assert (i); + + g_return_val_if_fail (i->load, FALSE); + + ok = i->load (w, file); + + if ( ok ) + add_most_recent (file, the_recent_mgr); + else + delete_recent (file, the_recent_mgr); + + psppire_window_set_unsaved (w, FALSE); + + return ok; +} + /* Puts FILE_NAME into the recent list. If it's already in the list, it moves it to the top */ -void +static void add_most_recent (const char *file_name, GtkRecentManager *rm) { gchar *uri = g_filename_to_uri (file_name, NULL, NULL); @@ -597,7 +627,7 @@ add_most_recent (const char *file_name, GtkRecentManager *rm) /* If FILE_NAME exists in the recent list, then delete it. */ -void +static void delete_recent (const char *file_name, GtkRecentManager *rm) { gchar *uri = g_filename_to_uri (file_name, NULL, NULL); diff --git a/src/ui/gui/psppire-window.h b/src/ui/gui/psppire-window.h index 67440ccd..71c06538 100644 --- a/src/ui/gui/psppire-window.h +++ b/src/ui/gui/psppire-window.h @@ -87,6 +87,7 @@ struct _PsppireWindowIface GTypeInterface g_iface; void (*save) (PsppireWindow *w); + gboolean (*load) (PsppireWindow *w, const gchar *); }; @@ -106,9 +107,7 @@ gboolean psppire_window_get_unsaved (PsppireWindow *); gint psppire_window_query_save (PsppireWindow *); void psppire_window_save (PsppireWindow *w); - -void add_most_recent (const char *file_name, GtkRecentManager *rm); -void delete_recent (const char *file_name, GtkRecentManager *rm); +gboolean psppire_window_load (PsppireWindow *w, const gchar *file); G_END_DECLS diff --git a/src/ui/gui/psppire.c b/src/ui/gui/psppire.c index dbba57b6..35dad91e 100644 --- a/src/ui/gui/psppire.c +++ b/src/ui/gui/psppire.c @@ -279,8 +279,7 @@ parse_non_options (int key, char *arg, struct argp_state *state) { case ARGP_KEY_ARG: { - psppire_data_window_load_file (PSPPIRE_DATA_WINDOW (the_data_window), - arg); + psppire_window_load (PSPPIRE_WINDOW (the_data_window), arg); break; } default: diff --git a/src/ui/gui/psppire.h b/src/ui/gui/psppire.h index 0d990119..408d858b 100644 --- a/src/ui/gui/psppire.h +++ b/src/ui/gui/psppire.h @@ -22,8 +22,6 @@ struct command_line_processor ; extern const struct argp non_option_argp ; -extern GtkRecentManager *the_recent_mgr; - void initialize (struct command_line_processor *, int argc, char **argv); void de_initialize (void); void psppire_quit (void);