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-06-30 10:13:06 -03:00
# include <LibCore/File.h>
# include <LibCore/StandardPaths.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>
2019-05-15 21:47:28 -03:00
2021-05-01 13:26:44 -03:00
using namespace TextEditor ;
2019-03-06 20:31:06 -03:00
int main ( int argc , char * * argv )
{
2021-05-13 18:20:26 -03:00
if ( pledge ( " stdio recvfd sendfd thread rpath cpath wpath unix " , nullptr ) < 0 ) {
2020-01-11 19:50:05 -03:00
perror ( " pledge " ) ;
return 1 ;
}
2020-07-04 09:05:19 -03:00
auto app = GUI : : Application : : construct ( argc , argv ) ;
2019-03-06 20:31:06 -03:00
2021-08-25 18:56:50 -03:00
Config : : pledge_domains ( " TextEditor " ) ;
2021-07-09 08:33:47 -03:00
char const * preview_mode = " auto " ;
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 ) ;
2020-07-06 11:18:49 -03:00
parser . parse ( argc , argv ) ;
2021-06-30 10:13:06 -03:00
if ( unveil ( " /res " , " r " ) < 0 ) {
perror ( " unveil " ) ;
return 1 ;
}
if ( unveil ( " /tmp/portal/launch " , " rw " ) < 0 ) {
perror ( " unveil " ) ;
return 1 ;
}
if ( unveil ( " /tmp/portal/webcontent " , " rw " ) < 0 ) {
perror ( " unveil " ) ;
return 1 ;
}
if ( unveil ( " /tmp/portal/filesystemaccess " , " rw " ) < 0 ) {
perror ( " unveil " ) ;
return 1 ;
}
unveil ( nullptr , nullptr ) ;
2020-07-06 11:18:49 -03:00
StringView preview_mode_view = preview_mode ;
2020-11-02 16:30:17 -03:00
auto app_icon = GUI : : Icon : : default_icon ( " app-text-editor " ) ;
2020-02-02 11:07:41 -03:00
auto window = GUI : : Window : : construct ( ) ;
2020-07-31 19:12:11 -03:00
window - > resize ( 640 , 400 ) ;
2019-04-13 11:59:55 -03:00
2021-05-01 13:26:44 -03:00
auto & text_widget = window - > set_main_widget < MainWidget > ( ) ;
2019-07-11 15:52:33 -03:00
2020-03-04 05:46:23 -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 {
2020-03-04 05:46:23 -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
} ;
2020-07-06 11:18:49 -03:00
if ( preview_mode_view = = " auto " ) {
text_widget . set_auto_detect_preview_mode ( true ) ;
} else if ( preview_mode_view = = " markdown " ) {
2021-05-01 13:26:44 -03:00
text_widget . set_preview_mode ( MainWidget : : PreviewMode : : Markdown ) ;
2020-07-06 11:18:49 -03:00
} else if ( preview_mode_view = = " html " ) {
2021-05-01 13:26:44 -03:00
text_widget . set_preview_mode ( MainWidget : : PreviewMode : : HTML ) ;
2020-07-06 11:18:49 -03:00
} else if ( preview_mode_view = = " none " ) {
2021-05-01 13:26:44 -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 ;
}
2021-07-21 16:21:03 -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 ) ;
2021-09-06 02:45:53 -03:00
auto response = FileSystemAccessClient : : Client : : the ( ) . request_file_read_only_approved ( window - > window_id ( ) , parsed_argument . filename ( ) ) ;
2021-06-30 10:13:06 -03:00
2021-09-06 02:45:53 -03:00
if ( response . error = = 0 ) {
if ( ! text_widget . read_file_and_close ( * response . fd , * response . chosen_file ) )
2021-07-21 14:35:52 -03:00
return 1 ;
text_widget . editor ( ) . set_cursor_and_focus_line ( parsed_argument . line ( ) . value_or ( 1 ) - 1 , parsed_argument . column ( ) . value_or ( 0 ) ) ;
} else {
2021-09-06 02:45:53 -03:00
text_widget . open_nonexistent_file ( parsed_argument . filename ( ) ) ;
2021-07-21 14:35:52 -03:00
}
2021-05-01 13:45:20 -03:00
}
2021-03-18 19:07:59 -03:00
text_widget . update_title ( ) ;
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
}