Merging from head
[beagle.git] / Tiles / Tile.cs
blob226ea33f37154d3c18be850029d70cbfa3f6d2ec
1 //
2 // Tile.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.Diagnostics;
29 using Gtk;
30 using Beagle.Util;
32 namespace Beagle.Tile {
34 public delegate void TileActionHandler ();
35 public delegate void TileChangedHandler (Tile tile);
37 public abstract class Tile {
39 static private object uidSrcLock = new object ();
40 static private long uidSrc = 0;
42 private long uid;
43 private Hit hit;
44 private Uri uri;
45 private Query query;
47 public Tile ()
49 lock (uidSrcLock) {
50 ++uidSrc;
51 uid = uidSrc;
55 public string UniqueKey {
56 get { return "_tile_" + uid; }
59 public Hit Hit {
60 get { return hit; }
61 set { hit = value; }
64 public Uri Uri {
65 get { return uri; }
66 set { uri = value; }
69 public Query Query {
70 get { return query; }
71 set { query = value; }
74 ////////////////////////
76 abstract public void Render (TileRenderContext ctx);
78 ////////////////////////
80 virtual public bool HandleUrlRequest (string url)
82 return false;
85 ////////////////////////
87 private TileChangedHandler changedHandler = null;
89 public void SetChangedHandler (TileChangedHandler ch)
91 changedHandler = ch;
94 protected virtual void Changed ()
96 if (changedHandler != null)
97 changedHandler (this);
100 private void OnErrorDialogResponse (object o, ResponseArgs args)
102 ((MessageDialog)o).Destroy ();
106 protected void LaunchError (string format, params string[] args)
108 string message = String.Format (format, args);
109 MessageDialog dlg = new MessageDialog (null,
111 MessageType.Error,
112 ButtonsType.Ok,
113 message);
115 dlg.Response += OnErrorDialogResponse;
116 dlg.Show ();
120 [TileAction]
121 public virtual void Open ()
123 System.Console.WriteLine ("Warning: Open method not implemented for this tile type");
126 protected void OpenFolder (string path)
128 if (path == null || path == "")
129 return;
131 Process p = new Process ();
132 p.StartInfo.UseShellExecute = false;
134 if ((!path.StartsWith ("\"")) && (!path.EndsWith ("\"")))
135 path = "\"" + path + "\"";
136 #if ENABLE_DESKTOP_LAUNCH
137 p.StartInfo.FileName = "desktop-launch";
138 p.StartInfo.Arguments = path;
139 #else
140 p.StartInfo.FileName = "nautilus";
141 p.StartInfo.Arguments = "--no-desktop " + path;
142 #endif
143 try {
144 p.Start ();
145 } catch (Exception e) {
146 Console.WriteLine ("Cannot open folder: " + e);
150 protected void OpenFromMime (Hit hit)
152 OpenFromMime (hit, null, null, false);
155 protected void OpenFromMime (Hit hit,
156 string command_fallback,
157 string args_fallback,
158 bool expects_uris_fallback)
160 string argument;
161 string command = command_fallback;
162 bool expects_uris = expects_uris_fallback;
164 // FIXME: This is evil. Nautilus should be handling
165 // inode/directory, not just x-directory/normal
166 if (hit.MimeType == "inode/directory")
167 hit.MimeType = "x-directory/normal";
168 #if ENABLE_DESKTOP_LAUNCH
169 command = "desktop-launch";
170 expects_uris = true;
171 #else
172 GnomeFu.VFSMimeApplication app;
173 app = GnomeFu.GetDefaultAction (hit.MimeType);
174 if (app.command != null) {
175 command = app.command;
176 expects_uris = (app.expects_uris != GnomeFu.VFSMimeApplicationArgumentType.Path);
178 #endif
179 if (command == null) {
180 LaunchError ("Can't open MimeType '{0}'", hit.MimeType);
181 return;
184 if (args_fallback != null)
185 argument = args_fallback;
186 else
187 argument = "";
189 if (expects_uris) {
190 argument = String.Format ("{0} '{1}'", argument, hit.Uri);
191 } else {
192 argument = String.Format ("{0} {1}", argument, hit.PathQuoted);
195 // Sometimes the command is 'quoted'
196 if (command.IndexOf ('\'') == 0 && command.LastIndexOf ('\'') == command.Length - 1)
197 command = command.Trim ('\'');
199 // This won't work if a program really has a space in
200 // the filename, but I think other things would break
201 // with that too, and in practice it doesn't seem to
202 // happen.
203 int idx = command.IndexOf (' ');
204 if (idx != -1) {
205 argument = String.Format ("{0} {1}", command.Substring (idx + 1), argument);
206 command = command.Substring (0, idx);
209 Console.WriteLine ("Cmd: {0}", command);
210 Console.WriteLine ("Arg: {0}", argument);
212 Process p = new Process ();
213 p.StartInfo.UseShellExecute = false;
214 p.StartInfo.FileName = command;
215 p.StartInfo.Arguments = argument;
217 try {
218 p.Start ();
219 } catch (Exception e) {
220 Console.WriteLine ("Error in OpenFromMime: " + e);
224 protected void SendFile (string attach)
226 if ((attach == null || attach == "")) {
227 Console.WriteLine ("SendFile got empty attachment");
228 return;
231 Process p = new Process ();
232 p.StartInfo.UseShellExecute = false;
233 p.StartInfo.FileName = "nautilus-sendto";
234 p.StartInfo.Arguments = String.Format ("--default-dir=/ '{0}'", attach);
236 try {
237 p.Start () ;
238 } catch (Exception e) {
239 // Fall back to just email
240 SendMailToAddress (null, attach);
244 protected void SendMailToAddress (string email, string attach)
246 if ((email == null || email == "") && (attach == null || attach == "")) {
247 Console.WriteLine ("SendMail got empty email address and attachment");
248 return;
251 Process p = new Process ();
252 p.StartInfo.UseShellExecute = false;
253 p.StartInfo.FileName = "evolution";
254 p.StartInfo.Arguments = "\"mailto:";
256 if (email != null && email != "")
257 p.StartInfo.Arguments += email;
259 if (attach != null && attach != "")
260 p.StartInfo.Arguments += "?attach=" + attach;
262 p.StartInfo.Arguments += "\"";
264 try {
265 p.Start () ;
266 } catch (Exception e) {
267 Console.WriteLine ("Error launching Evolution composer: " + e);
271 protected void SendIm (string protocol,
272 string screenname)
274 if (screenname == null)
275 return;
277 Console.WriteLine ("SendImAim {0}", screenname);
278 Process p = new Process ();
279 p.StartInfo.UseShellExecute = false;
280 p.StartInfo.FileName = "gaim-remote";
281 p.StartInfo.Arguments = "uri " + protocol + ":goim?screenname=" + screenname;
283 try {
284 p.Start () ;
285 } catch (Exception e) {
286 Console.WriteLine ("Error launching gaim-remote: " + e);
290 protected void SendImAim (string screenname)
292 SendIm ("aim", screenname);
295 protected void SendImIcq (string screenname)
297 SendIm ("icq", screenname);
300 protected void SendImJabber (string screenname)
302 SendIm ("jabber", screenname);
305 protected void SendImMsn (string screenname)
307 SendIm ("msn", screenname);
310 protected void SendImYahoo (string screenname)
312 SendIm ("yahoo", screenname);
315 protected void SendImGroupwise (string screenname)
317 SendIm ("novell", screenname);