~nonamenoname/slingshot/fix-1084101

« back to all changes in this revision

Viewing changes to src/Backend/GMenuEntries.vala

  • Committer: Giulio Collura
  • Date: 2011-08-24 20:09:46 UTC
  • Revision ID: random.cpp@gmail.com-20110824200946-0mgaf4ebpwro0s3r
First commit of the new Slingshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace Slingshot.Backend {
 
2
 
 
3
    public class GMenuEntries : GLib.Object {
 
4
    
 
5
        public static Gee.ArrayList<GMenu.TreeDirectory> get_categories () {
 
6
            var tree = GMenu.Tree.lookup ("applications.menu", GMenu.TreeFlags.INCLUDE_EXCLUDED);
 
7
            var root = tree.get_root_directory ();
 
8
     
 
9
            var main_directory_entries = new Gee.ArrayList<GMenu.TreeDirectory> ();
 
10
     
 
11
            foreach (GMenu.TreeItem item in root.get_contents()) {
 
12
                if (item.get_type() == GMenu.TreeItemType.DIRECTORY) {
 
13
                    main_directory_entries.add ((GMenu.TreeDirectory) item);
 
14
                }
 
15
            }
 
16
            return main_directory_entries;
 
17
        }
 
18
        
 
19
        public static Gee.ArrayList<GMenu.TreeEntry> get_applications_for_category (GMenu.TreeDirectory category) {
 
20
            var entries = new Gee.ArrayList<GMenu.TreeEntry> ();
 
21
 
 
22
            foreach (GMenu.TreeItem item in category.get_contents ()) {
 
23
                switch (item.get_type ()) {
 
24
                    case GMenu.TreeItemType.DIRECTORY:
 
25
                        entries.add_all (get_applications_for_category ((GMenu.TreeDirectory) item));
 
26
                        break;
 
27
                    case GMenu.TreeItemType.ENTRY:
 
28
                        entries.add ((GMenu.TreeEntry) item);
 
29
                        break;
 
30
                }
 
31
            }
 
32
            return entries;
 
33
        }
 
34
        
 
35
        public static Gee.ArrayList<GMenu.TreeEntry> get_all () {
 
36
            var the_apps = new Gee.ArrayList<GMenu.TreeEntry> ();       
 
37
                
 
38
                    var all_categories = get_categories ();
 
39
                      foreach (GMenu.TreeDirectory directory in all_categories) {
 
40
                                        
 
41
                                            var this_category_apps = get_applications_for_category (directory);
 
42
                                        
 
43
                                            foreach(GMenu.TreeEntry this_app in this_category_apps){
 
44
                                                    the_apps.add(this_app);
 
45
                                            }
 
46
                            }
 
47
                        
 
48
                    return the_apps;
 
49
            }
 
50
            
 
51
            public static void enumerate_apps (Gee.ArrayList<GMenu.TreeEntry> source, Gee.HashMap<string, Gdk.Pixbuf> icons, int icon_size, out Gee.ArrayList<Gee.HashMap<string, string>> list) {
 
52
    
 
53
            var icon_theme = Gtk.IconTheme.get_default();
 
54
            list = new Gee.ArrayList<Gee.HashMap<string, string>> ();
 
55
           
 
56
            foreach (GMenu.TreeEntry app in source) {
 
57
               if (app.get_is_nodisplay() == false && app.get_is_excluded() == false && app.get_icon() != null) {
 
58
                    var app_to_add = new Gee.HashMap<string, string> ();
 
59
                    app_to_add["description"] = app.get_comment();
 
60
                    app_to_add["name"] = app.get_name();
 
61
                    app_to_add["command"] = app.get_exec();
 
62
                    app_to_add["desktop_file"] = app.get_desktop_file_path();
 
63
                    if (!icons.has_key(app_to_add["command"])) {
 
64
                        var app_icon = app.get_icon ();
 
65
                        if (icon_theme.has_icon (app_icon)) {
 
66
                            icons[app_to_add["command"]] = icon_theme.lookup_icon(app_icon, icon_size, 0).load_icon ();
 
67
                        } else if (GLib.File.new_for_path(app_icon).query_exists()) {
 
68
                            try {
 
69
                                 icons[app_to_add["command"]] = new Gdk.Pixbuf.from_file_at_scale (app_icon.to_string (), -1, icon_size, true);
 
70
                             }
 
71
                             catch {
 
72
                                 icons[app_to_add["command"]] = icon_theme.lookup_icon("application-default-icon", icon_size, 0).load_icon ();
 
73
                                 stdout.printf("Failed to load icon from file.\n");
 
74
                             }
 
75
                        } else {
 
76
                            icons[app_to_add["command"]] = icon_theme.lookup_icon("application-default-icon", icon_size, 0).load_icon ();
 
77
                        }
 
78
                    }
 
79
                    
 
80
                    list.add (app_to_add);
 
81
                }
 
82
            }
 
83
            
 
84
        }
 
85
    }
 
86
}