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;
}
gboolean
ag_chart_save_to_db(AgChart *chart, AgDbSave **old_save, GtkWidget *window)
AgDbSave *
ag_chart_get_db_save(AgChart *chart, gint db_id)
{
GsweCoordinates *coords;
AgDb *db = ag_db_get();
AgChartPrivate *priv = ag_chart_get_instance_private(chart);
AgDbSave *save_data = g_new0(AgDbSave, 1);
GError *err = NULL;
GsweTimestamp *timestamp = gswe_moment_get_timestamp(GSWE_MOMENT(chart));
GEnumClass *house_system_class;
GEnumValue *house_system_enum;
gboolean ret;
if (old_save && *old_save) {
save_data->db_id = (*old_save)->db_id;
} else {
save_data->db_id = -1;
}
save_data->db_id = db_id;
save_data->name = g_strdup(priv->name);
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);
save_data->note = g_strdup(priv->note);
if (
(!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;
return save_data;
}

View File

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

View File

@ -3,6 +3,7 @@
#include <libgda/libgda.h>
#include <sql-parser/gda-sql-parser.h>
#include <gobject/gvaluecollector.h>
#include <glib/gi18n.h>
#include "config.h"
#include "ag-app.h"
@ -592,20 +593,18 @@ ag_db_save_data_free(AgDbSave *save_data)
* ag_db_save_chart:
* @db: the #AgDb object to operate on
* @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
*
* 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
* issues arise, a message dialog will pop up on @window
* created; otherwise the row with the given ID will be updated.
*
* Returns: TRUE if the save succeeds, FALSE otherwise
*/
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,
name = 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_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(
priv->conn,
"chart",
err,
&local_err,
"name", &name,
"country_name", &country,
"city_name", &city,
@ -692,14 +692,17 @@ ag_db_save_chart(AgDb *db, AgDbSave *save_data, GtkWidget *window, GError **err)
NULL
)) {
ag_app_message_dialog(
window,
GTK_MESSAGE_ERROR,
"Unable to save: %s",
(*err && (*err)->message)
? (*err)->message
: "no reason"
g_set_error(
err,
AG_DB_ERROR,
AG_DB_ERROR_DATABASE_ERROR,
"%s",
(local_err && local_err->message)
? local_err->message
: _("Reason unknown")
);
save_success = FALSE;
}
} else {
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",
"id",
&db_id,
err,
&local_err,
"name", &name,
"country_name", &country,
"city_name", &city,
@ -729,14 +732,17 @@ ag_db_save_chart(AgDb *db, AgDbSave *save_data, GtkWidget *window, GError **err)
NULL
)) {
ag_app_message_dialog(
window,
GTK_MESSAGE_ERROR,
"Unable to save: %s",
(*err && (*err)->message)
? (*err)->message
: "no reason"
g_set_error(
err,
AG_DB_ERROR,
AG_DB_ERROR_DATABASE_ERROR,
"%s",
(local_err && local_err->message)
? local_err->message
: _("Reason unknown")
);
save_success = FALSE;
}
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(&name);
return FALSE;
return save_success;
}
/**

View File

@ -50,7 +50,8 @@ typedef struct _AgDbSave {
} AgDbSave;
typedef enum {
AG_DB_ERROR_NO_CHART
AG_DB_ERROR_NO_CHART,
AG_DB_ERROR_DATABASE_ERROR,
} AgDbError;
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,
AgDbSave *save_data,
GtkWidget *window,
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);
AgWindowPrivate *priv = ag_window_get_instance_private(window);
AgDb *db = ag_db_get();
GError *err;
gint old_id;
AgDbSave *save_data;
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