diff --git a/Makefile.decl b/Makefile.decl index b98171c..21d26c0 100644 --- a/Makefile.decl +++ b/Makefile.decl @@ -1,5 +1,7 @@ # Definition of the install directories + # Libraries + if ENABLE_INSTALL hedera_libdir = $(pkglibdir) hedera_datadir = $(pkgdatadir) @@ -23,13 +25,12 @@ else module_libdir = $(hedera_libdir)/module/src/.libs endif -mysql_libdir = $(plugin_libdir)/mysql -pg_libdir = $(plugin_libdir)/pg - # Binaries (programs) + hedera_bindir = $(bindir) # Headers and development files + hedera_includedir = $(pkgincludedir) gvn_includedir = $(hedera_includedir)/gvn @@ -53,6 +54,7 @@ gladevn_libdir = $(prefix)/lib/glade/modules gladevn_datadir = $(datadir)/glade/catalogs # Data + vn_datadir = $(hedera_datadir)/vn vn_imagedir = $(vn_datadir)/image vn_guidir = $(vn_datadir)/gui diff --git a/configure.ac b/configure.ac index 39dfe8a..6c0886b 100644 --- a/configure.ac +++ b/configure.ac @@ -16,6 +16,7 @@ AC_PROG_CC # Check for additional packages PKG_CHECK_MODULES([glib], [glib-2.0]) PKG_CHECK_MODULES([gdome], [gdome2]) +PKG_CHECK_MODULES([sqlite], [sqlite3]) PKG_CHECK_MODULES([gtk], [gtk+-3.0]) PKG_CHECK_MODULES([gladeui], [gladeui-2.0]) PKG_CHECK_MODULES([gtksourceview], [gtksourceview-3.0] @@ -89,6 +90,7 @@ AC_CONFIG_FILES([ sql/parser/Makefile db/Makefile plugin/Makefile + plugin/sqlite/Makefile plugin/pg/Makefile plugin/mysql/Makefile vn/Makefile diff --git a/db/db-calc.c b/db/db-calc.c index f6f57f2..990a1ab 100644 --- a/db/db-calc.c +++ b/db/db-calc.c @@ -324,10 +324,10 @@ void db_calc_set_model (DbCalc * self, DbModel * model) if (self->model) { g_object_disconnect (self->model - ,"any_signal::line-updated", db_calc_on_update, self - ,"any_signal::line-updated", db_calc_on_update_after, self - ,"any_signal::line-deleted", db_calc_on_delete, self - ,"any_signal::status-changed", db_calc_on_model_refresh, self + ,"any_signal", db_calc_on_update, self + ,"any_signal", db_calc_on_update_after, self + ,"any_signal", db_calc_on_delete, self + ,"any_signal", db_calc_on_model_refresh, self ,NULL ); g_clear_object (&self->model); @@ -449,10 +449,14 @@ static void db_calc_init (DbCalc * self) g_value_init (self->value, GVN_TYPE_NULL); } +static void db_calc_dispose (DbCalc * self) +{ + db_calc_set_master (self, NULL); + db_calc_set_model (self, NULL); +} + static void db_calc_finalize (DbCalc * self) { - db_calc_set_model (self, NULL); - db_calc_set_master (self, NULL); g_value_unset (self->value); gvn_param_spec_free (self->spec); g_free (self->value); @@ -463,6 +467,7 @@ static void db_calc_finalize (DbCalc * self) static void db_calc_class_init (DbCalcClass * klass) { GObjectClass * k = G_OBJECT_CLASS (klass); + k->dispose = (GObjectFinalizeFunc) db_calc_dispose; k->finalize = (GObjectFinalizeFunc) db_calc_finalize; k->set_property = (GObjectSetPropertyFunc) db_calc_set_property; k->get_property = (GObjectGetPropertyFunc) db_calc_get_property; diff --git a/db/db-iterator.c b/db/db-iterator.c index 6d05ced..4823316 100644 --- a/db/db-iterator.c +++ b/db/db-iterator.c @@ -402,10 +402,11 @@ void db_iterator_move_iter (DbIterator * self, DbIter * iter) if (self->mode != DB_ITERATOR_MODE_ON_DEMAND) { - if (db_model_get_row_operations (self->model, &self->iter) == DB_MODEL_ROW_OP_INSERT) + if (self->row_selected + && db_model_get_row_operations (self->model, &self->iter) == DB_MODEL_ROW_OP_INSERT) db_model_reverse_operations (self->model); - - db_model_perform_operations (self->model, FALSE); + else + db_model_perform_operations (self->model, FALSE); } db_iterator_set_iter (self, iter); @@ -619,26 +620,6 @@ const GvnParamSpec * db_iterator_get_spec (DbIterator * self, gint column) return NULL; } -/** - * db_iterator_get_value: - * @self: a #DbIterator - * @column: the column index. - * - * Gets the value of the specified column index. - * - * Return value: (transfer none) (allow-none): the value or %NULL if - * can't get it because the model isn't ready. - **/ -const GValue * db_iterator_get_value (DbIterator * self, gint column) -{ - g_return_val_if_fail (DB_IS_ITERATOR (self), NULL); - - if (self->row_selected) - return db_model_get_value (self->model, &self->iter, column, NULL); - - return NULL; -} - /** * db_iterator_get_column_index: * @self: a #DbIterator @@ -655,11 +636,82 @@ gint db_iterator_get_column_index (DbIterator * self, const gchar * name) g_return_val_if_fail (IS_READY (self), -1); return db_model_get_column_index (self->model, name); +} + +/** + * db_iterator_get_value: + * @self: a #DbIterator + * @column_name: the column name. + * + * Gets the value of the specified column name. + * + * Return value: (transfer none) (allow-none): the value or %NULL if + * can't get it because the model isn't ready. + **/ +const GValue * db_iterator_get_value (DbIterator * self, const gchar * column_name) +{ + g_return_val_if_fail (DB_IS_ITERATOR (self), NULL); + + if (!self->row_selected) + { + gint column = db_model_get_column_index (self->model, column_name); + + if (column != -1) + return db_model_get_value (self->model, &self->iter, column, NULL); + } + + return NULL; } /** * db_iterator_set_value: * @self: a #DbIterator + * @column: the column name. + * @value: a #GValue with the new value. + * @err: (out) (allow-none): the return location for a #GError or %NULL. + * + * Sets the value of the specified column name. + * + * Return value: %TRUE if the value was changed, %FALSE otherwise + **/ +gboolean db_iterator_set_value (DbIterator * self, const gchar * column_name, const GValue * value, GError ** err) +{ + g_return_val_if_fail (DB_IS_ITERATOR (self), FALSE); + + if (db_iterator_check_row_selected (self)) + { + gint column = db_model_get_column_index (self->model, column_name); + + if (column != -1) + return db_model_set_value (self->model, &self->iter, column, value, err); + } + + return FALSE; +} + +/** + * db_iterator_get_value_by_index: + * @self: a #DbIterator + * @column: the column index. + * + * Gets the value of the specified column index. + * + * Return value: (transfer none) (allow-none): the value or %NULL if + * can't get it because the model isn't ready. + **/ +const GValue * db_iterator_get_value_by_index (DbIterator * self, gint column) +{ + g_return_val_if_fail (DB_IS_ITERATOR (self), NULL); + + if (self->row_selected) + return db_model_get_value (self->model, &self->iter, column, NULL); + + return NULL; +} + +/** + * db_iterator_set_value_by_index: + * @self: a #DbIterator * @column: the column index. * @value: a #GValue with the new value. * @err: (out) (allow-none): the return location for a #GError or %NULL. @@ -668,7 +720,7 @@ gint db_iterator_get_column_index (DbIterator * self, const gchar * name) * * Return value: %TRUE if the value was changed, %FALSE otherwise **/ -gboolean db_iterator_set_value (DbIterator * self, gint column, const GValue * value, GError ** err) +gboolean db_iterator_set_value_by_index (DbIterator * self, gint column, const GValue * value, GError ** err) { g_return_val_if_fail (DB_IS_ITERATOR (self), FALSE); diff --git a/db/db-model.c b/db/db-model.c index 3ed580d..48a1b92 100644 --- a/db/db-model.c +++ b/db/db-model.c @@ -1555,16 +1555,15 @@ void db_model_set_batch (DbModel * self, SqlBatch * batch) { g_signal_handlers_disconnect_by_func (priv->batch, db_model_on_batch_changed, self); - g_object_unref (priv->batch); + g_clear_object (&priv->batch); } if (batch) { + priv->batch = g_object_ref_sink (batch); g_signal_connect (batch, "changed", G_CALLBACK (db_model_on_batch_changed), self); - g_object_ref_sink (batch); + db_model_on_batch_changed (NULL, self); } - - priv->batch = batch; } static ColumnDef * db_model_create_column_def (DbModel * self, const gchar * field_str) @@ -2737,7 +2736,7 @@ void db_model_refresh (DbModel * self) if (sql_list_length (link_operands) > 0) sql_batch_add (tmp_batch, "link", link_op); - + // Executes the statement if its ready if (sql_batch_is_ready (tmp_batch)) diff --git a/gvn/gvn-misc.h b/gvn/gvn-misc.h index 245a514..af337ae 100644 --- a/gvn/gvn-misc.h +++ b/gvn/gvn-misc.h @@ -22,7 +22,7 @@ #include #include -void gvn_key_file_save (GKeyFile * obj, const gchar * file); +void gvn_key_file_save (GKeyFile * self, const gchar * file); gchar * gvn_encode (const gchar * string); gchar * gvn_decode (const gchar * string); diff --git a/gvn/gvn-param.h b/gvn/gvn-param.h index c07aae7..7de9660 100644 --- a/gvn/gvn-param.h +++ b/gvn/gvn-param.h @@ -30,14 +30,6 @@ typedef struct _GvnParam GvnParam; typedef struct _GvnParamInterface GvnParamInterface; -typedef enum _GvnParamStatus GvnParamStatus; - -typedef const GValue * (* GvnParamGetValueFunc) (GvnParam * self); -typedef gboolean (* GvnParamRequestValueFunc) (GvnParam * self, const GValue * value, GError ** err); -typedef GvnParam * (* GvnParamGetMasterFunc) (GvnParam * self); -typedef void (* GvnParamSetMasterFunc) (GvnParam * self, GvnParam * master); -typedef const GvnParamSpec * (* GvnParamGetSpecFunc) (GvnParam * self); -typedef GvnParamStatus (* GvnParamGetStatusFunc) (GvnParam * self); /** * GvnParamStatus: @@ -47,12 +39,20 @@ typedef GvnParamStatus (* GvnParamGetStatusFunc) (GvnParam * self); * * The status of the param. **/ -enum _GvnParamStatus +typedef enum { GVN_PARAM_STATUS_OK ,GVN_PARAM_STATUS_BUSY ,GVN_PARAM_STATUS_ERROR -}; +} +GvnParamStatus; + +typedef const GValue * (* GvnParamGetValueFunc) (GvnParam * self); +typedef gboolean (* GvnParamRequestValueFunc) (GvnParam * self, const GValue * value, GError ** err); +typedef GvnParam * (* GvnParamGetMasterFunc) (GvnParam * self); +typedef void (* GvnParamSetMasterFunc) (GvnParam * self, GvnParam * master); +typedef const GvnParamSpec * (* GvnParamGetSpecFunc) (GvnParam * self); +typedef GvnParamStatus (* GvnParamGetStatusFunc) (GvnParam * self); /** * GvnParam: diff --git a/module/data/customer.glade b/module/data/customer.glade index a4d2bab..df0c2ce 100644 --- a/module/data/customer.glade +++ b/module/data/customer.glade @@ -4,7 +4,7 @@ - + SELECT id, object FROM object diff --git a/plugin/Makefile.am b/plugin/Makefile.am index 2d415cb..3f5f20b 100644 --- a/plugin/Makefile.am +++ b/plugin/Makefile.am @@ -1,4 +1,5 @@ SUBDIRS = \ + sqlite \ pg \ - mysql \ No newline at end of file + mysql diff --git a/plugin/mysql/Makefile.am b/plugin/mysql/Makefile.am index 4efb366..947c17f 100644 --- a/plugin/mysql/Makefile.am +++ b/plugin/mysql/Makefile.am @@ -15,6 +15,8 @@ libdbmysql_la_SOURCES = \ db-mysql.h \ db-mysql.c +mysql_libdir = $(plugin_libdir)/mysql + install-data-hook: rm -f $(DESTDIR)$(mysql_libdir)/libdbmysql.la - rm -f $(DESTDIR)$(mysql_libdir)/libdbmysql.a \ No newline at end of file + rm -f $(DESTDIR)$(mysql_libdir)/libdbmysql.a diff --git a/plugin/mysql/db-mysql.c b/plugin/mysql/db-mysql.c index cc9fec3..00c583b 100644 --- a/plugin/mysql/db-mysql.c +++ b/plugin/mysql/db-mysql.c @@ -23,7 +23,7 @@ /** * SECTION: db-mysql - * @Short_description: manages a connection to a PostgreSQL database. + * @Short_description: manages a connection to a MySQL database. * @Title: DbMysql * @See_also: #DbConn * diff --git a/plugin/pg/Makefile.am b/plugin/pg/Makefile.am index 39731a1..42c4827 100644 --- a/plugin/pg/Makefile.am +++ b/plugin/pg/Makefile.am @@ -15,6 +15,8 @@ libdbpg_la_SOURCES = \ db-pg.h \ db-pg.c +pg_libdir = $(plugin_libdir)/pg + # FIXME error durante la ejecucion del parser: # llama a la funcion Parse en gram.c de sql en vez de en db-pg-gram.c # db-pg-parser.h \ @@ -38,4 +40,4 @@ libdbpg_la_SOURCES = \ install-data-hook: rm -f $(DESTDIR)$(pg_libdir)/libdbpg.la - rm -f $(DESTDIR)$(pg_libdir)/libdbpg.a \ No newline at end of file + rm -f $(DESTDIR)$(pg_libdir)/libdbpg.a diff --git a/sql/sql-value.c b/sql/sql-value.c index 72a2bf7..5c5d705 100644 --- a/sql/sql-value.c +++ b/sql/sql-value.c @@ -133,7 +133,7 @@ static void sql_value_render (SqlValue * self, SqlRender * render) switch (type) { case G_TYPE_BOOLEAN: - sql_render_add_token (render, g_value_get_boolean (value) ? "TRUE" : "FALSE"); + sql_render_add_token (render, g_value_get_boolean (value) ? "1" : "0"); break; case G_TYPE_CHAR: sql_render_printf (render, "'%c'", g_value_get_schar (value)); diff --git a/vn/gui/login.glade b/vn/gui/login.glade index 11f0282..c819b39 100644 --- a/vn/gui/login.glade +++ b/vn/gui/login.glade @@ -1,8 +1,45 @@ - + + + + + + SELECT rowid, name FROM connection + DB_MODEL_INSERT | DB_MODEL_UPDATE + + + + + SELECT rowid, name, plugin, schema, host, ssl_ca, user, password + FROM connection WHERE #link + DB_MODEL_UPDATE + + + + + + + + + + model-connections + + + name + + + + + + + model-connection + on-demand + + + False 15 @@ -22,6 +59,7 @@ 40 True False + center 15 @@ -56,40 +94,28 @@ - + True False 6 6 True - + + Remember True True - + False + 0 + True 1 - 0 + 3 - - True - False - 1 - User: - - - - - - 0 - 0 - - - - + True False 1 @@ -100,7 +126,7 @@ 0 - 1 + 2 @@ -114,23 +140,66 @@ + + 1 + 2 + + + + + True + True + + 1 1 - - Remember + True - True - False - 0 - True + False + 1 + User: + + + + + + 0 + 1 + + + + + True + False + 1 + Server: + + + + + + 0 + 0 + + + + + True + False + model-connections + + + False + + 1 - 2 + 0 @@ -172,8 +241,8 @@ True True - + False @@ -202,39 +271,24 @@ window - + False vertical 2 - + False spread - - gtk-cancel - True - True - True - True - - - - False - True - 0 - - - - - gtk-apply + + gtk-close True True True True - + False @@ -251,104 +305,254 @@ - + True False - 6 - 6 + 6 - - True - True - - - - 1 - 0 - - - - - True - True - - - - 1 - 1 - - - - + True False - 1 - Plugin: + vertical + 6 + + + 130 + True + True + in + + + True + True + iterator-connections + + + multiple + + + + + Connections + name + + + + + + + + + + True + True + 0 + + + + + True + False + end + iterator-connections + VN_HANDLER_SHOW_REMOVE | VN_HANDLER_SHOW_ADD + + + False + True + 1 + + - 0 - 0 + False + True + 0 - + True False - 1 - Host: + vertical + 6 + + + True + False + 6 + 6 + True + + + True + False + SSL CA: + + + 0 + 4 + + + + + True + False + 1 + Schema: + + + 0 + 3 + + + + + True + False + 1 + Host: + + + 0 + 2 + + + + + True + False + 1 + Plugin: + + + 0 + 1 + + + + + True + False + iterator-conection + ssl_ca + + + True + dialog-information-symbolic + + + + + 1 + 4 + + + + + True + False + iterator-conection + schema + + + True + + + + + 1 + 3 + + + + + True + False + iterator-conection + host + + + True + + + + + 1 + 2 + + + + + True + False + iterator-conection + plugin + + + False + + + + + 1 + 1 + + + + + True + False + 1 + Name: + + + 0 + 0 + + + + + True + False + iterator-conection + name + + + True + + + + + 1 + 0 + + + + + False + True + 0 + + + + + True + False + iterator-conection + VN_HANDLER_SHOW_UNDO | VN_HANDLER_SHOW_SAVE + + + False + True + 1 + + - 0 - 1 - - - - - True - False - 1 - Schema: - - - 0 - 2 - - - - - True - True - - - - 1 - 2 - - - - - True - False - SSL CA: - - - 0 - 3 - - - - - True - True - - dialog-information-symbolic - False - False - Path to the file containing the CA certificate, if this is empty SSL won't be used - - - 1 - 3 + False + True + 1 @@ -361,8 +565,7 @@ - cancel - apply + close diff --git a/vn/vn-field.c b/vn/vn-field.c index a9facaf..bec9320 100644 --- a/vn/vn-field.c +++ b/vn/vn-field.c @@ -540,12 +540,16 @@ static void vn_field_init (VnField * self) GTK_STATE_FLAG_NORMAL, &color); } +static void vn_field_dispose (VnField * self) +{ + vn_field_set_master (self, NULL); +} + static void vn_field_finalize (VnField * self) { gvn_param_spec_free (self->spec); gvn_param_spec_free (self->user_spec); g_clear_object (&self->iterator); - vn_field_set_master (self, NULL); g_value_unset (self->value); g_free (self->value); g_free (self->column_name); @@ -555,6 +559,7 @@ static void vn_field_finalize (VnField * self) static void vn_field_class_init (VnFieldClass * klass) { GObjectClass * k = G_OBJECT_CLASS (klass); + k->dispose = (GObjectFinalizeFunc) vn_field_dispose; k->finalize = (GObjectFinalizeFunc) vn_field_finalize; k->set_property = (GObjectSetPropertyFunc) vn_field_set_property; k->get_property = (GObjectGetPropertyFunc) vn_field_get_property; diff --git a/vn/vn-login.c b/vn/vn-login.c index d060615..13936e9 100644 --- a/vn/vn-login.c +++ b/vn/vn-login.c @@ -20,13 +20,17 @@ #define LOGIN_UI _GUI_DIR"/login.glade" #define IS_DEFINED(string) (string && g_strcmp0 (string, "")) -#define BUILDER_GET(obj, name) ((gpointer) gtk_builder_get_object (obj, name)) +#define BUILDER_GET(self, name) ((gpointer) gtk_builder_get_object (self, name)) typedef struct { - VnLogin * obj; + VnLogin * self; gchar * user; gchar * pass; + gchar * plugin; + gchar * host; + gchar * schema; + gchar * ssl_ca; DbConn * conn; GThread * thread; gboolean connected; @@ -34,8 +38,8 @@ typedef struct } ConnectData; -void vn_login_on_gui_logout (VnGui * gui, VnLogin * obj); -void vn_login_on_gui_exit (VnGui * gui, VnLogin * obj); +void vn_login_on_gui_logout (VnGui * gui, VnLogin * self); +void vn_login_on_gui_exit (VnGui * gui, VnLogin * self); G_DEFINE_TYPE (VnLogin, vn_login, G_TYPE_OBJECT); @@ -63,146 +67,154 @@ void connect_data_free (ConnectData * connect_data) g_free (connect_data->user); g_free (connect_data->pass); + g_free (connect_data->plugin); + g_free (connect_data->host); + g_free (connect_data->schema); + g_free (connect_data->ssl_ca); g_object_unref (connect_data->conn); - g_object_unref (connect_data->obj); + g_object_unref (connect_data->self); g_thread_unref (connect_data->thread); g_free (connect_data); } /* - * Shows the login window to the user. + * Loads the resources used by the login. */ -static void vn_login_show (VnLogin * obj) +static void vn_login_load (VnLogin * self) { - gboolean autologin = FALSE; + GError * err = NULL; + GtkBuilder * builder; + gchar * cfg_file; + + // Initializing SQLite connection + + cfg_file = g_build_filename (g_get_user_config_dir (), "config.db", NULL); + + self->cfg_conn = db_conn_new (); + + if (!db_conn_load_plugin (self->cfg_conn, "sqlite", &err) + || !db_conn_open (self->cfg_conn, NULL, cfg_file, NULL, NULL, &err)) + goto exit; + + // Loading interface + + builder = gtk_builder_new (); - if (!obj->window) + if (gtk_builder_add_from_file (builder, LOGIN_UI, &err)) { - gchar * user; - gchar * pass; - GError * err = NULL; - GtkBuilder * builder = gtk_builder_new (); + GSList * i; + VnSet * models; + + models = BUILDER_GET (self->builder, "models"); - if (gtk_builder_add_from_file (builder, LOGIN_UI, &err)) - { - obj->window = BUILDER_GET (builder, "window"); - obj->user = BUILDER_GET (builder, "user"); - obj->pass = BUILDER_GET (builder, "password"); - obj->remember = BUILDER_GET (builder, "remember"); - obj->connect = BUILDER_GET (builder, "connect"); - obj->settings_button = BUILDER_GET (builder, "settings"); - obj->spinner = BUILDER_GET (builder, "spinner"); - obj->settings_dialog = BUILDER_GET (builder, "settings-dialog"); - obj->plugin = BUILDER_GET (builder, "plugin"); - obj->host = BUILDER_GET (builder, "host"); - obj->schema = BUILDER_GET (builder, "schema"); - obj->ssl_ca = BUILDER_GET (builder, "ssl-ca"); - gtk_builder_connect_signals (builder, obj); + if (models) + for (i = vn_set_get_objects (models); i; i = i->next) + db_model_set_conn (i->data, self->cfg_conn); - user = g_settings_get_string (obj->settings, "user"); - pass = g_settings_get_string (obj->settings, "pass"); - - if (user && g_strcmp0 (user, "")) - gtk_entry_set_text (obj->user, user); - - if (pass && g_strcmp0 (pass, "")) - { - gchar * aux = pass; - pass = gvn_decode (aux); - gtk_entry_set_text (obj->pass, pass); - gtk_toggle_button_set_active (obj->remember, TRUE); - g_free (aux); - autologin = TRUE; - } - - g_free (user); - g_free (pass); - - gtk_application_add_window (obj->app, obj->window); - } - else if (err) - { - g_warning ("VnLogin: %s", err->message); - g_clear_error (&err); - } - - g_object_unref (builder); + self->window = BUILDER_GET (builder, "window"); + self->user = BUILDER_GET (builder, "user"); + self->pass = BUILDER_GET (builder, "password"); + self->remember = BUILDER_GET (builder, "remember"); + self->connect = BUILDER_GET (builder, "connect"); + self->settings_button = BUILDER_GET (builder, "settings"); + self->spinner = BUILDER_GET (builder, "spinner"); + self->settings_dialog = BUILDER_GET (builder, "settings-dialog"); + gtk_builder_connect_signals (builder, self); + + gtk_application_add_window (self->app, self->window); } - - gtk_widget_show_all (GTK_WIDGET (obj->window)); - gtk_widget_grab_focus (GTK_WIDGET (obj->user)); - - if (autologin) - gtk_button_clicked (obj->connect); + + g_object_unref (builder); + + // Freeing resources + + exit: + + if (err) + { + g_warning ("VnLogin: %s", err->message); + g_clear_error (&err); + } + + g_free (cfg_file); } /* - * Enables/disables the #GtkSpinner at login dialog. + * Shows the login window to the user. */ -static void vn_login_set_loading (VnLogin * obj, gboolean loading) +static void vn_login_show (VnLogin * self) { - gtk_widget_set_sensitive (GTK_WIDGET (obj->connect), !loading); - gtk_widget_set_sensitive (GTK_WIDGET (obj->settings_button), !loading); - gtk_widget_set_sensitive (GTK_WIDGET (obj->user), !loading); - gtk_widget_set_sensitive (GTK_WIDGET (obj->pass), !loading); - gtk_widget_set_sensitive (GTK_WIDGET (obj->remember), !loading); + gboolean autologin = FALSE; + + if (!self->window) + { + vn_login_load (self); + autologin = TRUE; + } + + if (self->window) + { + gtk_widget_show_all (GTK_WIDGET (self->window)); + gtk_widget_grab_focus (GTK_WIDGET (self->user)); + + if (autologin) + gtk_button_clicked (self->connect); + } +} + +/* + * Enables/disables the spinner at login dialog. + */ +static void vn_login_set_loading (VnLogin * self, gboolean loading) +{ + gtk_widget_set_sensitive (GTK_WIDGET (self->connect), !loading); + gtk_widget_set_sensitive (GTK_WIDGET (self->settings_button), !loading); + gtk_widget_set_sensitive (GTK_WIDGET (self->user), !loading); + gtk_widget_set_sensitive (GTK_WIDGET (self->pass), !loading); + gtk_widget_set_sensitive (GTK_WIDGET (self->remember), !loading); if (loading) { - gtk_widget_show (obj->spinner); - gtk_widget_hide (GTK_WIDGET (obj->connect)); + gtk_widget_show (self->spinner); + gtk_widget_hide (GTK_WIDGET (self->connect)); } else { - gtk_widget_hide (obj->spinner); - gtk_widget_show (GTK_WIDGET (obj->connect)); + gtk_widget_hide (self->spinner); + gtk_widget_show (GTK_WIDGET (self->connect)); } } /* * Frees the GUI object. */ -static void vn_login_free_gui (VnLogin * obj) +static void vn_login_free_gui (VnLogin * self) { - if (obj->gui) + if (self->gui) { - g_object_disconnect (obj->gui - ,"any_signal", vn_login_on_gui_exit, obj - ,"any_signal", vn_login_on_gui_logout, obj + g_object_disconnect (self->gui + ,"any_signal", vn_login_on_gui_exit, self + ,"any_signal", vn_login_on_gui_logout, self ,NULL ); - g_clear_object (&obj->gui); + g_clear_object (&self->gui); } } /* * Shows the settings dialog. */ -void vn_login_on_settings_clicked (GtkButton * button, VnLogin * obj) +void vn_login_on_settings_clicked (GtkButton * button, VnLogin * self) { - gchar * plugin = g_settings_get_string (obj->settings, "plugin"); - gchar * host = g_settings_get_string (obj->settings, "host"); - gchar * schema = g_settings_get_string (obj->settings, "schema"); - gchar * ssl_ca = g_settings_get_string (obj->settings, "ssl-ca"); - - gtk_entry_set_text (obj->plugin, plugin); - gtk_entry_set_text (obj->host, host); - gtk_entry_set_text (obj->schema, schema); - gtk_entry_set_text (obj->ssl_ca, ssl_ca); - gtk_dialog_run (obj->settings_dialog); - - g_free (plugin); - g_free (host); - g_free (schema); - g_free (ssl_ca); + gtk_dialog_run (self->settings_dialog); } /* * Hides the settings dialog. */ -void vn_login_on_settings_cancel_clicked (GtkButton * button, VnLogin * obj) +void vn_login_on_settings_cancel_clicked (GtkButton * button, VnLogin * self) { - gtk_widget_hide (GTK_WIDGET (obj->settings_dialog)); + gtk_widget_hide (GTK_WIDGET (self->settings_dialog)); } /* @@ -216,37 +228,28 @@ void vn_login_settings_on_delete_event (GtkWidget * settings_dialog) /* * Applies the changes made on settings dialog. */ -void vn_login_on_settings_apply_clicked (GtkButton * button, VnLogin * obj) +void vn_login_on_settings_apply_clicked (GtkButton * button, VnLogin * self) { - const gchar * plugin = gtk_entry_get_text (obj->plugin); - const gchar * host = gtk_entry_get_text (obj->host); - const gchar * schema = gtk_entry_get_text (obj->schema); - const gchar * ssl_ca = gtk_entry_get_text (obj->ssl_ca); - - g_settings_set_string (obj->settings, "plugin", plugin); - g_settings_set_string (obj->settings, "host", host); - g_settings_set_string (obj->settings, "schema", schema); - g_settings_set_string (obj->settings, "ssl-ca", ssl_ca); - gtk_widget_hide (GTK_WIDGET (obj->settings_dialog)); + gtk_widget_hide (GTK_WIDGET (self->settings_dialog)); } /* * Shows the login dialog when user logout from GUI. */ -void vn_login_on_gui_logout (VnGui * gui, VnLogin * obj) +void vn_login_on_gui_logout (VnGui * gui, VnLogin * self) { - g_settings_set_string (obj->settings, "pass", ""); - vn_login_free_gui (obj); - vn_login_show (obj); + g_settings_set_string (self->settings, "pass", ""); + vn_login_free_gui (self); + vn_login_show (self); } /* * Destroys the login dialog when user exits from GUI. */ -void vn_login_on_gui_exit (VnGui * gui, VnLogin * obj) +void vn_login_on_gui_exit (VnGui * gui, VnLogin * self) { - vn_login_free_gui (obj); - gtk_widget_destroy (GTK_WIDGET (obj->window)); + vn_login_free_gui (self); + gtk_widget_destroy (GTK_WIDGET (self->window)); } /* @@ -254,40 +257,37 @@ void vn_login_on_gui_exit (VnGui * gui, VnLogin * obj) */ static gboolean vn_login_done (ConnectData * connect_data) { - VnLogin * obj = connect_data->obj; + VnLogin * self = connect_data->self; if (connect_data->connected) { - const gchar * user; - - user = gtk_entry_get_text (obj->user); - g_settings_set_string (obj->settings, "user", user); + db_iterator_set_value (self->config, "user", gvn_param_get_value (self->user), NULL); + db_iterator_set_value (self->config, "remember", gvn_param_get_value (self->remember), NULL); - if (gtk_toggle_button_get_active (obj->remember)) + if (gvn_param_get_boolean (self->remember)) { - const gchar * pass = gtk_entry_get_text (obj->pass); - gchar * aux = gvn_encode (pass); - - gtk_toggle_button_set_active (obj->remember, FALSE); - g_settings_set_string (obj->settings, "pass", aux); - g_free (aux); + GValue encoded = G_VALUE_INIT; + g_value_take_string (&encoded, gvn_encode (gvn_param_get_string (self->pass))); + db_iterator_set_value (self->config, "password", &encoded, NULL); + g_value_unset (&encoded); } - gtk_entry_set_text (obj->pass, ""); - gtk_widget_hide (GTK_WIDGET (obj->window)); - obj->gui = vn_gui_new (obj->app, connect_data->conn); - g_object_connect (obj->gui - ,"signal::logout", vn_login_on_gui_logout, obj - ,"signal::exit", vn_login_on_gui_exit, obj + gvn_param_set_null (self->pass); + + gtk_widget_hide (GTK_WIDGET (self->window)); + self->gui = vn_gui_new (self->app, connect_data->conn); + g_object_connect (self->gui + ,"signal::logout", vn_login_on_gui_logout, self + ,"signal::exit", vn_login_on_gui_exit, self ,NULL ); - vn_gui_open (obj->gui); + vn_gui_open (self->gui); } else { GtkWidget * dialog; - dialog = gtk_message_dialog_new (obj->window + dialog = gtk_message_dialog_new (self->window ,GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT ,GTK_MESSAGE_ERROR ,GTK_BUTTONS_OK @@ -301,15 +301,15 @@ static gboolean vn_login_done (ConnectData * connect_data) "%s", connect_data->error->message); if (connect_data->error->code == DB_CONN_ERROR_BAD_LOGIN) - gtk_entry_set_text (obj->pass, ""); + gtk_entry_set_text (self->pass, ""); } gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (GTK_WIDGET (dialog)); - vn_login_show (obj); + vn_login_show (self); } - vn_login_set_loading (obj, FALSE); + vn_login_set_loading (self, FALSE); return G_SOURCE_REMOVE; } @@ -318,27 +318,18 @@ static gboolean vn_login_done (ConnectData * connect_data) */ static void vn_login_thread (ConnectData * connect_data) { - gchar * plugin; - gchar * host; - gchar * schema; - gchar * ssl_ca; - VnLogin * obj = connect_data->obj; + VnLogin * self = connect_data->self; - plugin = g_settings_get_string (obj->settings, "plugin"); - host = g_settings_get_string (obj->settings, "host"); - schema = g_settings_get_string (obj->settings, "schema"); - ssl_ca = g_settings_get_string (obj->settings, "ssl-ca"); - - if (IS_DEFINED (plugin) && IS_DEFINED (host) && IS_DEFINED (schema)) + if (IS_DEFINED (connect_data->plugin) && IS_DEFINED (connect_data->schema)) { - if (db_conn_load_plugin (connect_data->conn, plugin, &connect_data->error)) + if (db_conn_load_plugin (connect_data->conn, connect_data->plugin, &connect_data->error)) { - if (IS_DEFINED (ssl_ca)) - db_conn_set_ssl (connect_data->conn, ssl_ca); + if (IS_DEFINED (connect_data->ssl_ca)) + db_conn_set_ssl (connect_data->conn, connect_data->ssl_ca); connect_data->connected = db_conn_open (connect_data->conn - ,host - ,schema + ,connect_data->host + ,connect_data->schema ,connect_data->user ,connect_data->pass ,&connect_data->error @@ -351,11 +342,6 @@ static void vn_login_thread (ConnectData * connect_data) ,VN_LOGIN_ERR_BAD_SETTINGS ,_("Bad connection settings, please check it.") ); - - g_free (plugin); - g_free (host); - g_free (schema); - g_free (ssl_ca); g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, (GSourceFunc) vn_login_done, connect_data, (GDestroyNotify) connect_data_free); @@ -364,16 +350,20 @@ static void vn_login_thread (ConnectData * connect_data) /* * Opens the connection. */ -void vn_login_on_connect_clicked (GtkButton * button, VnLogin * obj) +void vn_login_on_connect_clicked (GtkButton * button, VnLogin * self) { ConnectData * connect_data; - vn_login_set_loading (obj, TRUE); + vn_login_set_loading (self, TRUE); connect_data = g_new (ConnectData, 1); - connect_data->obj = g_object_ref (obj); - connect_data->user = g_strdup (gtk_entry_get_text (obj->user)); - connect_data->pass = g_strdup (gtk_entry_get_text (obj->pass)); + connect_data->self = g_object_ref (self); + connect_data->user = g_strdup (gvn_param_get_string (self->user)); + connect_data->pass = g_strdup (gvn_param_get_string (self->pass)); + connect_data->plugin = g_strdup (db_iterator_get_string (self->config, "plugin")); + connect_data->host = g_strdup (db_iterator_get_string (self->config, "host")); + connect_data->schema = g_strdup (db_iterator_get_string (self->config, "schema")); + connect_data->ssl_ca = g_strdup (db_iterator_get_string (self->config, "ssl_ca")); connect_data->conn = db_conn_new (); connect_data->connected = FALSE; connect_data->error = NULL; @@ -381,43 +371,62 @@ void vn_login_on_connect_clicked (GtkButton * button, VnLogin * obj) (GThreadFunc) vn_login_thread, connect_data); } +/* + * Sets the default user and password for selected configuration. + */ +static void vn_login_on_server_changed (VnLogin * self) +{ + const GValue * pass = db_iterator_get_value (self->config, "password"); + + gvn_param_set_value (self->user, db_iterator_get_value (self->config, "user")); + gvn_param_set_value (self->remember, db_iterator_get_value (self->config, "remember")); + + if (!gvn_value_is_null (pass)) + { + GValue decoded = G_VALUE_INIT; + g_value_take_string (&decoded, gvn_decode (g_value_get_string (pass))); + gvn_param_set_value (self->pass, &decoded); + g_value_unset (&decoded); + } + else + gvn_param_set_value (self->pass, pass); +} + /* * Closes the application when login window is destroyed. */ -void vn_login_on_destroyed (GtkWidget * window, VnLogin * obj) +void vn_login_on_destroyed (GtkWidget * window, VnLogin * self) { - obj->window = NULL; + self->window = NULL; gtk_main_quit (); } void vn_login_on_pass_show (GtkEntry * entry, GtkEntryIconPosition * pos, - GdkEvent * event, VnLogin * obj) + GdkEvent * event, VnLogin * self) { gtk_entry_set_visibility (entry, TRUE); } void vn_login_on_pass_hide (GtkEntry * entry, GtkEntryIconPosition * pos, - GdkEvent * event, VnLogin * obj) + GdkEvent * event, VnLogin * self) { gtk_entry_set_visibility (entry, FALSE); } -static void vn_login_on_startup (GApplication * app, VnLogin * obj) +static void vn_login_on_startup (GApplication * app, VnLogin * self) { g_object_set (gtk_settings_get_default (), "gtk-button-images", TRUE, NULL); } -static void vn_login_on_activate (GApplication * app, VnLogin * obj) +static void vn_login_on_activate (GApplication * app, VnLogin * self) { - g_message ("Activated!"); - if (gtk_main_level () == 0) { - vn_login_show (obj); + vn_login_show (self); gtk_main (); } - else if (!obj->gui) - gtk_window_present (obj->window); + else if (!self->gui) + gtk_window_present (self->window); } //+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties @@ -427,62 +436,64 @@ enum PROP_APPLICATION = 1 }; -static void vn_login_set_property (VnLogin * obj, guint id, +static void vn_login_set_property (VnLogin * self, guint id, const GValue * value, GParamSpec * pspec) { switch (id) { case PROP_APPLICATION: { - obj->app = g_value_dup_object (value); - g_object_connect (obj->app - ,"signal::startup", vn_login_on_startup, obj - ,"signal::activate", vn_login_on_activate, obj + self->app = g_value_dup_object (value); + g_object_connect (self->app + ,"signal::startup", vn_login_on_startup, self + ,"signal::activate", vn_login_on_activate, self ,NULL ); - obj->settings = g_settings_new ( - g_application_get_application_id (G_APPLICATION (obj->app))); + self->settings = g_settings_new ( + g_application_get_application_id (G_APPLICATION (self->app))); break; } default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec); + G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec); } } -static void vn_login_get_property (VnLogin * obj, guint id, +static void vn_login_get_property (VnLogin * self, guint id, GValue * value, GParamSpec * pspec) { switch (id) { default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec); + G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec); } } //+++++++++++++++++++++++++++++++++++++++++++++++++++ Class -static void vn_login_init (VnLogin * obj) +static void vn_login_init (VnLogin * self) { - obj->gui = NULL; - obj->window = NULL; - obj->settings = NULL; - obj->app = NULL; + self->gui = NULL; + self->window = NULL; + self->settings = NULL; + self->app = NULL; + self->cfg_conn = NULL; } -static void vn_login_finalize (VnLogin * obj) +static void vn_login_finalize (VnLogin * self) { - vn_login_free_gui (obj); - g_clear_object (&obj->settings); - - g_object_disconnect (obj->app - ,"any_signal", vn_login_on_startup, obj - ,"any_signal", vn_login_on_activate, obj + vn_login_free_gui (self); + g_object_disconnect (self->app + ,"any_signal", vn_login_on_startup, self + ,"any_signal", vn_login_on_activate, self ,NULL ); - g_clear_object (&obj->app); - G_OBJECT_CLASS (vn_login_parent_class)->finalize (G_OBJECT (obj)); + g_clear_object (&self->cfg_conn); + g_clear_object (&self->settings); + g_clear_object (&self->app); + + G_OBJECT_CLASS (vn_login_parent_class)->finalize (G_OBJECT (self)); } static void vn_login_class_init (VnLoginClass * k) diff --git a/vn/vn-login.h b/vn/vn-login.h index af1b474..5599550 100644 --- a/vn/vn-login.h +++ b/vn/vn-login.h @@ -24,11 +24,11 @@ #define VN_LOGIN_LOG_DOMAIN (g_quark_from_string ("VnLogin")) #define VN_TYPE_LOGIN (vn_login_get_type ()) -#define VN_LOGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, VN_TYPE_LOGIN, VnLogin)) -#define VN_IS_LOGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, VN_TYPE_LOGIN)) +#define VN_LOGIN(self) (G_TYPE_CHECK_INSTANCE_CAST (self, VN_TYPE_LOGIN, VnLogin)) +#define VN_IS_LOGIN(self) (G_TYPE_CHECK_INSTANCE_TYPE (self, VN_TYPE_LOGIN)) #define VN_LOGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST (klass, VN_TYPE_LOGIN, VnLoginClass)) #define VN_IS_LOGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE (klass, VN_TYPE_LOGIN)) -#define VN_LOGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS (obj, VN_TYPE_LOGIN, VnLoginClass)) +#define VN_LOGIN_GET_CLASS(self) (G_TYPE_INSTANCE_GET_CLASS (self, VN_TYPE_LOGIN, VnLoginClass)) typedef struct _VnLogin VnLogin; typedef struct _VnLoginClass VnLoginClass; @@ -49,6 +49,7 @@ struct _VnLogin GtkWidget * stop; GtkButton * settings_button; GtkWidget * spinner; + DbConn * cfg_conn; GtkDialog * settings_dialog; GtkEntry * plugin;