Show total number of results for google driver too. Helps to know if my query resulte...
[beagle.git] / search / Tiles / Application.cs
blobc8e9cfdfc40b50139dfd5c65060ee592522abccd
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.EscapedUri, 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 try {
98 if (path.StartsWith ("/")) {
99 icon = new Gdk.Pixbuf (path);
100 } else {
101 if (path.EndsWith (".png"))
102 icon = WidgetFu.LoadThemeIcon (path.Substring (0, path.Length-4), size);
103 else
104 icon = WidgetFu.LoadThemeIcon (path, size);
106 if (icon == null) {
107 string kde_path = Beagle.Util.KdeUtils.LookupIcon (path);
109 if (System.IO.File.Exists (kde_path))
110 icon = new Gdk.Pixbuf (kde_path);
113 } catch (Exception e) {
114 Console.WriteLine ("Unable to load icon '{0}': {1}", path, e.Message);
118 if (icon != null) {
119 if (icon.Height > size) {
120 int scaled_width = (int) ((double) size / (double) icon.Height * icon.Width);
122 icon = icon.ScaleSimple (scaled_width, size, Gdk.InterpType.Bilinear);
125 image.Pixbuf = icon;
126 } else
127 base.LoadIcon (image, size);
130 [DllImport ("libgnome-desktop-2.so.2")]
131 static extern int gnome_desktop_item_launch (IntPtr ditem, IntPtr file_list, int flags, IntPtr error);
133 public override void Open ()
135 if (gnome_desktop_item_launch (ditem, IntPtr.Zero, 0, IntPtr.Zero) == -1)
136 Console.WriteLine ("Unable to launch application");
139 #if NOPE
140 public void MoveToTrash ()
142 // FIXME: What is the default way to uninstall an application
143 // in a distro-independent way?
145 // FIXME: The chance that the code below works is 1:100 :-)
146 ProcessStartInfo pi = new ProcessStartInfo ("rpm");
147 pi.Arguments = String.Format ("-e {0}", Hit ["fixme:Exec"]);
148 //Process.Start (pi); // FIXME: Safe sex
150 Console.WriteLine ("Would run 'rpm {0}'", pi.Arguments);
152 #endif