[PATCH] Google and Mozilla backends



Hi all,

I have modified my patch to the Mozilla backend, as per Nat's
recommendations : the browser and profile are autodetected. Currently it
works with Mozilla, Phoenix, Firebird and Firefox. It also adds favicon
support, and works with the proxy defined in gconf.

The google patch simply puts the dev key in gconf, and retrieves it by
the same way. This patch also includes a .schemas file, with the Google
and Bugzilla keys.

I think we should have meta-backends that only act as loaders. For
example, the browser meta-backend could look up the browser in gconf and
only load the relevant backend. This might be better than having each
backend load itself, find that it is not the default, then unload.

The proxy configuration could be put in a more generic place, used by
all the plugins in dashboard. This would limit the access to gconf, and
would simplify the update of the parameters if the various proxy-related
gconf key change.

Thanks for your comments on this,

Clement
diff -Nru dashboard/backends/GoogleBackend.cs dashboard-new/backends/GoogleBackend.cs
--- dashboard/backends/GoogleBackend.cs	2004-03-04 08:59:33.000000000 +0100
+++ dashboard-new/backends/GoogleBackend.cs	2004-03-04 18:31:26.000000000 +0100
@@ -4,6 +4,7 @@
 // Authors:
 //     Don Smith <donsmith77 optonline net>
 //
+// Set gconf:/apps/dashboard/backends/google/api_key to your API Key
 
 using System;
 using System.IO;
