summaryrefslogtreecommitdiff
path: root/lib/apps/application.vala
blob: ea22e7ad32724d2accfdfc820670ef14a86bd8ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
public class AstalApps.Application : Object {
    /**
     * The underlying DesktopAppInfo.
     */
    public DesktopAppInfo app { get; construct set; }

    /**
     * The number of times [[email protected]] was called on this Application.
     */
    public int frequency { get; set; default = 0; }

    /**
     * The name of this Application.
     */
    public string name { get { return app.get_name(); } }

    /**
     * Name of the .desktop of this Application.
     */
    public string entry { get { return app.get_id(); } }

    /**
     * Description of this Application.
     */
    public string description { get { return app.get_description(); } }

    /**
     * The StartupWMClass field from the desktop file.
     * This represents the WM_CLASS property of the main window of the application.
     */
    public string wm_class { get { return app.get_startup_wm_class(); } }

    /**
     * `Exec` field from the desktop file.
     * Note that if you want to launch this Application you should use the [[email protected]] method.
     */
    public string executable { owned get { return app.get_string("Exec"); } }

    /**
     * `Icon` field from the desktop file.
     * This is usually a named icon or a path to a file.
     */
    public string icon_name { owned get { return app.get_string("Icon"); } }

    internal Application(string id, int? frequency = 0) {
        Object(app: new DesktopAppInfo(id));
        this.frequency = frequency;
    }

    /**
     * Get a value from the .desktop file by its key.
     */
    public string get_key(string key) {
        return app.get_string(key);
    }

    /**
     * Launches this application.
     * The launched application inherits the environment of the launching process
     */
    public bool launch() {
        try {
            var s = app.launch(null, null);
            ++frequency;
            return s;
        } catch (Error err) {
            critical(err.message);
            return false;
        }
    }

    internal Score fuzzy_match(string term) {
        var score = Score();
        if (name != null)
            score.name = fuzzy_match_string(term, name);
        if (entry != null)
            score.entry = fuzzy_match_string(term, entry);
        if (executable != null)
            score.executable = fuzzy_match_string(term, executable);
        if (description != null)
            score.description = fuzzy_match_string(term, description);

        return score;
    }

    internal Score exact_match(string term) {
        var score = Score();
        if (name != null)
            score.name = name.down().contains(term.down()) ? 1 : 0;
        if (entry != null)
            score.entry = entry.down().contains(term.down()) ? 1 : 0;
        if (executable != null)
            score.executable = executable.down().contains(term.down()) ? 1 : 0;
        if (description != null)
            score.description = description.down().contains(term.down()) ? 1 : 0;

        return score;
    }

    internal Json.Node to_json() {
        return new Json.Builder()
            .begin_object()
            .set_member_name("name").add_string_value(name)
            .set_member_name("entry").add_string_value(entry)
            .set_member_name("executable").add_string_value(executable)
            .set_member_name("description").add_string_value(description)
            .set_member_name("icon_name").add_string_value(icon_name)
            .set_member_name("frequency").add_int_value(frequency)
            .end_object()
            .get_root();
    }
}

public struct AstalApps.Score {
    int name;
    int entry;
    int executable;
    int description;
}