* beagled/QueryDriver.cs,beagled/PropertyKeywordFu.cs,
[beagle.git] / Tiles / TileImLog.cs
blob18b35d6db09b441c6d97a63b104cbe84ec4c40cf
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 System.Diagnostics;
31 using Beagle.Util;
33 using Mono.Unix;
35 namespace Beagle.Tile {
37 [HitFlavor (Name="Conversations", Rank=900, Emblem="emblem-im-log.png", Color="#e5f5ef",
38 Type="IMLog")]
39 public class TileImLog : TileFromHitTemplate {
41 #if ENABLE_EVO_SHARP
42 private static Hashtable buddy_emails = new Hashtable ();
43 #endif
45 private string email = null;
46 private string speaking_alias = null;
48 public TileImLog (Hit _hit) : base (_hit,
49 "template-im-log.html")
51 if (Hit ["fixme:speakingto_alias"] != null)
52 email = GetEmailForName (Hit ["fixme:speakingto_alias"]);
55 protected override void PopulateTemplate ()
57 base.PopulateTemplate ();
59 Template["nice_duration"] = "(" +
60 StringFu.DurationToPrettyString (
61 StringFu.StringToDateTime (Hit ["fixme:endtime"]),
62 StringFu.StringToDateTime (Hit ["fixme:starttime"])) + ")";
63 if (Template ["nice_duration"] == "()")
64 Template ["nice_duration"] = "";
66 if (email != null)
67 Template ["SendMailAction"] = Catalog.GetString ("Send Mail");
69 // FIXME: This is a temporary hack until gaim supports other protocols than AIM via gaim-remote
70 if (Hit ["fixme:protocol"] == "aim")
71 Template ["SendIMAction"] = Catalog.GetString ("Send IM");
73 speaking_alias = (Hit ["fixme:speakingto_alias"] != null) ? Hit ["fixme:speakingto_alias"] : Hit ["fixme:speakingto"];
75 // FIXME: Hack to figure out if the conversation is taken place in a chat room
76 if (Hit["fixme:speakingto"].EndsWith (".chat"))
77 Template["title"] = String.Format (Catalog.GetString ("Conversation in {0}"), speaking_alias.Replace(".chat",""));
78 else
79 Template["title"] = String.Format (Catalog.GetString ("Conversation with {0}"), speaking_alias);
81 if (Hit ["fixme:speakingto_icon"] != null && File.Exists (Hit ["fixme:speakingto_icon"]))
82 Template["Icon"] = StringFu.PathToQuotedFileUri (Hit ["fixme:speakingto_icon"]);
83 else
84 Template["Icon"] = Images.GetHtmlSource ("gnome-gaim.png", "image/png");
86 #if ENABLE_GALAGO
87 if (Hit ["fixme:protocol"] == "aim") {
88 string status = GalagoTools.GetPresence (Hit ["fixme:protocol"], Hit["fixme:speakingto"]);
89 if (status != null && status != "")
90 Template ["Presence"] = status;
92 #endif
95 #if ENABLE_EVO_SHARP
96 static bool ebook_failed = false;
97 #endif
99 private string GetEmailForName (string name)
101 #if ENABLE_EVO_SHARP
102 if (name == null || name == "")
103 return null;
105 Evolution.Book addressbook = null;
108 // If we've previously failed to open the
109 // addressbook, don't keep trying.
110 if (ebook_failed)
111 return null;
113 // We keep a little cache so we don't have to query
114 // the addressbook too often.
115 if (buddy_emails.Contains (name)) {
116 string str = (string)buddy_emails[name];
117 return str != "" ? str : null;
120 // Connect to the Evolution addressbook.
121 try {
122 addressbook = Evolution.Book.NewSystemAddressbook ();
123 addressbook.Open (true);
124 } catch (Exception e) {
125 Console.WriteLine ("\nCould not open Evolution addressbook:\n" + e);
126 ebook_failed = true;
127 return null;
130 // Do a search.
131 string qstr =
132 String.Format ("(is \"full_name\" \"{0}\")", name);
134 Evolution.BookQuery query = Evolution.BookQuery.FromString (qstr);
135 Evolution.Contact [] matches = addressbook.GetContacts (query);
136 foreach (Evolution.Contact c in matches) {
137 Console.WriteLine ("FIXME: querying the evolution addressbook instead of using Lucene, this is slow and dumb");
138 Console.WriteLine ("Got match: {0} {1}", c.FullName, c.Email1);
139 if (c.Email1 != null) {
140 buddy_emails[name] = c.Email1;
141 return c.Email1;
144 buddy_emails[name] = "";
145 #endif
147 return null;
150 [TileAction]
151 public override void Open ()
153 //FIXME: At least for now
154 Process p = new Process ();
155 p.StartInfo.UseShellExecute = true;
156 p.StartInfo.FileName = "beagle-imlogviewer";
157 p.StartInfo.Arguments = String.Format ("--client \"{0}\" --highlight-search \"{1}\" {2}",
158 Hit ["fixme:client"], Query.QuotedText, Hit.Uri.LocalPath);
160 try {
161 p.Start ();
162 } catch (Exception e) {
163 Console.WriteLine ("Unable to run {0}: {1}", p.StartInfo.FileName, e.Message);
167 [TileAction]
168 public void SendMailForIm ()
170 if (email != null)
171 SendMailToAddress (email, null);
174 [TileAction]
175 public void SendIm ()
177 SendImAim (Hit ["fixme:speakingto"]);