[tracker/miner-web: 4/26] * Make Gnome Keyring support optional



commit d0f2a838c8bf23cc6f5a26d055dbd28d25af5775
Author: Adrien Bustany <madcat mymadcat com>
Date:   Wed Nov 4 11:40:40 2009 -0300

    * Make Gnome Keyring support optional
    * Replace the dummy password provider by one using GKeyFile
    * Add backend name in interface

 configure.ac                                       |   44 +++++++---
 src/libtracker-miner/Makefile.am                   |   15 ++--
 src/libtracker-miner/dummy-password-provider.vala  |   18 ----
 src/libtracker-miner/gnome-password-provider.vala  |    6 +-
 .../keyfile-password-provider.vala                 |   85 ++++++++++++++++++++
 src/libtracker-miner/password-provider.vala        |    7 +-
 src/libtracker-miner/test/Makefile.am              |   17 ++++-
 7 files changed, 150 insertions(+), 42 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 851a18f..0ef4f00 100644
--- a/configure.ac
+++ b/configure.ac
@@ -154,7 +154,7 @@ EDS_REQUIRED=2.25.5
 # Unlikely version for now, Nepomuk integration isn't finished in streamanalyzer atm
 LIBSTREAMANALYZER_REQUIRED=0.7.0
 GEE_REQUIRED=0.3
-GNOMEKEYRING_REQUIRED=2.26
+GNOME_KEYRING_REQUIRED=2.26
 
 # Library Checks
 PKG_CHECK_MODULES(GLIB2, [glib-2.0 >= $GLIB_REQUIRED])
@@ -698,6 +698,36 @@ fi
 
 AM_CONDITIONAL(HAVE_UNAC, test "x$have_unac" = "xyes")
 
