Rework chart save procedure

* Fixed some design flows: AgDb should not know about the window
* AgChart now only creates an AgDbSave structure, saving is done through
  AgWindow
This commit is contained in:
Gergely Polonkai 2014-08-02 01:37:11 +02:00
parent 860638ed98
commit ab12922d8a
5 changed files with 60 additions and 52 deletions

View File

@ -1539,24 +1539,17 @@ const gchar *ag_chart_get_note(AgChart *chart)
return priv->note; return priv->note;
} }
gboolean AgDbSave *
ag_chart_save_to_db(AgChart *chart, AgDbSave **old_save, GtkWidget *window) ag_chart_get_db_save(AgChart *chart, gint db_id)
{ {
GsweCoordinates *coords; GsweCoordinates *coords;
AgDb *db = ag_db_get();
AgChartPrivate *priv = ag_chart_get_instance_private(chart); AgChartPrivate *priv = ag_chart_get_instance_private(chart);
AgDbSave *save_data = g_new0(AgDbSave, 1); AgDbSave *save_data = g_new0(AgDbSave, 1);
GError *err = NULL;
GsweTimestamp *timestamp = gswe_moment_get_timestamp(GSWE_MOMENT(chart)); GsweTimestamp *timestamp = gswe_moment_get_timestamp(GSWE_MOMENT(chart));
GEnumClass *house_system_class; GEnumClass *house_system_class;
GEnumValue *house_system_enum; GEnumValue *house_system_enum;
gboolean ret;
if (old_save && *old_save) { save_data->db_id = db_id;
save_data->db_id = (*old_save)->db_id;
} else {
save_data->db_id = -1;
}
save_data->name = g_strdup(priv->name); save_data->name = g_strdup(priv->name);
save_data->country = g_strdup(priv->country); save_data->country = g_strdup(priv->country);
@ -1597,16 +1590,5 @@ ag_chart_save_to_db(AgChart *chart, AgDbSave **old_save, GtkWidget *window)
g_type_class_unref(house_system_class); g_type_class_unref(house_system_class);
save_data->note = g_strdup(priv->note); save_data->note = g_strdup(priv->note);
if ( return save_data;
(!old_save || !*old_save)
|| !ag_db_save_identical(*old_save, save_data)
) {
ret = ag_db_save_chart(db, save_data, window, &err);
} else {
ret = TRUE;
}
ag_db_save_data_free(save_data);
return ret;
} }

View File

@ -86,9 +86,7 @@ void ag_chart_set_note(AgChart *chart, const gchar *note);
const gchar *ag_chart_get_note(AgChart *chart); const gchar *ag_chart_get_note(AgChart *chart);
gboolean ag_chart_save_to_db(AgChart *chart, AgDbSave *ag_chart_get_db_save(AgChart *chart, gint db_id);
AgDbSave **old_save,
GtkWidget *window);
#define AG_CHART_ERROR (ag_chart_error_quark()) #define AG_CHART_ERROR (ag_chart_error_quark())
GQuark ag_chart_error_quark(void); GQuark ag_chart_error_quark(void);

View File

