Add a new archived file tile to beagle-search. Still needs a little love.
[beagle.git] / search / Tiles / MailMessage.cs
blob77abec5ae59cfb08b0f2f6827f5595fc9790745e
1 using System;
2 using System.Diagnostics;
3 using System.IO;
4 using Mono.Unix;
5 using Beagle.Util;
7 namespace Search.Tiles {
9 public class MailMessageActivator : TileActivator {
11 public MailMessageActivator () : base ()
13 AddSupportedFlavor (new HitFlavor (null, null, "message/rfc822"));
15 // This one allows us to handle HTML child indexables
16 // that contain the actual body of the message as real
17 // mail tiles. We don't have to worry about them also
18 // appearing as mail attachments because they aren't
19 // considered attachments during indexing.
20 AddSupportedFlavor (new HitFlavor (null, "MailMessage", "text/html"));
23 public override bool Validate (Beagle.Hit hit)
25 if (! base.Validate (hit))
26 return false;
28 if (hit ["beagle:HitType"] == "File") {
29 // This handles a case when a file with the
30 // message/rfc822 mimetype is indexed without
31 // gmime. Thus we fail to extract any info and
32 // this tile is useless.
33 string subject = hit.GetFirstProperty ("dc:title");
35 if (subject != null && subject != "")
36 return true;
38 return false;
41 return true;
44 public override Tile BuildTile (Beagle.Hit hit, Beagle.Query query)
46 return new MailMessage (hit, query);
50 public class MailMessage : TileFlat {
52 public MailMessage (Beagle.Hit hit, Beagle.Query query) : base (hit, query)
54 Group = TileGroup.Conversations;
56 string title = Utils.GetFirstPropertyOfParent (hit, "dc:title");
57 if (title == null || title == String.Empty)
58 title = Catalog.GetString ("(untitled)");
59 Subject.LabelProp = Title = title;
61 From.LabelProp = "<b>" + GetAddress (hit) + "</b>";
62 try {
63 Timestamp = Utils.ParseTimestamp (Utils.GetFirstPropertyOfParent (hit, "fixme:date"));
64 Date.LabelProp = Utils.NiceShortDate (Timestamp);
65 } catch {}
67 if (Utils.GetFirstPropertyOfParent (Hit, "fixme:client") == "evolution")
68 AddAction (new TileAction (Catalog.GetString ("Send in Mail"), SendInMail));
71 protected override void LoadIcon (Gtk.Image image, int size)
73 if (Utils.GetFirstPropertyOfParent (Hit, "fixme:isAnswered") != null)
74 image.Pixbuf = WidgetFu.LoadThemeIcon ("stock_mail-replied", size);
75 else if (Utils.GetFirstPropertyOfParent (Hit, "fixme:isSeen") != null)
76 image.Pixbuf = WidgetFu.LoadThemeIcon ("stock_mail-open", size);
77 else
78 image.Pixbuf = WidgetFu.LoadThemeIcon ("stock_mail", size);
81 private static string GetAddress (Beagle.Hit hit)
83 bool sent = (Utils.GetFirstPropertyOfParent (hit, "fixme:isSent") != null);
84 string address = sent ? Utils.GetFirstPropertyOfParent (hit, "fixme:to") : Utils.GetFirstPropertyOfParent (hit, "fixme:from");
86 if (address == null)
87 return "";
88 if (address.IndexOf (" <") != -1)
89 address = address.Substring (0, address.IndexOf (" <"));
91 return address;
94 protected override DetailsPane GetDetails ()
96 DetailsPane details = new DetailsPane ();
98 bool sent = (Utils.GetFirstPropertyOfParent (Hit, "fixme:isSent") != null);
100 details.AddLabelPair (Catalog.GetString ("Subject:"), SubjectLabel.Text);
102 string label = sent ? Catalog.GetString ("To:") : Catalog.GetString ("From:");
103 details.AddLabelPair (label, GetAddress (Hit));
105 label = sent ? Catalog.GetString ("Date Sent:") : Catalog.GetString ("Date Received:");
106 details.AddLabelPair (label, Utils.NiceLongDate (Timestamp));
108 string folder = Hit.GetFirstProperty ("fixme:folder");
110 if (folder != null)
111 details.AddLabelPair (Catalog.GetString ("Folder:"), folder);
113 details.AddSnippet ();
115 return details;
118 public static SafeProcess GetClientProcess (string client, string uri)
120 SafeProcess p = null;
122 if (client == "evolution") {
123 p = new SafeProcess ();
124 p.Arguments = new string [2];
125 p.Arguments [0] = "evolution";
126 p.Arguments [1] = uri;
127 #if ENABLE_THUNDERBIRD
128 } else if (client == "thunderbird") {
129 p = new SafeProcess ();
130 p.Arguments = new string [3];
131 p.Arguments [0] = Thunderbird.ExecutableName;
132 p.Arguments [1] = "-mail";
133 p.Arguments [2] = uri;
134 #endif
137 return p;
140 public override void Open ()
142 SafeProcess p;
143 if (Hit.ParentUri != null)
144 p = GetClientProcess (Utils.GetFirstPropertyOfParent (Hit, "fixme:client"), Hit.EscapedParentUri);
145 else
146 p = GetClientProcess (Utils.GetFirstPropertyOfParent (Hit, "fixme:client"), Hit.EscapedUri);
148 if (p == null) {
149 OpenFromMime (Hit);
150 return;
153 try {
154 p.Start ();
155 } catch (SafeProcessException e) {
156 Console.WriteLine ("Unable to run {0}: {1}", p.Arguments [0], e.Message);
160 public void SendInMail ()
162 SafeProcess p = new SafeProcess ();
164 string uri;
165 if (Hit.ParentUri != null)
166 uri = Hit.EscapedParentUri;
167 else
168 uri = Hit.EscapedUri;
170 p.Arguments = new string [] { "evolution", String.Format ("{0};forward=attached", uri) };
172 try {
173 p.Start () ;
174 } catch (Exception e) {
175 Console.WriteLine ("Error launching Evolution composer: " + e.Message);