~elementary-apps/slingshot/synapse-desktop-only

« back to all changes in this revision

Viewing changes to src/Backend/App.vala

  • Committer: Tom Beckmann
  • Date: 2014-06-12 09:14:57 UTC
  • Revision ID: tomjonabc@gmail.com-20140612091457-1spb2qtytxybnvkg
Remove all plugins but desktop-file and command one, fix code style

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
errordomain IconError
20
20
{
21
 
        NOT_FOUND
 
21
    NOT_FOUND
22
22
}
23
23
 
24
24
public class Slingshot.Backend.App : Object {
25
25
 
26
 
        public enum AppType {
27
 
                APP,
28
 
                COMMAND,
29
 
                SYNAPSE
30
 
        }
 
26
    public enum AppType {
 
27
        APP,
 
28
        COMMAND,
 
29
        SYNAPSE
 
30
    }
31
31
 
32
 
        public signal void start_search (Synapse.SearchMatch search_match, Synapse.Match? target);
 
32
    public signal void start_search (Synapse.SearchMatch search_match, Synapse.Match? target);
33
33
 
34
34
    public string name { get; construct set; }
35
35
    public string description { get; private set; default = ""; }
43
43
    public string desktop_path { get; private set; }
44
44
    public string categories { get; private set; }
45
45
    public string generic_name { get; private set; default = ""; }
46
 
        public AppType app_type { get; private set; default = AppType.APP; }
 
46
    public AppType app_type { get; private set; default = AppType.APP; }
47
47
 
48
 
        public Synapse.Match? match { get; private set; default = null; }
49
 
        public Synapse.Match? target { get; private set; default = null; }
 
48
    public Synapse.Match? match { get; private set; default = null; }
 
49
    public Synapse.Match? target { get; private set; default = null; }
50
50
 
51
51
    public signal void icon_changed ();
52
52
    public signal void launched (App app);
53
53
 
54
54
    public App (GMenu.TreeEntry entry) {
55
 
                app_type = AppType.APP;
 
55
        app_type = AppType.APP;
56
56
 
57
57
        unowned GLib.DesktopAppInfo info = entry.get_app_info ();
58
58
        name = info.get_display_name ().dup ();
87
87
    }
88
88
 
89
89
    public App.from_command (string command) {
90
 
                app_type = AppType.COMMAND;
 
90
        app_type = AppType.COMMAND;
91
91
 
92
92
        name = command;
93
93
        description = _("Run this command...");
99
99
 
100
100
    }
101
101
 
102
 
