[Patch] GalleryExport



Hey,

I didn't like much that f-spot saves my gallery password in clear text
to a file with 0644 permissions. Attached is a patch to save the
password using gnome-keyring instead.

Tambet
Index: GalleryExport.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/GalleryExport.cs,v
retrieving revision 1.43
diff -u -r1.43 GalleryExport.cs
--- GalleryExport.cs	28 Jul 2006 18:53:09 -0000	1.43
+++ GalleryExport.cs	26 Aug 2006 17:48:08 -0000
@@ -11,8 +11,8 @@
 
 namespace FSpot {
 	public class GalleryAccount {
-		public GalleryAccount (string name, string url, string username, string password) : this (name, url, username, password, GalleryVersion.VersionUnknown) {}
-		public GalleryAccount (string name, string url, string username, string password, GalleryVersion version)
+		public GalleryAccount (string name, Uri url, string username, string password) : this (name, url, username, password, GalleryVersion.VersionUnknown) {}
+		public GalleryAccount (string name, Uri url, string username, string password, GalleryVersion version)
 		{
 			this.name = name;
 			this.username = username;
@@ -22,7 +22,7 @@
 			if (version != GalleryVersion.VersionUnknown) {
 				this.version = version;
 			} else {
-				this.version = Gallery.DetectGalleryVersion(Url);
+				this.version = Gallery.DetectGalleryVersion (Url);
 			}
 		}
 
@@ -33,12 +33,12 @@
 			Gallery gal = null;
 
 			if (version == GalleryVersion.VersionUnknown)
-				this.version = Gallery.DetectGalleryVersion(Url);
+				this.version = Gallery.DetectGalleryVersion (Url);
 
 			if (version == GalleryVersion.Version1) {
-				gal = new Gallery1 (url, url);
+				gal = new Gallery1 (name, url);
 			} else if (version == GalleryVersion.Version2) {
-				gal = new Gallery2 (url, url);
+				gal = new Gallery2 (name, url);
 			} else {
 				throw new Exception ("Cannot connect to a Gallery for which the version is unknown");
 			}
@@ -98,8 +98,8 @@
 			}
 		}
 		
-		string url;
-		public string Url {
+		Uri url;
+		public Uri Url {
 			get {
 				return url;
 			}
@@ -217,12 +217,26 @@
 				writer.WriteStartElement ("Account");
 				writer.WriteElementString ("Name", account.Name);
 				
-				writer.WriteElementString ("Url", account.Url);
+				writer.WriteElementString ("Url", account.Url.ToString ());
 				writer.WriteElementString ("Username", account.Username);
-				writer.WriteElementString ("Password", account.Password);
 				writer.WriteElementString ("Version", account.Version.ToString());
 				writer.WriteEndElement (); //Account
+
+				UInt32 itemId;
+				GnomeKeyring.Result res = GnomeKeyring.NetworkPassword.Set (null,
+																			account.Username,
+																			null,
+																			account.Url.AbsoluteUri,
+																			"gallery_password",
+																			account.Url.Scheme,
+																			null,
+																			(uint) account.Url.Port,
+																			account.Password,
+																			out itemId);
+				if (res != GnomeKeyring.Result.Ok)
+					Console.WriteLine ("Password not saved");
 			}
+
 			writer.WriteEndElement ();
 			writer.WriteEndDocument ();
 			writer.Close ();
@@ -235,7 +249,7 @@
 				return null;
 
 			string name = null;
-			string url = null;
+			Uri url = null;
 			string username = null;
 			string password = null;
 			GalleryVersion version = GalleryVersion.VersionUnknown;
@@ -245,8 +259,9 @@
 					name = child.ChildNodes [0].Value;
 				
 				} else if (child.Name == "Url") {
-					url = child.ChildNodes [0].Value;
+					url = new Uri (child.ChildNodes [0].Value);
 				} else if (child.Name == "Password") {
+					// FIXME: The password is here only for compatibility, remove it when the time is right.
 					password = child.ChildNodes [0].Value;
 				} else if (child.Name == "Username") {
 					username = child.ChildNodes [0].Value;
@@ -260,6 +275,23 @@
 						Console.WriteLine ("Unexpected versions string: " + versionString);
 				}
 			}
