~nonamenoname/slingshot/fix-1084101

« back to all changes in this revision

Viewing changes to src/Backend/App.vala

  • Committer: RabbitBot
  • Author(s): Corentin Noël
  • Date: 2014-01-14 13:05:03 UTC
  • mfrom: (396.1.1 slingshot)
  • Revision ID: rabbitbot-20140114130503-vl01yvhtdlu0dvs4
* Removed every using statement
* Changed VWidgets and HWidgets to Widgets with the corresponding orientation (Gtk deprecation)
* Ported Zeitgeist parts to version 2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
17
//
18
18
 
19
 
namespace Slingshot.Backend {
20
 
 
21
 
    public class App : Object {
22
 
 
23
 
        public string name { get; construct set; }
24
 
        public string description { get; private set; default = ""; }
25
 
        public string desktop_id { get; construct set; }
26
 
        public string exec { get; private set; }
27
 
        public string icon_name { get; private set; default = ""; }
28
 
        public string[] keywords { get; private set;}
29
 
        public Gdk.Pixbuf icon { get; private set; }
30
 
        public double popularity { get; set; }
31
 
        public double relevancy { get; set; }
32
 
        public string desktop_path { get; private set; }
33
 
        public string generic_name { get; private set; default = ""; }
34
 
 
35
 
        private bool is_command = false;
36
 
 
37
 
        public signal void icon_changed ();
38
 
        public signal void launched (App app);
39
 
 
40
 
        public App (GMenu.TreeEntry entry) {
41
 
            name = entry.get_display_name ();
42
 
            description = entry.get_comment () ?? name;
43
 
            exec = entry.get_exec ();
44
 
            desktop_id = entry.get_desktop_file_id ();
45
 
            icon_name = entry.get_icon () ?? "application-default-icon";
46
 
            desktop_path = entry.get_desktop_file_path ();
47
 
            keywords = Unity.AppInfoManager.get_default ().get_keywords (desktop_id);
48
 
            generic_name = entry.get_generic_name ();
49
 
 
50
 
            update_icon ();
51
 
            Slingshot.icon_theme.changed.connect (update_icon);
52
 
        }
53
 
 
54
 
        public App.from_command (string command) {
55
 
 
56
 
            name = command;
57
 
            description = _("Run this command...");
58
 
            exec = command;
59
 
            desktop_id = command;
60
 
            icon_name = "system-run";
61
 
 
62
 
            is_command = true;
63
 
 
64
 
            update_icon ();
65
 
 
66
 
        }
67
 
 
68
 
        public void update_icon () {
69
 
            icon = load_icon (Slingshot.settings.icon_size);
70
 
            icon_changed ();
71
 
        }
72
 
 
73
 
        private delegate void IconLoadFallback ();
74
 
 
75
 
        private class IconLoadFallbackMethod {
76
 
            public unowned IconLoadFallback load_icon;
77
 
 
78
 
            public IconLoadFallbackMethod (IconLoadFallback fallback) {
79
 
                load_icon = fallback;
80
 
            }
81
 
        }
82
 
 
83
 
        public Gdk.Pixbuf load_icon (int size) {
84
 
            Gdk.Pixbuf icon = null;
85
 
            var flags = Gtk.IconLookupFlags.FORCE_SIZE;
86
 
 
87
 
            IconLoadFallbackMethod[] fallbacks = {
88
 
                new IconLoadFallbackMethod (() => {
89
 
                    try {
90
 
                        icon = Slingshot.icon_theme.load_icon (icon_name, size, flags);
91
 
                    } catch (Error e) {
92
 
                        warning ("Could not load icon. Falling back to method 2");
93
 
                    }
94
 
                }),
95
 
 
96
 
                new IconLoadFallbackMethod (() => {
97
 
                    try {
98
 
                        if (icon_name.last_index_of (".") > 0) {
99
 
                            var name = icon_name[0:icon_name.last_index_of (".")];
100
 
                            icon = Slingshot.icon_theme.load_icon (name, size, flags);
101
 
                        }
102
 
                    } catch (Error e) {
103
 
                        warning ("Could not load icon. Falling back to method 3");
104
 
                    }
105
 
                }),
106
 
 
107
 
                new IconLoadFallbackMethod (() => {
108
 
                    try {
109
 
                        icon = new Gdk.Pixbuf.from_file_at_scale (icon_name, size, size, false);
110
 
                    } catch (Error e) {
111
 
                        warning ("Could not load icon. Falling back to method 4");
112
 
                    }
113
 
                }),
114
 
 
115
 
                new IconLoadFallbackMethod (() => {
116
 
                    try {
117
 
                        icon = Slingshot.icon_theme.load_icon ("application-default-icon", size, flags);
118
 
                     } catch (Error e) {
119
 
                         warning ("Could not load icon. Falling back to method 5");
120
 
                     }
121
 
                }),
122
 
 
123
 
                new IconLoadFallbackMethod (() => {
124
 
                     try {
125
 
                        icon = Slingshot.icon_theme.load_icon ("gtk-missing-image", size, flags);
126
 
                     } catch (Error e) {
127
 
                        error ("Could not find a fallback icon to load");
128
 
                     }
129
 
                })
130
 
            };
131
 
 
132
 
            foreach (IconLoadFallbackMethod fallback in fallbacks) {
133
 
                fallback.load_icon ();
134
 
                if (icon != null)
135
 
                    break;
136
 
            }
137
 
 
138
 
            return icon;
139
 
        }
140
 
 
141
 
