Compute lucene-style scores for our hits.
[beagle.git] / Tiles / TileMailMessage.cs
blob9a67001da3e70f8d0c2f0e7f430aad68095bae1a
1 //
2 // TileMailMessage.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all 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
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.Diagnostics;
30 using System.Text;
31 using System.Text.RegularExpressions;
32 using BU = Beagle.Util;
33 using GMime;
34 using Mono.Posix;
36 namespace Beagle.Tile {
38 [HitFlavor (Name="Email", Rank=1100, Emblem="emblem-mail-message.png", Color="#f5f5f5",
39 Type="MailMessage"),
40 HitFlavor (Name="Email", Rank=1100, Emblem="emblem-mail-message.png", Color="#f5f5fe",
41 Type="File", MimeType="message/rfc822")]
42 public class TileMailMessage : TileFromHitTemplate {
44 public TileMailMessage (Hit _hit) : base (_hit, "template-mail-message.html")
48 private static string GetHitProperty (Hit hit, string name)
50 // FIXME: We should handle this case better, but
51 // for now, if we match an attachment, we just want
52 // to display the properties for the parent message.
53 if (hit.ParentUri == null)
54 return hit [name];
55 else
56 return hit ["parent:" + name];
59 private string GetMailIcon ()
61 string icon;
63 if (GetHitProperty (Hit, "fixme:isAnswered") != null)
64 icon = Images.GetHtmlSourceForStock ("stock_mail-replied", 48);
65 else if (GetHitProperty (Hit, "fixme:isSeen") != null)
66 icon = Images.GetHtmlSourceForStock ("stock_mail-open", 48);
67 else
68 icon = Images.GetHtmlSourceForStock ("stock_mail", 48);
70 return icon;
73 protected override void PopulateTemplate ()
75 base.PopulateTemplate ();
77 bool sent = (GetHitProperty (Hit, "fixme:isSent") != null);
78 string str = GetHitProperty (Hit, "dc:title");
80 if (str == null)
81 str = String.Format ("<i>{0}</i>", Catalog.GetString ("No Subject"));
83 if (GetHitProperty (Hit, "_IsDeleted") != null)
84 str = "<strike>" + str + "</strike>";
86 Template["Icon"] = GetMailIcon ();
87 Template["Subject"] = str;
88 Template["ToFrom"] = sent ? Catalog.GetString ("To") : Catalog.GetString ("From");
90 // Limit the number of recipients to 3, so the
91 // tile doesn't look terrible.
92 if (sent) {
93 string[] values = Hit.GetProperties ("fixme:to");
95 if (values != null) {
96 StringBuilder sb = new StringBuilder ();
97 int i;
99 for (i = 0; i < 3 && i < values.Length; i++) {
100 if (i != 0)
101 sb.Append (", ");
103 sb.Append (values [i]);
106 if (i < values.Length)
107 sb.Append (", ...");
109 Template["Who"] = sb.ToString ();
111 } else
112 Template["Who"] = GetHitProperty (Hit, "fixme:from");
114 Template["Folder"] = GetHitProperty (Hit, "fixme:folder");
115 Template["Account"] = GetHitProperty (Hit, "fixme:account");
116 Template["SentReceived"] = sent ? Catalog.GetString ("Sent") : Catalog.GetString ("Received");
117 Template["When"] = GetHitProperty (Hit, "fixme:date");
119 if (GetHitProperty(Hit, "fixme:client") == "evolution")
120 Template ["CanReply"] = "";
122 // FIXME: Gross attachment rendering
123 if (Hit.ParentUri != null) {
124 Template["Subject"] = Hit ["fixme:attachment_title"] + " [" + Catalog.GetString ("Email attachment") + "]";
125 Template["EmailSubject"] = str;
126 Gtk.IconSize size = (Gtk.IconSize) 48;
127 string path = BU.GnomeIconLookup.LookupMimeIcon (Hit.MimeType, size);
128 Template["Icon"] = Images.GetHtmlSource (path, BU.GnomeIconLookup.GetFileMimeType (path));
131 if (GetHitProperty (Hit, "fixme:isFlagged") != null)
132 Template["FollowupIcon"] = Images.GetHtmlSourceForStock ("stock_mail-priority-high", 16);
133 if (GetHitProperty (Hit, "fixme:hasAttachments") != null)
134 Template["AttachmentIcon"] = Images.GetHtmlSourceForStock ("stock_attach", 16);
136 #if ENABLE_EVO_SHARP
137 GetImNames (Template["Who"]);
139 if (aim_name != null)
140 Template["CanSendIm"] = "";
141 #endif
143 #if ENABLE_GALAGO
144 #if ENABLE_EVO_SHARP
145 if (aim_name != null) {
146 string status = BU.GalagoTools.GetPresence ("aim", aim_name);
147 if (status != null && status != "")
148 Template ["Presence"] = status;
150 #endif
151 #endif
154 private string GetEmail (string who)
156 Regex re = new Regex (@".*<(?<email>.*)>");
157 MatchCollection matches = re.Matches (who);
158 foreach (Match match in matches) {
159 if (match.Length != 0) {
160 return match.Groups["email"].ToString ();
164 return who;
168 #if ENABLE_EVO_SHARP
169 private string aim_name;
170 private string groupwise_name;
171 private string icq_name;
172 private string jabber_name;
173 private string msn_name;
174 private string yahoo_name;
176 static bool ebook_failed = false;
178 private void GetImNames (string who)
180 if (who == null || who == "")
181 return;
183 Evolution.Book addressbook = null;
185 if (ebook_failed)
186 return;
188 try {
189 addressbook = Evolution.Book.NewSystemAddressbook ();
190 addressbook.Open (true);
191 } catch (Exception e) {
192 Console.WriteLine ("\nCould not open Evolution addressbook:\n" + e);
193 ebook_failed = true;
194 return;
197 string email = GetEmail (who);
199 System.Console.WriteLine ("Looking for im name for {0}",
200 email);
201 System.Console.WriteLine ("FIXME: This query is using the Evolution addressbook instead of querying Beagle directly. This is slow, dumb, etc.");
203 string qstr =
204 String.Format ("(is \"email\" \"{0}\")", email);
206 Evolution.BookQuery query = Evolution.BookQuery.FromString (qstr);
208 if (query == null)
209 return;
211 Evolution.Contact[] matches = addressbook.GetContacts (query);
212 foreach (Evolution.Contact c in matches) {
213 if (c.ImAim.Length > 0)
214 aim_name = c.ImAim[0];
215 if (c.ImIcq.Length > 0)
216 icq_name = c.ImIcq[0];
217 if (c.ImJabber.Length > 0)
218 jabber_name = c.ImJabber[0];
219 if (c.ImMsn.Length > 0)
220 msn_name = c.ImMsn[0];
221 if (c.ImYahoo.Length > 0)
222 yahoo_name = c.ImYahoo[0];
223 if (c.ImGroupwise.Length > 0)
224 groupwise_name = c.ImGroupwise[0];
227 #endif
229 [TileAction]
230 public override void Open ()
232 string uri_str;
234 if (GetHitProperty (Hit, "fixme:client") != "evolution") {
235 OpenFromMime (Hit);
236 return;
239 Process p = new Process ();
240 p.StartInfo.UseShellExecute = false;
241 p.StartInfo.FileName = "evolution";
243 if (Hit.ParentUriAsString != null)
244 uri_str = Hit.ParentUriAsString;
245 else
246 uri_str = Hit.UriAsString;
248 p.StartInfo.Arguments = "'" + uri_str + "'";
250 try {
251 p.Start ();
252 } catch (System.ComponentModel.Win32Exception e) {
253 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
257 [TileAction]
258 public void Mail ()
260 bool sent = (GetHitProperty (Hit, "fixme:isSent") != null);
261 string address = sent ? GetHitProperty (Hit, "fixme:to") : GetHitProperty (Hit, "fixme:from");
263 Process p = new Process ();
264 p.StartInfo.UseShellExecute = false;
265 p.StartInfo.FileName = "evolution";
266 p.StartInfo.Arguments = "'mailto:" + address + "'";
268 try {
269 p.Start ();
270 } catch (System.ComponentModel.Win32Exception e) {
271 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
275 [TileAction]
276 public void Reply ()
278 string uri_str;
280 Process p = new Process ();
281 p.StartInfo.UseShellExecute = false;
282 p.StartInfo.FileName = "evolution";
284 if (Hit.ParentUriAsString != null)
285 uri_str = Hit.ParentUriAsString;
286 else
287 uri_str = Hit.UriAsString;
289 p.StartInfo.Arguments = String.Format ("'{0};reply=sender'", uri_str);
291 try {
292 p.Start ();
293 } catch (System.ComponentModel.Win32Exception e) {
294 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
299 #if ENABLE_EVO_SHARP
300 [TileAction]
301 public void SendIm ()
303 if (aim_name != null)
304 SendImAim (aim_name);
305 if (groupwise_name != null)
306 SendImGroupwise (groupwise_name);
307 if (icq_name != null)
308 SendImIcq (icq_name);
309 if (jabber_name != null)
310 SendImJabber (jabber_name);
311 if (msn_name != null)
312 SendImMsn (msn_name);
313 if (yahoo_name != null)
314 SendImYahoo (yahoo_name);
316 #endif