2021-01-13 12:10:00 +00:00
/*
* Copyright ( c ) 2021 , Nick Vella < nick @ nxk . io >
2022-02-10 19:28:48 +00:00
* Copyright ( c ) 2022 , the SerenityOS developers .
2021-01-13 12:10:00 +00:00
*
2021-04-22 08:24:48 +00:00
* SPDX - License - Identifier : BSD - 2 - Clause
2021-01-13 12:10:00 +00:00
*/
# include "RunWindow.h"
2023-05-26 10:42:58 +00:00
# include "MainWidget.h"
2021-02-15 09:49:37 +00:00
# include <AK/LexicalPath.h>
2023-03-15 16:59:31 +00:00
# include <LibCore/Process.h>
2021-02-15 09:49:37 +00:00
# include <LibCore/StandardPaths.h>
2021-01-13 12:10:00 +00:00
# include <LibDesktop/Launcher.h>
2023-03-24 09:56:53 +00:00
# include <LibFileSystem/FileSystem.h>
2021-01-13 12:10:00 +00:00
# include <LibGUI/Button.h>
# include <LibGUI/Event.h>
# include <LibGUI/FilePicker.h>
# include <LibGUI/Icon.h>
# include <LibGUI/ImageWidget.h>
# include <LibGUI/MessageBox.h>
# include <LibGUI/Widget.h>
2024-03-18 03:22:27 +00:00
# include <LibURL/URL.h>
2021-01-13 12:10:00 +00:00
# include <spawn.h>
# include <stdio.h>
2021-11-07 01:15:10 +00:00
# include <string.h>
2021-01-13 12:10:00 +00:00
# include <sys/wait.h>
# include <unistd.h>
2024-01-30 20:54:20 +00:00
namespace Run {
2021-01-13 12:10:00 +00:00
RunWindow : : RunWindow ( )
2021-02-15 09:49:37 +00:00
: m_path_history ( )
2023-12-16 14:19:34 +00:00
, m_path_history_model ( GUI : : ItemListModel < ByteString > : : create ( m_path_history ) )
2021-01-13 12:10:00 +00:00
{
2022-04-06 16:36:46 +00:00
// FIXME: Handle failure to load history somehow.
( void ) load_history ( ) ;
2021-02-15 09:49:37 +00:00
2022-07-11 17:32:29 +00:00
auto app_icon = GUI : : Icon : : default_icon ( " app-run " sv ) ;
2021-01-13 12:10:00 +00:00
set_title ( " Run " ) ;
set_icon ( app_icon . bitmap_for_size ( 16 ) ) ;
2021-07-27 19:03:06 +00:00
resize ( 345 , 100 ) ;
2021-01-13 12:10:00 +00:00
set_resizable ( false ) ;
set_minimizable ( false ) ;
2024-01-30 20:54:20 +00:00
auto main_widget = MUST ( Run : : MainWidget : : try_create ( ) ) ;
set_main_widget ( main_widget ) ;
2021-01-13 12:10:00 +00:00
2023-01-06 16:48:37 +00:00
m_icon_image_widget = * main_widget - > find_descendant_of_type_named < GUI : : ImageWidget > ( " icon " ) ;
2021-01-13 12:10:00 +00:00
m_icon_image_widget - > set_bitmap ( app_icon . bitmap_for_size ( 32 ) ) ;
2023-01-06 16:48:37 +00:00
m_path_combo_box = * main_widget - > find_descendant_of_type_named < GUI : : ComboBox > ( " path " ) ;
2021-02-15 09:49:37 +00:00
m_path_combo_box - > set_model ( m_path_history_model ) ;
2022-06-30 15:30:34 +00:00
if ( ! m_path_history . is_empty ( ) )
m_path_combo_box - > set_selected_index ( 0 ) ;
2021-01-13 12:10:00 +00:00
2023-01-06 16:48:37 +00:00
m_ok_button = * main_widget - > find_descendant_of_type_named < GUI : : DialogButton > ( " ok_button " ) ;
2021-01-13 12:10:00 +00:00
m_ok_button - > on_click = [ this ] ( auto ) {
do_run ( ) ;
} ;
2022-05-27 17:36:06 +00:00
m_ok_button - > set_default ( true ) ;
2021-01-13 12:10:00 +00:00
2023-01-06 16:48:37 +00:00
m_cancel_button = * main_widget - > find_descendant_of_type_named < GUI : : DialogButton > ( " cancel_button " ) ;
2021-01-13 12:10:00 +00:00
m_cancel_button - > on_click = [ this ] ( auto ) {
close ( ) ;
} ;
2024-01-30 20:54:20 +00:00
m_browse_button = * main_widget - > find_descendant_of_type_named < GUI : : DialogButton > ( " browse_button " ) ;
2021-01-13 12:10:00 +00:00
m_browse_button - > on_click = [ this ] ( auto ) {
2023-12-16 14:19:34 +00:00
Optional < ByteString > path = GUI : : FilePicker : : get_open_filepath ( this , { } , Core : : StandardPaths : : home_directory ( ) , false , GUI : : Dialog : : ScreenPosition : : Center ) ;
2021-01-13 12:10:00 +00:00
if ( path . has_value ( ) )
2021-02-15 09:49:37 +00:00
m_path_combo_box - > set_text ( path . value ( ) . view ( ) ) ;
2021-01-13 12:10:00 +00:00
} ;
}
void RunWindow : : event ( Core : : Event & event )
{
2022-08-02 11:07:19 +00:00
if ( event . type ( ) = = GUI : : Event : : KeyDown ) {
2021-01-13 12:10:00 +00:00
auto & key_event = static_cast < GUI : : KeyEvent & > ( event ) ;
if ( key_event . key ( ) = = Key_Escape ) {
// Escape key pressed, close dialog
close ( ) ;
return ;
2021-02-20 21:28:54 +00:00
} else if ( ( key_event . key ( ) = = Key_Up | | key_event . key ( ) = = Key_Down ) & & m_path_history . is_empty ( ) ) {
return ;
2021-01-13 12:10:00 +00:00
}
}
Window : : event ( event ) ;
}
void RunWindow : : do_run ( )
{
2021-02-15 09:49:37 +00:00
auto run_input = m_path_combo_box - > text ( ) . trim_whitespace ( ) ;
2021-01-13 12:10:00 +00:00
hide ( ) ;
if ( run_via_launch ( run_input ) | | run_as_command ( run_input ) ) {
close ( ) ;
return ;
}
2022-07-11 17:32:29 +00:00
GUI : : MessageBox : : show_error ( this , " Failed to run. Please check your command, path, or address, and try again. " sv ) ;
2021-01-13 12:10:00 +00:00
show ( ) ;
}
2023-12-16 14:19:34 +00:00
bool RunWindow : : run_as_command ( ByteString const & run_input )
2021-01-13 12:10:00 +00:00
{
2023-03-15 16:59:31 +00:00
// TODO: Query and use the user's preferred shell.
auto maybe_child_pid = Core : : Process : : spawn ( " /bin/Shell " sv , Array { " -c " , run_input . characters ( ) } , { } , Core : : Process : : KeepAsChild : : Yes ) ;
if ( maybe_child_pid . is_error ( ) )
2021-01-13 12:10:00 +00:00
return false ;
2023-03-15 16:59:31 +00:00
pid_t child_pid = maybe_child_pid . release_value ( ) ;
2021-01-13 12:10:00 +00:00
2024-03-03 20:49:03 +00:00
// The child shell was able to start. Let's save it to the history immediately so users can see it as the first entry the next time they run this program.
prepend_history ( run_input ) ;
// FIXME: Handle failure to save history somehow.
( void ) save_history ( ) ;
2021-01-13 12:10:00 +00:00
int status ;
if ( waitpid ( child_pid , & status , 0 ) < 0 )
return false ;
int child_error = WEXITSTATUS ( status ) ;
dbgln ( " Child shell exited with code {} " , child_error ) ;
// 127 is typically the shell indicating command not found. 126 for all other errors.
if ( child_error = = 126 | | child_error = = 127 ) {
2024-03-03 20:49:03 +00:00
// There's an opportunity to remove the history entry here since it failed during its runtime, but other implementations (e.g. Windows 11) don't bother removing the entry.
// This makes sense, especially for cases where a user is debugging a failing program.
2021-01-13 12:10:00 +00:00
return false ;
}
dbgln ( " Ran via command shell. " ) ;
return true ;
}
2023-12-16 14:19:34 +00:00
bool RunWindow : : run_via_launch ( ByteString const & run_input )
2021-01-13 12:10:00 +00:00
{
auto url = URL : : create_with_url_or_path ( run_input ) ;
2022-09-28 23:30:58 +00:00
if ( url . scheme ( ) = = " file " ) {
2023-04-14 19:12:03 +00:00
auto file_path = url . serialize_path ( ) ;
auto real_path_or_error = FileSystem : : real_path ( file_path ) ;
2023-03-24 09:56:53 +00:00
if ( real_path_or_error . is_error ( ) ) {
2023-04-14 19:12:03 +00:00
warnln ( " Failed to launch '{}': {} " , file_path , real_path_or_error . error ( ) ) ;
2021-01-13 12:10:00 +00:00
return false ;
}
2024-01-15 16:23:24 +00:00
url = URL : : create_with_url_or_path ( real_path_or_error . release_value ( ) ) ;
2021-01-13 12:10:00 +00:00
}
if ( ! Desktop : : Launcher : : open ( url ) ) {
warnln ( " Failed to launch '{}' " , url ) ;
return false ;
}
2024-03-03 20:49:03 +00:00
prepend_history ( run_input ) ;
// FIXME: Handle failure to save history somehow.
( void ) save_history ( ) ;
2021-01-13 12:10:00 +00:00
dbgln ( " Ran via URL launch. " ) ;
return true ;
}
2021-02-15 09:49:37 +00:00
2023-12-16 14:19:34 +00:00
ByteString RunWindow : : history_file_path ( )
2021-02-15 09:49:37 +00:00
{
2023-12-16 14:19:34 +00:00
return LexicalPath : : canonicalized_path ( ByteString : : formatted ( " {}/{} " , Core : : StandardPaths : : config_directory ( ) , " RunHistory.txt " ) ) ;
2021-02-15 09:49:37 +00:00
}
2022-04-06 16:36:46 +00:00
ErrorOr < void > RunWindow : : load_history ( )
2021-02-15 09:49:37 +00:00
{
m_path_history . clear ( ) ;
2023-02-09 02:02:46 +00:00
auto file = TRY ( Core : : File : : open ( history_file_path ( ) , Core : : File : : OpenMode : : Read ) ) ;
2023-05-03 22:45:18 +00:00
auto buffered_file = TRY ( Core : : InputBufferedFile : : create ( move ( file ) ) ) ;
2022-04-06 16:36:46 +00:00
Array < u8 , PAGE_SIZE > line_buffer ;
2021-02-15 09:49:37 +00:00
2022-04-06 16:36:46 +00:00
while ( ! buffered_file - > is_eof ( ) ) {
StringView line = TRY ( buffered_file - > read_line ( line_buffer ) ) ;
2021-02-15 09:49:37 +00:00
if ( ! line . is_empty ( ) & & ! line . is_whitespace ( ) )
m_path_history . append ( line ) ;
}
2022-04-06 16:36:46 +00:00
return { } ;
2021-02-15 09:49:37 +00:00
}
2024-03-03 20:49:03 +00:00
void RunWindow : : prepend_history ( ByteString const & input )
{
m_path_history . remove_all_matching ( [ & ] ( ByteString const & entry ) { return input = = entry ; } ) ;
m_path_history . prepend ( input ) ;
}
2022-04-06 16:36:46 +00:00
ErrorOr < void > RunWindow : : save_history ( )
2021-02-15 09:49:37 +00:00
{
2023-02-09 02:02:46 +00:00
auto file = TRY ( Core : : File : : open ( history_file_path ( ) , Core : : File : : OpenMode : : Write ) ) ;
2021-02-15 09:49:37 +00:00
// Write the first 25 items of history
for ( int i = 0 ; i < min ( static_cast < int > ( m_path_history . size ( ) ) , 25 ) ; i + + )
2024-04-04 01:44:40 +00:00
TRY ( file - > write_until_depleted ( ByteString : : formatted ( " {} \n " , m_path_history [ i ] ) ) ) ;
2022-04-06 16:36:46 +00:00
return { } ;
2021-02-15 09:49:37 +00:00
}
2024-01-30 20:54:20 +00:00
}