4 // Copyright (C) 2004 Novell, Inc.
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.
28 using System
.Collections
;
29 using System
.Diagnostics
;
31 using System
.Text
.RegularExpressions
;
32 using BU
= Beagle
.Util
;
36 namespace Beagle
.Tile
{
38 [HitFlavor (Name
="Email", Rank
=1100, Emblem
="emblem-mail-message.png", Color
="#f5f5f5",
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)
56 return hit
["parent:" + name
];
59 private string GetMailIcon ()
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);
68 icon
= Images
.GetHtmlSourceForStock ("stock_mail", 48);
73 protected override void PopulateTemplate ()
75 base.PopulateTemplate ();
77 bool sent
= (GetHitProperty (Hit
, "fixme:isSent") != null);
78 string str
= GetHitProperty (Hit
, "dc:title");
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.
93 string[] values
= Hit
.GetProperties ("fixme:to");
96 StringBuilder sb
= new StringBuilder ();
99 for (i
= 0; i
< 3 && i
< values
.Length
; i
++) {
103 sb
.Append (values
[i
]);
106 if (i
< values
.Length
)
109 Template
["Who"] = sb
.ToString ();
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);
137 GetImNames (Template
["Who"]);
139 if (aim_name
!= null)
140 Template
["CanSendIm"] = "";
145 if (aim_name
!= null) {
146 string status
= BU
.GalagoTools
.GetPresence ("aim", aim_name
);
147 if (status
!= null && status
!= "")
148 Template
["Presence"] = status
;
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 ();
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
== "")
183 Evolution
.Book addressbook
= null;
189 addressbook
= Evolution
.Book
.NewSystemAddressbook ();
190 addressbook
.Open (true);
191 } catch (Exception e
) {
192 Console
.WriteLine ("\nCould not open Evolution addressbook:\n" + e
);
197 string email
= GetEmail (who
);
199 System
.Console
.WriteLine ("Looking for im name for {0}",
201 System
.Console
.WriteLine ("FIXME: This query is using the Evolution addressbook instead of querying Beagle directly. This is slow, dumb, etc.");
204 String
.Format ("(is \"email\" \"{0}\")", email
);
206 Evolution
.BookQuery query
= Evolution
.BookQuery
.FromString (qstr
);
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];
230 public override void Open ()
234 if (GetHitProperty (Hit
, "fixme:client") != "evolution") {
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
;
246 uri_str
= Hit
.UriAsString
;
248 p
.StartInfo
.Arguments
= "'" + uri_str
+ "'";
252 } catch (System
.ComponentModel
.Win32Exception e
) {
253 Console
.WriteLine ("Unable to run {0}: {1}", p
.StartInfo
.FileName
, e
.Message
);
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
+ "'";
270 } catch (System
.ComponentModel
.Win32Exception e
) {
271 Console
.WriteLine ("Unable to run {0}: {1}", p
.StartInfo
.FileName
, e
.Message
);
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
;
287 uri_str
= Hit
.UriAsString
;
289 p
.StartInfo
.Arguments
= String
.Format ("'{0};reply=sender'", uri_str
);
293 } catch (System
.ComponentModel
.Win32Exception e
) {
294 Console
.WriteLine ("Unable to run {0}: {1}", p
.StartInfo
.FileName
, e
.Message
);
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
);