~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to search/Beagle.Search.Tiles/Application.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Diagnostics;
 
3
using System.Runtime.InteropServices;
 
4
using Mono.Unix;
 
5
 
 
6
using GConf;
 
7
 
 
8
namespace Beagle.Search.Tiles {
 
9
 
 
10
        public class ApplicationActivator : TileActivator {
 
11
 
 
12
                static bool checked_gconf = false;
 
13
                static bool disable_command_line = false;
 
14
 
 
15
                public ApplicationActivator () : base ()
 
16
                {
 
17
                        AddSupportedFlavor (new HitFlavor (null, null, "application/x-desktop"));
 
18
                }
 
19
 
 
20
                [DllImport ("libgnome-desktop-2.so.2")]
 
21
                static extern IntPtr gnome_desktop_item_new_from_uri (string uri, int flags, IntPtr error);
 
22
 
 
23
                [DllImport ("libgnome-desktop-2.so.2")]
 
24
                static extern string gnome_desktop_item_get_string (IntPtr ditem, string attr);
 
25
 
 
26
                [DllImport ("libgnome-desktop-2.so.2")]
 
27
                static extern void gnome_desktop_item_unref (IntPtr ditem);
 
28
 
 
29
                IntPtr ditem;
 
30
 
 
31
                ~ApplicationActivator ()
 
32
                {
 
33
                        if (ditem != IntPtr.Zero)
 
34
                                gnome_desktop_item_unref (ditem);
 
35
                }
 
36
 
 
37
                static void CheckLockdown ()
 
38
                {
 
39
                        GConf.Client client = new GConf.Client ();
 
40
 
 
41
                        try {
 
42
                                disable_command_line = (bool) client.Get ("/desktop/gnome/lockdown/disable_command_line");
 
43
                        } catch {
 
44
                                // The key isn't set for some reason
 
45
                                disable_command_line = false;
 
46
                        }
 
47
 
 
48
                        checked_gconf = true;
 
49
                }
 
50
 
 
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.
 
55
 
 
56
                // FIXME: we shouldn't be hardcoding GNOME in BuildTile, it should depend
 
57
                // on what the running desktop is.
 
58
 
 
59
                public override bool Validate (Beagle.Hit hit)
 
60
                {
 
61
                        if (!base.Validate (hit))
 
62
                                return false;
 
63
 
 
64
                        ditem = gnome_desktop_item_new_from_uri (hit.EscapedUri, 0, IntPtr.Zero);
 
65
                        if (ditem == IntPtr.Zero)
 
66
                                return false;
 
67
                        
 
68
                        // Make sure this is a real desktop file, not a .desktop.in
 
69
                        string _name = gnome_desktop_item_get_string (ditem, "_Name");
 
70
                        if (_name != null)
 
71
                                return false;
 
72
 
 
73
                        return true;
 
74
                }
 
75
 
 
76
                public override Tile BuildTile (Beagle.Hit hit, Beagle.Query query)
 
77
                {
 
78
                        if (ditem == IntPtr.Zero)
 
79
                                return null;
 
80
 
 
81
                        string notshow = gnome_desktop_item_get_string (ditem, "NotShowIn");
 
82
                        if (notshow != null && notshow.IndexOf ("GNOME") != -1)
 
83
                                return null;
 
84
 
 
85
                        string onlyshow = gnome_desktop_item_get_string (ditem, "OnlyShowIn");
 
86
                        if (onlyshow != null && onlyshow.IndexOf ("GNOME") == -1)
 
87
                                return null;
 
88
 
 
89
 
 
90
                        if (!checked_gconf)
 
91
                                CheckLockdown();
 
92
 
 
93
                        if (disable_command_line) {
 
94
                                string[] categories = hit.GetProperties ("fixme:Categories");
 
95
 
 
96
                                if (categories != null && Array.IndexOf (categories, "TerminalEmulator") != -1)
 
97
                                        return null;
 
98
                        }
 
99
 
 
100
                        return new Application (hit, query, ditem);
 
101
                }
 
102
        }
 
103
 
 
104
        public class Application : TileTemplate {
 
105
 
 
106
                IntPtr ditem;
 
107
 
 
108
                public Application (Beagle.Hit hit, Beagle.Query query, IntPtr ditem) : this (hit, query)
 
109
                {
 
110
                        this.ditem = ditem;
 
111
//                      AddAction (new TileAction (Catalog.GetString ("Move to trash"), Gtk.Stock.Delete, MoveToTrash));
 
112
                }
 
113
 
 
114
                protected Application (Beagle.Hit hit, Beagle.Query query) : base (hit, query)
 
115
                {
 
116
                        Group = TileGroup.Application;
 
117
                        Title = Hit.GetFirstProperty ("fixme:Name");
 
118
                        Description = Hit ["fixme:Comment"];
 
119
                }
 
120
 
 
121
                protected override void LoadIcon (Gtk.Image image, int size)
 
122
                {
 
123
                        Gdk.Pixbuf icon = null;
 
124
                        string path = Hit ["fixme:Icon"];
 
125
                        
 
126
                        if (path != null && path != "") {
 
127
                                try {
 
128
                                        if (path.StartsWith ("/")) {
 
129
                                                icon = new Gdk.Pixbuf (path);
 
130
                                        } else {
 
131
                                                if (path.EndsWith (".png")) 
 
132
                                                        icon = WidgetFu.LoadThemeIcon (path.Substring (0, path.Length-4), size);
 
133
                                                else
 
134
                                                        icon = WidgetFu.LoadThemeIcon (path, size);
 
135
                                        
 
136
                                                if (icon == null) {
 
137
                                                        string kde_path = Beagle.Util.KdeUtils.LookupIcon (path);
 
138
                                                        
 
139
                                                        if (System.IO.File.Exists (kde_path))
 
140
                                                                icon = new Gdk.Pixbuf (kde_path);
 
141
                                                }
 
142
                                        }
 
143
                                } catch (Exception e) {
 
144
                                        Console.WriteLine ("Unable to load icon '{0}': {1}", path, e.Message);
 
145
                                }
 
146
                        }
 
147
 
 
148
                        if (icon != null) {
 
149
                                if (icon.Height > size) {
 
150
                                        int scaled_width = (int) ((double) size / (double) icon.Height * icon.Width);
 
151
 
 
152
                                        icon = icon.ScaleSimple (scaled_width, size, Gdk.InterpType.Bilinear);
 
153
                                }
 
154
 
 
155
                                image.Pixbuf = icon;
 
156
                        } else
 
157
                                base.LoadIcon (image, size);
 
158
                }
 
159
 
 
160
                [DllImport ("libgnome-desktop-2.so.2")]
 
161
                static extern int gnome_desktop_item_launch (IntPtr ditem, IntPtr file_list, int flags, IntPtr error);
 
162
 
 
163
                public override void Open ()
 
164
                {
 
165
                        if (gnome_desktop_item_launch (ditem, IntPtr.Zero, 0, IntPtr.Zero) == -1)
 
166
                                Console.WriteLine ("Unable to launch application");
 
167
                }
 
168
 
 
169
#if NOPE
 
170
                public void MoveToTrash ()
 
171
                {
 
172
                        // FIXME: What is the default way to uninstall an application
 
173
                        // in a distro-independent way?
 
174
 
 
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
 
179
 
 
180
                        Console.WriteLine ("Would run 'rpm {0}'", pi.Arguments);
 
181
                }
 
182
#endif
 
183
        }
 
184
}