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 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)
54 return hit
["parent:" + name
];
57 private string GetMailIcon ()
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);
66 icon
= Images
.GetHtmlSourceForStock ("stock_mail", 48);
71 protected override void PopulateTemplate ()
73 base.PopulateTemplate ();
75 bool sent
= (GetHitProperty (Hit
, "fixme:isSent") != null);
76 string str
= GetHitProperty (Hit
, "dc:title");
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.
91 string[] values
= Hit
.GetProperties ("fixme:to");
94 StringBuilder sb
= new StringBuilder ();
97 for (i
= 0; i
< 3 && i
< values
.Length
; i
++) {
101 sb
.Append (values
[i
]);
104 if (i
< values
.Length
)
107 Template
["Who"] = sb
.ToString ();
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);
132 GetImNames (Template
["Who"]);
134 if (aim_name
!= null)
135 Template
["CanSendIm"] = "";
140 if (aim_name
!= null) {
141 string status
= BU
.GalagoTools
.GetPresence ("aim", aim_name
);
142 if (status
!= null && status
!= "")
143 Template
["Presence"] = status
;
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 ();
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
== "")
178 Evolution
.Book addressbook
= null;
184 addressbook
= Evolution
.Book
.NewSystemAddressbook ();
185 addressbook
.Open (true);
186 } catch (Exception e
) {
187 Console
.WriteLine ("\nCould not open Evolution addressbook:\n" + e
);
192 string email
= GetEmail (who
);
194 System
.Console
.WriteLine ("Looking for im name for {0}",
196 System
.Console
.WriteLine ("FIXME: This query is using the Evolution addressbook instead of querying Beagle directly. This is slow, dumb, etc.");
199 String
.Format ("(is \"email\" \"{0}\")", email
);
201 Evolution
.BookQuery query
= Evolution
.BookQuery
.FromString (qstr
);
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];
225 public override void Open ()
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
;
236 uri_str
= Hit
.UriAsString
;
238 p
.StartInfo
.Arguments
= "'" + uri_str
+ "'";
242 } catch (System
.ComponentModel
.Win32Exception e
) {
243 Console
.WriteLine ("Unable to run {0}: {1}", p
.StartInfo
.FileName
, e
.Message
);
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
+ "'";
260 } catch (System
.ComponentModel
.Win32Exception e
) {
261 Console
.WriteLine ("Unable to run {0}: {1}", p
.StartInfo
.FileName
, e
.Message
);
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
;
277 uri_str
= Hit
.UriAsString
;
279 p
.StartInfo
.Arguments
= String
.Format ("'{0};reply=sender'", uri_str
);
283 } catch (System
.ComponentModel
.Win32Exception e
) {
284 Console
.WriteLine ("Unable to run {0}: {1}", p
.StartInfo
.FileName
, e
.Message
);
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
);