summaryrefslogtreecommitdiff
path: root/Userland/Applications/Assistant/Providers.h
blob: 9120e6419251e15137c566d4c8686f4588cdc085 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Queue.h>
#include <AK/String.h>
#include <AK/URL.h>
#include <LibDesktop/AppFile.h>
#include <LibGUI/Desktop.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/VM.h>
#include <LibThreading/BackgroundAction.h>
#include <typeinfo>

namespace Assistant {

class Result : public RefCounted<Result> {
public:
    virtual ~Result() = default;

    virtual void activate() const = 0;

    virtual Gfx::Bitmap const* bitmap() const { return nullptr; }

    String const& title() const { return m_title; }
    String const& subtitle() const { return m_subtitle; }
    int score() const { return m_score; }
    bool equals(Result const& other) const
    {
        return typeid(this) == typeid(&other)
            && title() == other.title()
            && subtitle() == other.subtitle();
    }

protected:
    Result(String title, String subtitle, int score = 0)
        : m_title(move(title))
        , m_subtitle(move(subtitle))
        , m_score(score)
    {
    }

private:
    String m_title;
    String m_subtitle;
    int m_score { 0 };
};

class AppResult final : public Result {
public:
    AppResult(RefPtr<Gfx::Bitmap> bitmap, String title, String subtitle, NonnullRefPtr<Desktop::AppFile> af, int score)
        : Result(move(title), move(subtitle), score)
        , m_app_file(move(af))
        , m_bitmap(move(bitmap))
    {
    }
    ~AppResult() override = default;
    void activate() const override;

    virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }

private:
    NonnullRefPtr<Desktop::AppFile> m_app_file;
    RefPtr<Gfx::Bitmap> m_bitmap;
};

class CalculatorResult : public Result {
public:
    explicit CalculatorResult(String title)
        : Result(move(title), "'Enter' will copy to clipboard"sv, 100)
        , m_bitmap(GUI::Icon::default_icon("app-calculator").bitmap_for_size(16))
    {
    }
    ~CalculatorResult() override = default;
    void activate() const override;

    virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }

private:
    RefPtr<Gfx::Bitmap> m_bitmap;
};

class FileResult : public Result {
public:
    explicit FileResult(String title, int score)
        : Result(move(title), "", score)
    {
    }
    ~FileResult() override = default;
    void activate() const override;

    virtual Gfx::Bitmap const* bitmap() const override;
};

class TerminalResult : public Result {
public:
    explicit TerminalResult(String command)
        : Result(move(command), "Run command in Terminal"sv, 100)
        , m_bitmap(GUI::Icon::default_icon("app-terminal").bitmap_for_size(16))
    {
    }
    ~TerminalResult() override = default;
    void activate() const override;

private:
    RefPtr<Gfx::Bitmap> m_bitmap;
};

class URLResult : public Result {
public:
    explicit URLResult(const URL& url)
        : Result(url.to_string(), "'Enter' will open this URL in the browser"sv, 50)
        , m_bitmap(GUI::Icon::default_icon("app-browser").bitmap_for_size(16))
    {
    }
    ~URLResult() override = default;
    void activate() const override;

    virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }

private:
    RefPtr<Gfx::Bitmap> m_bitmap;
};

class Provider {
public:
    virtual ~Provider() = default;

    virtual void query(const String&, Function<void(NonnullRefPtrVector<Result>)> on_complete) = 0;
};

class AppProvider : public Provider {
public:
    void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
};

class CalculatorProvider : public Provider {
public:
    void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
};

class FileProvider : public Provider {
public:
    FileProvider();

    void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
    void build_filesystem_cache();

private:
    RefPtr<Threading::BackgroundAction<NonnullRefPtrVector<Result>>> m_fuzzy_match_work;
    bool m_building_cache { false };
    Vector<String> m_full_path_cache;
    Queue<String> m_work_queue;
};

class TerminalProvider : public Provider {
public:
    void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
};

class URLProvider : public Provider {
public:
    void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
};

}