2020-01-18 05:38:21 -03:00
/*
2021-05-01 13:26:44 -03:00
* Copyright ( c ) 2018 - 2021 , Andreas Kling < kling @ serenityos . org >
2020-01-18 05:38:21 -03:00
*
2021-04-22 05:24:48 -03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 05:38:21 -03:00
*/
2021-03-06 04:13:22 -03:00
# include "FileArgument.h"
2021-05-01 13:26:44 -03:00
# include "MainWidget.h"
2021-08-25 18:56:50 -03:00
# include <LibConfig/Client.h>
2020-07-06 11:18:49 -03:00
# include <LibCore/ArgsParser.h>
2021-11-23 10:21:27 -03:00
# include <LibCore/System.h>
2021-09-06 02:45:53 -03:00
# include <LibFileSystemAccessClient/Client.h>
2021-04-13 11:18:20 -03:00
# include <LibGUI/Menubar.h>
2021-07-11 04:39:00 -03:00
# include <LibGUI/MessageBox.h>
2021-11-23 10:21:27 -03:00
# include <LibMain/Main.h>
2019-05-15 21:47:28 -03:00
2021-05-01 13:26:44 -03:00
using namespace TextEditor ;
2021-11-23 10:21:27 -03:00
ErrorOr < int > serenity_main ( Main : : Arguments arguments )
2019-03-06 20:31:06 -03:00
{
2022-10-03 10:32:18 -03:00
TRY ( Core : : System : : pledge ( " stdio recvfd sendfd thread rpath cpath wpath unix " ) ) ;
2020-01-11 19:50:05 -03:00
2023-05-05 01:24:53 -03:00
auto app = TRY ( GUI : : Application : : create ( arguments ) ) ;
2019-03-06 20:31:06 -03:00
2022-02-11 13:33:09 -03:00
Config : : pledge_domain ( " TextEditor " ) ;
2021-08-25 18:56:50 -03:00
2023-08-07 06:12:38 -03:00
app - > set_config_domain ( " TextEditor " _string ) ;
2023-02-06 14:44:11 -03:00
2022-07-11 14:32:29 -03:00
auto preview_mode = " auto " sv ;
2023-02-28 17:41:43 -03:00
StringView file_to_edit ;
2020-07-06 11:18:49 -03:00
Core : : ArgsParser parser ;
parser . add_option ( preview_mode , " Preview mode, one of 'none', 'html', 'markdown', 'auto' " , " preview-mode " , ' \0 ' , " mode " ) ;
2021-03-06 04:13:22 -03:00
parser . add_positional_argument ( file_to_edit , " File to edit, with optional starting line and column number " , " file[:line[:column]] " , Core : : ArgsParser : : Required : : No ) ;
2021-11-23 10:21:27 -03:00
parser . parse ( arguments ) ;
2020-07-06 11:18:49 -03:00
2021-11-23 10:21:27 -03:00
TRY ( Core : : System : : unveil ( " /res " , " r " ) ) ;
2022-09-06 03:04:06 -03:00
TRY ( Core : : System : : unveil ( " /tmp/session/%sid/portal/launch " , " rw " ) ) ;
TRY ( Core : : System : : unveil ( " /tmp/session/%sid/portal/webcontent " , " rw " ) ) ;
TRY ( Core : : System : : unveil ( " /tmp/session/%sid/portal/filesystemaccess " , " rw " ) ) ;
2021-11-23 10:21:27 -03:00
TRY ( Core : : System : : unveil ( nullptr , nullptr ) ) ;
2021-06-30 10:13:06 -03:00
2022-07-11 14:32:29 -03:00
auto app_icon = GUI : : Icon : : default_icon ( " app-text-editor " sv ) ;
2020-11-02 16:30:17 -03:00
2023-09-16 08:51:33 -03:00
auto window = GUI : : Window : : construct ( ) ;
2023-09-21 13:05:11 -03:00
window - > restore_size_and_position ( " TextEditor " sv , " Window " sv , { { 640 , 400 } } ) ;
window - > save_size_and_position_on_close ( " TextEditor " sv , " Window " sv ) ;
2019-04-13 11:59:55 -03:00
2023-09-18 21:13:48 -03:00
auto text_widget = window - > set_main_widget < MainWidget > ( ) ;
2019-07-11 15:52:33 -03:00
2022-01-07 10:14:47 -03:00
text_widget - > editor ( ) . set_focus ( true ) ;
2019-11-30 11:34:08 -03:00
2020-02-02 11:07:41 -03:00
window - > on_close_request = [ & ] ( ) - > GUI : : Window : : CloseRequestDecision {
2022-01-07 10:14:47 -03:00
if ( text_widget - > request_close ( ) )
2020-02-02 11:07:41 -03:00
return GUI : : Window : : CloseRequestDecision : : Close ;
return GUI : : Window : : CloseRequestDecision : : StayOpen ;
2019-08-27 15:37:41 -03:00
} ;
2022-07-11 14:32:29 -03:00
if ( preview_mode = = " auto " ) {
2022-01-07 10:14:47 -03:00
text_widget - > set_auto_detect_preview_mode ( true ) ;
2022-07-11 14:32:29 -03:00
} else if ( preview_mode = = " markdown " ) {
2022-01-07 10:14:47 -03:00
text_widget - > set_preview_mode ( MainWidget : : PreviewMode : : Markdown ) ;
2022-07-11 14:32:29 -03:00
} else if ( preview_mode = = " html " ) {
2022-01-07 10:14:47 -03:00
text_widget - > set_preview_mode ( MainWidget : : PreviewMode : : HTML ) ;
2022-07-11 14:32:29 -03:00
} else if ( preview_mode = = " none " ) {
2022-01-07 10:14:47 -03:00
text_widget - > set_preview_mode ( MainWidget : : PreviewMode : : None ) ;
2020-07-06 11:18:49 -03:00
} else {
2020-10-06 14:25:44 -03:00
warnln ( " Invalid mode '{}' " , preview_mode ) ;
2020-07-06 11:18:49 -03:00
return 1 ;
}
2023-04-16 09:44:26 -03:00
TRY ( text_widget - > initialize_menubar ( * window ) ) ;
2022-10-12 09:23:14 -03:00
text_widget - > update_title ( ) ;
2021-02-27 07:10:37 -03:00
2021-09-06 02:45:53 -03:00
window - > show ( ) ;
window - > set_icon ( app_icon . bitmap_for_size ( 16 ) ) ;
2023-02-28 17:41:43 -03:00
if ( ! file_to_edit . is_empty ( ) ) {
auto filename = TRY ( String : : from_utf8 ( file_to_edit ) ) ;
2023-02-14 22:01:31 -03:00
FileArgument parsed_argument ( filename ) ;
2023-05-18 09:50:38 -03:00
FileSystemAccessClient : : Client : : the ( ) . set_silence_errors ( FileSystemAccessClient : : ErrorFlag : : NoEntries ) ;
2023-02-14 22:01:31 -03:00
auto response = FileSystemAccessClient : : Client : : the ( ) . request_file_read_only_approved ( window , parsed_argument . filename ( ) . to_deprecated_string ( ) ) ;
2021-06-30 10:13:06 -03:00
2022-01-22 20:39:23 -03:00
if ( response . is_error ( ) ) {
if ( response . error ( ) . code ( ) = = ENOENT )
2023-02-14 22:01:31 -03:00
text_widget - > open_nonexistent_file ( parsed_argument . filename ( ) . to_deprecated_string ( ) ) ;
2022-01-17 00:43:40 -03:00
} else {
2023-01-14 20:02:35 -03:00
TRY ( text_widget - > read_file ( response . value ( ) . filename ( ) , response . value ( ) . stream ( ) ) ) ;
2022-01-07 10:14:47 -03:00
text_widget - > editor ( ) . set_cursor_and_focus_line ( parsed_argument . line ( ) . value_or ( 1 ) - 1 , parsed_argument . column ( ) . value_or ( 0 ) ) ;
2021-07-21 14:35:52 -03:00
}
2022-10-12 09:23:14 -03:00
text_widget - > update_title ( ) ;
2023-05-18 09:50:38 -03:00
FileSystemAccessClient : : Client : : the ( ) . set_silence_errors ( FileSystemAccessClient : : ErrorFlag : : None ) ;
2021-05-01 13:45:20 -03:00
}
2022-02-22 17:03:16 -03:00
text_widget - > update_statusbar ( ) ;
2019-07-11 15:52:33 -03:00
2020-07-04 09:05:19 -03:00
return app - > exec ( ) ;
2019-07-23 13:20:00 -03:00
}