        public void launch () {
142
 
            try {
143
 
                if (is_command) {
144
 
                    debug (@"Launching command: $name");
145
 
                    Process.spawn_command_line_async (exec);
146
 
                } else {
147
 
                    launched (this); // Emit launched signal
148
 
                    new DesktopAppInfo (desktop_id).launch (null, null);
149
 
                    debug (@"Launching application: $name");
150
 
                }
151
 
            } catch (Error e) {
152
 
                warning ("Failed to launch %s: %s", name, exec);
153
 
            }
154
 
        }
155
 
 
 
19
public class Slingshot.Backend.App : Object {
 
20
 
 
21
    public string name { get; construct set; }
 
22
    public string description { get; private set; default = ""; }
 
23
    public string desktop_id { get; construct set; }
 
24
    public string exec { get; private set; }
 
25
    public string icon_name { get; private set; default = ""; }
 
26
    public string[] keywords { get; private set;}
 
27
    public Gdk.Pixbuf icon { get; private set; }
 
28
    public double popularity { get; set; }
 
29
    public double relevancy { get; set; }
 
30
    public string desktop_path { get; private set; }
 
31
    public string generic_name { get; private set; default = ""; }
 
32
 
 
33
    private bool is_command = false;
 
34
 
 
35
    public signal void icon_changed ();
 
36
    public signal void launched (App app);
 
37
 
 
38
    public App (GMenu.TreeEntry entry) {
 
39
        name = entry.get_display_name ();
 
40
        description = entry.get_comment () ?? name;
 
41
        exec = entry.get_exec ();
 
42
        desktop_id = entry.get_desktop_file_id ();
 
43
        icon_name = entry.get_icon () ?? "application-default-icon";
 
44
        desktop_path = entry.get_desktop_file_path ();
 
45
        keywords = Unity.AppInfoManager.get_default ().get_keywords (desktop_id);
 
46
        generic_name = entry.get_generic_name ();
 
47
 
 
48
        update_icon ();
 
49
        Slingshot.icon_theme.changed.connect (update_icon);
 
50
    }
 
51
 
 
52
    public App.from_command (string command) {
 
53
 
 
54
        name = command;
 
55
        description = _("Run this command...");
 
56
        exec = command;
 
57
        desktop_id = command;
 
58
        icon_name = "system-run";
 
59
 
 
60
        is_command = true;
 
61
 
 
62
        update_icon ();
 
63
 
 
64
    }
 
65
 
 
66
    public void update_icon () {
 
67
        icon = load_icon (Slingshot.settings.icon_size);
 
68
        icon_changed ();
 
69
    }
 
70
 
 
71
    private delegate void IconLoadFallback ();
 
72
 
 
73
    private class IconLoadFallbackMethod {
 
74
        public unowned IconLoadFallback load_icon;
 
75
 
 
76
        public IconLoadFallbackMethod (IconLoadFallback fallback) {
 
77
            load_icon = fallback;
 
78
        }
 
79
    }
 
80
 
 
81
    public Gdk.Pixbuf load_icon (int size) {
 
82
        Gdk.Pixbuf icon = null;
 
83
        var flags = Gtk.IconLookupFlags.FORCE_SIZE;
 
84
 
 
85
        IconLoadFallbackMethod[] fallbacks = {
 
86
            new IconLoadFallbackMethod (() => {
 
87
                try {
 
88
                    icon = Slingshot.icon_theme.load_icon (icon_name, size, flags);
 
89
                } catch (Error e) {
 
90
                    warning ("Could not load icon. Falling back to method 2");
 
91
                }
 
92
            }),
 
93
 
 
94
            new IconLoadFallbackMethod (() => {
 
95
                try {
 
96
                    if (icon_name.last_index_of (".") > 0) {
 
97
                        var name = icon_name[0:icon_name.last_index_of (".")];
 
98
                        icon = Slingshot.icon_theme.load_icon (name, size, flags);
 
99
                    }
 
100
                } catch (Error e) {
 
101
                    warning ("Could not load icon. Falling back to method 3");
 
102
                }
 
103
            }),
 
104
 
 
105
            new IconLoadFallbackMethod (() => {
 
106
                try {
 
107
                    icon = new Gdk.Pixbuf.from_file_at_scale (icon_name, size, size, false);
 
108
                } catch (Error e) {
 
109
                    warning ("Could not load icon. Falling back to method 4");
 
110
                }
 
111
            }),
 
112
 
 
113
            new IconLoadFallbackMethod (() => {
 
114
                try {
 
115
                    icon = Slingshot.icon_theme.load_icon ("application-default-icon", size, flags);
 
116
                 } catch (Error e) {
 
117
                     warning ("Could not load icon. Falling back to method 5");
 
118
                 }
 
119
            }),
 
120
 
 
121
            new IconLoadFallbackMethod (() => {
 
122
                 try {
 
123
                    icon = Slingshot.icon_theme.load_icon ("gtk-missing-image", size, flags);
 
124
                 } catch (Error e) {
 
125
                    error ("Could not find a fallback icon to load");
 
126
                 }
 
127
            })
 
128
        };
 
129
 
 
130
        foreach (IconLoadFallbackMethod fallback in fallbacks) {
 
131
            fallback.load_icon ();
 
132
            if (icon != null)
 
133
                break;
 
134
        }
 
135
 
 
136
        return icon;
 
137
    }
 
138
 
 
139
    public void launch () {
 
140
        try {
 
141
            if (is_command) {
 
142
                debug (@"Launching command: $name");
 
143
                Process.spawn_command_line_async (exec);
 
144
            } else {
 
145
                launched (this); // Emit launched signal
 
146
                new DesktopAppInfo (desktop_id).launch (null, null);
 
147
                debug (@"Launching application: $name");
 
148
            }
 
149
        } catch (Error e) {
 
150
            warning ("Failed to launch %s: %s", name, exec);
 
151
        }
156
152
    }
157
153
 
158
154
}