Patch for ephybookmarks backend



Hello.

The attached patch (or full .cs) adds a File.Exists check on the favicon
file.  This should fix bug #128935.

Could someone add this to cvs please?

-- 
Kevin Godby <godbyk yahoo com>
--- /home/kevin/temp/dashboard/backends/backend-ephybookmarks.cs	2003-11-17 08:23:27.000000000 -0600
+++ backend-ephybookmarks.cs	2003-12-10 01:46:13.000000000 -0600
@@ -143,6 +143,12 @@
 				string path = Path.Combine (home_dir, ".gnome2/epiphany/favicon_cache");
 				string iconfile = Path.Combine (path, favicon.InnerText);
 
+				// Fixes bug #128935
+				if (! File.Exists (iconfile)) {
+					iconfile = "internal:bookmark.png";
+					return iconfile;
+				}
+
 				Gdk.Pixbuf icon = new Pixbuf (iconfile);
 				Gdk.Pixbuf bookmark =
 					new Pixbuf (GetImage ("bookmark.png"));
//
// backend-ephybookmarks.cs: Epiphany bookmarks backend.  Reads from
// ~/.gnome2/epiphany/bookmarks.rdf.
//
// Authors:
//     Jonas Heylen <jonas heylen pandora be> 
//


using System;
using System.IO;
using System.Xml;
using Gdk;
using System.Reflection;

namespace Dashboard {

	class EpiphanyBookmarks : BackendSimple {
		XmlDocument         doc;
		XmlNodeList         items;
		XmlNamespaceManager nsm;

		XmlDocument         favicondoc;
		
		public override bool Startup ()
		{
			Name = "Epiphany bookmarks";

			string home_dir = Environment.GetEnvironmentVariable ("HOME");
			string path;

			try {
				path = Path.Combine (home_dir, ".gnome2/epiphany/bookmarks.rdf");

				if (!File.Exists (path))
					return false;

				doc = new XmlDocument ();
				doc.Load (path);

				nsm = new XmlNamespaceManager (doc.NameTable);
				nsm.AddNamespace ("rss", "http://purl.org/rss/1.0/";);

				items = doc.SelectNodes ("//rss:item", nsm);

				Console.WriteLine ("Epiphany bookmarks backend loaded {0} bookmarks.", items.Count);

                                 path = Path.Combine (home_dir, ".gnome2/epiphany/ephy-favicon-cache.xml");
                                 if (File.Exists (path)) {
					 favicondoc = new XmlDocument ();
					 favicondoc.Load (path);
				 }

				// FIXME: need to subscribe to clues that we
				// should try matching against. Probably
				// textblock and sentence_at_point:
				// this.SubscribeToClues ("textblock", "sentence_at_point");

				this.Initialized = true;

				return true;
			} catch {
				return false;
			}
		}

		protected override Match ProcessClueSimple (Clue clue)
		{

			if (! this.Initialized)
				return null;

			if (clue.Text.Length == 0)
				return null;

			string clue_text = clue.Text.ToLower ();

			string html = "<table border=0 width=100%><tr><td><h2><u>Bookmarks</u></h2></td></tr></table>";

			int match_count = 0;
			foreach (XmlNode node in items) {
				string t = node.SelectSingleNode ("rss:title", nsm).InnerText;
				string lower = t.ToLower ();
				
				if (lower.IndexOf (clue_text) == -1)
					continue;
				
				string url = node.SelectSingleNode ("rss:link", nsm).InnerText;

				string icon = this.GetIconPath (url);

				html += String.Format (
"<table border=0 cellpadding=0 cellspacing=0>" +
"<tr>" +
"    <td valign=center>" +
"        <a href=\"{1}\"><img src=\"{2}\" border=0></a>" +
"    </td>" +
"<td>&nbsp;&nbsp;</td>" +
"    <td valign=top>" +
"        <a href=\"{1}\" style=\"text-decoration: none;\">{0}<br>" +
"        <font size=-1 color=#666666>{1}</font></a>" +
"    </td>" +
"</tr>" +
"</table>", t, url, icon);

				match_count ++;
			}

			if (match_count == 0)
				return null;

			return new Match (this, html, 0, String.Format ("bookmark:{0}", clue_text));
		}

		private Stream GetImage (string name)
                {
                        Assembly assembly = System.Reflection.Assembly.GetEntryAssembly ();
			Console.WriteLine ("ASSEMBLY NAMED " + assembly.FullName);
                        System.IO.Stream s = assembly.GetManifestResourceStream (name);

                        return s;
                }

		private string GetIconPath (string url)
		{
			if (favicondoc == null)
			        return "internal:bookmark.png";

			int index = url.IndexOf ("/", 7);
			Console.WriteLine (String.Format ("url is {0}; index is {1}", url, index));
			if (index > -1)
			   url = url.Substring (0, index);
			Console.WriteLine ("url base: " + url);

			string xpath = "descendant::node[starts-with(child::property[1]/child::text(), '" + url + "')]";
			Console.WriteLine ("xpath expression: " + xpath);
			XmlNode fav_node = favicondoc.SelectSingleNode (xpath);

			if (fav_node != null) {
				xpath = "child::property[position()=2]";
				XmlNode favicon = fav_node.SelectSingleNode (xpath);
				string home_dir = Environment.GetEnvironmentVariable ("HOME");
				string path = Path.Combine (home_dir, ".gnome2/epiphany/favicon_cache");
				string iconfile = Path.Combine (path, favicon.InnerText);

				// Fixes bug #128935
				if (! File.Exists (iconfile)) {
					iconfile = "internal:bookmark.png";
					return iconfile;
				}

				Gdk.Pixbuf icon = new Pixbuf (iconfile);
				Gdk.Pixbuf bookmark =
					new Pixbuf (GetImage ("bookmark.png"));
				Gdk.Pixbuf white =
					new Pixbuf (GetImage ("white.png"));

      				white.Composite (bookmark,
		     			0, 0, //dest x,y
		     			48, 20, //height,width
		     			0, 0, //offset x,y
		     			1, 1, // scaling x,y
		     			Gdk.InterpType.Bilinear,
		     			127); //Alpha

				// I just want to make the icon be 16x16.
				// This does it for me!
      				Gdk.Pixbuf small_icon = icon.ScaleSimple (
					16, 16, //dest x,y
					Gdk.InterpType.Bilinear);

				small_icon.Composite(bookmark,
		     			0, 0, //dest x,y
					 48, 18, //height,width
					 31, 2, //offset x,y
		     			1, 1, // scaling x,y
		     			Gdk.InterpType.Bilinear,
		     			255); //Alpha

				iconfile = Path.GetFileName (iconfile);
				string home = Environment.GetEnvironmentVariable ("HOME");
				iconfile = Path.Combine( Path.Combine (home,
				String.Format (".dashboard/tmp/")),
				iconfile.ToString());

				bookmark.Savev (iconfile, "png", null, null);
				return iconfile;
			}

			return "internal:bookmark.png";
		}
	}
}


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