Add a new archived file tile to beagle-search. Still needs a little love.
[beagle.git] / search / Tiles / OpenWithMenu.cs
blobedec2630495969463634c7b95cf093d376575a67
1 using System;
2 using System.Collections;
3 using System.Runtime.InteropServices;
4 using Gtk;
5 using Gdk;
6 using Gnome.Vfs;
8 namespace Search.Tiles {
10 public class OpenWithMenu : Gtk.Menu {
12 public delegate void OpenWithHandler (MimeApplication app);
13 public event OpenWithHandler ApplicationActivated;
15 private ArrayList list = null;
17 private bool show_icons = true;
18 public bool ShowIcons {
19 get { return show_icons; }
20 set { show_icons = value; }
23 static OpenWithMenu ()
25 Gnome.Vfs.Vfs.Initialize ();
28 public OpenWithMenu (string mime)
30 list = GetApplications (mime);
32 if (list == null)
33 return;
35 foreach (MimeApplication app in list) {
36 ApplicationMenuItem i = new ApplicationMenuItem (this, app);
37 i.Activated += HandleItemActivated;
38 Append (i);
42 public void AppendToMenu (Gtk.Menu menu)
44 if (list == null || list.Count == 0)
45 return;
47 Gtk.MenuItem open_with = new Gtk.MenuItem (Mono.Unix.Catalog.GetString ("Open With"));
48 open_with.Submenu = this;
49 open_with.ShowAll ();
50 menu.Append (open_with);
53 private ArrayList GetApplications (string mime)
55 if (mime == null || mime == "")
56 return null;
58 ArrayList list = new ArrayList ();
60 MimeApplication [] apps = Gnome.Vfs.Mime.GetAllApplications (mime);
62 foreach (MimeApplication app in apps) {
63 // Skip apps that don't take URIs
64 if (! app.SupportsUris ())
65 continue;
67 if (! list.Contains (app))
68 list.Add (app);
71 return list;
74 private void HandleItemActivated (object sender, EventArgs args)
76 if (ApplicationActivated != null)
77 ApplicationActivated ((sender as ApplicationMenuItem).Application);
80 private class ApplicationMenuItem : ImageMenuItem {
82 private MimeApplication application;
83 public MimeApplication Application {
84 get { return application; }
85 set { application = value; }
88 public ApplicationMenuItem (OpenWithMenu menu, MimeApplication ma) : base (ma.Name)
90 this.application = ma;
92 if (!menu.ShowIcons)
93 return;
95 if (ma.Icon == null)
96 return;
98 Gdk.Pixbuf pixbuf = null;
100 try {
101 if (ma.Icon.StartsWith ("/"))
102 pixbuf = new Gdk.Pixbuf (ma.Icon, 16, 16);
103 else
104 pixbuf = IconTheme.Default.LoadIcon (ma.Icon, 16,
105 (IconLookupFlags)0);
106 } catch {
107 pixbuf = null;
110 if (pixbuf != null)
111 Image = new Gtk.Image (pixbuf);