Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / Tiles / TileMailMessage.cs
blob7e314685a90362f189da9dea9da81684844c2a20
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 public class TileMailMessage : TileFromHitTemplate {
42 public TileMailMessage (Hit _hit) : base (_hit, "template-mail-message.html")
46 private static string GetHitProperty (Hit hit, string name)
48 // FIXME: We should handle this case better, but
49 // for now, if we match an attachment, we just want
50 // to display the properties for the parent message.
51 if (hit.ParentUri == null)
52 return hit [name];
53 else
54 return hit ["parent:" + name];
57 private string GetMailIcon ()
59 string icon;
61 if (GetHitProperty (Hit, "fixme:isAnswered") != null)
62 icon = Images.GetHtmlSourceForStock ("stock_mail-replied", 48);
63 else if (GetHitProperty (Hit, "fixme:isSeen") != null)
64 icon = Images.GetHtmlSourceForStock ("stock_mail-open", 48);
65 else
66 icon = Images.GetHtmlSourceForStock ("stock_mail", 48);
68 return icon;
71 protected override void PopulateTemplate ()
73 base.PopulateTemplate ();
75 bool sent = (GetHitProperty (Hit, "fixme:isSent") != null);
76 string str = GetHitProperty (Hit, "dc:title");
78 if (str == null)
79 str = String.Format ("<i>{0}</i>", Catalog.GetString ("No Subject"));
81 if (GetHitProperty (Hit, "_IsDeleted") != null)
82 str = "<strike>" + str + "</strike>";
84 Template["Icon"] = GetMailIcon ();
85 Template["Subject"] = str;
86 Template["ToFrom"] = sent ? Catalog.GetString ("To") : Catalog.GetString ("From");
88 // Limit the number of recipients to 3, so the
89 // tile doesn't look terrible.
90 if (sent) {
91 string[] values = Hit.GetProperties ("fixme:to");
93 if (values != null) {
94 StringBuilder sb = new StringBuilder ();
95 int i;
97 for (i = 0; i < 3 && i < values.Length; i++) {
98 if (i != 0)
99 sb.Append (", ");
101 sb.Append (values [i]);
104 if (i < values.Length)
105 sb.Append (", ...");
107 Template["Who"] = sb.ToString ();
109 } else
110 Template["Who"] = GetHitProperty (Hit, "fixme:from");
112 Template["Folder"] = GetHitProperty (Hit, "fixme:folder");
113 Template["Account"] = GetHitProperty (Hit, "fixme:account");
114 Template["SentReceived"] = sent ? Catalog.GetString ("Sent") : Catalog.GetString ("Received");
115 Template["When"] = sent ? GetHitProperty (Hit, "fixme:sentdate") : GetHitProperty (Hit, "fixme:received");
117 // FIXME: Gross attachment rendering
118 if (Hit.ParentUri != null) {
119 Template["Subject"] = Hit ["fixme:attachment_title"] + " [" + Catalog.GetString ("Email attachment") + "]";
120 Template["EmailSubject"] = str;
121 Gtk.IconSize size = (Gtk.IconSize) 48;
122 string path = BU.GnomeIconLookup.LookupMimeIcon (Hit.MimeType, size);
123 Template["Icon"] = Images.GetHtmlSource (path, BU.GnomeIconLookup.GetFileMimeType (path));
126 if (GetHitProperty (Hit, "fixme:isFlagged") != null)
127 Template["FollowupIcon"] = Images.GetHtmlSourceForStock ("stock_mail-priority-high", 16);
128 if (GetHitProperty (Hit, "fixme:hasAttachments") != null)
129 Template["AttachmentIcon"] = Images.GetHtmlSourceForStock ("stock_attach", 16);
131 #if ENABLE_EVO_SHARP
132 GetImNames (Template["Who"]);
134 if (aim_name != null)
135 Template["CanSendIm"] = "";
136 #endif
138 #if ENABLE_GALAGO
139 #if ENABLE_EVO_SHARP
140 if (aim_name != null) {
141 string status = BU.GalagoTools.GetPresence ("aim", aim_name);
142 if (status != null && status != "")
143 Template ["Presence"] = status;
145 #endif
146 #endif
149 private string GetEmail (string who)
151 Regex re = new Regex (@".*<(?<email>.*)>");
152 MatchCollection matches = re.Matches (who);
153 foreach (Match match in matches) {
154 if (match.Length != 0) {
155 return match.Groups["email"].ToString ();
159 return who;
163 #if ENABLE_EVO_SHARP
164 private string aim_name;
165 private string groupwise_name;
166 private string icq_name;
167 private string jabber_name;
168 private string msn_name;
169 private string yahoo_name;
171 static bool ebook_failed = false;
173 private void GetImNames (string who)
175 if (who == null || who == "")
176 return;
178 Evolution.Book addressbook = null;
180 if (ebook_failed)
181 return;
183 try {
184 addressbook = Evolution.Book.NewSystemAddressbook ();
185 addressbook.Open (true);
186 } catch (Exception e) {
187 Console.WriteLine ("\nCould not open Evolution addressbook:\n" + e);
188 ebook_failed = true;
189 return;
192 string email = GetEmail (who);
194 System.Console.WriteLine ("Looking for im name for {0}",
195 email);
196 System.Console.WriteLine ("FIXME: This query is using the Evolution addressbook instead of querying Beagle directly. This is slow, dumb, etc.");
198 string qstr =
199 String.Format ("(is \"email\" \"{0}\")", email);
201 Evolution.BookQuery query = Evolution.BookQuery.FromString (qstr);
203 if (query == null)
204 return;
206 Evolution.Contact[] matches = addressbook.GetContacts (query);
207 foreach (Evolution.Contact c in matches) {
208 if (c.ImAim.Length > 0)
209 aim_name = c.ImAim[0];
210 if (c.ImIcq.Length > 0)
211 icq_name = c.ImIcq[0];
212 if (c.ImJabber.Length > 0)
213 jabber_name = c.ImJabber[0];
214 if (c.ImMsn.Length > 0)
215 msn_name = c.ImMsn[0];
216 if (c.ImYahoo.Length > 0)
217 yahoo_name = c.ImYahoo[0];
218 if (c.ImGroupwise.Length > 0)
219 groupwise_name = c.ImGroupwise[0];
222 #endif
224 [TileAction]
225 public override void Open ()
227 string uri_str;
229 Process p = new Process ();
230 p.StartInfo.UseShellExecute = false;
231 p.StartInfo.FileName = "evolution";
233 if (Hit.ParentUriAsString != null)
234 uri_str = Hit.ParentUriAsString;
235 else
236 uri_str = Hit.UriAsString;
238 p.StartInfo.Arguments = "'" + uri_str + "'";
240 try {
241 p.Start ();
242 } catch (System.ComponentModel.Win32Exception e) {
243 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
247 [TileAction]
248 public void Mail ()
250 bool sent = (GetHitProperty (Hit, "fixme:isSent") != null);
251 string address = sent ? GetHitProperty (Hit, "fixme:to") : GetHitProperty (Hit, "fixme:from");
253 Process p = new Process ();
254 p.StartInfo.UseShellExecute = false;
255 p.StartInfo.FileName = "evolution";
256 p.StartInfo.Arguments = "'mailto:" + address + "'";
258 try {
259 p.Start ();
260 } catch (System.ComponentModel.Win32Exception e) {
261 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
265 [TileAction]
266 public void Reply ()
268 string uri_str;
270 Process p = new Process ();
271 p.StartInfo.UseShellExecute = false;
272 p.StartInfo.FileName = "evolution";
274 if (Hit.ParentUriAsString != null)
275 uri_str = Hit.ParentUriAsString;
276 else
277 uri_str = Hit.UriAsString;
279 p.StartInfo.Arguments = String.Format ("'{0};reply=sender'", uri_str);
281 try {
282 p.Start ();
283 } catch (System.ComponentModel.Win32Exception e) {
284 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
289 #if ENABLE_EVO_SHARP
290 [TileAction]
291 public void SendIm ()
293 if (aim_name != null)
294 SendImAim (aim_name);
295 if (groupwise_name != null)
296 SendImGroupwise (groupwise_name);
297 if (icq_name != null)
298 SendImIcq (icq_name);
299 if (jabber_name != null)
300 SendImJabber (jabber_name);
301 if (msn_name != null)
302 SendImMsn (msn_name);
303 if (yahoo_name != null)
304 SendImYahoo (yahoo_name);
306 #endif