File renderer



This renderer should work in conjunction with Nat's new Dewey backend.

If you have any suggestions for improvements or if you find any bugs,
please let me know.

You can download the renderer from here:
  http://www.godby.org/dashboard/FileMatchRenderer.cs

And the latest screenshot from here:
  http://www.godby.org/dashboard/file-renderer.png

-- 
Kevin Godby <godbyk yahoo com>
//
// GNOME Dashboard
//
// FileMatchRenderer.cs: Knows how to render File matches.
//
// Author:
//   Kevin Godby <godbyk yahoo com>
//

using System;
using System.Collections;
using System.Xml;
using System.IO;

[assembly:Dashboard.MatchRendererFactory ("Dashboard.FileMatchRenderer")]

namespace Dashboard {

	class FileMatchRenderer : MatchRenderer {

		public override void Startup ()
		{
			Type = "File";
		}

		public override string HTMLRenderMatches (ArrayList matches)
		{
			StringWriter sw = new StringWriter ();
			XmlWriter xw = new XmlTextWriter (sw);

			xw.WriteStartElement ("div");	// Start the File results block
			xw.WriteElementString ("u", "Files");	// Print the title

			xw.WriteStartElement ("table");	// Start the results table
			xw.WriteAttributeString ("width", "100%");
			xw.WriteAttributeString ("border", "0");
			xw.WriteAttributeString ("cellpadding", "0");
			xw.WriteAttributeString ("cellspacing", "0");
			
			bool color_band = true;
			foreach (Match m in matches) {
				HTMLRenderSingleFile (m, color_band, xw);
				color_band = !color_band;
			}

			xw.WriteEndElement ();	// End results table
			xw.WriteEndElement ();	// End File results block

			xw.Close ();

			Console.WriteLine ("-- File Renderer ----------------------------------------------------");
			Console.WriteLine (sw.ToString ());
			Console.WriteLine ("---------------------------------------------------------------------\n\n");


			return sw.ToString ();
		}

		private void HTMLRenderSingleFile (Match m, bool color_band, XmlWriter xw)
		{
			string Path    = Convert.ToString (m ["Path"]);
			string Text    = Convert.ToString (m ["Text"]);
			string Icon    = Convert.ToString (m ["Icon"]);

			// Strip protocol from file
			// FIXME: Will we ever see a non-file:// protocol?
			string File = Path;
			if (File.StartsWith ("file://"))
				File = File.Substring (7);

			// Check to make sure the file exists
			FileInfo file = new FileInfo (File);
			if (!file.Exists) {
				Console.WriteLine ("Ack!  File does not exist: {0}", Path);
				return;
			}

			// DEBUG
			Console.WriteLine ("File name: {0}", file.FullName);
			Console.WriteLine ("Creation time: {0}", file.CreationTime);
			Console.WriteLine ("Last Access time: {0}", file.LastAccessTime);
			Console.WriteLine ("Last Write Time: {0}", file.LastWriteTime);
			Console.WriteLine ("Size: {0}", file.Length);

			string LastModifiedDate = file.LastWriteTime.ToLongDateString ();

			xw.WriteStartElement ("tr");
			if (color_band)		// highlight every other row
				xw.WriteAttributeString ("bgcolor", "#eeeeee");	//originally #f6f2f6
			xw.WriteStartElement ("td");
			xw.WriteAttributeString ("valign", "top");
			xw.WriteAttributeString ("align", "center");

			// Show the file's icon (and make it a link)
			xw.WriteStartElement ("a");	// link
			xw.WriteAttributeString ("href", Path);
			xw.WriteStartElement ("img");	// icon
			xw.WriteAttributeString ("src", Icon);
			xw.WriteAttributeString ("border", "0");
			xw.WriteEndElement ();	// img
			xw.WriteEndElement ();	// a
			xw.WriteEndElement ();	// td

			xw.WriteStartElement ("td");
			xw.WriteAttributeString ("valign", "top");
			xw.WriteStartElement ("font");	// Make the font smaller to fit window width
			xw.WriteAttributeString ("size", "-1");

			// Print the filename (w/ hyperlink)
			xw.WriteStartElement ("a");
			xw.WriteAttributeString ("href", /* "file://" + */ Path);
			xw.WriteString (Text);
			xw.WriteEndElement ();	// a href
			xw.WriteStartElement ("br");
			xw.WriteEndElement ();	// br

			// Print 'Last modified: date'
			xw.WriteStartElement ("font");
			xw.WriteAttributeString ("color", "#666666");
			xw.WriteString ("Last modified: " + LastModifiedDate);
			xw.WriteEndElement ();	// font

			xw.WriteEndElement ();	// font
			xw.WriteEndElement ();	// td
			xw.WriteEndElement ();	// tr

		}
	}
}


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