@ -3,6 +3,7 @@
#include <libgda/libgda.h> #include <libgda/libgda.h>
#include <sql-parser/gda-sql-parser.h> #include <sql-parser/gda-sql-parser.h>
#include <gobject/gvaluecollector.h> #include <gobject/gvaluecollector.h>
#include <glib/gi18n.h>
#include "config.h" #include "config.h"
#include "ag-app.h" #include "ag-app.h"
@ -592,20 +593,18 @@ ag_db_save_data_free(AgDbSave *save_data)
* ag_db_save_chart: * ag_db_save_chart:
* @db: the #AgDb object to operate on * @db: the #AgDb object to operate on
* @save_data: the data to save. * @save_data: the data to save.
* @window: the window to use as the parent of possible message dialogs (TODO:
* this is a design flow. This function should merely rely only on
* @err)
* @err: a #GError for storing errors * @err: a #GError for storing errors
* *
* Saves @save_data to the database. If its db_id field is -1, a new record is * Saves @save_data to the database. If its db_id field is -1, a new record is
* created; otherwise the row with the given ID will be updated. Should any * created; otherwise the row with the given ID will be updated.
* issues arise, a message dialog will pop up on @window
* *
* Returns: TRUE if the save succeeds, FALSE otherwise * Returns: TRUE if the save succeeds, FALSE otherwise
*/ */
gboolean gboolean
ag_db_save_chart(AgDb *db, AgDbSave *save_data, GtkWidget *window, GError **err) ag_db_save_chart(AgDb *db, AgDbSave *save_data, GError **err)
{ {
GError *local_err = NULL;
gboolean save_success = TRUE;
GValue db_id = G_VALUE_INIT, GValue db_id = G_VALUE_INIT,
name = G_VALUE_INIT, name = G_VALUE_INIT,
country = G_VALUE_INIT, country = G_VALUE_INIT,
@ -669,11 +668,12 @@ ag_db_save_chart(AgDb *db, AgDbSave *save_data, GtkWidget *window, GError **err)
g_value_init(&note, G_TYPE_STRING); g_value_init(&note, G_TYPE_STRING);
g_value_set_string(&note, save_data->note); g_value_set_string(&note, save_data->note);
if (save_data->db_id == -1) { /* It is possible to get 0 here, which is as non-existant as -1 */
if (save_data->db_id < 0) {
if (!gda_connection_insert_row_into_table( if (!gda_connection_insert_row_into_table(
priv->conn, priv->conn,
"chart", "chart",
err, &local_err,
"name", &name, "name", &name,
"country_name", &country, "country_name", &country,
"city_name", &city, "city_name", &city,
@ -692,14 +692,17 @@ ag_db_save_chart(AgDb *db, AgDbSave *save_data, GtkWidget *window, GError **err)
NULL NULL
)) { )) {
ag_app_message_dialog( g_set_error(
window, err,
GTK_MESSAGE_ERROR, AG_DB_ERROR,
"Unable to save: %s", AG_DB_ERROR_DATABASE_ERROR,
(*err && (*err)->message) "%s",
? (*err)->message (local_err && local_err->message)
: "no reason" ? local_err->message
: _("Reason unknown")
); );
save_success = FALSE;
} }
} else { } else {
g_value_init(&db_id, G_TYPE_INT); g_value_init(&db_id, G_TYPE_INT);
@ -710,7 +713,7 @@ ag_db_save_chart(AgDb *db, AgDbSave *save_data, GtkWidget *window, GError **err)
"chart", "chart",
"id", "id",
&db_id, &db_id,
err, &local_err,
"name", &name, "name", &name,
"country_name", &country, "country_name", &country,
"city_name", &city, "city_name", &city,
@ -729,14 +732,17 @@ ag_db_save_chart(AgDb *db, AgDbSave *save_data, GtkWidget *window, GError **err)
NULL NULL
)) { )) {
ag_app_message_dialog( g_set_error(
window, err,
GTK_MESSAGE_ERROR, AG_DB_ERROR,
"Unable to save: %s", AG_DB_ERROR_DATABASE_ERROR,
(*err && (*err)->message) "%s",
? (*err)->message (local_err && local_err->message)
: "no reason" ? local_err->message
: _("Reason unknown")
); );
save_success = FALSE;
} }
g_value_unset(&db_id); g_value_unset(&db_id);
@ -758,7 +764,7 @@ ag_db_save_chart(AgDb *db, AgDbSave *save_data, GtkWidget *window, GError **err)
g_value_unset(&country); g_value_unset(&country);
g_value_unset(&name); g_value_unset(&name);
return FALSE; return save_success;
} }
/** /**

View File

@ -50,7 +50,8 @@ typedef struct _AgDbSave {
} AgDbSave; } AgDbSave;
typedef enum { typedef enum {
AG_DB_ERROR_NO_CHART AG_DB_ERROR_NO_CHART,
AG_DB_ERROR_DATABASE_ERROR,
} AgDbError; } AgDbError;
GType ag_db_get_type(void) G_GNUC_CONST; GType ag_db_get_type(void) G_GNUC_CONST;
@ -61,7 +62,6 @@ void ag_db_save_data_free(AgDbSave *save_data);
gboolean ag_db_save_chart(AgDb *db, gboolean ag_db_save_chart(AgDb *db,
AgDbSave *save_data, AgDbSave *save_data,
GtkWidget *window,
GError **err); GError **err);
GList *ag_db_get_chart_list(AgDb *db, GError **err); GList *ag_db_get_chart_list(AgDb *db, GError **err);

View File

@ -183,10 +183,32 @@ ag_window_save_action(GSimpleAction *action,
{ {
AgWindow *window = AG_WINDOW(user_data); AgWindow *window = AG_WINDOW(user_data);
AgWindowPrivate *priv = ag_window_get_instance_private(window); AgWindowPrivate *priv = ag_window_get_instance_private(window);
AgDb *db = ag_db_get();
GError *err;
gint old_id;
AgDbSave *save_data;
recalculate_chart(window); recalculate_chart(window);
ag_chart_save_to_db(priv->chart, &(priv->saved_data), GTK_WIDGET(window)); old_id = (priv->saved_data) ? priv->saved_data->db_id : -1;
save_data = ag_chart_get_db_save(priv->chart, old_id);
if (!ag_db_save_identical(priv->saved_data, save_data)) {
if (!ag_db_save_chart(db, save_data, &err)) {
ag_app_message_dialog(
GTK_WIDGET(window),
GTK_MESSAGE_ERROR,
_("Unable to save: %s"),
err->message
);
}
ag_db_save_data_free(priv->saved_data);
priv->saved_data = save_data;
} else {
ag_db_save_data_free(save_data);
}
} }
static void static void