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
{
2021-12-29 15:10:12 -03:00
TRY ( Core : : System : : pledge ( " stdio recvfd sendfd thread rpath cpath wpath unix " ) ) ;
2020-01-11 19:50:05 -03:00
2021-11-23 10:21:27 -03:00
auto app = TRY ( GUI : : Application : : try_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
2022-07-11 14:32:29 -03:00
auto preview_mode = " auto " sv ;
2021-07-09 08:33:47 -03:00
char const * file_to_edit = nullptr ;
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-08-07 13:19:42 -03:00
TRY ( Core : : System : : unveil ( " /tmp/user/%uid/portal/launch " , " rw " ) ) ;
2022-07-24 10:12:04 -03:00
TRY ( Core : : System : : unveil ( " /tmp/user/%uid/portal/webcontent " , " rw " ) ) ;
2022-07-24 10:28:42 -03:00
TRY ( Core : : System : : unveil ( " /tmp/user/%uid/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
2021-11-23 10:21:27 -03:00
auto window = TRY ( GUI : : Window : : try_create ( ) ) ;
2020-07-31 19:12:11 -03:00
window - > resize ( 640 , 400 ) ;
2019-04-13 11:59:55 -03:00
2022-01-07 10:14:47 -03:00
auto text_widget = TRY ( window - > try_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 ;
}
2022-01-07 10:14:47 -03:00
text_widget - > initialize_menubar ( * window ) ;
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 ) ) ;
2021-05-01 13:45:20 -03:00
if ( file_to_edit ) {
2021-03-06 04:13:22 -03:00
FileArgument parsed_argument ( file_to_edit ) ;
2022-01-17 00:43:40 -03:00
auto response = FileSystemAccessClient : : Client : : the ( ) . try_request_file_read_only_approved ( window , parsed_argument . filename ( ) ) ;
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 )
text_widget - > open_nonexistent_file ( parsed_argument . filename ( ) ) ;
else
return 1 ;
2022-01-17 00:43:40 -03:00
} else {
if ( ! text_widget - > read_file ( * response . value ( ) ) )
2021-07-21 14:35:52 -03:00
return 1 ;
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
}
2021-05-01 13:45:20 -03:00
}
2022-01-07 10:14:47 -03:00
text_widget - > update_title ( ) ;
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
}