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.
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",
36 public class TileImLog
: TileFromHitTemplate
{
37 private static BU
.GaimBuddyListReader list
= null;
40 private static Hashtable buddy_emails
= new Hashtable ();
43 private BU
.ImBuddy buddy
= null;
44 private string email
= null;
46 public TileImLog (Hit _hit
) : base (_hit
,
47 "template-im-log.html")
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"] = "";
69 Template
["SendMailAction"] = "Send Mail";
72 Template
["snippet"] = getSnippet ();
74 if (buddy
!= null && buddy
.Alias
!= "")
75 Template
["speakingalias"] = buddy
.Alias
;
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);
92 Template
["Icon"] = Images
.GetHtmlSourceForStock ("gnome-gaim", 48);
97 static bool ebook_failed
= false;
100 private string GetEmailForIm (string im
)
103 Evolution
.Book addressbook
= null;
105 // If we've previously failed to open the
106 // addressbook, don't keep trying.
110 if (buddy_emails
.Contains (im
)) {
111 string str
= (string)buddy_emails
[im
];
112 return str
!= "" ? str
: null;
115 // Connect to the Evolution addressbook.
117 addressbook
= Evolution
.Book
.NewSystemAddressbook ();
118 addressbook
.Open (true);
119 } catch (Exception e
) {
120 Console
.WriteLine ("\nCould not open Evolution addressbook:\n" + e
);
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}\") " +
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
;
144 buddy_emails
[im
] = "";
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
;
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
);
175 hili
= hili
.Insert (ni
, highlight_start
);
176 hili
= hili
.Insert (ni
+ highlight_start
.Length
+ needle
.Length
, highlight_end
);
178 h_up
= hili
.ToUpper ();
181 ni
= h_up
.IndexOf (n_up
, ni
+ highlight_start
.Length
+ needle
.Length
+ highlight_end
.Length
);
192 private string getSnippet ()
194 ICollection logs
= BU
.GaimLog
.ScanLog (new FileInfo (Hit
["fixme:file"]));
198 foreach (BU
.ImLog log
in logs
) {
199 foreach (BU
.ImLog
.Utterance utt
in log
.Utterances
) {
200 // FIXME: Query.Text is broken for me
202 string s
= HighlightOrNull (utt
.Text
, Query
.Text
);
215 return snip
.Substring (0, System
.Math
.Min (256, snip
.Length
));
220 public void SendMailForIm ()
223 SendMailToAddress (email
, null);
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)
236 SendImAim (Hit
["fixme:speakingto"]);