@@ -17,21 +18,31 @@
 
 	class GoogleBackend : BackendSimple {
 
-		// Set your key below.  You can get a Google API key
-		// at http://www.google.com/apis/.
-		string google_api_key = "INSERT DEVTAG HERE"; 
+		string google_api_key = "DEVTAG"; 
 		
 		public override bool Startup ()
 		{
 			Name = "Google";
 
-			if (google_api_key.Length == 0)
-				return false;
+			GConf.Client client = new GConf.Client ();
+			try {
+				string api_key = (string) client.Get ("/apps/dashboard/backends/google/api_key");
+				if (api_key != "")
+					google_api_key = api_key;
+				else {
+                    Console.WriteLine ("Please put in a Google Dev Tag");
+					return false;
+				}
+			}
+			catch (GConf.NoSuchKeyException) {
+                    Console.WriteLine ("Please put in a Google Dev Tag");
+					return false;
+            }
 
-			if (google_api_key == "INSERT DEVTAG HERE"){
-                                Console.WriteLine ("Please put in a Google Dev Tag");
+			if (google_api_key == "DEVTAG"){
+				Console.WriteLine ("Please put in a Google Dev Tag");
 				return false;
-                        }
+			}
 
 			try {
 				// Subscribe to artist clues
diff -Nru dashboard/configure.in dashboard-new/configure.in
--- dashboard/configure.in	2004-02-14 10:16:59.000000000 +0100
+++ dashboard-new/configure.in	2004-03-04 18:25:40.000000000 +0100
@@ -8,6 +8,7 @@
 AC_PROG_INSTALL
 AC_HEADER_STDC
 AM_PROG_LIBTOOL
+AM_GCONF_SOURCE_2
 
 CFLAGS='-ggdb -Wall -Wunused -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Wshadow -Wpointer-arith -Wno-cast-qual -Wcast-align -Wwrite-strings'
 
diff -Nru dashboard/engine/Makefile.am dashboard-new/engine/Makefile.am
--- dashboard/engine/Makefile.am	2004-03-04 08:59:34.000000000 +0100
+++ dashboard-new/engine/Makefile.am	2004-03-04 18:39:36.000000000 +0100
@@ -64,7 +64,11 @@
 
 dashboardlibdir = $(prefix)/lib/dashboard
 
-install-data-local: dashboard.exe
+install-schemas:
+	      GCONF_CONFIG_SOURCE=$(GCONF_SCHEMA_CONFIG_SOURCE) \
+		  	      gconftool --makefile-install-rule dashboard.schemas
+
+install-data-local: dashboard.exe install-schemas
 	$(mkinstalldirs) $(DESTDIR)$(dashboardlibdir)
 	$(INSTALL_DATA) dashboard.exe $(DESTDIR)$(dashboardlibdir)/dashboard.exe
 
diff -Nru dashboard/engine/dashboard.schemas dashboard-new/engine/dashboard.schemas
--- dashboard/engine/dashboard.schemas	1970-01-01 01:00:00.000000000 +0100
+++ dashboard-new/engine/dashboard.schemas	2004-03-04 18:26:25.000000000 +0100
@@ -0,0 +1,34 @@
+<gconfschemafile>
+	<schemalist>
+		<schema>
+			<key>/schemas/apps/dashboard/backends/bugzilla/host</key>
+			<applyto>/apps/dashboard/backends/bugzilla/host</applyto>
+			<owner>Dashboard</owner>
+			<type>string</type>
+			<locale name="C">
+				<default>http://bugzilla.ximian.com</default>
+				<short>Bugzilla host URL</short>
+			</locale>
+			<locale name="fr">
+				<default>http://bugzilla.ximian.com</default>
+				<short>URL du serveur Bugzilla</short>
+			</locale>
+		</schema>
+		<schema>
+			<key>/schemas/apps/dashboard/backends/google/api_key</key>
+			<applyto>/apps/dashboard/backends/google/api_key</applyto>
+			<owner>Dashboard</owner>
+			<type>string</type>
+			<locale name="C">
+				<default>DEVTAG</default>
+				<short>Google API key</short>
+				<long>Set this to your Google developer key. You can get a Google API key at http://www.google.com/apis/</long>
+			</locale>
+			<locale name="fr">
+				<default>DEVTAG</default>
+				<short>Clé pour l'API Google</short>
+				<long>Mettez ici votre clé de développeur Google. Vous pouvez obtenir une clé pour l'API Google à l'adresse suivante : http://www.google.com/apis/</long>
+			</locale>
+		</schema>
+	</schemalist>
+</gconfschemafile>
diff -Nru dashboard/backends/MozillaBookmarksBackend.cs dashboard-new/backends/MozillaBookmarksBackend.cs
--- dashboard/backends/MozillaBookmarksBackend.cs	2004-03-15 13:28:18.000000000 +0100
+++ dashboard-new/backends/MozillaBookmarksBackend.cs	2004-03-15 16:14:26.000000000 +0100
@@ -27,12 +27,12 @@
 //
 //read the Mozilla or Thunderbird bookmarks
 
-//FIXME: application directory and profile should not be "hard coded".
-
 using System;
 using System.IO;
 using System.Web;
 using System.Collections;
+using System.Net;
+using System.Text;
 using System.Text.RegularExpressions;
 
 [assembly:Dashboard.BackendFactory ("Dashboard.MozillaBookmarks")]
@@ -41,9 +41,10 @@
 
 	class MozillaBookmarks : BackendSimple {
 
-		const string browser_dirname = ".phoenix";
-		const string mozilla_dirname = ".mozilla";
-		const string profile_name = "default";
+		string[] browser_list	= new string[4] {".phoenix", ".mozilla", ".firebird", ".firefox"};
+		private string browser_dirname;
+		private string profile_name;
+		private WebProxy proxy = null;
 
 		ArrayList bookmarks = new ArrayList (); 
 
@@ -53,18 +54,92 @@
 
 			string home_dir = Environment.GetEnvironmentVariable ("HOME");
 			string path;
+			
+			GConf.Client client = new GConf.Client ();
+			
+			try {
+				string browser = (string) client.Get ("/desktop/gnome/applications/browser/exec");
+				if (browser != "")
+				{
+					switch (browser) {
+					case "mozilla"	:
+					case "phoenix"	:
+					case "firebird"	:
+					case "firefox"	:
+						browser_dirname = "." + browser;
+						break;
+					default	:
+						Console.WriteLine ("Bookmarks-mozilla: don't know how to handle '{0}'.", browser);
+						return false;
+					}
+				}
+				else {
+					Console.WriteLine ("Bookmarks-Mozilla: the Gnome browser preference is empty !");
+					Console.WriteLine ("Bookmarks-Mozilla: Trying standard Mozilla paths...");
+					
+					foreach (String s in browser_list)
+						if (Directory.Exists (Path.Combine (home_dir, s)))
+							browser_dirname = s;
+							
+					Console.WriteLine ("Bookmarks-Mozilla: Could not find a suitable browser");
+					return false;
+				}
+			}
+			catch (GConf.NoSuchKeyException) {
+				Console.WriteLine ("Bookmarks-Mozilla: the Gnome browser preference does not exist.");
+				return false;
+			}
+			
+			try {
+				bool use_proxy = (bool) client.Get ("/system/http_proxy/use_http_proxy");
+				if (use_proxy) {
+					string host = (string) client.Get ("/system/http_proxy/host");
+					int port = -1;
+					port = (int) client.Get ("/system/http_proxy/port");
+					if (host != "")	{
+						string prox_str = "http://"; + host + (port == -1 ? "/" : ":" + port + "/");
+						proxy = new WebProxy (prox_str,true);
+						bool use_auth = (bool) client.Get ("/system/http_proxy/use_authentication");
+						if (use_auth) {
+							string user = (string) client.Get ("/system/http_proxy/authentication_user");
+							string pass = (string) client.Get ("/system/http_proxy/authentication_password");
+							proxy.Credentials = new NetworkCredential (user,pass);
+						}
+					}
+				}
+			}
+			catch(GConf.NoSuchKeyException) {}
+						
+			try {
+					path = Path.Combine (Path.Combine (home_dir, browser_dirname), "appreg");
+					FileStream fs_appreg = new FileStream (path, FileMode.Open);
+					BinaryReader br = new BinaryReader (fs_appreg);
+ 
+					StringBuilder str = new StringBuilder ("");
+					int length = (int)fs_appreg.Length - 64;
+					for (int i = 0; i<length; i++)
+						str.Append(br.ReadChar ().ToString ());
+					fs_appreg.Close ();
+					
+					string reg = "CurrentProfile\x00([^\x00]+)\x00";
+ 					Regex re = new Regex (reg, RegexOptions.None);
+					RegularExpressions.Match m = re.Match (str.ToString ());
+ 
+					if (m.Success)
+						profile_name = m.Groups [1].Captures [0].ToString ();
+					else {
+						Console.WriteLine ("Bookmarks-Mozilla: could not find the default profile");
+						return false;
+					}	
+			}
+			catch (System.IO.FileNotFoundException) {
+				Console.WriteLine ("Bookmarks-Mozilla: could not find the preferences file.");
+				return false;
+			}
 
 			try {
 				path = Path.Combine (Path.Combine (home_dir, browser_dirname), profile_name);
 
-				if (!Directory.Exists (path)) {
-                    path = Path.Combine (Path.Combine (home_dir, mozilla_dirname), profile_name);
-                    if (!Directory.Exists (path)) {
-                        Console.WriteLine (string.Format ("Bookmarks-Mozilla: cannot find {0}",path));
-                        return false;
-                    }
-				}
-
 				string[] dirs = Directory.GetDirectories (path);
 				foreach (string dir in dirs) {
 					if (dir.IndexOf (".slt") != -1) {
@@ -75,10 +150,12 @@
 						return false;
 					}
 				}
-
+				
+				if (! Directory.Exists (home_dir + "/.dashboard/backend-data/mozilla"))
+					Directory.CreateDirectory (home_dir + "/.dashboard/backend-data/mozilla");
+				
 				ExtractBookmarks (path);
-		
-
+				
 				this.SubscribeToClues ("textblock", "keyword", "word_at_point");
 		
 				this.Initialized = true;
@@ -91,19 +168,54 @@
 			}
 		}
 
-		// FIXME - should use xpath, should get icon if possible
-		private void ExtractBookmarks (string path) {
-			string pat = "<A HREF=\"([^\"]+)\"[^>]*>([^<]*)</A>";
-			string text = File.OpenText (path).ReadToEnd ();
-	  
-			Regex re = new Regex (pat, RegexOptions.None);
-			int[] gnums = re.GetGroupNumbers ();
-			System.Text.RegularExpressions.Match m = re.Match (text);
-			while (m.Success) {
-				Bookmark bk = new Bookmark ();
-				bk.name = m.Groups[2].Captures[0].ToString ();
-				bk.lname = m.Groups[2].Captures[0].ToString ().ToLower ();
-				bk.url = m.Groups[1].Captures[0].ToString ();
+		private void ExtractBookmarks (string path)
+		{
+			//FIXME Find a regexp catching lines with or without ICON
+			string pat = "<A HREF=\"([^\"]+)\"([^>]*)>([^<]*)</A>";
+ 			string text = File.OpenText (path).ReadToEnd ();
+ 	  
+ 			Regex re = new Regex (pat, RegexOptions.None);
+			RegularExpressions.Match m = re.Match (text);
+ 			while (m.Success) {
+ 				Bookmark bk = new Bookmark ();
+				bk.name = m.Groups [3].Captures [0].ToString ();
+				bk.lname = m.Groups [3].Captures [0].ToString ().ToLower ();
+				bk.url = m.Groups [1].Captures [0].ToString ();
+
+				string fav_finder = "ICON=\"([^{>,\"}]+)\"";
+				Regex fav_rex = new Regex (fav_finder);
+				RegularExpressions.Match fav_m = fav_rex.Match (m.Groups [2].ToString ());
+				if (fav_m.Success) {
+					string fav_url = fav_m.Groups [1].ToString ();
+						
+					string home_dir = Environment.GetEnvironmentVariable ("HOME");
+					string fav_name = fav_url.Replace ('/','-');
+					string moz_path = ".dashboard/backend-data/mozilla/" + fav_name;
+					string fav_path = Path.Combine (home_dir, moz_path);
+
+					
+					if (!File.Exists (fav_path)) { 
+						// HTTP request to put the favicon in the cache
+						HttpWebRequest fav_req = (HttpWebRequest) WebRequest.Create (fav_url);
+						if (proxy != null)
+							fav_req.Proxy = proxy;
+						HttpWebResponse fav_res = (HttpWebResponse) fav_req.GetResponse ();
+						Stream rec_str = fav_res.GetResponseStream ();
+
+						FileStream fs = new FileStream (fav_path, FileMode.Create);
+						BinaryWriter bw = new BinaryWriter (fs);
+						byte [] data  = new Byte [512];
+
+						int count = rec_str.Read (data,0,512);
+						bw.Write (data,0,count);
+
+						bw.Close ();
+						fs.Close ();
+					}
+
+					bk.icon = fav_path;
+				}
+	
 				bookmarks.Add (bk);
 		  
 				m = m.NextMatch ();
@@ -126,9 +238,10 @@
 					continue;
 
 				Match match = new Match ("WebLink", clue);
-				match ["Icon"] = "internal:bookmark.png";
 				match ["Title"] = bk.name;
-				match ["Url"]   = bk.url;
+				match ["URL"]   = bk.url;
+				if (bk.icon.Length != 0)
+					match ["Emblem-Icon"] = bk.icon;
 
 				matches.Add (match);
 			}
@@ -140,6 +253,7 @@
 			public string name;
 			public string lname;
 			public string url;
+			public string icon;
 		}
 
 	}


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