summaryrefslogtreecommitdiff
path: root/lib/apps/application.vala
blob: c137183e8febb6ebfba1aeeb673410ceb5d390d4 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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"); } }

    /**
     * `Keywords` field from the desktop file.
     */
    public string[] keywords { owned get { return app.get_keywords(); } }

    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;
        }
    }

    /**
     * Calculate a score for an application using fuzzy matching algorithm.
     */
    public 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);
        foreach (var keyword in keywords) {
            var s = fuzzy_match_string(term, keyword);
            if (s > score.keywords) {
                score.keywords = s;
            }
        }

        return score;
    }

    /**
     * Calculate a score using exact string algorithm.
     */
    public 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;
        foreach (var keyword in keywords) {
            if (score.keywords == 0) {
                score.keywords = keyword.down().contains(term.down()) ? 1 : 0;
            }
        }

        return score;
    }

    internal Json.Node to_json() {
        var builder = 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)
            .set_member_name("keywords")
            .begin_array();

        foreach (string keyword in keywords) {
            builder.add_string_value(keyword);
        }

        return builder
            .end_array()
            .end_object()
            .get_root();
    }
}

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