2
using System.Diagnostics;
3
using System.Runtime.InteropServices;
8
namespace Beagle.Search.Tiles {
10
public class ApplicationActivator : TileActivator {
12
static bool checked_gconf = false;
13
static bool disable_command_line = false;
15
public ApplicationActivator () : base ()
17
AddSupportedFlavor (new HitFlavor (null, null, "application/x-desktop"));
20
[DllImport ("libgnome-desktop-2.so.2")]
21
static extern IntPtr gnome_desktop_item_new_from_uri (string uri, int flags, IntPtr error);
23
[DllImport ("libgnome-desktop-2.so.2")]
24
static extern string gnome_desktop_item_get_string (IntPtr ditem, string attr);
26
[DllImport ("libgnome-desktop-2.so.2")]
27
static extern void gnome_desktop_item_unref (IntPtr ditem);
31
~ApplicationActivator ()
33
if (ditem != IntPtr.Zero)
34
gnome_desktop_item_unref (ditem);
37
static void CheckLockdown ()
39
GConf.Client client = new GConf.Client ();
42
disable_command_line = (bool) client.Get ("/desktop/gnome/lockdown/disable_command_line");
44
// The key isn't set for some reason
45
disable_command_line = false;
51
// invalid .desktop files get filtered out by Validate(), so they won't
52
// show up as Application tiles, but will show up as File tiles. But
53
// valid .desktop files marked to not show up in GNOME get eaten by
54
// BuildTile instead, so that they won't get picked up by the File tile.
56
// FIXME: we shouldn't be hardcoding GNOME in BuildTile, it should depend
57
// on what the running desktop is.
59
public override bool Validate (Beagle.Hit hit)
61
if (!base.Validate (hit))
64
ditem = gnome_desktop_item_new_from_uri (hit.EscapedUri, 0, IntPtr.Zero);
65
if (ditem == IntPtr.Zero)
68
// Make sure this is a real desktop file, not a .desktop.in
69
string _name = gnome_desktop_item_get_string (ditem, "_Name");
76
public override Tile BuildTile (Beagle.Hit hit, Beagle.Query query)
78
if (ditem == IntPtr.Zero)
81
string notshow = gnome_desktop_item_get_string (ditem, "NotShowIn");
82
if (notshow != null && notshow.IndexOf ("GNOME") != -1)
85
string onlyshow = gnome_desktop_item_get_string (ditem, "OnlyShowIn");
86
if (onlyshow != null && onlyshow.IndexOf ("GNOME") == -1)
93
if (disable_command_line) {
94
string[] categories = hit.GetProperties ("fixme:Categories");
96
if (categories != null && Array.IndexOf (categories, "TerminalEmulator") != -1)
100
return new Application (hit, query, ditem);
104
public class Application : TileTemplate {
108
public Application (Beagle.Hit hit, Beagle.Query query, IntPtr ditem) : this (hit, query)
111
// AddAction (new TileAction (Catalog.GetString ("Move to trash"), Gtk.Stock.Delete, MoveToTrash));
114
protected Application (Beagle.Hit hit, Beagle.Query query) : base (hit, query)
116
Group = TileGroup.Application;
117
Title = Hit.GetFirstProperty ("fixme:Name");
118
Description = Hit ["fixme:Comment"];
121
protected override void LoadIcon (Gtk.Image image, int size)
123
Gdk.Pixbuf icon = null;
124
string path = Hit ["fixme:Icon"];
126
if (path != null && path != "") {
128
if (path.StartsWith ("/")) {
129
icon = new Gdk.Pixbuf (path);
131
if (path.EndsWith (".png"))
132
icon = WidgetFu.LoadThemeIcon (path.Substring (0, path.Length-4), size);
134
icon = WidgetFu.LoadThemeIcon (path, size);
137
string kde_path = Beagle.Util.KdeUtils.LookupIcon (path);
139
if (System.IO.File.Exists (kde_path))
140
icon = new Gdk.Pixbuf (kde_path);
143
} catch (Exception e) {
144
Console.WriteLine ("Unable to load icon '{0}': {1}", path, e.Message);
149
if (icon.Height > size) {
150
int scaled_width = (int) ((double) size / (double) icon.Height * icon.Width);
152
icon = icon.ScaleSimple (scaled_width, size, Gdk.InterpType.Bilinear);
157
base.LoadIcon (image, size);
160
[DllImport ("libgnome-desktop-2.so.2")]
161
static extern int gnome_desktop_item_launch (IntPtr ditem, IntPtr file_list, int flags, IntPtr error);
163
public override void Open ()
165
if (gnome_desktop_item_launch (ditem, IntPtr.Zero, 0, IntPtr.Zero) == -1)
166
Console.WriteLine ("Unable to launch application");
170
public void MoveToTrash ()
172
// FIXME: What is the default way to uninstall an application
173
// in a distro-independent way?
175
// FIXME: The chance that the code below works is 1:100 :-)
176
ProcessStartInfo pi = new ProcessStartInfo ("rpm");
177
pi.Arguments = String.Format ("-e {0}", Hit ["fixme:Exec"]);
178
//Process.Start (pi); // FIXME: Safe sex
180
Console.WriteLine ("Would run 'rpm {0}'", pi.Arguments);