summaryrefslogtreecommitdiff
path: root/lib/apps/application.vala
blob: 75ff6b251faafc0795d129b616b3b58f92b15c6e (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
namespace AstalApps {
public class Application : Object {
    public DesktopAppInfo app { get; construct set; }
    public int frequency { get; set; default = 0; }
    public string name { get { return app.get_name(); } }
    public string entry { get { return app.get_id(); } }
    public string description { get { return app.get_description(); } }
    public string wm_class { get { return app.get_startup_wm_class(); } }
    public string executable { owned get { return app.get_string("Exec"); } }
    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;
    }

    public string get_key(string key) {
        return app.get_string(key);
    }

    public bool launch() {
        try {
            var s = app.launch(null, null);
            ++frequency;
            return s;
        } catch (Error err) {
            critical(err.message);
            return false;
        }
    }

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

        return score;
    }

    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;

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

int min3(int a, int b, int c) {
    return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
}

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