Generalize chart loading functions

* ag_app_import_chart() became ag_app_import_file(), and has the potential
  to load any file type
* ag_app_import_cb() so it can open multiple file types
* ag_chart_load_from_file() is now called ag_chart_load_from_agc()
This commit is contained in:
Gergely Polonkai 2014-08-10 10:15:04 +02:00
parent a3927458b4
commit a30d19d1bb
5 changed files with 47 additions and 14 deletions

View File

@ -133,14 +133,31 @@ quit_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
} }
static void static void
ag_app_import_chart(AgApp *app, GFile *file) ag_app_import_file(AgApp *app, GFile *file, AgAppImportType type)
{ {
GtkWidget *window; GtkWidget *window;
AgChart *chart; AgChart *chart;
GError *err = NULL; GError *err = NULL;
if ((chart = ag_chart_load_from_file(file, &err)) == NULL) { switch (type) {
g_print("Error: '%s'\n", err->message); case AG_APP_IMPORT_AGC:
chart = ag_chart_load_from_agc(file, &err);
break;
default:
g_error("Unknown import type!");
break;
}
if (chart == NULL) {
ag_app_message_dialog(
NULL,
GTK_MESSAGE_ERROR,
"Error while loading: %s",
err->message
);
return; return;
} }
@ -155,9 +172,19 @@ ag_app_import_chart(AgApp *app, GFile *file)
static void static void
ag_app_import_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data) ag_app_import_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
{ {
gint response; gint response;
GtkWidget *fs; GtkWidget *fs;
GSList *filenames = NULL; GtkFileFilter *filter;
GSList *filenames = NULL;
const gchar *target_type = g_variant_get_string(parameter, NULL);
AgAppImportType type = AG_APP_IMPORT_NONE;
if (strncmp("agc", target_type, 3) == 0) {
type = AG_APP_IMPORT_AGC;
filter = filter_chart;
} else {
g_error("Unknown import type!");
}
fs = gtk_file_chooser_dialog_new(_("Select charts"), fs = gtk_file_chooser_dialog_new(_("Select charts"),
NULL, NULL,
@ -166,8 +193,8 @@ ag_app_import_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
_("_Import"), GTK_RESPONSE_ACCEPT, _("_Import"), GTK_RESPONSE_ACCEPT,
NULL); NULL);
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(fs), filter_all); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(fs), filter_all);
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(fs), filter_chart); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(fs), filter);
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(fs), filter_chart); gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(fs), filter);
gtk_dialog_set_default_response(GTK_DIALOG(fs), GTK_RESPONSE_ACCEPT); gtk_dialog_set_default_response(GTK_DIALOG(fs), GTK_RESPONSE_ACCEPT);
gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(fs), TRUE); gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(fs), TRUE);
gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(fs), FALSE); gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(fs), FALSE);
@ -190,7 +217,7 @@ ag_app_import_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
} }
file = g_file_new_for_commandline_arg(data); file = g_file_new_for_commandline_arg(data);
ag_app_import_chart(AG_APP(user_data), file); ag_app_import_file(AG_APP(user_data), file, type);
} }
} }
@ -250,7 +277,7 @@ static GActionEntry app_entries[] = {
{ "about", about_cb, NULL, NULL, NULL }, { "about", about_cb, NULL, NULL, NULL },
{ "quit", quit_cb, NULL, NULL, NULL }, { "quit", quit_cb, NULL, NULL, NULL },
{ "raise", raise_cb, NULL, NULL, NULL }, { "raise", raise_cb, NULL, NULL, NULL },
{ "import", ag_app_import_cb, NULL, NULL, NULL }, { "import", ag_app_import_cb, "s", NULL, NULL },
{ "help", help_cb, NULL, NULL, NULL }, { "help", help_cb, NULL, NULL, NULL },
}; };
@ -335,7 +362,7 @@ ag_app_import(GApplication *gapp,
gint i; gint i;
for (i = 0; i < n_files; i++) { for (i = 0; i < n_files; i++) {
ag_app_import_chart(AG_APP(gapp), files[i]); ag_app_import_file(AG_APP(gapp), files[i], AG_APP_IMPORT_AGC);
} }
} }

View File

@ -7,6 +7,11 @@
G_BEGIN_DECLS G_BEGIN_DECLS
typedef enum {
AG_APP_IMPORT_NONE,
AG_APP_IMPORT_AGC,
} AgAppImportType;
#define AG_TYPE_APP (ag_app_get_type()) #define AG_TYPE_APP (ag_app_get_type())
#define AG_APP(o) (G_TYPE_CHECK_INSTANCE_CAST((o), \ #define AG_APP(o) (G_TYPE_CHECK_INSTANCE_CAST((o), \
AG_TYPE_APP, \ AG_TYPE_APP, \

View File

@ -577,7 +577,7 @@ get_by_xpath(xmlXPathContextPtr xpath_context,
} }
AgChart * AgChart *
ag_chart_load_from_file(GFile *file, GError **err) ag_chart_load_from_agc(GFile *file, GError **err)
{ {
AgChart *chart = NULL; AgChart *chart = NULL;
gchar *uri, gchar *uri,

View File

@ -48,8 +48,8 @@ AgChart *ag_chart_new_full(GsweTimestamp *timestamp,
gdouble altitude, gdouble altitude,
GsweHouseSystem house_system); GsweHouseSystem house_system);
AgChart *ag_chart_load_from_file(GFile *file, AgChart *ag_chart_load_from_agc(GFile *file,
GError **err); GError **err);
AgChart *ag_chart_new_from_db_save(AgDbSave *save_data, GError **err); AgChart *ag_chart_new_from_db_save(AgDbSave *save_data, GError **err);

View File

@ -11,6 +11,7 @@
<item> <item>
<attribute name="label" translatable="yes">Import</attribute> <attribute name="label" translatable="yes">Import</attribute>
<attribute name="action">app.import</attribute> <attribute name="action">app.import</attribute>
<attribute name="target">agc</attribute>
<attribute name="accel">&lt;Primary&gt;o</attribute> <attribute name="accel">&lt;Primary&gt;o</attribute>
</item> </item>
</section> </section>