+##################################################################
+# Enable Gnome Keyring support?
+##################################################################
+
+AC_ARG_ENABLE(gnome_keyring, 
+	      AS_HELP_STRING([--enable-gnome-keyring],
+			     [enable Gnome Keyring support to store credentials [[default=auto]]]),,
+	      [enable_gnome_keyring=auto])
+
+if test "x$enable_gnome_keyring" != "xno"; then
+	PKG_CHECK_MODULES(GNOME_KEYRING,
+					  [ gnome-keyring-1 >= $GNOME_KEYRING_REQUIRED ],
+					  [have_gnome_keyring=yes],
+					  [have_gnome_keyring=no])
+	AC_SUBST(GNOME_KEYRING_LIBS)
+	AC_SUBST(GNOME_KEYRING_CFLAGS)
+
+	if test "x$have_gnome_keyring" = "xyes"; then
+		AC_DEFINE(HAVE_GNOME_KEYRING, [], [Define if we have Gnome Keyring for password storage])
+	fi
+fi
+
+if test "x$enable_gnome_keyring" = "xyes"; then
+   if test "x$have_gnome_keyring" != "xyes"; then
+      AC_MSG_ERROR([Couldn't find Gnome Keyring >= $GNOME_KEYRING_REQUIRED.])
+   fi
+fi
+
+AM_CONDITIONAL(HAVE_GNOME_KEYRING, test "x$have_gnome_keyring" = "xyes")
+
 ####################################################################
 # Push modules
 ####################################################################
@@ -810,18 +840,6 @@ PKG_CHECK_MODULES(TRACKER_VALA,
 AC_SUBST(TRACKER_VALA_CFLAGS)
 AC_SUBST(TRACKER_VALA_LIBS)
 
-PKG_CHECK_MODULES(GNOMEKEYRING,
-                  [ gnome-keyring-1 >= $GNOMEKEYRING_REQUIRED ],
-                  [have_gnomekeyring=yes],
-                  [have_gnomekeyring=no])
-AC_SUBST(GNOMEKEYRING_LIBS)
-AC_SUBST(GNOMEKEYRING_CFLAGS)
-
-if test "x$have_gnomekeyring" = "xyes"; then
-	AC_DEFINE(HAVE_GNOMEKEYRING, [], [Define if we have Gnome Keyring for password storage])
-fi
-
-AM_CONDITIONAL(HAVE_GNOMEKEYRING, test "x$have_gnomekeyring" = "xyes")
 
 ##################################################################
 # Enable building libtracker-gtk?
diff --git a/src/libtracker-miner/Makefile.am b/src/libtracker-miner/Makefile.am
index 2487536..e46fa49 100644
--- a/src/libtracker-miner/Makefile.am
+++ b/src/libtracker-miner/Makefile.am
@@ -29,8 +29,7 @@ libtracker_miner_LTLIBRARIES = libtracker-miner.la
 libtracker_miner_la_VALASOURCES=     \
     tracker-miner-web.vala           \
     password-provider.vala           \
-    gnome-password-provider.vala     \
-	dummy-password-provider.vala
+	keyfile-password-provider.vala
 
 libtracker_miner_la_SOURCES = 				\
 	tracker-crawler.c				\
@@ -101,22 +100,26 @@ dbus_sources = 						\
 
 VALAPKGS =                          \
 	--pkg dbus-glib-1               \
+	--pkg posix                     \
 	--pkg tracker-miner-0.7
 
 VALAFLAGS =                         \
 	--vapidir $(top_srcdir)/src/libtracker-miner
 
-if HAVE_GNOMEKEYRING
+if HAVE_GNOME_KEYRING
 VALAPKGS +=                         \
 	--pkg gnome-keyring-1
 VALAFLAGS +=                        \
-	-D HAVE_GNOMEKEYRING
+	-D HAVE_GNOME_KEYRING
 
 INCLUDES +=                         \
-	$(GNOMEKEYRING_CFLAGS)
+	$(GNOME_KEYRING_CFLAGS)
 
 libtracker_miner_la_LIBADD +=       \
-	$(GNOMEKEYRING_LIBS)
+	$(GNOME_KEYRING_LIBS)
+
+libtracker_miner_la_VALASOURCES +=     \
+    gnome-password-provider.vala
 endif
 
 
diff --git a/src/libtracker-miner/gnome-password-provider.vala b/src/libtracker-miner/gnome-password-provider.vala
index fc91e08..b53dcdb 100644
--- a/src/libtracker-miner/gnome-password-provider.vala
+++ b/src/libtracker-miner/gnome-password-provider.vala
@@ -1,5 +1,6 @@
 namespace Tracker {
 	class GnomePasswordProvider : Tracker.PasswordProvider, GLib.Object {
+		private const string BACKEND_NAME = "Gnome Keyring";
 
 		private const GnomeKeyring.PasswordSchema password_schema = {
 			GnomeKeyring.ItemType.GENERIC_SECRET,
@@ -8,6 +9,9 @@ namespace Tracker {
 			  { null, 0 } }
 		};
 
+
+		public string name { get { return BACKEND_NAME; }}
+
 		public void store (string service, string description, string username, string password) throws PasswordProviderError
 		{
 			GnomeKeyring.Result r = GnomeKeyring.store_password_sync (password_schema, null /* default keyring */, description, password, "username", username, "service", service, null);
@@ -34,7 +38,7 @@ namespace Tracker {
 					weak GnomeKeyring.Found f = (GnomeKeyring.Found)l.data;
 					for (int j = 0 ; j < f.attributes.length ; ++j) {
 						weak GnomeKeyring.Attribute a = f.attributes.index (j);
-						if (a.name == "username" && username != null) {
+						if (a.name == "username" && &username != null) {
 							username = a.value.string;
 						}
 					}
diff --git a/src/libtracker-miner/keyfile-password-provider.vala b/src/libtracker-miner/keyfile-password-provider.vala
new file mode 100644
index 0000000..3540cd6
--- /dev/null
+++ b/src/libtracker-miner/keyfile-password-provider.vala
@@ -0,0 +1,85 @@
+namespace Tracker {
+	class KeyFilePasswordProvider : Tracker.PasswordProvider, GLib.Object {
+		private const string BACKEND_NAME = "KeyFile";
+
+		private string PASSWORD_FILE_PATH = GLib.Environment.get_user_config_dir () + "/tracker/passwords.cfg";
+
+		public string name { get { return BACKEND_NAME; }}
+
+		public void store (string service, string description, string username, string password) throws PasswordProviderError
+		{
+			GLib.KeyFile password_file = new GLib.KeyFile ();
+
+			try {
+				password_file.load_from_file (PASSWORD_FILE_PATH, GLib.KeyFileFlags.NONE);
+			} catch (Error e) {
+				if (!(e is GLib.FileError.NOENT)) { // Don't throw error if the files does not exist, we'll create it
+					throw new PasswordProviderError.SERVICE (e.message);
+				}
+			}
+
+			password_file.set_string (service, "username", username);
+			password_file.set_string (service, "password", password);
+
+			try {
+				message (PASSWORD_FILE_PATH);
+				FileUtils.set_contents (PASSWORD_FILE_PATH, password_file.to_data ());
+			} catch (Error e) {
+				throw new PasswordProviderError.SERVICE (e.message);
+			}
+		}
+
+		public new string get (string service, out string? username) throws PasswordProviderError
+		{
+			GLib.KeyFile password_file = new GLib.KeyFile ();
+
+			try {
+				password_file.load_from_file (PASSWORD_FILE_PATH, GLib.KeyFileFlags.NONE);
+			} catch (Error e) {
+				throw new PasswordProviderError.SERVICE (e.message);
+			}
+
+			if (&username != null) {
+				try {
+					username = password_file.get_string (service, "username");
+				} catch (Error e) {
+					throw new PasswordProviderError.NOTFOUND ("Unkown username/password");
+				}
+			}
+
+			string password = null;
+			try {
+				password = password_file.get_string (service, "password");
+				Posix.mlock (password, sizeof(string)*password.length);
+			} catch (Error e) {
+				throw new PasswordProviderError.NOTFOUND ("Unkown username/password");
+			}
+
+			return password;
+		}
+
+		public void forget (string service, string username) throws PasswordProviderError
+		{
+			GLib.KeyFile password_file = new GLib.KeyFile ();
+
+			try {
+				password_file.load_from_file (PASSWORD_FILE_PATH, GLib.KeyFileFlags.NONE);
+			} catch (Error e) {
+				throw new PasswordProviderError.SERVICE (e.message);
+			}
+
+			try {
+				password_file.remove_group (service);
+			} catch (Error e) {
+				throw new PasswordProviderError.SERVICE (e.message);
+			}
+
+			try {
+				FileUtils.set_contents (PASSWORD_FILE_PATH, password_file.to_data ());
+			} catch (Error e) {
+				throw new PasswordProviderError.SERVICE (e.message);
+			}
+		}
+
+	}
+} // Namespace Tracker
diff --git a/src/libtracker-miner/password-provider.vala b/src/libtracker-miner/password-provider.vala
index 68ff186..b01a554 100644
--- a/src/libtracker-miner/password-provider.vala
+++ b/src/libtracker-miner/password-provider.vala
@@ -9,17 +9,18 @@ namespace Tracker {
 
 	[CCode (cheader_filename = "libtracker-miner/tracker-miner-web.h")]
 	public interface PasswordProvider : GLib.Object {
+		public abstract string name { get; }
 		public abstract void store (string service, string description, string username, string password) throws PasswordProviderError;
 		public abstract string @get (string service, out string? username) throws PasswordProviderError;
 		public abstract void forget (string service, string username) throws PasswordProviderError;
 
-		static PasswordProvider password_provider {
+		public static PasswordProvider password_provider {
 			get {
 				if (_password_provider_instance == null) {
-#if HAVE_GNOMEKEYRING
+#if HAVE_GNOME_KEYRING
 					_password_provider_instance = new GnomePasswordProvider ();
 #else
-					_password_provider_instance = new DummyPasswordProvider ();
+					_password_provider_instance = new KeyFilePasswordProvider ();
 #endif
 				}
 
diff --git a/src/libtracker-miner/test/Makefile.am b/src/libtracker-miner/test/Makefile.am
index 602f635..17b0dbf 100644
--- a/src/libtracker-miner/test/Makefile.am
+++ b/src/libtracker-miner/test/Makefile.am
@@ -17,7 +17,7 @@ INCLUDES =							\
 	$(DBUS_CFLAGS)						\
 	$(UNAC_CFLAGS)
 
-noinst_PROGRAMS = tracker-miner-test
+noinst_PROGRAMS = tracker-miner-test password-provider-test
 
 tracker_miner_test_SOURCES =   					\
 	tracker-miner-test.c    				\
@@ -34,3 +34,18 @@ tracker_miner_test_LDADD =                             		\
        $(GLIB2_LIBS)                                   		\
        -lz                                             		\
        -lm
+
+password_provider_test_SOURCES =      \
+	password-provider-test.c
+
+password_provider_test_LDADD =        \
+	$(top_builddir)/src/libtracker-miner/libtracker-miner.la	\
+	$(DBUS_LIBS)                                    		\
+	$(GMODULE_LIBS)                                 		\
+	$(GTHREAD_LIBS)                                 		\
+	$(GIO_LIBS)                                     		\
+	$(GCOV_LIBS)                                    		\
+	$(GLIB2_LIBS)                                   		\
+	-lz                                             		\
+	-lm
+



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