11 /*
2- * Copyright (c) 2020 (https://github.com/phase1geo/Outliner)
2+ * Copyright (c) 2020-2025 (https://github.com/phase1geo/Outliner)
33*
44* This program is free software; you can redistribute it and/or
55* modify it under the terms of the GNU General Public
@@ -24,104 +24,124 @@ using GLib;
2424
2525public class Outliner : Gtk .Application {
2626
27- private static bool show_version = false ;
28- private static bool new_file = false ;
29- private static bool testing = false ;
30- public static string version = " 1.7.0" ;
27+ public static string version = " 2.0.0" ;
3128 public static GLib . Settings settings;
32- private bool loaded = false ;
3329 private MainWindow appwin;
3430
31+ // -------------------------------------------------------------
32+ // Constructor
3533 public Outliner () {
3634
37- Object ( application_id: " com.github.phase1geo.outliner" , flags: ApplicationFlags . HANDLES_OPEN );
35+ Object ( application_id: " com.github.phase1geo.outliner" , flags: ApplicationFlags . HANDLES_COMMAND_LINE );
3836
3937 Intl . setlocale( LocaleCategory . ALL , " " );
4038 Intl . bindtextdomain( GETTEXT_PACKAGE , LOCALEDIR );
4139 Intl . bind_textdomain_codeset( GETTEXT_PACKAGE , " UTF-8" );
4240 Intl . textdomain( GETTEXT_PACKAGE );
4341
4442 startup. connect( start_application );
45- open . connect( open_files );
43+ command_line . connect( handle_command_line );
4644
4745 }
4846
49- /* Begins execution of the application */
47+ // -------------------------------------------------------------
48+ // Begins execution of the application
5049 private void start_application () {
5150
52- /* Initialize the settings */
51+ // Initialize the settings
5352 settings = new GLib .Settings ( " com.github.phase1geo.outliner" );
5453
55- /* Add the application-specific icons */
54+ // Add the application-specific icons
5655 weak IconTheme default_theme = IconTheme . get_for_display( Gdk . Display . get_default() );
5756 default_theme. add_resource_path( " /com/github/phase1geo/outliner" );
5857
59- /* Add the application CSS */
58+ // Add the application CSS
6059 var provider = new Gtk .CssProvider ();
6160 provider. load_from_resource( " /com/github/phase1geo/outliner/css/style.css" );
6261 StyleContext . add_provider_for_display( Gdk . Display . get_default(), provider, STYLE_PROVIDER_PRIORITY_APPLICATION );
6362
64- /* Create the main window */
63+ // Create the main window
6564 appwin = new MainWindow ( this );
6665
67- /* Load the tab information */
68- loaded = appwin. load_tab_state();
66+ // Load the tab information
67+ appwin. load_tab_state();
6968
7069 }
7170
72- /* Parses the list of open files and stores them for opening later during activation */
73- private void open_files ( File [] files , string hint ) {
74- foreach ( File open_file in files ) {
75- var file = open_file. get_path();
76- if ( ! appwin. open_file( file ) ) {
77- stderr. printf( " ERROR: Unable to open file '%s '\n " , file );
78- }
71+ // -------------------------------------------------------------
72+ // Called when the command-line argument handler exits.
73+ private int end_cl ( ApplicationCommandLine cl , int status ) {
74+ // If we are the primary instance, exit now
75+ if ( ! cl. get_is_remote() ) {
76+ Process . exit( status );
77+ } else {
78+ cl. set_exit_status( status );
79+ cl. done();
7980 }
81+ return ( status );
8082 }
8183
82- /* This is called if files aren't specified on the command-line */
83- protected override void activate () {
84- if ( new_file || ! loaded ) {
85- appwin. do_new_file();
86- }
87- }
84+ // -------------------------------------------------------------
85+ // Parse the command-line arguments
86+ private int handle_command_line ( ApplicationCommandLine cl ) {
8887
89- /* Parse the command-line arguments */
90- private void parse_arguments ( ref unowned string [] args ) {
88+ var show_version = false ;
89+ var show_help = false ;
90+ var new_file = false ;
9191
9292 var context = new OptionContext ( " [files]" );
93- var options = new OptionEntry [3 ];
93+ var options = new OptionEntry [4 ];
94+ var args = cl. get_arguments();
9495
95- /* Create the command-line options */
96+ // Create the command-line options
9697 options[0 ] = {" version" , 0 , 0 , OptionArg . NONE , ref show_version, " Display version number" , null };
97- options[1 ] = {" new" , ' n' , 0 , OptionArg . NONE , ref new_file, " Starts Outliner with a new file" , null };
98- options[2 ] = {null };
98+ options[1 ] = {" help" , 0 , 0 , OptionArg . NONE , ref show_help, " Display this help information" , null };
99+ options[2 ] = {" new" , ' n' , 0 , OptionArg . NONE , ref new_file, " Starts Outliner with a new file" , null };
100+ options[3 ] = {null };
99101
100- /* Parse the arguments */
102+ // Parse the arguments
101103 try {
102- context. set_help_enabled( true );
104+ context. set_help_enabled( false );
103105 context. add_main_entries( options, null );
104- context. parse ( ref args );
106+ context. parse_strv ( ref args );
105107 } catch ( OptionError e ) {
106- stdout. printf( " ERROR: %s\n " , e. message );
107- stdout. printf( " Run '%s --help' to see valid options\n " , args[0 ] );
108- Process . exit( 1 );
108+ stdout. printf( _( " ERROR: %s\n " ), e. message );
109+ stdout. printf( _( " Run '%s --help' to see valid options\n " ), args[0 ] );
110+ return ( end_cl( cl, 1 ) );
111+ }
112+
113+ if ( show_help ) {
114+ stdout. printf( context. get_help( true , null ) );
115+ return ( end_cl( cl, 0 ) );
109116 }
110117
111- /* If the version was specified, output it and then exit */
118+ // If the version was specified, output it and then exit
112119 if ( show_version ) {
113120 stdout. printf( " %s\n " , version );
114- Process . exit( 0 );
121+ return ( end_cl( cl, 0 ) );
115122 }
116123
124+ // This is called if files aren't specified on the command-line
125+ if ( new_file ) {
126+ appwin. do_new_file();
127+ } else {
128+ for ( int i= 1 ; i< args. length; i++ ) {
129+ var file = args[i];
130+ if ( ! appwin. open_file( file ) ) {
131+ stdout. printf( _( " ERROR: Unable to open file '%s '\n " ), file );
132+ }
133+ }
134+ }
135+
136+ return ( 0 );
137+
117138 }
118139
119- /* Main routine which gets everything started */
140+ // -------------------------------------------------------------
141+ // Main routine which gets everything started
120142 public static int main ( string [] args ) {
121143
122144 var app = new Outliner ();
123- app. parse_arguments( ref args );
124-
125145 return ( app. run( args ) );
126146
127147 }
0 commit comments