[libgda] Gda-SQL and programs without a main loop can not fetch authentification from Gnome keyring



commit c47b2af6a7c3facb8a619e1ccdab9cd74d0b597a
Author: Vivien Malerba <malerba gnome-db org>
Date:   Wed Jul 14 17:00:41 2010 +0200

    Gda-SQL and programs without a main loop can not fetch authentification from Gnome keyring

 doc/C/libgda-4.0-docs.sgml |    5 +++
 libgda/gda-config.c        |   83 ++++++++++++++++++++++++++++++++++----------
 tools/gda-sql.c            |   16 ++++++--
 3 files changed, 81 insertions(+), 23 deletions(-)
---
diff --git a/doc/C/libgda-4.0-docs.sgml b/doc/C/libgda-4.0-docs.sgml
index c463542..49d6e5f 100644
--- a/doc/C/libgda-4.0-docs.sgml
+++ b/doc/C/libgda-4.0-docs.sgml
@@ -597,6 +597,11 @@
 		<link linkend="gda-data-model-dump">gda_data_model_dump()</link> will also include a description of
 	      the data model's attributes</para>
 	    </listitem>
+	  <listitem>
+              <para>GDA_CONFIG_SYNCHRONOUS: if set, and if the Gnome Keyring is used to store DSN authentification information,
+		then the requests to the Gnome Keyring will be made synchronously. This is usefull if Libgda is used in a program where
+		there is no main loop (otherwise no authentification will be fetched).</para>
+            </listitem>
 	  </itemizedlist>
 	</para>
       </sect1>
diff --git a/libgda/gda-config.c b/libgda/gda-config.c
index 9996736..b5c8652 100644
--- a/libgda/gda-config.c
+++ b/libgda/gda-config.c
@@ -80,6 +80,9 @@ static void gda_config_get_property (GObject *object,
 				     GValue *value,
 				     GParamSpec *pspec);
 static GdaConfig *unique_instance = NULL;
+#ifdef HAVE_GNOME_KEYRING
+static gboolean sync_keyring = FALSE;
+#endif
 
 static gint data_source_info_compare (GdaDsnInfo *infoa, GdaDsnInfo *infob);
 static void data_source_info_free (GdaDsnInfo *info);
@@ -241,7 +244,7 @@ gda_config_init (GdaConfig *conf, GdaConfigClass *klass)
 
 #ifdef HAVE_GNOME_KEYRING
 static void
