* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / Renderers / HitRendererHtml.cs
blob25650f86f9268cea88e812df4f9bf166c6b71cf5
1 //
2 // HtmlRenderer.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
28 using System;
29 using System.Collections;
30 using System.Diagnostics;
31 using System.IO;
32 using System.Reflection;
33 using System.Net;
35 using Gtk;
37 using BU = Beagle.Util;
39 namespace Beagle {
41 public class HitRendererHtml : HitRenderer {
43 private Gtk.HTML html = new Gtk.HTML ();
44 private Hashtable tiles = new Hashtable ();
46 public HitRendererHtml ()
48 this.html.LinkClicked += new LinkClickedHandler (LinkClicked);
49 this.html.UrlRequested += new UrlRequestedHandler (UrlRequested);
52 ////////////////////////////////////
54 private Tile HitToTile (Hit hit)
56 Tile t = null;
58 // FIXME: We don't want to allow heterogenous containers.
60 switch (hit.Type) {
62 case "Contact":
63 t = new Tile ("contact.html", hit);
64 break;
66 case "File":
67 t = new Tile ("file-generic.html", hit);
68 break;
70 case "IMLog":
71 t = new Tile ("im-log.html", hit);
72 break;
74 case "MailMessage":
75 string icon = "mail.png";
76 if (hit ["_IsAnswered"] != null)
77 icon = "mail-replied.png";
78 else if (hit ["_IsSeen"] != null)
79 icon = "mail-read.png";
80 hit ["mail:Icon"] = icon;
82 if (hit ["_IsSent"] != null)
83 t = new Tile ("email-sent.html", hit);
84 else
85 t = new Tile ("email.html", hit);
86 break;
88 case "WebHistory":
89 t = new Tile ("web-history.html", hit);
90 break;
92 case "WebLink":
93 if (hit.Source == "Google")
94 t = new Tile ("google.html", hit);
95 break;
98 return t;
101 protected override bool ProcessHit (Hit hit)
103 Tile tile = HitToTile (hit);
104 if (tile == null)
105 return false;
107 tiles [hit] = tile;
108 return true;
111 protected override void ProcessClear ()
113 tiles.Clear ();
116 ////////////////////////////////////
118 public override Gtk.Widget Widget {
119 get { return html; }
122 protected override void DoRefresh ()
124 Gtk.HTMLStream stream = html.Begin ();
125 stream.Write ("<html><body>");
126 if (DisplayedCount > 0) {
127 // FIXME: layout in a table, or something
128 for (int i = FirstDisplayed; i <= LastDisplayed; ++i) {
129 Hit hit = (Hit) Hits [i];
130 Tile t = (Tile) tiles [hit];
131 stream.Write (t.Html);
134 stream.Write ("</body></html>");
137 ///////////////////////////////////
140 // Provides data for urls requested (images). Things prefixed
141 // with `internal:' we pull for one of the embedded streams
143 private void UrlRequested (object o, UrlRequestedArgs args)
145 Stream s = DataBarn.GetStream (args.Url);
146 if (s == null) {
147 Console.WriteLine ("Could not obtain image '{0}'", args.Url);
148 return;
151 byte [] buffer = new byte [8192];
152 int n;
153 while ( (n = s.Read (buffer, 0, 8192)) != 0)
154 args.Handle.Write (buffer, n);
157 private void LinkClicked (object o, LinkClickedArgs args)
159 String command = null, arguments = null;
161 if (args.Url.StartsWith ("exec:")) {
162 command = args.Url.Substring ("exec:".Length);
163 int i = command.IndexOf (' ');
164 if (i != -1) {
165 arguments = command.Substring (i+1);
166 command = command.Substring (0, i);
168 } else if (args.Url.StartsWith ("http")) {
169 command = "epiphany";
170 arguments = args.Url;
171 } else if (args.Url.StartsWith ("email")) {
172 command = "evolution-1.5";
173 arguments = args.Url;
174 } else if (args.Url.StartsWith ("mailto:")) {
175 command = "evolution-1.5";
176 arguments = args.Url;
177 } else if (args.Url.StartsWith ("file://")) {
178 // Hacky: we extract the mime type from inside of
179 // the file Url.
180 arguments = args.Url.Substring ("file://".Length);
181 int i = arguments.IndexOf (' ');
182 String mimeType;
183 if (i == -1) {
184 mimeType = BU.GnomeIconLookup.GetMimeType (arguments);
185 } else {
186 mimeType = arguments.Substring (i+1);
187 arguments = arguments.Substring (0, i);
190 // Try to open w/ the default handler
191 BU.GnomeVFSMimeApplication app;
192 app = BU.GnomeIconLookup.GetDefaultAction (mimeType);
193 command = app.command;
196 Console.WriteLine ("Command=[{0}] Args=[{1}]", command, arguments);
198 if (command != null) {
199 Process p = new Process ();
200 p.StartInfo.UseShellExecute = false;
201 p.StartInfo.FileName = command;
202 if (arguments != null)
203 p.StartInfo.Arguments = arguments;
204 try {
205 p.Start ();
206 } catch { }
207 return;