Serenity Operating System
at master 193 lines 6.3 kB view raw
1/* 2 * Copyright (c) 2021, Nick Vella <nick@nxk.io> 3 * Copyright (c) 2022, the SerenityOS developers. 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include "RunWindow.h" 9#include <AK/LexicalPath.h> 10#include <AK/URL.h> 11#include <Applications/Run/RunGML.h> 12#include <LibCore/DeprecatedFile.h> 13#include <LibCore/StandardPaths.h> 14#include <LibDesktop/Launcher.h> 15#include <LibGUI/Button.h> 16#include <LibGUI/Event.h> 17#include <LibGUI/FilePicker.h> 18#include <LibGUI/Icon.h> 19#include <LibGUI/ImageWidget.h> 20#include <LibGUI/MessageBox.h> 21#include <LibGUI/Widget.h> 22#include <spawn.h> 23#include <stdio.h> 24#include <string.h> 25#include <sys/wait.h> 26#include <unistd.h> 27 28RunWindow::RunWindow() 29 : m_path_history() 30 , m_path_history_model(GUI::ItemListModel<DeprecatedString>::create(m_path_history)) 31{ 32 // FIXME: Handle failure to load history somehow. 33 (void)load_history(); 34 35 auto app_icon = GUI::Icon::default_icon("app-run"sv); 36 37 set_title("Run"); 38 set_icon(app_icon.bitmap_for_size(16)); 39 resize(345, 100); 40 set_resizable(false); 41 set_minimizable(false); 42 43 auto main_widget = set_main_widget<GUI::Widget>().release_value_but_fixme_should_propagate_errors(); 44 main_widget->load_from_gml(run_gml).release_value_but_fixme_should_propagate_errors(); 45 46 m_icon_image_widget = *main_widget->find_descendant_of_type_named<GUI::ImageWidget>("icon"); 47 m_icon_image_widget->set_bitmap(app_icon.bitmap_for_size(32)); 48 49 m_path_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("path"); 50 m_path_combo_box->set_model(m_path_history_model); 51 if (!m_path_history.is_empty()) 52 m_path_combo_box->set_selected_index(0); 53 54 m_ok_button = *main_widget->find_descendant_of_type_named<GUI::DialogButton>("ok_button"); 55 m_ok_button->on_click = [this](auto) { 56 do_run(); 57 }; 58 m_ok_button->set_default(true); 59 60 m_cancel_button = *main_widget->find_descendant_of_type_named<GUI::DialogButton>("cancel_button"); 61 m_cancel_button->on_click = [this](auto) { 62 close(); 63 }; 64 65 m_browse_button = *find_descendant_of_type_named<GUI::DialogButton>("browse_button"); 66 m_browse_button->on_click = [this](auto) { 67 Optional<DeprecatedString> path = GUI::FilePicker::get_open_filepath(this, {}, Core::StandardPaths::home_directory(), false, GUI::Dialog::ScreenPosition::Center); 68 if (path.has_value()) 69 m_path_combo_box->set_text(path.value().view()); 70 }; 71} 72 73void RunWindow::event(Core::Event& event) 74{ 75 if (event.type() == GUI::Event::KeyDown) { 76 auto& key_event = static_cast<GUI::KeyEvent&>(event); 77 if (key_event.key() == Key_Escape) { 78 // Escape key pressed, close dialog 79 close(); 80 return; 81 } else if ((key_event.key() == Key_Up || key_event.key() == Key_Down) && m_path_history.is_empty()) { 82 return; 83 } 84 } 85 86 Window::event(event); 87} 88 89void RunWindow::do_run() 90{ 91 auto run_input = m_path_combo_box->text().trim_whitespace(); 92 93 hide(); 94 95 if (run_via_launch(run_input) || run_as_command(run_input)) { 96 // Remove any existing history entry, prepend the successful run string to history and save. 97 m_path_history.remove_all_matching([&](DeprecatedString v) { return v == run_input; }); 98 m_path_history.prepend(run_input); 99 // FIXME: Handle failure to save history somehow. 100 (void)save_history(); 101 102 close(); 103 return; 104 } 105 106 GUI::MessageBox::show_error(this, "Failed to run. Please check your command, path, or address, and try again."sv); 107 108 show(); 109} 110 111bool RunWindow::run_as_command(DeprecatedString const& run_input) 112{ 113 pid_t child_pid; 114 char const* shell_executable = "/bin/Shell"; // TODO query and use the user's preferred shell. 115 char const* argv[] = { shell_executable, "-c", run_input.characters(), nullptr }; 116 117 if ((errno = posix_spawn(&child_pid, shell_executable, nullptr, nullptr, const_cast<char**>(argv), environ))) { 118 perror("posix_spawn"); 119 return false; 120 } 121 122 // Command spawned in child shell. Hide and wait for exit code. 123 int status; 124 if (waitpid(child_pid, &status, 0) < 0) 125 return false; 126 127 int child_error = WEXITSTATUS(status); 128 dbgln("Child shell exited with code {}", child_error); 129 130 // 127 is typically the shell indicating command not found. 126 for all other errors. 131 if (child_error == 126 || child_error == 127) { 132 return false; 133 } 134 135 dbgln("Ran via command shell."); 136 137 return true; 138} 139 140bool RunWindow::run_via_launch(DeprecatedString const& run_input) 141{ 142 auto url = URL::create_with_url_or_path(run_input); 143 144 if (url.scheme() == "file") { 145 auto real_path = Core::DeprecatedFile::real_path_for(url.path()); 146 if (real_path.is_null()) { 147 // errno *should* be preserved from Core::DeprecatedFile::real_path_for(). 148 warnln("Failed to launch '{}': {}", url.path(), strerror(errno)); 149 return false; 150 } 151 url = URL::create_with_url_or_path(real_path); 152 } 153 154 if (!Desktop::Launcher::open(url)) { 155 warnln("Failed to launch '{}'", url); 156 return false; 157 } 158 159 dbgln("Ran via URL launch."); 160 161 return true; 162} 163 164DeprecatedString RunWindow::history_file_path() 165{ 166 return LexicalPath::canonicalized_path(DeprecatedString::formatted("{}/{}", Core::StandardPaths::config_directory(), "RunHistory.txt")); 167} 168 169ErrorOr<void> RunWindow::load_history() 170{ 171 m_path_history.clear(); 172 auto file = TRY(Core::File::open(history_file_path(), Core::File::OpenMode::Read)); 173 auto buffered_file = TRY(Core::BufferedFile::create(move(file))); 174 Array<u8, PAGE_SIZE> line_buffer; 175 176 while (!buffered_file->is_eof()) { 177 StringView line = TRY(buffered_file->read_line(line_buffer)); 178 if (!line.is_empty() && !line.is_whitespace()) 179 m_path_history.append(line); 180 } 181 return {}; 182} 183 184ErrorOr<void> RunWindow::save_history() 185{ 186 auto file = TRY(Core::File::open(history_file_path(), Core::File::OpenMode::Write)); 187 188 // Write the first 25 items of history 189 for (int i = 0; i < min(static_cast<int>(m_path_history.size()), 25); i++) 190 TRY(file->write_until_depleted(DeprecatedString::formatted("{}\n", m_path_history[i]).bytes())); 191 192 return {}; 193}