-password_found (GnomeKeyringResult res, const gchar *password, const gchar *dsnname)
+password_found_cb (GnomeKeyringResult res, const gchar *password, const gchar *dsnname)
 {
         if (res == GNOME_KEYRING_RESULT_OK) {
 		GdaDsnInfo *dsn;
@@ -257,6 +260,9 @@ password_found (GnomeKeyringResult res, const gchar *password, const gchar *dsnn
 		if (unique_instance->priv->emit_signals)
 			g_signal_emit (unique_instance, gda_config_signals [DSN_CHANGED], 0, dsn);
 	}
+	else if (res != GNOME_KEYRING_RESULT_NO_MATCH)
+		g_warning (_("Error loading authentification information for '%s' DSN: %s"),
+			     dsnname, gnome_keyring_result_to_message (res));
 }
 #endif
 
@@ -363,13 +369,30 @@ load_config_file (const gchar *file, gboolean is_system)
 
 #ifdef HAVE_GNOME_KEYRING
 			if (! is_system) {
-				gchar *tmp = g_strdup (info->name);
-				gnome_keyring_find_password (GNOME_KEYRING_NETWORK_PASSWORD,
-							     (GnomeKeyringOperationGetStringCallback) password_found, tmp, g_free,
-							     "server", tmp, NULL);
+				if (sync_keyring) {
+					GnomeKeyringResult res;
+					gchar *auth = NULL;
+					res = gnome_keyring_find_password_sync (GNOME_KEYRING_NETWORK_PASSWORD, &auth,
+										"server", info->name, NULL);
+					if (res == GNOME_KEYRING_RESULT_OK) {
+						/*g_print ("Loaded sync. auth info for '%s': %s\n", info->name, auth);*/
+						info->auth_string = g_strdup (auth);
+					}
+					else if (res != GNOME_KEYRING_RESULT_NO_MATCH)
+						g_warning (_("Error loading authentification information for '%s' DSN: %s"),
+							   info->name, gnome_keyring_result_to_message (res));
+					if (auth)
+						gnome_keyring_free_password (auth);
+				}
+				else {
+					gchar *tmp = g_strdup (info->name);
+					gnome_keyring_find_password (GNOME_KEYRING_NETWORK_PASSWORD,
+								     (GnomeKeyringOperationGetStringCallback) password_found_cb,
+								     tmp, g_free,
+								     "server", tmp, NULL);
+				}
 			}
 #endif
-
 			/* signals */
 			if (is_new) {
 				unique_instance->priv->dsn_list = g_slist_insert_sorted (unique_instance->priv->dsn_list, info,
@@ -469,6 +492,11 @@ gda_config_constructor (GType type,
 	GObject *object;
   
 	if (!unique_instance) {
+#ifdef HAVE_GNOME_KEYRING
+		if (g_getenv ("GDA_CONFIG_SYNCHRONOUS"))
+			sync_keyring = TRUE;
+#endif
+
 		gint i;
 		gboolean user_file_set = FALSE, system_file_set = FALSE;
 
@@ -810,7 +838,6 @@ gda_config_get_dsn_info (const gchar *dsn_name)
 static void
 password_stored_cb (GnomeKeyringResult res, const gchar *dsnname)
 {
-        /* user_data will be the same as was passed to gnome_keyring_store_password() */
         if (res != GNOME_KEYRING_RESULT_OK)
                 g_warning (_("Couldn't save authentification information for DSN '%s': %s"), dsnname,
 			     gnome_keyring_result_to_message (res));
@@ -896,14 +923,24 @@ gda_config_define_dsn (const GdaDsnInfo *info, GError **error)
 #ifdef HAVE_GNOME_KEYRING
 	if (! info->is_system) {
 		/* save to keyring */
-		gchar *tmp, *tmp1;
+		gchar *tmp;
 		tmp = g_strdup_printf (_("Authentification for the '%s' DSN"), info->name);
-		tmp1 = g_strdup (info->name);
-		gnome_keyring_store_password (GNOME_KEYRING_NETWORK_PASSWORD,
-					      GNOME_KEYRING_DEFAULT,
-					      tmp, info->auth_string,
-					      (GnomeKeyringOperationDoneCallback) password_stored_cb, tmp1, g_free,
-					      "server", info->name, NULL);
+		if (sync_keyring) {
+			GnomeKeyringResult res;
+			res = gnome_keyring_store_password_sync (GNOME_KEYRING_NETWORK_PASSWORD, GNOME_KEYRING_DEFAULT,
+								 tmp, info->auth_string,
+								 "server", info->name, NULL);
+			password_stored_cb (res, info->name);
+		}
+		else {
+			gchar *tmp1;
+			tmp1 = g_strdup (info->name);
+			gnome_keyring_store_password (GNOME_KEYRING_NETWORK_PASSWORD,
+						      GNOME_KEYRING_DEFAULT,
+						      tmp, info->auth_string,
+						      (GnomeKeyringOperationDoneCallback) password_stored_cb, tmp1, g_free,
+						      "server", info->name, NULL);
+		}
 		g_free (tmp);
 	}
 #endif
@@ -978,11 +1015,19 @@ gda_config_remove_dsn (const gchar *dsn_name, GError **error)
 #ifdef HAVE_GNOME_KEYRING
 	if (! info->is_system) {
 		/* remove from keyring */
-		gchar *tmp;
-		tmp = g_strdup (dsn_name);
-		gnome_keyring_delete_password (GNOME_KEYRING_NETWORK_PASSWORD,
-					       (GnomeKeyringOperationDoneCallback) password_deleted_cb, tmp, g_free,
-					       "server", tmp, NULL);
+		if (sync_keyring) {
+			GnomeKeyringResult res;
+			res = gnome_keyring_delete_password_sync (GNOME_KEYRING_NETWORK_PASSWORD, GNOME_KEYRING_DEFAULT,
+								  "server", info->name, NULL);
+			password_deleted_cb (res, info->name);
+		}
+		else {
+			gchar *tmp;
+			tmp = g_strdup (dsn_name);
+			gnome_keyring_delete_password (GNOME_KEYRING_NETWORK_PASSWORD,
+						       (GnomeKeyringOperationDoneCallback) password_deleted_cb, tmp, g_free,
+						       "server", tmp, NULL);
+		}
 	}
 #endif
 
diff --git a/tools/gda-sql.c b/tools/gda-sql.c
index db88b8f..34cfba8 100644
--- a/tools/gda-sql.c
+++ b/tools/gda-sql.c
@@ -177,6 +177,7 @@ main (int argc, char *argv[])
 		return 0;
 	}
 
+	g_setenv ("GDA_CONFIG_SYNCHRONOUS", "1", TRUE);
         gda_init ();
 
 	has_threads = g_thread_supported ();
@@ -1310,13 +1311,20 @@ open_connection (SqlConsole *console, const gchar *cnc_name, const gchar *cnc_st
 		return NULL;
 	}
 
-	if (!user && info && info->auth_string) {
+	if ((!user || !pass) && info && info->auth_string) {
 		GdaQuarkList* ql;
 		const gchar *tmp;
 		ql = gda_quark_list_new_from_string (info->auth_string);
-		tmp = gda_quark_list_find (ql, "USERNAME");
-		if (tmp)
-			user = g_strdup (tmp);
+		if (!user) {
+			tmp = gda_quark_list_find (ql, "USERNAME");
+			if (tmp)
+				user = g_strdup (tmp);
+		}
+		if (!pass) {
+			tmp = gda_quark_list_find (ql, "PASSWORD");
+			if (tmp)
+				pass = g_strdup (tmp);
+		}
 		gda_quark_list_free (ql);
 	}
 	if (need_user && ((user && !*user) || !user)) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]