Forgot to bump FSQ version. Weekend syndrome.
[beagle.git] / search / Tiles / Application.cs
blobe8bba1a0b35b1157f6e952dd4068599b4d236907
1 using System;
2 using System.Diagnostics;
3 using System.Runtime.InteropServices;
4 using Mono.Unix;
6 namespace Search.Tiles {
8 public class ApplicationActivator : TileActivator {
10 public ApplicationActivator () : base ()
12 AddSupportedFlavor (new HitFlavor (null, null, "application/x-desktop"));
15 [DllImport ("libgnome-desktop-2.so.2")]
16 static extern IntPtr gnome_desktop_item_new_from_uri (string uri, int flags, IntPtr error);
18 [DllImport ("libgnome-desktop-2.so.2")]
19 static extern string gnome_desktop_item_get_string (IntPtr ditem, string attr);
21 [DllImport ("libgnome-desktop-2.so.2")]
22 static extern void gnome_desktop_item_unref (IntPtr ditem);
24 IntPtr ditem;
26 ~ApplicationActivator ()
28 if (ditem != IntPtr.Zero)
29 gnome_desktop_item_unref (ditem);
32 // invalid .desktop files get filtered out by Validate(), so they won't
33 // show up as Application tiles, but will show up as File tiles. But
34 // valid .desktop files marked to not show up in GNOME get eaten by
35 // BuildTile instead, so that they won't get picked up by the File tile.
37 // FIXME: we shouldn't be hardcoding GNOME in BuildTile, it should depend
38 // on what the running desktop is.
40 public override bool Validate (Beagle.Hit hit)
42 if (!base.Validate (hit))
43 return false;
45 ditem = gnome_desktop_item_new_from_uri (hit.UriAsString, 0, IntPtr.Zero);
46 if (ditem == IntPtr.Zero)
47 return false;
49 // Make sure this is a real desktop file, not a .desktop.in
50 string _name = gnome_desktop_item_get_string (ditem, "_Name");
51 if (_name != null)
52 return false;
54 return true;
57 public override Tile BuildTile (Beagle.Hit hit, Beagle.Query query)
59 if (ditem == IntPtr.Zero)
60 return null;
62 string notshow = gnome_desktop_item_get_string (ditem, "NotShowIn");
63 if (notshow != null && notshow.IndexOf ("GNOME") != -1)
64 return null;
66 string onlyshow = gnome_desktop_item_get_string (ditem, "OnlyShowIn");
67 if (onlyshow != null && onlyshow.IndexOf ("GNOME") == -1)
68 return null;
70 return new Application (hit, query, ditem);
74 public class Application : TileTemplate {
76 IntPtr ditem;
78 public Application (Beagle.Hit hit, Beagle.Query query, IntPtr ditem) : this (hit, query)
80 this.ditem = ditem;
81 // AddAction (new TileAction (Catalog.GetString ("Move to trash"), Gtk.Stock.Delete, MoveToTrash));
84 protected Application (Beagle.Hit hit, Beagle.Query query) : base (hit, query)
86 Group = TileGroup.Application;
87 Title = Hit.GetFirstProperty ("fixme:Name");
88 Description = Hit ["fixme:Comment"];
91 protected override void LoadIcon (Gtk.Image image, int size)
93 Gdk.Pixbuf icon = null;
94 string path = Hit ["fixme:Icon"];
96 if (path != null && path != "") {
97 if (path.StartsWith ("/")) {
98 icon = new Gdk.Pixbuf (path);
99 } else {
100 if (path.EndsWith (".png"))
101 icon = WidgetFu.LoadThemeIcon (path.Substring (0, path.Length-4), size);
102 else
103 icon = WidgetFu.LoadThemeIcon (path, size);
105 if (icon == null) {
106 string kde_path = Beagle.Util.KdeUtils.LookupIcon (path);
108 if (System.IO.File.Exists (kde_path))
109 icon = new Gdk.Pixbuf (kde_path);
114 if (icon != null) {
115 if (icon.Height > size) {
116 int scaled_width = (int) ((double) size / (double) icon.Height * icon.Width);
118 icon = icon.ScaleSimple (scaled_width, size, Gdk.InterpType.Bilinear);
121 image.Pixbuf = icon;
122 } else
123 base.LoadIcon (image, size);
126 [DllImport ("libgnome-desktop-2.so.2")]
127 static extern int gnome_desktop_item_launch (IntPtr ditem, IntPtr file_list, int flags, IntPtr error);
129 public override void Open ()
131 if (gnome_desktop_item_launch (ditem, IntPtr.Zero, 0, IntPtr.Zero) == -1)
132 Console.WriteLine ("Unable to launch application");
135 #if NOPE
136 public void MoveToTrash ()
138 // FIXME: What is the default way to uninstall an application
139 // in a distro-independent way?
141 // FIXME: The chance that the code below works is 1:100 :-)
142 ProcessStartInfo pi = new ProcessStartInfo ("rpm");
143 pi.Arguments = String.Format ("-e {0}", Hit ["fixme:Exec"]);
144 //Process.Start (pi); // FIXME: Safe sex
146 Console.WriteLine ("Would run 'rpm {0}'", pi.Arguments);
148 #endif