/** * Adds a named parameter to the given libgda connection string. @param cnc libgda connection string @param name parameter name @param value parameter value @param def default value (used if given value is NULL) */ static void cnc_add_part( GString *cnc, const gchar *name, const gchar *value, const gchar *def) { g_assert(cnc != NULL); g_assert(name != NULL); if (value == NULL) value = def; if (*value) { gchar *tmp; /* Add a separating semicolon if there already are parameters before this one. */ if (cnc->len > 0) { g_string_append_c(cnc, ';'); } tmp = gda_rfc1738_encode (name); g_string_append(cnc, tmp); g_free (tmp); g_string_append_c(cnc, '='); tmp = gda_rfc1738_encode (value); g_string_append(cnc, tmp); g_free (tmp); } }
/* * compute cache file's absolute name */ static gchar * compute_data_file_name (GdaQuarkList *params, gboolean is_cache, const gchar *data_type) { /* real cache file name */ GString *string; gchar *cfile, *evalue; const gchar *base_dn; const gchar *host; const gchar *require_ssl; const gchar *port; gint rport; gboolean use_ssl; base_dn = gda_quark_list_find (params, "DB_NAME"); host = gda_quark_list_find (params, "HOST"); if (!host) host = "127.0.0.1"; port = gda_quark_list_find (params, "PORT"); require_ssl = gda_quark_list_find (params, "USE_SSL"); use_ssl = (require_ssl && ((*require_ssl == 't') || (*require_ssl == 'T'))) ? TRUE : FALSE; if (port && *port) rport = atoi (port); else { if (use_ssl) rport = LDAPS_PORT; else rport = LDAP_PORT; } string = g_string_new (""); evalue = gda_rfc1738_encode (host); g_string_append_printf (string, ",=%s", evalue); g_free (evalue); g_string_append_printf (string, ";PORT=%d", rport); if (base_dn) { evalue = gda_rfc1738_encode (base_dn); g_string_append_printf (string, ";BASE_DN,=%s", evalue); g_free (evalue); } evalue = g_compute_checksum_for_string (G_CHECKSUM_SHA1, string->str, -1); g_string_free (string, TRUE); if (is_cache) cfile = g_strdup_printf ("%s_%s", evalue, data_type); else cfile = g_strdup_printf ("ldap-%s.%s", evalue, data_type); g_free (evalue); gchar *fname; if (is_cache) fname = g_build_path (G_DIR_SEPARATOR_S, g_get_user_cache_dir (), "libgda", "ldap", cfile, NULL); else fname = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (), "libgda", cfile, NULL); g_free (cfile); return fname; }
static gchar * params_to_string (GdauiProviderAuthEditor *auth) { GString *string = NULL; gchar *str; GdaSet *dset; GSList *list; g_assert (auth->priv->auth_widget); if (! GDAUI_IS_BASIC_FORM (auth->priv->auth_widget)) return NULL; dset = gdaui_basic_form_get_data_set (GDAUI_BASIC_FORM (auth->priv->auth_widget)); for (list = dset->holders; list; list = list->next) { GdaHolder *param = GDA_HOLDER (list->data); if (gda_holder_is_valid (param)) { const GValue *value; value = gda_holder_get_value (param); str = NULL; if (value && !gda_value_is_null ((GValue *) value)) { GdaDataHandler *dh; GType dtype; dtype = gda_holder_get_g_type (param); dh = gda_data_handler_get_default (dtype); str = gda_data_handler_get_str_from_value (dh, value); } if (str && *str) { gchar *name; gchar *ename, *evalue; if (!string) string = g_string_new (""); else g_string_append_c (string, ';'); g_object_get (G_OBJECT (list->data), "id", &name, NULL); ename = gda_rfc1738_encode (name); evalue = gda_rfc1738_encode (str); g_string_append_printf (string, "%s=%s", ename, evalue); g_free (ename); g_free (evalue); } g_free (str); } } str = string ? string->str : NULL; if (string) g_string_free (string, FALSE); return str; }
/* * Creates an SQLite .db file from the definitions in @sqlfile */ gboolean create_sqlite_db (const gchar *dir, const gchar *dbname, const gchar *sqlfile, GError **error) { GdaBatch *batch; GdaSqlParser *parser; GdaServerProvider *prov; GdaConnection *cnc; /* create batch */ prov = gda_config_get_provider ("SQLite", NULL); if (!prov) { g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC, "%s", "Cannot find the SQLite provider"); return FALSE; } parser = gda_server_provider_create_parser (prov, NULL); if (!parser) parser = gda_sql_parser_new (); batch = gda_sql_parser_parse_file_as_batch (parser, sqlfile, error); g_object_unref (parser); if (!batch) return FALSE; /* clean any previous DB file */ gchar *fname, *tmp; tmp = g_strdup_printf ("%s.db", dbname); fname = g_build_filename (dir, tmp, NULL); g_free (tmp); g_unlink (fname); g_free (fname); /* open a connection */ gchar *cnc_string; gchar *edir, *edbname; edir = gda_rfc1738_encode (dir); edbname = gda_rfc1738_encode (dbname); cnc_string = g_strdup_printf ("DB_DIR=%s;DB_NAME=%s", edir, edbname); g_free (edir); g_free (edbname); cnc = gda_connection_open_from_string ("SQLite", cnc_string, NULL, GDA_CONNECTION_OPTIONS_NONE, error); g_free (cnc_string); if (!cnc) { g_object_unref (batch); return FALSE; } /* execute batch */ GSList *list; const GSList *stmt_list; gboolean retval = TRUE; list = gda_connection_batch_execute (cnc, batch, NULL, GDA_STATEMENT_MODEL_RANDOM_ACCESS, error); stmt_list = gda_batch_get_statements (batch); if (g_slist_length (list) != g_slist_length ((GSList *) stmt_list)) retval = FALSE; g_slist_foreach (list, (GFunc) g_object_unref, NULL); g_slist_free (list); g_assert (gda_connection_close (cnc, NULL)); g_object_unref (cnc); g_object_unref (batch); return retval; }
static void set_preview_widget (TablePreferences *tpref) { GtkWidget *preview = NULL; GtkTreeIter iter; if (!tpref->priv->current_column) return; if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (tpref->priv->plugins_combo), &iter)) { GdauiPlugin *plugin; GtkTreeModel *model; GType gtype; gtype = tpref->priv->current_column->gtype; model = tpref->priv->plugins_model; gtk_tree_model_get (model, &iter, PL_COLUMN_PLUGIN, &plugin, -1); if (plugin) { GString *string = NULL; if (tpref->priv->options_wid) { GdaSet *plist; GSList *list; plist = gdaui_basic_form_get_data_set (GDAUI_BASIC_FORM (tpref->priv->options_wid)); for (list = plist->holders; list; list = list->next) { GdaHolder *holder; holder = GDA_HOLDER (list->data); if (gda_holder_is_valid (holder)) { const GValue *cvalue; cvalue = gda_holder_get_value (holder); if (cvalue && (G_VALUE_TYPE (cvalue) != GDA_TYPE_NULL)) { gchar *str = gda_value_stringify (cvalue); gchar *r1, *r2; if (!string) string = g_string_new (""); else g_string_append_c (string, ';'); r1 = gda_rfc1738_encode (gda_holder_get_id (holder)); r2 = gda_rfc1738_encode (str); g_free (str); g_string_append_printf (string, "%s=%s", r1, r2); g_free (r1); g_free (r2); } } } } if (string) { g_string_prepend_c (string, ':'); g_string_prepend (string, plugin->plugin_name); preview = GTK_WIDGET (gdaui_new_data_entry (gtype, string->str)); g_string_free (string, TRUE); } else preview = GTK_WIDGET (gdaui_new_data_entry (gtype, plugin->plugin_name)); } else preview = GTK_WIDGET (gdaui_new_data_entry (gtype, NULL)); } GValue *prev_value = NULL; if (tpref->priv->preview_wid) { prev_value = gdaui_data_entry_get_value (GDAUI_DATA_ENTRY (tpref->priv->preview_wid)); gtk_widget_destroy (tpref->priv->preview_wid); gtk_widget_show (tpref->priv->preview_none); tpref->priv->preview_wid = NULL; } if (preview) { if (prev_value && (G_VALUE_TYPE (prev_value) == gdaui_data_entry_get_value_type (GDAUI_DATA_ENTRY (preview)))) gdaui_data_entry_set_value (GDAUI_DATA_ENTRY (preview), prev_value); gdaui_data_entry_set_attributes (GDAUI_DATA_ENTRY (preview), 0, GDA_VALUE_ATTR_ACTIONS_SHOWN); tpref->priv->preview_wid = preview; gtk_box_pack_start (GTK_BOX (tpref->priv->preview_vbox), preview, TRUE, TRUE, 0); gtk_widget_hide (tpref->priv->preview_none); gtk_widget_show (tpref->priv->preview_wid); } if (prev_value) gda_value_free (prev_value); }
static void options_form_param_changed_cb (G_GNUC_UNUSED GdauiBasicForm *form, G_GNUC_UNUSED GdaHolder *param, G_GNUC_UNUSED gboolean is_user_modif, TablePreferences *tpref) { GtkTreeIter iter; if (tpref->priv->save_plugin_changes && gtk_combo_box_get_active_iter (GTK_COMBO_BOX (tpref->priv->plugins_combo), &iter)) { GdauiPlugin *plugin; GError *error = NULL; GString *plugin_all = NULL; gtk_tree_model_get (tpref->priv->plugins_model, &iter, PL_COLUMN_PLUGIN, &plugin, -1); if (plugin) { plugin_all = g_string_new (plugin->plugin_name); if (tpref->priv->options_wid) { GdaSet *plist; GSList *list; gboolean first = TRUE; plist = gdaui_basic_form_get_data_set (GDAUI_BASIC_FORM (tpref->priv->options_wid)); for (list = plist->holders; list; list = list->next) { GdaHolder *holder; const GValue *cvalue; gchar *str, *r1, *r2; holder = GDA_HOLDER (list->data); if (! gda_holder_is_valid (holder)) continue; cvalue = gda_holder_get_value (holder); if (G_VALUE_TYPE (cvalue) == GDA_TYPE_NULL) continue; if (first) { g_string_append_c (plugin_all, ':'); first = FALSE; } else g_string_append_c (plugin_all, ';'); str = gda_value_stringify (cvalue); r1 = gda_rfc1738_encode (str); g_free (str); r2 = gda_rfc1738_encode (gda_holder_get_id (holder)); g_string_append_printf (plugin_all, "%s=%s", r2, r1); g_free (r1); g_free (r2); } } } g_signal_handlers_block_by_func (tpref->priv->columns_store, G_CALLBACK (columns_model_row_changed_cb), tpref); if (tpref->priv->current_table && tpref->priv->current_column && ! browser_connection_set_table_column_attribute (tpref->priv->bcnc, tpref->priv->current_table, tpref->priv->current_column, BROWSER_CONNECTION_COLUMN_PLUGIN, plugin_all ? plugin_all->str : NULL, &error)) { TO_IMPLEMENT; /* FIXME: add a notice somewhere in the UI */ g_warning ("Error: %s\n", error && error->message ? error->message : _("No detail")); g_clear_error (&error); } g_signal_handlers_unblock_by_func (tpref->priv->columns_store, G_CALLBACK (columns_model_row_changed_cb), tpref); if (plugin_all) g_string_free (plugin_all, TRUE); } set_preview_widget (tpref); }
/* * Open a connection */ static GdaConnection* open_connection (const gchar *cnc_string, GError **error) { GdaConnection *cnc = NULL; GdaDsnInfo *info; gchar *user, *pass, *real_cnc, *real_provider, *real_auth_string = NULL; gda_connection_string_split (cnc_string, &real_cnc, &real_provider, &user, &pass); if (!real_cnc) { g_free (user); g_free (pass); g_free (real_provider); g_set_error (error, GDA_CONNECTION_ERROR, GDA_CONNECTION_DSN_NOT_FOUND_ERROR, "Malformed connection string '%s'", cnc_string); return NULL; } if (ask_pass) { if (user && !*user) { gchar buf[80]; g_print ("\tUsername for '%s': ", cnc_string); if (scanf ("%80s", buf) == -1) { g_free (real_cnc); g_free (user); g_free (pass); g_free (real_provider); g_set_error (error, GDA_CONNECTION_ERROR, GDA_CONNECTION_DSN_NOT_FOUND_ERROR, "No username for '%s'", cnc_string); return NULL; } g_free (user); user = g_strdup (buf); } if (pass && !*pass) { gchar buf[80]; g_print ("\tPassword for '%s': ", cnc_string); if (scanf ("%80s", buf) == -1) { g_free (real_cnc); g_free (user); g_free (pass); g_free (real_provider); g_set_error (error, GDA_CONNECTION_ERROR, GDA_CONNECTION_DSN_NOT_FOUND_ERROR, "No password for '%s'", cnc_string); return NULL; } g_free (pass); pass = g_strdup (buf); } if (user || pass) { gchar *s1; s1 = gda_rfc1738_encode (user); if (pass) { gchar *s2; s2 = gda_rfc1738_encode (pass); real_auth_string = g_strdup_printf ("USERNAME=%s;PASSWORD=%s", s1, s2); g_free (s2); } else real_auth_string = g_strdup_printf ("USERNAME=%s", s1); g_free (s1); } } info = gda_config_get_dsn_info (real_cnc); if (info && !real_provider) cnc = gda_connection_open_from_dsn (cnc_string, real_auth_string, 0, error); else cnc = gda_connection_open_from_string (NULL, cnc_string, real_auth_string, 0, error); g_free (real_cnc); g_free (user); g_free (pass); g_free (real_provider); g_free (real_auth_string); return cnc; }