        public App.from_synapse_match (Synapse.Match match, Synapse.Match? target = null) {
103
 
 
104
 
                app_type = AppType.SYNAPSE;
105
 
 
106
 
                name = match.title;
107
 
                description = match.description;
108
 
                icon_name = match.icon_name;
109
 
 
110
 
                this.match = match;
111
 
                this.target = target;
112
 
 
113
 
                update_icon ();
114
 
 
115
 
        }
 
102
    public App.from_synapse_match (Synapse.Match match, Synapse.Match? target = null) {
 
103
 
 
104
        app_type = AppType.SYNAPSE;
 
105
 
 
106
        name = match.title;
 
107
        description = match.description;
 
108
        icon_name = match.icon_name;
 
109
 
 
110
        this.match = match;
 
111
        this.target = target;
 
112
 
 
113
        update_icon ();
 
114
 
 
115
    }
116
116
 
117
117
    public void update_icon () {
118
118
        icon = load_icon (Slingshot.settings.icon_size);
130
130
    }
131
131
 
132
132
    public Gdk.Pixbuf? load_icon (int size) {
133
 
                if (app_type == AppType.SYNAPSE) {
134
 
                        try {
135
 
                                // for contacts we can load the thumbnail because we expect it to be
136
 
                                // the avatar. For other types it'd be ridiculously small.
137
 
                                if (match.match_type == Synapse.MatchType.CONTACT && match.has_thumbnail) {
138
 
                                        return new Gdk.Pixbuf.from_file_at_scale (match.thumbnail_path, size, size, true);
139
 
                                }
140
 
 
141
 
                                var icon = Icon.new_for_string (icon_name);
142
 
                                var info = Gtk.IconTheme.get_default ().lookup_by_gicon (icon,
143
 
                                        size, Gtk.IconLookupFlags.FORCE_SIZE);
144
 
 
145
 
                                if (info == null)
146
 
                                        throw new IconError.NOT_FOUND ("Not found");
147
 
 
148
 
                                return info.load_icon ();
149
 
                        } catch (Error e) {
150
 
                                warning ("Failed to load icon: %s\n", e.message);
151
 
                        }
152
 
 
153
 
                        return Slingshot.icon_theme.load_icon ("application-default-icon",
154
 
                                size, Gtk.IconLookupFlags.FORCE_SIZE);
155
 
                }
 
133
        if (app_type == AppType.SYNAPSE) {
 
134
            try {
 
135
                // for contacts we can load the thumbnail because we expect it to be
 
136
                // the avatar. For other types it'd be ridiculously small.
 
137
                if (match.match_type == Synapse.MatchType.CONTACT && match.has_thumbnail) {
 
138
                    return new Gdk.Pixbuf.from_file_at_scale (match.thumbnail_path, size, size, true);
 
139
                }
 
140
 
 
141
                var icon = Icon.new_for_string (icon_name);
 
142
                var info = Gtk.IconTheme.get_default ().lookup_by_gicon (icon,
 
143
                    size, Gtk.IconLookupFlags.FORCE_SIZE);
 
144
 
 
145
                if (info == null)
 
146
                    throw new IconError.NOT_FOUND ("Not found");
 
147
 
 
148
                return info.load_icon ();
 
149
            } catch (Error e) {
 
150
                warning ("Failed to load icon: %s\n", e.message);
 
151
            }
 
152
 
 
153
            return Slingshot.icon_theme.load_icon ("application-default-icon",
 
154
                size, Gtk.IconLookupFlags.FORCE_SIZE);
 
155
        }
156
156
 
157
157
        Gdk.Pixbuf icon = null;
158
158
        var flags = Gtk.IconLookupFlags.FORCE_SIZE;
214
214
    public bool launch () {
215
215
        try {
216
216
            switch (app_type) {
217
 
                                case AppType.COMMAND:
218
 
                                        debug (@"Launching command: $name");
219
 
                                        Process.spawn_command_line_async (exec);
220
 
                                        break;
221
 
                                case AppType.APP:
222
 
                                        launched (this); // Emit launched signal
223
 
                                        new DesktopAppInfo (desktop_id).launch (null, null);
224
 
                                        debug (@"Launching application: $name");
225
 
                                        break;
226
 
                                case AppType.SYNAPSE:
227
 
                                        if (match.match_type == Synapse.MatchType.SEARCH) {
228
 
                                                start_search (match as Synapse.SearchMatch, target);
229
 
                                                return false;
230
 
                                        } else {
231
 
                                                if (target == null)
232
 
                                                        Backend.SynapseSearch.find_actions_for_match (match).get (0).execute_with_target (match);
233
 
                                                else
234
 
                                                        match.execute_with_target (target);
235
 
                                        }
236
 
                                        break;
 
217
                case AppType.COMMAND:
 
218
                    debug (@"Launching command: $name");
 
219
                    Process.spawn_command_line_async (exec);
 
220
                    break;
 
221
                case AppType.APP:
 
222
                    launched (this); // Emit launched signal
 
223
                    new DesktopAppInfo (desktop_id).launch (null, null);
 
224
                    debug (@"Launching application: $name");
 
225
                    break;
 
226
                case AppType.SYNAPSE:
 
227
                    if (match.match_type == Synapse.MatchType.SEARCH) {
 
228
                        start_search (match as Synapse.SearchMatch, target);
 
229
                        return false;
 
230
                    } else {
 
231
                        if (target == null)
 
232
                            Backend.SynapseSearch.find_actions_for_match (match).get (0).execute_with_target (match);
 
233
                        else
 
234
                            match.execute_with_target (target);
 
235
                    }
 
236
                    break;
237
237
            }
238
238
        } catch (Error e) {
239
239
            warning ("Failed to launch %s: %s", name, exec);
240
240
        }
241
241
 
242
 
                return true;
 
242
        return true;
243
243
    }
244
244
 
245
245
}