+
+			if (password == null) {
+				GnomeKeyring.NetworkPasswordData[] list;
+				GnomeKeyring.Result res = GnomeKeyring.NetworkPassword.Find (username,
+																			 null,
+																			 url.AbsoluteUri,
+																			 "gallery_password",
+																			 url.Scheme,
+																			 null,
+																			 (uint) url.Port,
+																			 out list);
+				if (res == GnomeKeyring.Result.Ok && list.Length > 0)
+					password = list[0].Password;
+				else
+					password = String.Empty;
+			}
+
 			return new GalleryAccount (name, url, username, password, version);
 		}
 
@@ -314,7 +346,7 @@
 
 			if (account != null) {
 				gallery_entry.Text = account.Name;
-				url_entry.Text = account.Url;
+				url_entry.Text = account.Url.AbsoluteUri;
 				password_entry.Text = account.Password;
 				username_entry.Text = account.Username;
 				add_button.Label = Gtk.Stock.Ok;
@@ -351,8 +383,10 @@
 		protected void HandleAddResponse (object sender, Gtk.ResponseArgs args)
 		{
 			if (args.ResponseId == Gtk.ResponseType.Ok) {
+				Uri uri;
+
 				try {
-					Uri uri = new Uri (url);
+					uri = new Uri (url);
 					if (uri.Scheme != Uri.UriSchemeHttp &&
 					    uri.Scheme != Uri.UriSchemeHttps)
 						throw new System.UriFormatException ();
@@ -371,7 +405,7 @@
 				}
 
 				GalleryAccount account = new GalleryAccount (name, 
-									     url, 
+									     uri, 
 									     username,
 									     password);
 				GalleryAccountManager.GetInstance ().AddAccount (account);
@@ -383,7 +417,7 @@
 		{
 			if (args.ResponseId == Gtk.ResponseType.Ok) {
 				account.Name = name;
-				account.Url = url;
+				account.Url = new Uri (url);
 				account.Username = username;
 				account.Password = password;
 				GalleryAccountManager.GetInstance ().MarkChanged (true, account);
Index: GalleryRemote.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/GalleryRemote.cs,v
retrieving revision 1.15
diff -u -r1.15 GalleryRemote.cs
--- GalleryRemote.cs	22 Mar 2006 16:57:27 -0000	1.15
+++ GalleryRemote.cs	26 Aug 2006 17:48:09 -0000
@@ -271,16 +271,16 @@
 			albums = new ArrayList ();
 		}
 
-		public static GalleryVersion DetectGalleryVersion (string url)
+		public static GalleryVersion DetectGalleryVersion (Uri url)
 		{
 			//Figure out if the url is for G1 or G2
 			Console.WriteLine ("Detecting Gallery version");
 
 			GalleryVersion version;
 
-			if (url.EndsWith (Gallery1.script_name)) {
+			if (url.AbsolutePath.EndsWith (Gallery1.script_name)) {
 				version = GalleryVersion.Version1;
-			} else if (url.EndsWith (Gallery2.script_name)) {
+			} else if (url.AbsolutePath.EndsWith (Gallery2.script_name)) {
 				version = GalleryVersion.Version2;
 			} else {
 				//check what script is available on the server
@@ -288,13 +288,13 @@
 				FormClient client = new FormClient ();
 
 				try {
-					client.Submit (new Uri (Gallery.FixUrl (url, Gallery1.script_name)));
+					client.Submit (new Uri (url, Gallery1.script_name));
 					version =  GalleryVersion.Version1;
 
 				} catch (System.Net.WebException e) {
 
 					try {
-						client.Submit (new Uri (Gallery.FixUrl (url, Gallery2.script_name)));
+						client.Submit (new Uri (url, Gallery2.script_name));
 						version =  GalleryVersion.Version2;
 
 					} catch (System.Net.WebException e2) {
@@ -614,28 +614,14 @@
 			}
 			return match;
 		}
-
-		public static string FixUrl(string url, string end)
-		{
-			string fixedUrl = url;
-			if (!url.EndsWith (end)) {
-				if (!url.EndsWith ("/"))
-					fixedUrl = url + "/";
-				fixedUrl = fixedUrl + end;
-			}
-			return fixedUrl;
-			
-		}
-		
 	}
 
 	public class Gallery1 : Gallery
 	{
 		public const string script_name = "gallery_remote2.php"; 
-		public Gallery1 (string url) : this (url, url) {}
-		public Gallery1 (string name, string url) : base (name)
+		public Gallery1 (string name, Uri url) : base (name)
 		{
-			this.uri = new Uri (FixUrl (url, script_name));
+			this.uri = new Uri (url, script_name);
 			version = GalleryVersion.Version1;
 		}
 
@@ -860,10 +846,9 @@
 	{
 		public const string script_name = "main.php";
 
-		public Gallery2 (string url) : this (url, url) {}
-		public Gallery2 (string name, string url) : base (name)
+		public Gallery2 (string name, Uri url) : base (name)
 		{
-			this.uri = new Uri (FixUrl (url, script_name));
+			this.uri = new Uri (url, script_name);
 			version = GalleryVersion.Version2;
 		}
 
Index: Makefile.am
===================================================================
RCS file: /cvs/gnome/f-spot/src/Makefile.am,v
retrieving revision 1.64
diff -u -r1.64 Makefile.am
--- Makefile.am	18 Jul 2006 23:16:24 -0000	1.64
+++ Makefile.am	26 Aug 2006 17:48:09 -0000
@@ -36,6 +36,7 @@
 	$(srcdir)/GalleryExport.cs		\
 	$(srcdir)/GladeDialog.cs		\
 	$(srcdir)/Global.cs			\
+	$(srcdir)/GnomeKeyring.cs		\
 	$(srcdir)/GroupAdaptor.cs		\
 	$(srcdir)/GroupSelector.cs		\
 	$(srcdir)/Accelerometer.cs		\
using System;
using System.Runtime.InteropServices;

namespace GnomeKeyring {
    public enum Result {
        Ok,
        Denied,
        NoKeyringDaemon,
        AlreadyUnlocked,
        NoSuchKeyring,
        BadArguments,
        IOError,
        Cancelled,
        AlreadyExists
    }

    [StructLayout (LayoutKind.Sequential)]
    public struct NetworkPasswordData {
        public string Keyring;
        public UInt32 ItemId;

        public string Protocol;
        public string Server;
        public string Obj;
        public string AuthType;
        public UInt32 Port;

        public string User;
        public string Domain;
        public string Password;
    }

    public class NetworkPassword {
        public static Result Find (string user, string domain, string server, string obj,
                                   string protocol, string authtype, UInt32 port,
                                   out NetworkPasswordData[] result) {

            IntPtr listPtr;
            Result res = gnome_keyring_find_network_password_sync (user, domain, server, obj, protocol,
                                                                   authtype, port, out listPtr);

            if (res == Result.Ok && listPtr != IntPtr.Zero) {
                GLib.List list = new GLib.List (listPtr, typeof (NetworkPasswordData), false, false);
                result = new NetworkPasswordData[list.Count];
                list.CopyTo (result, 0);
                gnome_keyring_network_password_list_free (listPtr);
            } else {
                result = new NetworkPasswordData[0];
            }

            return res;
        }

        public static Result Set (string keyring, string user, string domain, string server, string obj,
                                  string protocol, string authtype, UInt32 port, string password,
                                  out UInt32 itemId) {
            return gnome_keyring_set_network_password_sync (keyring, user, domain, server, obj, protocol,
                                                            authtype, port, password, out itemId);
        }

        [DllImport ("libgnome-keyring")]
        static extern Result gnome_keyring_find_network_password_sync (string user, string domain, string server,
                                                                       string obj, string protocol, string authtype,
                                                                       UInt32 port, out IntPtr result);

        [DllImport ("libgnome-keyring")]
        static extern void gnome_keyring_network_password_list_free (IntPtr list);

        [DllImport ("libgnome-keyring")]
        static extern Result gnome_keyring_set_network_password_sync (string keyring, string user, string domain,
                                                                      string server, string obj, string protocol,
                                                                      string authtype, UInt32 port, string password,
                                                                      out UInt32 itemId);
    }
}


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