Merge branch 'loadsave'

Conflicts:
	data/examples/saved-chart.agc
This commit is contained in:
Gergely Polonkai 2013-09-18 11:27:15 +02:00
commit bfaae8fede
7 changed files with 570 additions and 202 deletions

View File

@ -1,22 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<chartinfo>
<data>
<name>Gergely Polonkai</name>
<place>
<country>Hungary</country>
<city>Budapest</city>
<longitude>19.03991</longitude>
<latitude>47.49801</latitude>
<altitude>280.0</altitude>
</place>
<time>
<year>1983</year>
<month>3</month>
<day>7</day>
<hour>11</hour>
<minute>54</minute>
<second>45</second>
<timezone>1.0</timezone>
</time>
</data>
<data>
<name>Gergely Polonkai</name>
<place>
<country>Hungary</country>
<city>Budapest</city>
<longitude>47.49801000</longitude>
<latitude>19.03990999</latitude>
<altitude>280</altitude>
</place>
<time>
<year>1983</year>
<month>3</month>
<day>7</day>
<hour>11</hour>
<minute>54</minute>
<second>45</second>
<timezone>1</timezone>
</time>
</data>
</chartinfo>

View File

@ -1,20 +1,10 @@
#include <glib/gi18n.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <errno.h>
#include "ag-app.h"
#include "ag-window.h"
#include "ag-chart.h"
#include "config.h"
#include "astrognome.h"
typedef enum {
XML_CONVERT_STRING,
XML_CONVERT_DOUBLE,
XML_CONVERT_INT
} XmlConvertType;
struct _AgAppPrivate {
};
@ -114,182 +104,21 @@ quit_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
}
}
GVariant *
get_by_xpath(xmlXPathContextPtr ctx, const gchar *xpath, XmlConvertType type)
{
xmlXPathObjectPtr xpathObj;
const gchar *text;
char *endptr;
GVariant *ret = NULL;
gdouble d;
gint i;
if ((xpathObj = xmlXPathEvalExpression((const xmlChar *)xpath, ctx)) == NULL) {
// TODO: Warn with a popup or similar way
g_warning("Could not initialize XPath");
return NULL;
}
if (xpathObj->nodesetval == NULL) {
// TODO: Warn with a popup or similar way
g_warning("Required element not found. This is not a valid save file!");
xmlXPathFreeObject(xpathObj);
return NULL;
}
if (xpathObj->nodesetval->nodeNr > 1) {
// TODO: Warn with a popup or similar way
g_warning("Too many elements. This is not a valid save file!");
xmlXPathFreeObject(xpathObj);
return NULL;
}
text = (const gchar *)xpathObj->nodesetval->nodeTab[0]->content;
switch (type) {
case XML_CONVERT_STRING:
ret = g_variant_new_string(text);
break;
case XML_CONVERT_DOUBLE:
d = g_ascii_strtod(text, &endptr);
if ((*endptr != 0) || (errno != 0)) {
ret = NULL;
} else {
ret = g_variant_new_double(d);
}
break;
case XML_CONVERT_INT:
i = strtol(text, &endptr, 10);
if ((*endptr != 0) || (errno != 0)) {
ret = NULL;
} else {
ret = g_variant_new_int32(i);
}
break;
}
xmlXPathFreeObject(xpathObj);
return ret;
}
static void
ag_app_open_chart(AgApp *app, GFile *file)
{
GError *err = NULL;
gchar *uri,
*xml;
guint length;
xmlDocPtr doc;
xmlXPathContextPtr xpathCtx;
GVariant *chart_name,
*country,
*city,
*longitude,
*latitude,
*altitude,
*year,
*month,
*day,
*hour,
*minute,
*second,
*timezone;
GtkWidget *window;
AgChart *chart;
GsweTimestamp *timestamp;
uri = g_file_get_uri(file);
if (!g_file_load_contents(file, NULL, &xml, &length, NULL, &err)) {
// TODO: Warn with a popup or similar way
g_warning("Could not open file '%s': %s", uri, err->message);
g_clear_error(&err);
g_free(uri);
return;
}
if ((doc = xmlReadMemory(xml, length, "chart.xml", NULL, 0)) == NULL) {
// TODO: Warn with a popup or similar way
g_warning("Saved chart is corrupt (or not a saved chart at all)");
g_free(xml);
g_free(uri);
return;
}
if ((xpathCtx = xmlXPathNewContext(doc)) == NULL) {
// TODO: Warn with a popup or similar way
g_warning("Could not initialize XPath");
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return;
}
chart_name = get_by_xpath(xpathCtx, "/chartinfo/data/name/text()", XML_CONVERT_STRING);
country = get_by_xpath(xpathCtx, "/chartinfo/data/place/country/text()", XML_CONVERT_STRING);
city = get_by_xpath(xpathCtx, "/chartinfo/data/place/city/text()", XML_CONVERT_STRING);
longitude = get_by_xpath(xpathCtx, "/chartinfo/data/place/longitude/text()", XML_CONVERT_DOUBLE);
latitude = get_by_xpath(xpathCtx, "/chartinfo/data/place/latitude/text()", XML_CONVERT_DOUBLE);
altitude = get_by_xpath(xpathCtx, "/chartinfo/data/place/altitude/text()", XML_CONVERT_DOUBLE);
year = get_by_xpath(xpathCtx, "/chartinfo/data/time/year/text()", XML_CONVERT_INT);
month = get_by_xpath(xpathCtx, "/chartinfo/data/time/month/text()", XML_CONVERT_INT);
day = get_by_xpath(xpathCtx, "/chartinfo/data/time/day/text()", XML_CONVERT_INT);
hour = get_by_xpath(xpathCtx, "/chartinfo/data/time/hour/text()", XML_CONVERT_INT);
minute = get_by_xpath(xpathCtx, "/chartinfo/data/time/minute/text()", XML_CONVERT_INT);
second = get_by_xpath(xpathCtx, "/chartinfo/data/time/second/text()", XML_CONVERT_INT);
timezone = get_by_xpath(xpathCtx, "/chartinfo/data/time/timezone/text()", XML_CONVERT_DOUBLE);
GError *err = NULL;
gchar *uri;
chart = ag_chart_load_from_file(file, &err);
window = ag_app_create_window(app);
timestamp = gswe_timestamp_new_from_gregorian_full(
g_variant_get_int32(year),
g_variant_get_int32(month),
g_variant_get_int32(day),
g_variant_get_int32(hour),
g_variant_get_int32(minute),
g_variant_get_int32(second),
0,
g_variant_get_double(timezone)
);
// TODO: Make house system configurable (and saveable)
chart = ag_chart_new_full(timestamp, g_variant_get_double(longitude), g_variant_get_double(latitude), g_variant_get_double(altitude), GSWE_HOUSE_SYSTEM_PLACIDUS);
ag_chart_set_name(chart, g_variant_get_string(chart_name, NULL));
ag_chart_set_country(chart, g_variant_get_string(country, NULL));
ag_chart_set_city(chart, g_variant_get_string(city, NULL));
ag_window_set_chart(AG_WINDOW(window), chart);
ag_window_update_from_chart(AG_WINDOW(window));
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
g_variant_unref(longitude);
g_variant_unref(latitude);
g_variant_unref(altitude);
g_variant_unref(year);
g_variant_unref(month);
g_variant_unref(day);
g_variant_unref(hour);
g_variant_unref(minute);
g_variant_unref(second);
g_free(xml);
uri = g_file_get_uri(file);
ag_window_set_uri(AG_WINDOW(window), uri);
g_free(uri);
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
}
static void
@ -365,9 +194,10 @@ setup_actions(AgApp *app)
static void
setup_accelerators(AgApp *app)
{
gtk_application_add_accelerator(GTK_APPLICATION(app), "<Primary>w", "win.close", NULL);
gtk_application_add_accelerator(GTK_APPLICATION(app), "<Primary>s", "win.save", NULL);
gtk_application_add_accelerator(GTK_APPLICATION(app), "F10", "win.gear-menu", NULL);
gtk_application_add_accelerator(GTK_APPLICATION(app), "<Primary>w", "win.close", NULL);
gtk_application_add_accelerator(GTK_APPLICATION(app), "<Primary>s", "win.save", NULL);
gtk_application_add_accelerator(GTK_APPLICATION(app), "<Primary><Shift>s", "win.save-as", NULL);
gtk_application_add_accelerator(GTK_APPLICATION(app), "F10", "win.gear-menu", NULL);
}
static void

