From 310caa929294b3b079923b8df919ac6cf035d015 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 31 Jul 2014 00:37:17 +0200 Subject: [PATCH] Add function ag_chart_new_from_db_save() It constructs an AgChart from an AgDbSave struct --- src/ag-chart.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/ag-chart.h | 6 +++++ 2 files changed, 70 insertions(+) diff --git a/src/ag-chart.c b/src/ag-chart.c index 43675fa..f2619d6 100644 --- a/src/ag-chart.c +++ b/src/ag-chart.c @@ -920,6 +920,70 @@ ag_chart_load_from_file(GFile *file, GError **err) return chart; } +AgChart * +ag_chart_new_from_db_save(AgDbSave *save_data, GError **err) +{ + GsweTimestamp *timestamp; + gchar *house_system_enum_name; + GTypeClass *house_system_class; + GEnumValue *enum_value; + GsweHouseSystem house_system; + AgChart *chart; + + if (save_data == NULL) { + g_set_error( + err, + AG_CHART_ERROR, AG_CHART_ERROR_EMPTY_RECORD, + "Invalid chart" + ); + + return NULL; + } + + house_system_enum_name = g_utf8_strdown(save_data->house_system, -1); + house_system_class = g_type_class_ref(GSWE_TYPE_HOUSE_SYSTEM); + + if ((enum_value = g_enum_get_value_by_nick( + G_ENUM_CLASS(house_system_class), + house_system_enum_name + )) == NULL) { + g_free(house_system_enum_name); + g_set_error( + err, + AG_CHART_ERROR, AG_CHART_ERROR_INVALID_HOUSE_SYSTEM, + "Invalid house system: '%s'", + save_data->house_system + ); + + return NULL; + } + + g_free(house_system_enum_name); + + house_system = enum_value->value; + + timestamp = gswe_timestamp_new_from_gregorian_full( + save_data->year, save_data->month, save_data->day, + save_data->hour, save_data->minute, save_data->second, 0, + save_data->timezone + ); + + chart = ag_chart_new_full( + timestamp, + save_data->longitude, + save_data->latitude, + save_data->altitude, + house_system + ); + + ag_chart_set_name(chart, save_data->name); + ag_chart_set_country(chart, save_data->country); + ag_chart_set_city(chart, save_data->city); + ag_chart_set_note(chart, save_data->note); + + return chart; +} + static xmlDocPtr create_save_doc(AgChart *chart) { diff --git a/src/ag-chart.h b/src/ag-chart.h index e9f8177..ab6ac3e 100644 --- a/src/ag-chart.h +++ b/src/ag-chart.h @@ -4,11 +4,15 @@ #include #include +#include "ag-db.h" + G_BEGIN_DECLS typedef enum { AG_CHART_ERROR_LIBXML, AG_CHART_ERROR_CORRUPT_FILE, + AG_CHART_ERROR_EMPTY_RECORD, + AG_CHART_ERROR_INVALID_HOUSE_SYSTEM, } AgChartError; #define AG_TYPE_CHART (ag_chart_get_type()) @@ -46,6 +50,8 @@ AgChart *ag_chart_new_full(GsweTimestamp *timestamp, AgChart *ag_chart_load_from_file(GFile *file, GError **err); +AgChart *ag_chart_new_from_db_save(AgDbSave *save_data, GError **err); + void ag_chart_save_to_file(AgChart *chart, GFile *file, GError **err);