2005-04-19 Gabor Kelemen <kelemeng@gnome.hu>
[beagle.git] / Tiles / TileMailMessage.cs
blob12a99f69348b0d229aabd7a6f3ae3fd321543b25
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 #if ENABLE_EVO_SHARP
43 string aim_name;
44 string groupwise_name;
45 string icq_name;
46 string jabber_name;
47 string msn_name;
48 string yahoo_name;
50 static bool ebook_failed = false;
51 #endif
53 public TileMailMessage (Hit _hit) : base (_hit,
54 "template-mail-message.html")
58 protected override void PopulateTemplate ()
60 base.PopulateTemplate ();
62 bool sent = (Hit ["fixme:isSent"] != null);
64 string str;
66 str = Hit ["fixme:subject"];
67 if (str == null)
68 str = String.Format ("<i>{0}</i>", Catalog.GetString ("No Subject"));
69 if (Hit ["_IsDeleted"] != null)
70 str = "<strike>" + str + "</strike>";
71 Template["Subject"] = str;
73 Template["ToFrom"] = sent ? Catalog.GetString ("To") : Catalog.GetString ("From");
75 if (sent) {
76 // Limit the number of recipients to 3, so the
77 // tile doesn't look terrible.
78 ICollection list = InternetAddress.ParseString (Hit ["fixme:to"]);
80 if (list.Count <= 3)
81 Template["Who"] = Hit["fixme:to"];
82 else {
83 StringBuilder sb = new StringBuilder ();
85 int count = 0;
86 foreach (InternetAddress ia in list) {
87 sb.Append (ia.ToString (false));
88 sb.Append (", ");
90 ++count;
91 if (count == 3)
92 break;
95 sb.Append ("et al");
97 Template["Who"] = sb.ToString ();
99 } else
100 Template["Who"] = Hit ["fixme:from"];
102 Template["Folder"] = Hit ["fixme:folder"];
103 Template["Account"] = Hit ["fixme:account"];
104 Template["SentReceived"] = sent ? Catalog.GetString ("Sent") : Catalog.GetString ("Received");
105 Template["When"] = sent ? Hit ["fixme:sentdate"] : Hit ["fixme:received"];
107 string icon;
108 if (Hit ["fixme:isAnswered"] != null)
109 icon = Images.GetHtmlSourceForStock ("stock_mail-replied", 48);
110 else if (Hit ["fixme:isSeen"] != null)
111 icon = Images.GetHtmlSourceForStock ("stock_mail-open", 48);
112 else
113 icon = Images.GetHtmlSourceForStock ("stock_mail", 48);
115 Template["Icon"] = icon;
116 if (Hit ["fixme:isFlagged"] != null)
117 Template["FollowupIcon"] = Images.GetHtmlSourceForStock ("stock_mail-priority-high", 16);
118 if (Hit ["fixme:hasAttachments"] != null)
119 Template["AttachmentIcon"] = Images.GetHtmlSourceForStock ("stock_attach", 16);
121 GetImNames (Template["Who"]);
123 #if ENABLE_EVO_SHARP
124 if (aim_name != null)
125 Template["CanSendIm"] = "";
126 #endif
129 private string GetEmail (string who)
131 Regex re = new Regex (@".*<(?<email>.*)>");
132 MatchCollection matches = re.Matches (who);
133 foreach (Match match in matches) {
134 if (match.Length != 0) {
135 return match.Groups["email"].ToString ();
139 return who;
142 private void GetImNames (string who)
144 #if ENABLE_EVO_SHARP
145 Evolution.Book addressbook = null;
147 if (ebook_failed)
148 return;
150 try {
151 addressbook = Evolution.Book.NewSystemAddressbook ();
152 addressbook.Open (true);
153 } catch (Exception e) {
154 Console.WriteLine ("\nCould not open Evolution addressbook:\n" + e);
155 ebook_failed = true;
156 return;
159 string email = GetEmail (who);
161 System.Console.WriteLine ("Looking for im name for {0}",
162 email);
163 System.Console.WriteLine ("FIXME: This query is using the Evolution addressbook instead of querying Beagle directly. This is slow, dumb, etc.");
165 string qstr =
166 String.Format ("(is \"email\" \"{0}\")", email);
168 Evolution.BookQuery query = Evolution.BookQuery.FromString (qstr);
170 if (query == null)
171 return;
173 Evolution.Contact[] matches = addressbook.GetContacts (query);
174 foreach (Evolution.Contact c in matches) {
175 if (c.ImAim.Length > 0)
176 aim_name = c.ImAim[0];
177 if (c.ImIcq.Length > 0)
178 icq_name = c.ImIcq[0];
179 if (c.ImJabber.Length > 0)
180 jabber_name = c.ImJabber[0];
181 if (c.ImMsn.Length > 0)
182 msn_name = c.ImMsn[0];
183 if (c.ImYahoo.Length > 0)
184 yahoo_name = c.ImYahoo[0];
185 if (c.ImGroupwise.Length > 0)
186 groupwise_name = c.ImGroupwise[0];
188 #endif
191 [TileAction]
192 public override void Open ()
194 Process p = new Process ();
195 p.StartInfo.UseShellExecute = false;
196 p.StartInfo.FileName = "evolution";
197 p.StartInfo.Arguments = "'" + Hit.Uri + "'";
199 try {
200 p.Start ();
201 } catch (System.ComponentModel.Win32Exception e) {
202 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
207 [TileAction]
208 public void Mail ()
210 bool sent = (Hit ["fixme:isSent"] != null);
211 string address = sent ? Hit ["fixme:to"] : Hit ["fixme:from"];
213 Process p = new Process ();
214 p.StartInfo.UseShellExecute = false;
215 p.StartInfo.FileName = "evolution";
216 p.StartInfo.Arguments = "'mailto:" + address + "'";
218 try {
219 p.Start ();
220 } catch (System.ComponentModel.Win32Exception e) {
221 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
225 #if ENABLE_EVO_SHARP
226 [TileAction]
227 public void SendIm ()
229 if (aim_name != null)
230 SendImAim (aim_name);
231 if (groupwise_name != null)
232 SendImGroupwise (groupwise_name);
233 if (icq_name != null)
234 SendImIcq (icq_name);
235 if (jabber_name != null)
236 SendImJabber (jabber_name);
237 if (msn_name != null)
238 SendImMsn (msn_name);
239 if (yahoo_name != null)
240 SendImYahoo (yahoo_name);
242 #endif