View File

@ -1,3 +1,7 @@
#include <errno.h>
#include <gio/gio.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <swe-glib.h>
#include "ag-chart.h"
@ -16,6 +20,14 @@ enum {
PROP_CITY
};
typedef enum {
XML_CONVERT_STRING,
XML_CONVERT_DOUBLE,
XML_CONVERT_INT
} XmlConvertType;
G_DEFINE_QUARK(ag-chart-error-quark, ag_chart_error);
G_DEFINE_TYPE(AgChart, ag_chart, GSWE_TYPE_MOMENT);
#define GET_PRIVATE(instance) (G_TYPE_INSTANCE_GET_PRIVATE((instance), AG_TYPE_CHART, AgChartPrivate))
@ -186,3 +198,457 @@ ag_chart_get_city(AgChart *chart)
return g_strdup(chart->priv->city);
}
static GVariant *
get_by_xpath(xmlXPathContextPtr xpath_context, const gchar *uri, const gchar *xpath, XmlConvertType type, GError **err)
{
xmlXPathObjectPtr xpathObj;
const gchar *text;
char *endptr;
GVariant *ret = NULL;
gdouble d;
gint i;
if ((xpathObj = xmlXPathEvalExpression((const xmlChar *)xpath, xpath_context)) == NULL) {
g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_LIBXML, "File '%s' could not be parsed due to internal XML error.", uri);
return NULL;
}
if (xpathObj->nodesetval == NULL) {
g_debug("No such node '%s'", xpath);
g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_CORRUPT_FILE, "File '%s' doesn't look like a valid saved chart.", uri);
xmlXPathFreeObject(xpathObj);
return NULL;
}
if (xpathObj->nodesetval->nodeNr > 1) {
g_debug("Too many '%s' nodes", xpath);
g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_CORRUPT_FILE, "File '%s' doesn't look like a valid saved chart.", uri);
xmlXPathFreeObject(xpathObj);
return NULL;
}
text = (const gchar *)xpathObj->nodesetval->nodeTab[0]->content;
switch (type) {
case XML_CONVERT_STRING:
ret = g_variant_new_string(text);
break;
case XML_CONVERT_DOUBLE:
d = g_ascii_strtod(text, &endptr);
if ((*endptr != 0) || (errno != 0)) {
g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_CORRUPT_FILE, "File '%s' doesn't look like a valid saved chart.", uri);
ret = NULL;
} else {
ret = g_variant_new_double(d);
}
break;
case XML_CONVERT_INT:
i = strtol(text, &endptr, 10);
if ((*endptr != 0) || (errno != 0)) {
g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_CORRUPT_FILE, "File '%s' doesn't look like a valid saved chart.", uri);
ret = NULL;
} else {
ret = g_variant_new_int32(i);
}
break;
}
xmlXPathFreeObject(xpathObj);
return ret;
}
AgChart *
ag_chart_load_from_file(GFile *file, GError **err)
{
AgChart *chart = NULL;
gchar *uri,
*xml = NULL;
guint length;
xmlDocPtr doc;
xmlXPathContextPtr xpath_context;
GVariant *chart_name,
*country,
*city,
*longitude,
*latitude,
*altitude,
*year,
*month,
*day,
*hour,
*minute,
*second,
*timezone;
GsweTimestamp *timestamp;
uri = g_file_get_uri(file);
if (!g_file_load_contents(file, NULL, &xml, &length, NULL, err)) {
g_free(uri);
return NULL;
}
if ((doc = xmlReadMemory(xml, length, "chart.xml", NULL, 0)) == NULL) {
g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_CORRUPT_FILE, "File '%s' can not be read. Maybe it is corrupt, or not a save file at all", uri);
g_free(xml);
g_free(uri);
return NULL;
}
if ((xpath_context = xmlXPathNewContext(doc)) == NULL) {
g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_LIBXML, "File '%s' could not be loaded due to internal LibXML error", uri);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((chart_name = get_by_xpath(xpath_context, uri, "/chartinfo/data/name/text()", XML_CONVERT_STRING, err)) == NULL) {
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((country = get_by_xpath(xpath_context, uri, "/chartinfo/data/place/country/text()", XML_CONVERT_STRING, err)) == NULL) {
g_variant_unref(chart_name);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((city = get_by_xpath(xpath_context, uri, "/chartinfo/data/place/city/text()", XML_CONVERT_STRING, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
xmlFreeDoc(doc);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((longitude = get_by_xpath(xpath_context, uri, "/chartinfo/data/place/longitude/text()", XML_CONVERT_DOUBLE, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((latitude = get_by_xpath(xpath_context, uri, "/chartinfo/data/place/latitude/text()", XML_CONVERT_DOUBLE, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
g_variant_unref(longitude);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((altitude = get_by_xpath(xpath_context, uri, "/chartinfo/data/place/altitude/text()", XML_CONVERT_DOUBLE, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
g_variant_unref(longitude);
g_variant_unref(latitude);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((year = get_by_xpath(xpath_context, uri, "/chartinfo/data/time/year/text()", XML_CONVERT_INT, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
g_variant_unref(longitude);
g_variant_unref(latitude);
g_variant_unref(altitude);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((month = get_by_xpath(xpath_context, uri, "/chartinfo/data/time/month/text()", XML_CONVERT_INT, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
g_variant_unref(longitude);
g_variant_unref(latitude);
g_variant_unref(altitude);
g_variant_unref(year);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((day = get_by_xpath(xpath_context, uri, "/chartinfo/data/time/day/text()", XML_CONVERT_INT, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
g_variant_unref(longitude);
g_variant_unref(latitude);
g_variant_unref(altitude);
g_variant_unref(year);
g_variant_unref(month);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((hour = get_by_xpath(xpath_context, uri, "/chartinfo/data/time/hour/text()", XML_CONVERT_INT, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
g_variant_unref(longitude);
g_variant_unref(latitude);
g_variant_unref(altitude);
g_variant_unref(year);
g_variant_unref(month);
g_variant_unref(day);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((minute = get_by_xpath(xpath_context, uri, "/chartinfo/data/time/minute/text()", XML_CONVERT_INT, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
g_variant_unref(longitude);
g_variant_unref(latitude);
g_variant_unref(altitude);
g_variant_unref(year);
g_variant_unref(month);
g_variant_unref(day);
g_variant_unref(hour);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((second = get_by_xpath(xpath_context, uri, "/chartinfo/data/time/second/text()", XML_CONVERT_INT, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
g_variant_unref(longitude);
g_variant_unref(latitude);
g_variant_unref(altitude);
g_variant_unref(year);
g_variant_unref(month);
g_variant_unref(day);
g_variant_unref(hour);
g_variant_unref(minute);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
if ((timezone = get_by_xpath(xpath_context, uri, "/chartinfo/data/time/timezone/text()", XML_CONVERT_DOUBLE, err)) == NULL) {
g_variant_unref(chart_name);
g_variant_unref(country);
g_variant_unref(city);
g_variant_unref(longitude);
g_variant_unref(latitude);
g_variant_unref(altitude);
g_variant_unref(year);
g_variant_unref(month);
g_variant_unref(day);
g_variant_unref(hour);
g_variant_unref(minute);
g_variant_unref(second);
xmlFreeDoc(doc);
g_free(xml);
g_free(uri);
return NULL;
}
timestamp = gswe_timestamp_new_from_gregorian_full(
g_variant_get_int32(year),
g_variant_get_int32(month),
g_variant_get_int32(day),
g_variant_get_int32(hour),
g_variant_get_int32(minute),
g_variant_get_int32(second),
0,
g_variant_get_double(timezone)
);
g_variant_unref(year);
g_variant_unref(month);
g_variant_unref(day);
g_variant_unref(hour);
g_variant_unref(minute);
g_variant_unref(second);
g_variant_unref(timezone);
// TODO: Make house system configurable (and saveable)
chart = ag_chart_new_full(timestamp, g_variant_get_double(longitude), g_variant_get_double(latitude), g_variant_get_double(altitude), GSWE_HOUSE_SYSTEM_PLACIDUS);
g_variant_unref(longitude);
g_variant_unref(latitude);
g_variant_unref(altitude);
ag_chart_set_name(chart, g_variant_get_string(chart_name, NULL));
g_variant_unref(chart_name);
ag_chart_set_country(chart, g_variant_get_string(country, NULL));
g_variant_unref(country);
ag_chart_set_city(chart, g_variant_get_string(city, NULL));
g_variant_unref(city);
g_free(xml);
g_free(uri);
xmlXPathFreeContext(xpath_context);
xmlFreeDoc(doc);
return chart;
}
static xmlDocPtr
create_save_doc(AgChart *chart)
{
xmlDocPtr doc = NULL;
xmlNodePtr root_node = NULL,
data_node = NULL,
place_node = NULL,
time_node = NULL;
gchar *value;
GsweCoordinates *coordinates;
GsweTimestamp *timestamp;
doc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "chartinfo");
xmlDocSetRootElement(doc, root_node);
// Begin <data> node
data_node = xmlNewChild(root_node, NULL, BAD_CAST "data", NULL);
value = ag_chart_get_name(chart);
xmlNewChild(data_node, NULL, BAD_CAST "name", BAD_CAST value);
g_free(value);
// Begin <place> node
place_node = xmlNewChild(data_node, NULL, BAD_CAST "place", NULL);
value = ag_chart_get_country(chart);
xmlNewChild(place_node, NULL, BAD_CAST "country", BAD_CAST value);
g_free(value);
value = ag_chart_get_city(chart);
xmlNewChild(place_node, NULL, BAD_CAST "city", BAD_CAST value);
g_free(value);
coordinates = gswe_moment_get_coordinates(GSWE_MOMENT(chart));
value = g_malloc0(12);
g_ascii_dtostr(value, 12, coordinates->longitude);
xmlNewChild(place_node, NULL, BAD_CAST "longitude", BAD_CAST value);
g_free(value);
value = g_malloc0(12);
g_ascii_dtostr(value, 12, coordinates->latitude);
xmlNewChild(place_node, NULL, BAD_CAST "latitude", BAD_CAST value);
g_free(value);
value = g_malloc0(12);
g_ascii_dtostr(value, 12, coordinates->altitude);
xmlNewChild(place_node, NULL, BAD_CAST "altitude", BAD_CAST value);
g_free(value);
g_free(coordinates);
// Begin <time> node
time_node = xmlNewChild(data_node, NULL, BAD_CAST "time", NULL);
timestamp = gswe_moment_get_timestamp(GSWE_MOMENT(chart));
value = g_malloc0(10);
g_ascii_dtostr(value, 10, gswe_timestamp_get_gregorian_year(timestamp, NULL));
xmlNewChild(time_node, NULL, BAD_CAST "year", BAD_CAST value);
g_free(value);
value = g_malloc0(3);
g_ascii_dtostr(value, 3, gswe_timestamp_get_gregorian_month(timestamp, NULL));
xmlNewChild(time_node, NULL, BAD_CAST "month", BAD_CAST value);
g_free(value);
value = g_malloc0(3);
g_ascii_dtostr(value, 3, gswe_timestamp_get_gregorian_day(timestamp, NULL));
xmlNewChild(time_node, NULL, BAD_CAST "day", BAD_CAST value);
g_free(value);
value = g_malloc0(3);
g_ascii_dtostr(value, 3, gswe_timestamp_get_gregorian_hour(timestamp, NULL));
xmlNewChild(time_node, NULL, BAD_CAST "hour", BAD_CAST value);
g_free(value);
value = g_malloc0(3);
g_ascii_dtostr(value, 3, gswe_timestamp_get_gregorian_minute(timestamp, NULL));
xmlNewChild(time_node, NULL, BAD_CAST "minute", BAD_CAST value);
g_free(value);
value = g_malloc0(3);
g_ascii_dtostr(value, 3, gswe_timestamp_get_gregorian_second(timestamp, NULL));
xmlNewChild(time_node, NULL, BAD_CAST "second", BAD_CAST value);
g_free(value);
value = g_malloc0(7);
g_ascii_dtostr(value, 7, gswe_timestamp_get_gregorian_timezone(timestamp));
xmlNewChild(time_node, NULL, BAD_CAST "timezone", BAD_CAST value);
g_free(value);
return doc;
}
void
ag_chart_save_to_file(AgChart *chart, GFile *file, GError **err)
{
xmlChar *content = NULL;
int length;
xmlDocPtr save_doc = create_save_doc(chart);
xmlDocDumpFormatMemoryEnc(save_doc, &content, &length, "UTF-8", 1);
g_file_replace_contents(file, (const gchar *)content, length, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, err);
xmlFreeDoc(save_doc);
}

View File

@ -6,6 +6,11 @@
G_BEGIN_DECLS
typedef enum {
AG_CHART_ERROR_LIBXML,
AG_CHART_ERROR_CORRUPT_FILE,
} AgChartError;
#define AG_TYPE_CHART (ag_chart_get_type())
#define AG_CHART(o) (G_TYPE_CHECK_INSTANCE_CAST((o), AG_TYPE_CHART, AgChart))
#define AG_CHART_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), AG_TYPE_CHART, AgChartClass))
@ -28,9 +33,8 @@ struct _AgChartClass {
GType ag_chart_get_type(void) G_GNUC_CONST;
AgChart *ag_chart_new_full(GsweTimestamp *timestamp, gdouble longitude, gdouble latitude, gdouble altitude, GsweHouseSystem house_system);
void ag_chart_load_from_file(const gchar *path, GError **err);
void ag_chart_save_to_file(const gchar *path, GError **err);
AgChart *ag_chart_load_from_file(GFile *file, GError **err);
void ag_chart_save_to_file(AgChart *chart, GFile *file, GError **err);
void ag_chart_set_name(AgChart *chart, const gchar *name);
gchar *ag_chart_get_name(AgChart *chart);
@ -39,6 +43,9 @@ gchar *ag_chart_get_country(AgChart *chart);
void ag_chart_set_city(AgChart *chart, const gchar *city);
gchar *ag_chart_get_city(AgChart *chart);
#define AG_CHART_ERROR (ag_chart_error_quark())
GQuark ag_chart_error_quark(void);
G_END_DECLS
#endif /* __AG_CHART_H__ */

View File

@ -1,6 +1,8 @@
#include <string.h>
#include <glib/gi18n.h>
#include <libgd/gd.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <swe-glib.h>
@ -36,12 +38,15 @@ struct _AgWindowPrivate {
GsweTimestamp *timestamp;
AgChart *chart;
gchar *uri;
};
G_DEFINE_TYPE(AgWindow, ag_window, GTK_TYPE_APPLICATION_WINDOW);
#define GET_PRIVATE(instance) (G_TYPE_INSTANCE_GET_PRIVATE((instance), AG_TYPE_WINDOW, AgWindowPrivate))
static void recalculate_chart(AgWindow *window);
static void
gear_menu_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
{
@ -62,9 +67,43 @@ close_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
gtk_widget_destroy(GTK_WIDGET(window));
}
static void
ag_window_save_as(AgWindow *window, GError **err)
{
}
static void
save_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
{
AgWindow *window = AG_WINDOW(user_data);
GError *err = NULL;
gchar *uri;
recalculate_chart(window);
uri = ag_window_get_uri(window);
if (uri != NULL) {
GFile *file = g_file_new_for_uri(uri);
g_free(uri);
ag_chart_save_to_file(window->priv->chart, file, &err);
} else {
ag_window_save_as(window, &err);
}
// TODO: Check err!
}
static void
save_as_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
{
AgWindow *window = AG_WINDOW(user_data);
GError *err = NULL;
recalculate_chart(window);
ag_window_save_as(window, &err);
// TODO: Check err!
}
void
@ -135,6 +174,7 @@ recalculate_chart(AgWindow *window)
if (priv->chart == NULL) {
// TODO: make house system configurable
priv->chart = ag_chart_new_full(priv->timestamp, longitude, latitude, 380.0, GSWE_HOUSE_SYSTEM_PLACIDUS);
ag_chart_set_name(priv->chart, gtk_entry_get_text(GTK_ENTRY(priv->name)));
g_signal_connect(priv->chart, "changed", G_CALLBACK(chart_changed), window);
ag_window_update_from_chart(window);
} else {
@ -165,6 +205,7 @@ tab_changed_cb(GdStack *stack, GParamSpec *pspec, AgWindow *window)
static GActionEntry win_entries[] = {
{ "close", close_cb, NULL, NULL, NULL },
{ "save", save_cb, NULL, NULL, NULL },
{ "save-as", save_as_cb, NULL, NULL, NULL },
{ "gear-menu", gear_menu_cb, NULL, "false", NULL },
};
@ -179,6 +220,7 @@ ag_window_init(AgWindow *window)
priv->timestamp = NULL;
priv->chart = NULL;
priv->uri = NULL;
gtk_window_set_hide_titlebar_when_maximized(GTK_WINDOW(window), TRUE);
@ -383,3 +425,19 @@ ag_window_get_chart(AgWindow *window)
return window->priv->chart;
}
void
ag_window_set_uri(AgWindow *window, const gchar *uri)
{
if (window->priv->uri != NULL) {
g_free(window->priv->uri);
}
window->priv->uri = g_strdup(uri);
}
gchar *
ag_window_get_uri(AgWindow *window)
{
return g_strdup(window->priv->uri);
}

View File

@ -32,6 +32,8 @@ GtkWidget *ag_window_new(AgApp *app);
void ag_window_set_chart(AgWindow *window, AgChart *chart);
AgChart *ag_window_get_chart(AgWindow *window);
void ag_window_update_from_chart(AgWindow *window);
void ag_window_set_uri(AgWindow *window, const gchar *uri);
gchar *ag_window_get_uri(AgWindow *window);
G_END_DECLS

View File

@ -39,5 +39,10 @@
<attribute name="action">win.save</attribute>
<attribute name="accel">&lt;Primary&gt;s</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Save as…</attribute>
<attribute name="action">win.save-as</attribute>
<attribute name="accel">&lt;Primary&gt;&lt;Shift&gt;s</attribute>
</item>
</menu>
</interface>