Fix a potential crash in html parsing code. How come it was never reported ?
[beagle.git] / Tiles / TileMailMessage.cs
blobdf5335a7b34b0f4284e4c59ad5d618477c1bdea4
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 Beagle.Util;
33 using GMime;
34 using Mono.Unix;
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 bool IsAttachment (Hit hit)
50 // check if there is parent and parent has attachments
51 string str = hit ["parent:fixme:hasAttachments"];
52 return (hit.ParentUri != null && str != null && (str == "true"));
55 private static string GetHitProperty (Hit hit, string name)
57 // FIXME: We should handle this case better, but
58 // for now, if we match an attachment, we just want
59 // to display the properties for the parent message.
60 if (!IsAttachment (hit))
61 return hit [name];
62 else
63 return hit ["parent:" + name];
66 private string GetMailIcon ()
68 string icon;
70 if (GetHitProperty (Hit, "fixme:isAnswered") != null)
71 icon = Images.GetHtmlSourceForStock ("stock_mail-replied", 48);
72 else if (GetHitProperty (Hit, "fixme:isSeen") != null)
73 icon = Images.GetHtmlSourceForStock ("stock_mail-open", 48);
74 else
75 icon = Images.GetHtmlSourceForStock ("stock_mail", 48);
77 return icon;
80 protected override void PopulateTemplate ()
82 base.PopulateTemplate ();
84 bool sent = (GetHitProperty (Hit, "fixme:isSent") != null);
85 string str = GetHitProperty (Hit, "dc:title");
87 if (str == null)
88 str = String.Format ("<i>{0}</i>", Catalog.GetString ("No Subject"));
90 if (GetHitProperty (Hit, "_IsDeleted") != null)
91 str = "<strike>" + str + "</strike>";
93 Template["Icon"] = GetMailIcon ();
94 Template["Subject"] = str;
95 Template["ToFrom"] = sent ? Catalog.GetString ("To") : Catalog.GetString ("From");
97 // Limit the number of recipients to 3, so the
98 // tile doesn't look terrible.
99 if (sent) {
100 string[] values = Hit.GetProperties ("fixme:to");
102 if (values != null) {
103 StringBuilder sb = new StringBuilder ();
104 int i;
106 for (i = 0; i < 3 && i < values.Length; i++) {
107 if (i != 0)
108 sb.Append (", ");
110 sb.Append (values [i]);
113 if (i < values.Length)
114 sb.Append (", ...");
116 Template["Who"] = sb.ToString ();
118 } else
119 Template["Who"] = GetHitProperty (Hit, "fixme:from");
121 Template["Folder"] = GetHitProperty (Hit, "fixme:folder");
122 Template["Account"] = GetHitProperty (Hit, "fixme:account");
123 Template["SentReceived"] = sent ? Catalog.GetString ("Sent") : Catalog.GetString ("Received");
124 Template["When"] = GetHitProperty (Hit, "fixme:date");
126 if (GetHitProperty(Hit, "fixme:client") == "evolution")
127 Template ["CanReply"] = "";
129 // FIXME: Gross attachment rendering
130 if (IsAttachment (Hit)) {
131 Template["Subject"] = Hit ["fixme:attachment_title"] + " [" + Catalog.GetString ("Email attachment") + "]";
132 Template["EmailSubject"] = str;
133 string path = GnomeFu.GetMimeIconPath (Hit.MimeType);
134 Template["Icon"] = Images.GetHtmlSource (path, Hit.MimeType);
137 if (GetHitProperty (Hit, "fixme:isFlagged") != null)
138 Template["FollowupIcon"] = Images.GetHtmlSourceForStock ("stock_mail-priority-high", 16);
139 if (GetHitProperty (Hit, "fixme:hasAttachments") != null)
140 Template["AttachmentIcon"] = Images.GetHtmlSourceForStock ("stock_attach", 16);
142 #if ENABLE_EVO_SHARP
143 GetImNames (Template["Who"]);
145 if (aim_name != null)
146 Template["CanSendIm"] = "";
147 #endif
149 #if ENABLE_GALAGO
150 #if ENABLE_EVO_SHARP
151 if (aim_name != null) {
152 string status = GalagoTools.GetPresence ("aim", aim_name);
153 if (status != null && status != "")
154 Template ["Presence"] = status;
156 #endif
157 #endif
160 #if ENABLE_EVO_SHARP
161 private string GetEmail (string who)
163 Regex re = new Regex (@".*<(?<email>.*)>");
164 MatchCollection matches = re.Matches (who);
165 foreach (Match match in matches) {
166 if (match.Length != 0) {
167 return match.Groups["email"].ToString ();
171 return who;
174 private string aim_name;
175 private string groupwise_name;
176 private string icq_name;
177 private string jabber_name;
178 private string msn_name;
179 private string yahoo_name;
181 static bool ebook_failed = false;
183 private void GetImNames (string who)
185 if (who == null || who == "")
186 return;
188 Evolution.Book addressbook = null;
190 if (ebook_failed)
191 return;
193 try {
194 addressbook = Evolution.Book.NewSystemAddressbook ();
195 addressbook.Open (true);
196 } catch (Exception e) {
197 Console.WriteLine ("\nCould not open Evolution addressbook:\n" + e);
198 ebook_failed = true;
199 return;
202 string email = GetEmail (who);
204 System.Console.WriteLine ("Looking for im name for {0}",
205 email);
206 System.Console.WriteLine ("FIXME: This query is using the Evolution addressbook instead of querying Beagle directly. This is slow, dumb, etc.");
208 string qstr =
209 String.Format ("(is \"email\" \"{0}\")", email);
211 Evolution.BookQuery query = Evolution.BookQuery.FromString (qstr);
213 if (query == null)
214 return;
216 Evolution.Contact[] matches = addressbook.GetContacts (query);
217 foreach (Evolution.Contact c in matches) {
218 if (c.ImAim.Length > 0)
219 aim_name = c.ImAim[0];
220 if (c.ImIcq.Length > 0)
221 icq_name = c.ImIcq[0];
222 if (c.ImJabber.Length > 0)
223 jabber_name = c.ImJabber[0];
224 if (c.ImMsn.Length > 0)
225 msn_name = c.ImMsn[0];
226 if (c.ImYahoo.Length > 0)
227 yahoo_name = c.ImYahoo[0];
228 if (c.ImGroupwise.Length > 0)
229 groupwise_name = c.ImGroupwise[0];
232 #endif
234 [TileAction]
235 public override void Open ()
237 string uri_str;
239 if (GetHitProperty (Hit, "fixme:client") != "evolution") {
240 OpenFromMime (Hit);
241 return;
244 Process p = new Process ();
245 p.StartInfo.UseShellExecute = false;
246 p.StartInfo.FileName = "evolution";
248 if (Hit.ParentUriAsString != null)
249 uri_str = Hit.ParentUriAsString;
250 else
251 uri_str = Hit.UriAsString;
253 p.StartInfo.Arguments = "'" + uri_str + "'";
255 try {
256 p.Start ();
257 } catch (System.ComponentModel.Win32Exception e) {
258 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
262 [TileAction]
263 public void Mail ()
265 bool sent = (GetHitProperty (Hit, "fixme:isSent") != null);
266 string address = sent ? GetHitProperty (Hit, "fixme:to") : GetHitProperty (Hit, "fixme:from");
268 Process p = new Process ();
269 p.StartInfo.UseShellExecute = false;
270 p.StartInfo.FileName = "evolution";
271 p.StartInfo.Arguments = "'mailto:" + address + "'";
273 try {
274 p.Start ();
275 } catch (System.ComponentModel.Win32Exception e) {
276 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
280 [TileAction]
281 public void Reply ()
283 string uri_str;
285 Process p = new Process ();
286 p.StartInfo.UseShellExecute = false;
287 p.StartInfo.FileName = "evolution";
289 if (Hit.ParentUriAsString != null)
290 uri_str = Hit.ParentUriAsString;
291 else
292 uri_str = Hit.UriAsString;
294 p.StartInfo.Arguments = String.Format ("'{0};reply=sender'", uri_str);
296 try {
297 p.Start ();
298 } catch (System.ComponentModel.Win32Exception e) {
299 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
304 #if ENABLE_EVO_SHARP
305 [TileAction]
306 public void SendIm ()
308 if (aim_name != null)
309 SendImAim (aim_name);
310 if (groupwise_name != null)
311 SendImGroupwise (groupwise_name);
312 if (icq_name != null)
313 SendImIcq (icq_name);
314 if (jabber_name != null)
315 SendImJabber (jabber_name);
316 if (msn_name != null)
317 SendImMsn (msn_name);
318 if (yahoo_name != null)
319 SendImYahoo (yahoo_name);
321 #endif