RTF filter complies to MS RTF 1.5 specification. (works well with 1.8 as well).
[beagle.git] / Tiles / TileImLog.cs
blob8402cb59a02e7e04740696289ca438343faf3f03
1 //
2 // TileImLog.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.IO;
29 using System.Collections;
30 using BU = Beagle.Util;
32 namespace Beagle.Tile {
34 [HitFlavor (Name="Conversations", Rank=900, Emblem="emblem-im-log.png", Color="#e5f5ef",
35 Type="IMLog")]
36 public class TileImLog : TileFromHitTemplate {
37 private static BU.GaimBuddyListReader list = null;
39 #if ENABLE_EVO_SHARP
40 private static Hashtable buddy_emails = new Hashtable ();
41 #endif
43 private BU.ImBuddy buddy = null;
44 private string email = null;
46 public TileImLog (Hit _hit) : base (_hit,
47 "template-im-log.html")
49 if (list == null) {
50 list = new BU.GaimBuddyListReader ();
53 buddy = list.Search (Hit ["fixme:speakingto"]);
54 email = GetEmailForIm (Hit ["fixme:speakingto"]);
57 protected override void PopulateTemplate ()
59 base.PopulateTemplate ();
61 Template["nice_duration"] = "(" +
62 BU.StringFu.DurationToPrettyString (
63 BU.StringFu.StringToDateTime (Hit ["fixme:endtime"]),
64 BU.StringFu.StringToDateTime (Hit ["fixme:starttime"])) + ")";
65 if (Template ["nice_duration"] == "()")
66 Template ["nice_duration"] = "";
68 if (email != null)
69 Template ["SendMailAction"] = "Send Mail";
71 #if false
72 Template["snippet"] = getSnippet ();
73 #endif
74 if (buddy != null && buddy.Alias != "")
75 Template["speakingalias"] = buddy.Alias;
76 else
77 Template["speakingalias"] = Hit["fixme:speakingto"];
79 if (buddy != null && buddy.BuddyIconLocation != "") {
80 string homedir = Environment.GetEnvironmentVariable ("HOME");
81 string fullpath = Path.Combine (homedir, ".gaim");
82 fullpath = Path.Combine (fullpath, "icons");
83 fullpath = Path.Combine (fullpath, buddy.BuddyIconLocation);
85 Console.WriteLine ("Icon for {0}: {1}", buddy.Alias, fullpath);
87 if (File.Exists (fullpath)) {
88 Template["Icon"] = "file://" + fullpath; } else {
89 Template["Icon"] = Images.GetHtmlSourceForStock ("gnome-gaim", 48);
91 } else {
92 Template["Icon"] = Images.GetHtmlSourceForStock ("gnome-gaim", 48);
96 #if ENABLE_EVO_SHARP
97 static bool ebook_failed = false;
98 #endif
100 private string GetEmailForIm (string im)
102 #if ENABLE_EVO_SHARP
103 Evolution.Book addressbook = null;
105 // If we've previously failed to open the
106 // addressbook, don't keep trying.
107 if (ebook_failed)
108 return null;
110 if (buddy_emails.Contains (im)) {
111 string str = (string)buddy_emails[im];
112 return str != "" ? str : null;
115 // Connect to the Evolution addressbook.
116 try {
117 addressbook = Evolution.Book.NewSystemAddressbook ();
118 addressbook.Open (true);
119 } catch (Exception e) {
120 Console.WriteLine ("\nCould not open Evolution addressbook:\n" + e);
121 ebook_failed = true;
122 return null;
125 // Do a search.
126 string qstr =
127 String.Format ("(or " +
128 "(is \"im_aim\" \"{0}\") " +
129 "(is \"im_yahoo\" \"{0}\") " +
130 "(is \"im_msn\" \"{0}\") " +
131 "(is \"im_icq\" \"{0}\") " +
132 "(is \"im_jabber\" \"{0}\") " +
133 "(is \"im_groupwise\" \"{0}\") " +
134 ")",
135 im);
137 Evolution.BookQuery query = Evolution.BookQuery.FromString (qstr);
138 Evolution.Contact [] matches = addressbook.GetContacts (query);
139 foreach (Evolution.Contact c in matches) {
140 Console.WriteLine ("Got match: " + c.FullName);
141 buddy_emails[im] = c.Email1;
142 return c.Email1;
144 buddy_emails[im] = "";
145 #endif
147 return null;
150 private string HighlightOrNull (string haystack, string [] needles)
152 string [] highlight_start_list = {"<font color=red>",
153 "<font color=orange>",
154 "<font color=green>",
155 "<font color=blue>"};
157 string highlight_end = "</font>";
159 string hili = haystack;
160 bool dirty = false;
161 int hicolor = -1;
163 foreach (string needle in needles) {
164 string h_up = hili.ToUpper ();
165 string n_up = needle.ToUpper ();
167 hicolor = (hicolor + 1) % 4;
168 string highlight_start = highlight_start_list [hicolor];
170 int ni = h_up.IndexOf (n_up);
171 if (ni == -1)
172 continue;
174 while (ni != -1) {
175 hili = hili.Insert (ni, highlight_start);
176 hili = hili.Insert (ni + highlight_start.Length + needle.Length, highlight_end);
178 h_up = hili.ToUpper ();
179 dirty = true;
181 ni = h_up.IndexOf (n_up, ni + highlight_start.Length + needle.Length + highlight_end.Length);
185 if (dirty) {
186 return hili;
188 else
189 return null;
192 private string getSnippet ()
194 ICollection logs = BU.GaimLog.ScanLog (new FileInfo (Hit ["fixme:file"]));
196 string snip = "";
198 foreach (BU.ImLog log in logs) {
199 foreach (BU.ImLog.Utterance utt in log.Utterances) {
200 // FIXME: Query.Text is broken for me
201 #if false
202 string s = HighlightOrNull (utt.Text, Query.Text);
203 #else
204 string s = utt.Text;
205 #endif
206 if (s != null) {
207 if (snip != "")
208 snip += " ... ";
209 snip += s;
215 return snip.Substring (0, System.Math.Min (256, snip.Length));
219 [TileAction]
220 public void SendMailForIm ()
222 if (email != null)
223 SendMailToAddress (email, null);
226 [TileAction]
227 public void SendIm ()
229 // FIXME: The hit should really have a field
230 // for the IM protocol that was used. This is
231 // an ugly hack to check whether the
232 // conversation took place over aim.
233 if (Hit ["fixme:file"].IndexOf ("logs/aim") == -1)
234 return;
236 SendImAim (Hit ["fixme:speakingto"]);