static void dsn_name_changed_cb (GtkEntry *entry, GdauiDsnAssistant *assistant) { const gchar *name; gboolean page_complete = TRUE; GdaDsnInfo *dsn_info; /* check required fields have values */ name = gtk_entry_get_text (GTK_ENTRY (assistant->priv->general_name)); if (!name || strlen (name) < 1) { gtk_widget_grab_focus (assistant->priv->general_name); page_complete = FALSE; } dsn_info = gda_config_get_dsn_info (name); if (dsn_info) { gint i = 2; gchar *str = NULL; do { g_free (str); str = g_strdup_printf ("%s_%d", name, i); dsn_info = gda_config_get_dsn_info (str); } while (dsn_info); gtk_entry_set_text (entry, str); g_free (str); /*gtk_widget_grab_focus (assistant->priv->general_name);*/ } gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), assistant->priv->general_page, page_complete); }
/** * gdaui_login_set_connection_information: * @login: a #GdauiLogin object * @cinfo: a pointer to a structure representing the information to display. * * Changes the information displayed in @login, to represent @cinfo. * If @login's mode has %GDA_UI_LOGIN_HIDE_DIRECT_CONNECTION_MODE, then * if @cinfo->name is not %NULL it is displayed in the DSN selector, otherwise * a warning is shown and the result * is the same as having passed %NULL for the @cinfo argument. * * In any case @login's mode (set by gdaui_login_set_mode()) is not changed. */ void gdaui_login_set_connection_information (GdauiLogin *login, const GdaDsnInfo *cinfo) { g_return_if_fail (GDAUI_IS_LOGIN (login)); GdauiLoginPrivate *priv = gdaui_login_get_instance_private (login); if (!cinfo) { _gdaui_dsn_selector_set_dsn (GDAUI_DSN_SELECTOR (priv->dsn_selector), NULL); } else { GdaDsnInfo *info = NULL; if (cinfo->name) info = gda_config_get_dsn_info (cinfo->name); if (info) _gdaui_dsn_selector_set_dsn (GDAUI_DSN_SELECTOR (priv->dsn_selector), cinfo->name); else { /* force switch to */ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->rb_dsn), FALSE); } g_signal_handlers_block_by_func (priv->prov_selector, G_CALLBACK (prov_entry_changed_cb), login); gdaui_provider_selector_set_provider (GDAUI_PROVIDER_SELECTOR (priv->prov_selector), cinfo->provider); g_signal_handlers_unblock_by_func (priv->prov_selector, G_CALLBACK (prov_entry_changed_cb), login); _gdaui_provider_spec_editor_set_provider (GDAUI_PROVIDER_SPEC_EDITOR (priv->cnc_params_editor), cinfo->provider); _gdaui_provider_spec_editor_set_specs (GDAUI_PROVIDER_SPEC_EDITOR (priv->cnc_params_editor), cinfo->cnc_string); _gdaui_provider_auth_editor_set_provider (GDAUI_PROVIDER_AUTH_EDITOR (priv->auth_widget), cinfo->provider); _gdaui_provider_auth_editor_set_auth (GDAUI_PROVIDER_AUTH_EDITOR (priv->auth_widget), cinfo->auth_string); } }
/** * gdaui_dsn_editor_has_been_changed: * @config: a #GdauiDsnEditor * * Tells if the user has some changes to the DSN being edited, which can be comitted * * Returns: %TRUE if some changes can be comitted */ gboolean gdaui_dsn_editor_has_been_changed (GdauiDsnEditor *config) { g_return_val_if_fail (GDAUI_IS_DSN_EDITOR (config), FALSE); GdaDsnInfo *orig; orig = gda_config_get_dsn_info (config->priv->name); return ! gda_dsn_info_equal (gdaui_dsn_editor_get_dsn (config), orig); }
static void dsn_reset_cb (G_GNUC_UNUSED GSimpleAction *action, GVariant *state, gpointer data) { GdauiDsnEditor *editor; editor = GDAUI_DSN_EDITOR (data); GdaDsnInfo *orig; orig = gda_config_get_dsn_info (editor->priv->name); gdaui_dsn_editor_set_dsn (editor, orig); }
/** * gdaui_login_set_dsn: * @login: a #GdauiLogin object * @dsn: (nullable): a data source name, or %NULL * * Changes the information displayed in @login, to represent @dsn. * If @login's mode has %GDA_UI_LOGIN_HIDE_DSN_SELECTION_MODE, then * the DSN information is extracted and displayed in the direct login area. * * If @dsn is not a declared data source name, then a warning is shown and the result * is the same as having passed %NULL for the @dsn argument. * * In any case @login's mode (set by gdaui_login_set_mode()) is not changed. */ void gdaui_login_set_dsn (GdauiLogin *login, const gchar *dsn) { GdaDsnInfo *cinfo; g_return_if_fail (GDAUI_IS_LOGIN (login)); cinfo = gda_config_get_dsn_info (dsn); if (!cinfo) g_warning (_("Unknown DSN '%s'"), dsn); gdaui_login_set_connection_information (login, cinfo); }
static void copy_dlg_entry_changed_cb (GtkEntry *entry, GtkDialog *dlg) { gboolean can_create = FALSE; const gchar *name; name = gtk_entry_get_text (entry); if (name && *name && !gda_config_get_dsn_info (name)) can_create = TRUE; GtkWidget *resp; resp = gtk_dialog_get_widget_for_response (dlg, GTK_RESPONSE_ACCEPT); gtk_widget_set_sensitive (resp, can_create); }
static void save_cb (GtkButton *button, GtkWidget *dsn) { DsnConfigPrivate *priv; priv = g_object_get_data (G_OBJECT (dsn), DSN_CONFIG_DATA); const GdaDsnInfo *newdsn; newdsn = gdaui_dsn_editor_get_dsn (priv->dsn_editor); GError *error = NULL; if (! gda_config_define_dsn (newdsn, &error)) { _gdaui_utility_show_error (NULL, _("Could not save DSN definition: %s"), error ? error->message : _("No detail")); g_clear_error (&error); } /* update reference DSN and UI */ GdaDsnInfo *dsn_info; dsn_info = gda_config_get_dsn_info (newdsn->name); gdaui_dsn_editor_set_dsn (priv->dsn_editor, dsn_info); }
/** * gdaui_login_get_connection_information: * @login: a #GdauiLogin object * * Get the information specified in @login as a pointer to a (read-only) #GdaDsnInfo. * If the connection is not specified by a DSN, then the 'name' attribute of the returned * #GdaDsnInfo will be %NULL, and otherwise it will contain the name of the selected DSN. * * Returns: (transfer none): a pointer to a (read-only) #GdaDsnInfo. * * Since: 4.2 */ const GdaDsnInfo * gdaui_login_get_connection_information (GdauiLogin *login) { g_return_val_if_fail (GDAUI_IS_LOGIN (login), NULL); GdauiLoginPrivate *priv = gdaui_login_get_instance_private (login); clear_dsn_info (login); if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->rb_dsn))) { GdaDsnInfo *info = NULL; gchar *dsn; dsn = _gdaui_dsn_selector_get_dsn (GDAUI_DSN_SELECTOR (priv->dsn_selector)); if (dsn && *dsn) info = gda_config_get_dsn_info (dsn); g_free (dsn); if (info) { priv->dsn_info.name = g_strdup (info->name); if (info->provider) priv->dsn_info.provider = g_strdup (info->provider); if (info->description) priv->dsn_info.description = g_strdup (info->description); if (info->cnc_string) priv->dsn_info.cnc_string = g_strdup (info->cnc_string); priv->dsn_info.is_system = info->is_system; } priv->dsn_info.auth_string = _gdaui_provider_auth_editor_get_auth (GDAUI_PROVIDER_AUTH_EDITOR (priv->auth_widget)); } else { const gchar *str; str = gdaui_provider_selector_get_provider (GDAUI_PROVIDER_SELECTOR (priv->prov_selector)); if (str) priv->dsn_info.provider = g_strdup (str); priv->dsn_info.cnc_string = _gdaui_provider_spec_editor_get_specs (GDAUI_PROVIDER_SPEC_EDITOR (priv->cnc_params_editor)); priv->dsn_info.auth_string = _gdaui_provider_auth_editor_get_auth (GDAUI_PROVIDER_AUTH_EDITOR (priv->auth_widget)); } return &(priv->dsn_info); }
static void dsn_entry_changed_cb (GdauiDsnSelector *sel, GdauiLogin *login) { GdauiLoginPrivate *priv = gdaui_login_get_instance_private (login); gchar *dsn; GdaDsnInfo *info = NULL; dsn = _gdaui_dsn_selector_get_dsn (sel); if (dsn) { info = gda_config_get_dsn_info (dsn); g_free (dsn); } /* update prov_selector */ g_signal_handlers_block_by_func (priv->prov_selector, G_CALLBACK (prov_entry_changed_cb), login); gdaui_provider_selector_set_provider (GDAUI_PROVIDER_SELECTOR (priv->prov_selector), info ? info->provider : NULL); g_signal_handlers_unblock_by_func (priv->prov_selector, G_CALLBACK (prov_entry_changed_cb), login); /* update auth editor */ _gdaui_provider_auth_editor_set_provider (GDAUI_PROVIDER_AUTH_EDITOR (priv->auth_widget), info ? info->provider : NULL); _gdaui_provider_auth_editor_set_auth (GDAUI_PROVIDER_AUTH_EDITOR (priv->auth_widget), info && info->auth_string ? info->auth_string : NULL); /* update spec editor */ _gdaui_provider_spec_editor_set_provider (GDAUI_PROVIDER_SPEC_EDITOR (priv->cnc_params_editor), info ? info->provider : NULL); _gdaui_provider_spec_editor_set_specs (GDAUI_PROVIDER_SPEC_EDITOR (priv->cnc_params_editor), info ? info->cnc_string : NULL); if (priv->auth_widget) gtk_widget_grab_focus (priv->auth_widget); g_signal_emit (login, gdaui_login_signals [CHANGED], 0, settings_are_valid (login)); }
static void dsn_copy_cb (G_GNUC_UNUSED GSimpleAction *action, GVariant *state, gpointer data) { GdauiDsnEditor *editor; editor = GDAUI_DSN_EDITOR (data); GtkWindow *parent = NULL; parent = (GtkWindow*) gtk_widget_get_ancestor (GTK_WIDGET (editor), GTK_TYPE_WINDOW); GtkWidget *dialog = NULL; dialog = gtk_dialog_new_with_buttons (_("Data source copy"), parent, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, _("Create data source"), GTK_RESPONSE_ACCEPT, _("_Cancel"), GTK_RESPONSE_REJECT, NULL); gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_REJECT); GtkWidget *grid, *dlg_box, *label, *entry; dlg_box = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); grid = gtk_grid_new (); gtk_box_pack_start (GTK_BOX (dlg_box), grid, TRUE, TRUE, 6); gtk_container_set_border_width (GTK_CONTAINER (grid), 0); gtk_grid_set_row_spacing (GTK_GRID (grid), 6); gtk_grid_set_column_spacing (GTK_GRID (grid), 5); gchar *str; str = g_strdup_printf (_("Define the named of the new data source which will be created as a copy " "of '%s':"), editor->priv->name); label = gtk_label_new (str); g_free (str); gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 2, 1); str = _gdaui_utility_markup_title (_("Data source name"), FALSE); label = gtk_label_new (""); gtk_label_set_markup (GTK_LABEL (label), str); g_free (str); gtk_widget_set_halign (label, GTK_ALIGN_START); gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1); entry = gtk_entry_new (); guint i; for (i = 1; ; i++) { str = g_strdup_printf ("%s_%u", editor->priv->name, i); if (! gda_config_get_dsn_info (str)) { gtk_entry_set_text (GTK_ENTRY (entry), str); g_free (str); break; } g_free (str); } gtk_editable_set_editable (GTK_EDITABLE (entry), TRUE); g_signal_connect (entry, "changed", G_CALLBACK (copy_dlg_entry_changed_cb), dialog); gtk_widget_set_hexpand (entry, TRUE); gtk_grid_attach (GTK_GRID (grid), entry, 1, 1, 1, 1); gtk_widget_show_all (grid); if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { const gchar *name; name = gtk_entry_get_text (GTK_ENTRY (entry)); GdaDsnInfo *dsn_info = NULL; GError *error = NULL; dsn_info = gda_config_get_dsn_info (editor->priv->name); if (dsn_info) { dsn_info = gda_dsn_info_copy (dsn_info); g_free (dsn_info->name); dsn_info->name = g_strdup (name); gda_config_define_dsn (dsn_info, &error); gda_dsn_info_free (dsn_info); } else g_set_error (&error, GDA_CONFIG_ERROR, GDA_CONFIG_DSN_NOT_FOUND_ERROR, ("DSN '%s' does not exist anymore"), editor->priv->name); if (error) { _gdaui_utility_show_error (GTK_WINDOW (dialog), _("Could not create data source: %s"), error->message); g_clear_error (&error); } } gtk_widget_destroy (dialog); }
static void list_selection_changed_cb (GdauiRawGrid *grid, gpointer user_data) { DsnConfigPrivate *priv; GdaDataModel *model; GArray *selection; gchar *str; const GValue *cvalue; GtkWidget *win = gtk_widget_get_toplevel (GTK_WIDGET (grid)); if (gtk_widget_is_toplevel (win)) { g_simple_action_set_enabled (G_SIMPLE_ACTION (g_action_map_lookup_action (G_ACTION_MAP (win), "DatasourceDelete")), FALSE); } priv = g_object_get_data (G_OBJECT (user_data), DSN_CONFIG_DATA); selection = gdaui_data_selector_get_selected_rows (GDAUI_DATA_SELECTOR (priv->dsn_list)); if (!selection) { gtk_stack_set_visible_child_name (GTK_STACK (priv->stack), ST_NOPROP); return; } model = gdaui_data_selector_get_model (GDAUI_DATA_SELECTOR (priv->dsn_list)); if (!GDA_IS_DATA_MODEL (model)) { g_array_free (selection, TRUE); gtk_stack_set_visible_child_name (GTK_STACK (priv->stack), ST_NOPROP); return; } cvalue = gda_data_model_get_value_at (model, 0, g_array_index (selection, gint, 0), NULL); g_array_free (selection, TRUE); if (!cvalue) { gtk_stack_set_visible_child_name (GTK_STACK (priv->stack), ST_NOPROP); return; } str = gda_value_stringify ((GValue *) cvalue); g_print ("==> %s\n", str); GdaDsnInfo *dsn_info; dsn_info = gda_config_get_dsn_info (str); g_free (str); if (!dsn_info) { /* something went wrong here... */ gtk_stack_set_visible_child_name (GTK_STACK (priv->stack), ST_NOPROP); return; } gdaui_dsn_editor_set_dsn (priv->dsn_editor, dsn_info); if (gdaui_dsn_editor_need_authentication (priv->dsn_editor)) gtk_widget_show (GTK_WIDGET (priv->view_buttons [GDAUI_DSN_EDITOR_PANE_AUTH])); else gtk_widget_hide (GTK_WIDGET (priv->view_buttons [GDAUI_DSN_EDITOR_PANE_AUTH])); gtk_toggle_button_set_active (priv->view_buttons [GDAUI_DSN_EDITOR_PANE_DEFINITION], TRUE); gtk_stack_set_visible_child_name (GTK_STACK (priv->stack), ST_PROP); if (gtk_widget_is_toplevel (win)) { g_simple_action_set_enabled (G_SIMPLE_ACTION (g_action_map_lookup_action (G_ACTION_MAP (win), "DatasourceDelete")), TRUE); } }
/* * 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; }
int main (int argc, char **argv) { GError *error = NULL; GOptionContext *context; GdaConnection *cnc; gchar *auth_string = NULL; gchar *blob_data; /* command line parsing */ context = g_option_context_new ("Tests opening a connection"); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_warning ("Can't parse arguments: %s", error->message); exit (1); } g_option_context_free (context); if (direct && dsn) { g_print ("DSN and connection string are exclusive\n"); exit (1); } if (!direct && !dsn) { g_print ("You must specify a connection to open either as a DSN or a connection string\n"); exit (1); } if (direct && !prov) { g_print ("You must specify a provider when using a connection string\n"); exit (1); } gda_init (); /* open connection */ if (user) { if (pass) auth_string = g_strdup_printf ("USERNAME=%s;PASSWORD=%s", user, pass); else auth_string = g_strdup_printf ("USERNAME=%s", user); } if (dsn) { GdaDsnInfo *info = NULL; info = gda_config_get_dsn_info (dsn); if (!info) g_error (_("DSN '%s' is not declared"), dsn); else { cnc = gda_connection_open_from_dsn (info->name, auth_string ? auth_string : info->auth_string, 0, &error); if (!cnc) { g_warning (_("Can't open connection to DSN %s: %s\n"), info->name, error && error->message ? error->message : "???"); exit (1); } prov = info->provider; } } else { cnc = gda_connection_open_from_string (prov, direct, auth_string, 0, &error); if (!cnc) { g_warning (_("Can't open specified connection: %s\n"), error && error->message ? error->message : "???"); exit (1); } } g_free (auth_string); g_print (_("Connection successfully opened!\n")); parser = gda_connection_create_parser (cnc); gda_connection_begin_transaction (cnc, NULL, GDA_TRANSACTION_ISOLATION_UNKNOWN, NULL); /* * clear all blobs */ if (!clear_blobs (cnc, &error)) g_error ("Blobs clear error: %s", error && error->message ? error->message : "No detail"); /* insert a blob */ blob_data = "Blob Data 1"; if (!insert_blob (cnc, 1, blob_data, strlen (blob_data), &error)) g_error ("Blob insert error: %s", error && error->message ? error->message : "No detail"); else if (error) { g_print ("Msg: %s\n", error->message); g_error_free (error); error = NULL; } /* insert a blob */ blob_data = "Blob Data 2"; if (!insert_blob (cnc, 2, blob_data, strlen (blob_data), &error)) g_error ("Blob insert error: %s", error && error->message ? error->message : "No detail"); else if (error) { g_print ("Msg: %s\n", error->message); g_error_free (error); error = NULL; } if (!display_blobs (cnc, &error)) g_error ("Blobs display error: %s", error && error->message ? error->message : "No detail"); /* update blob */ blob_data = "New blob 1 contents is now this one..."; if (!update_blob (cnc, 1, blob_data, strlen (blob_data), &error)) g_error ("Blob update error: %s", error && error->message ? error->message : "No detail"); else if (error) { g_print ("Msg: %s\n", error->message); g_error_free (error); error = NULL; } if (!display_blobs (cnc, &error)) g_error ("Blobs display error: %s", error && error->message ? error->message : "No detail"); /* update blob */ blob_data = "After several blobs updated"; if (!update_multiple_blobs (cnc, blob_data, strlen (blob_data), &error)) g_error ("Multiple blob update error: %s", error && error->message ? error->message : "No detail"); else if (error) { g_print ("Msg: %s\n", error->message); g_error_free (error); error = NULL; } if (!display_blobs (cnc, &error)) g_error ("Blobs display error: %s", error && error->message ? error->message : "No detail"); /* SQL Postgres: create table blobs (id serial not null primary key, name varchar (50), data oid); SQL Oracle: CREATE TABLE blobs (id number primary key, name varchar2 (50), data BLOB); */ gda_connection_commit_transaction (cnc, NULL, NULL); if (! gda_connection_close (cnc, &error)) g_error ("Can't close connection: %s", error && error->message ? error->message : "No detail"); return 0; }