summaryrefslogtreecommitdiff
path: root/Userland/Applications/Assistant/Providers.cpp
blob: bfdebf10e453764832f1d3d440ec3e358716ff0a (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "Providers.h"
#include "FuzzyMatch.h"
#include <AK/LexicalPath.h>
#include <AK/URL.h>
#include <LibCore/DirIterator.h>
#include <LibCore/ElapsedTimer.h>
#include <LibCore/File.h>
#include <LibCore/StandardPaths.h>
#include <LibDesktop/Launcher.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/FileIconProvider.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Lexer.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <errno.h>
#include <fcntl.h>
#include <serenity.h>
#include <spawn.h>
#include <unistd.h>

namespace Assistant {

void AppResult::activate() const
{
    if (chdir(Core::StandardPaths::home_directory().characters()) < 0) {
        perror("chdir");
        exit(1);
    }

    m_app_file->spawn();
}

void CalculatorResult::activate() const
{
    GUI::Clipboard::the().set_plain_text(title());
}

void FileResult::activate() const
{
    Desktop::Launcher::open(URL::create_with_file_protocol(title()));
}

void TerminalResult::activate() const
{
    pid_t pid;
    char const* argv[] = { "Terminal", "-e", title().characters(), nullptr };

    if ((errno = posix_spawn(&pid, "/bin/Terminal", nullptr, nullptr, const_cast<char**>(argv), environ))) {
        perror("posix_spawn");
    } else {
        if (disown(pid) < 0)
            perror("disown");
    }
}

void URLResult::activate() const
{
    Desktop::Launcher::open(URL::create_with_url_or_path(title()));
}

void AppProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)
{
    if (query.starts_with("=") || query.starts_with('$'))
        return;

    NonnullRefPtrVector<Result> results;

    Desktop::AppFile::for_each([&](NonnullRefPtr<Desktop::AppFile> app_file) {
        auto match_result = fuzzy_match(query, app_file->name());
        if (!match_result.matched)
            return;

        auto icon = GUI::FileIconProvider::icon_for_executable(app_file->executable());
        results.append(adopt_ref(*new AppResult(icon.bitmap_for_size(16), app_file->name(), {}, app_file, match_result.score)));
    });

    on_complete(move(results));
}

void CalculatorProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)
{
    if (!query.starts_with("="))
        return;

    auto vm = JS::VM::create();
    auto interpreter = JS::Interpreter::create<JS::GlobalObject>(*vm);

    auto source_code = query.substring(1);
    auto parser = JS::Parser(JS::Lexer(source_code));
    auto program = parser.parse_program();
    if (parser.has_errors())
        return;

    interpreter->run(interpreter->global_object(), *program);
    if (interpreter->exception())
        return;

    auto result = interpreter->vm().last_value();
    String calculation;
    if (!result.is_number()) {
        calculation = "0";
    } else {
        calculation = result.to_string_without_side_effects();
    }

    NonnullRefPtrVector<Result> results;
    results.append(adopt_ref(*new CalculatorResult(calculation)));
    on_complete(move(results));
}

Gfx::Bitmap const* FileResult::bitmap() const
{
    return GUI::FileIconProvider::icon_for_path(title()).bitmap_for_size(16);
}

FileProvider::FileProvider()
{
    build_filesystem_cache();
}

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

    if (m_fuzzy_match_work)
        m_fuzzy_match_work->cancel();

    m_fuzzy_match_work = Threading::BackgroundAction<NonnullRefPtrVector<Result>>::create(
        [this, query](auto& task) {
            NonnullRefPtrVector<Result> results;

            for (auto& path : m_full_path_cache) {
                if (task.is_cancelled())
                    return results;

                auto match_result = fuzzy_match(query, path);
                if (!match_result.matched)
                    continue;
                if (match_result.score < 0)
                    continue;

                results.append(adopt_ref(*new FileResult(path, match_result.score)));
            }
            return results;
        },
        [on_complete = move(on_complete)](auto results) {
            on_complete(move(results));
        });
}

void FileProvider::build_filesystem_cache()
{
    if (m_full_path_cache.size() > 0 || m_building_cache)
        return;

    m_building_cache = true;
    m_work_queue.enqueue("/");

    Threading::BackgroundAction<int>::create(
        [this](auto&) {
            String slash = "/";
            Core::ElapsedTimer timer;
            timer.start();
            while (!m_work_queue.is_empty()) {
                auto base_directory = m_work_queue.dequeue();

                if (base_directory.template is_one_of("/proc"sv, "/sys"sv))
                    continue;

                Core::DirIterator di(base_directory, Core::DirIterator::SkipDots);

                while (di.has_next()) {
                    auto path = di.next_path();
                    struct stat st = {};
                    if (fstatat(di.fd(), path.characters(), &st, AT_SYMLINK_NOFOLLOW) < 0) {
                        perror("fstatat");
                        continue;
                    }

                    if (S_ISLNK(st.st_mode))
                        continue;

                    auto full_path = LexicalPath::join(slash, base_directory, path).string();

                    m_full_path_cache.append(full_path);

                    if (S_ISDIR(st.st_mode)) {
                        m_work_queue.enqueue(full_path);
                    }
                }
            }
            dbgln("Built cache in {} ms", timer.elapsed());
            return 0;
        },
        [this](auto) {
            m_building_cache = false;
        });
}

void TerminalProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)
{
    if (!query.starts_with('$'))
        return;

    auto command = query.substring(1);

    NonnullRefPtrVector<Result> results;
    results.append(adopt_ref(*new TerminalResult(move(command))));
    on_complete(move(results));
}

void URLProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)
{
    if (query.is_empty() || query.starts_with('=') || query.starts_with('$'))
        return;

    URL url = URL(query);

    if (url.scheme().is_empty())
        url.set_scheme("http");
    if (url.host().is_empty())
        url.set_host(query);
    if (url.paths().is_empty())
        url.set_paths({ "" });

    if (!url.is_valid())
        return;

    NonnullRefPtrVector<Result> results;
    results.append(adopt_ref(*new URLResult(url)));
    on_complete(results);
}

}