2020-03-19 15:55:25 -03:00
/*
* Copyright ( c ) 2019 - 2020 , Ryan Grieb < ryan . m . grieb @ gmail . com >
*
2021-04-22 05:24:48 -03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-03-19 15:55:25 -03:00
*/
2020-08-19 10:29:43 -03:00
# include "AddEventDialog.h"
2021-03-29 21:43:40 -03:00
# include <Applications/Calendar/CalendarWindowGML.h>
2022-07-15 20:33:39 -03:00
# include <LibConfig/Client.h>
2022-01-04 14:14:56 -03:00
# include <LibCore/System.h>
2020-03-10 18:41:48 -03:00
# include <LibGUI/Action.h>
2021-03-29 21:43:40 -03:00
# include <LibGUI/ActionGroup.h>
2020-03-10 18:41:48 -03:00
# include <LibGUI/Application.h>
2020-08-19 10:29:43 -03:00
# include <LibGUI/BoxLayout.h>
# include <LibGUI/Calendar.h>
# include <LibGUI/Icon.h>
2020-03-10 18:41:48 -03:00
# include <LibGUI/Menu.h>
2021-04-13 11:18:20 -03:00
# include <LibGUI/Menubar.h>
2022-07-17 18:29:44 -03:00
# include <LibGUI/Process.h>
2021-04-13 11:18:20 -03:00
# include <LibGUI/Toolbar.h>
2020-03-10 18:41:48 -03:00
# include <LibGUI/Window.h>
2022-01-04 14:14:56 -03:00
# include <LibMain/Main.h>
2020-03-10 18:41:48 -03:00
2022-01-04 14:14:56 -03:00
ErrorOr < int > serenity_main ( Main : : Arguments arguments )
2020-03-10 18:41:48 -03:00
{
2022-07-17 18:29:44 -03:00
TRY ( Core : : System : : pledge ( " stdio recvfd sendfd rpath proc exec unix " ) ) ;
2020-03-10 18:41:48 -03:00
2022-01-04 14:14:56 -03:00
auto app = TRY ( GUI : : Application : : try_create ( arguments ) ) ;
2020-03-10 18:41:48 -03:00
2022-07-15 20:33:39 -03:00
Config : : pledge_domain ( " Calendar " ) ;
2022-09-19 16:23:40 -03:00
Config : : monitor_domain ( " Calendar " ) ;
2022-07-15 20:33:39 -03:00
2022-07-17 18:29:44 -03:00
TRY ( Core : : System : : pledge ( " stdio recvfd sendfd rpath proc exec " ) ) ;
2022-01-20 14:27:56 -03:00
TRY ( Core : : System : : unveil ( " /etc/timezone " , " r " ) ) ;
2022-01-04 14:14:56 -03:00
TRY ( Core : : System : : unveil ( " /res " , " r " ) ) ;
2022-07-17 18:29:44 -03:00
TRY ( Core : : System : : unveil ( " /bin/CalendarSettings " , " x " ) ) ;
2022-01-04 14:14:56 -03:00
TRY ( Core : : System : : unveil ( nullptr , nullptr ) ) ;
2020-03-10 18:41:48 -03:00
2022-07-11 14:32:29 -03:00
auto app_icon = TRY ( GUI : : Icon : : try_create_default_icon ( " app-calendar " sv ) ) ;
2022-01-04 14:14:56 -03:00
auto window = TRY ( GUI : : Window : : try_create ( ) ) ;
2020-03-10 18:41:48 -03:00
window - > set_title ( " Calendar " ) ;
2020-08-19 10:29:43 -03:00
window - > resize ( 600 , 480 ) ;
window - > set_icon ( app_icon . bitmap_for_size ( 16 ) ) ;
2020-03-10 18:41:48 -03:00
2022-01-04 14:14:56 -03:00
auto main_widget = TRY ( window - > try_set_main_widget < GUI : : Widget > ( ) ) ;
main_widget - > load_from_gml ( calendar_window_gml ) ;
2020-08-19 10:29:43 -03:00
2022-01-04 14:14:56 -03:00
auto toolbar = main_widget - > find_descendant_of_type_named < GUI : : Toolbar > ( " toolbar " ) ;
auto calendar = main_widget - > find_descendant_of_type_named < GUI : : Calendar > ( " calendar " ) ;
2020-08-19 10:29:43 -03:00
2022-07-11 14:32:29 -03:00
auto prev_date_action = GUI : : Action : : create ( { } , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/go-back.png " sv ) ) , [ & ] ( const GUI : : Action & ) {
2021-03-29 21:43:40 -03:00
unsigned view_month = calendar - > view_month ( ) ;
unsigned view_year = calendar - > view_year ( ) ;
if ( calendar - > mode ( ) = = GUI : : Calendar : : Month ) {
view_month - - ;
if ( calendar - > view_month ( ) = = 1 ) {
view_month = 12 ;
view_year - - ;
2020-08-19 10:29:43 -03:00
}
} else {
2021-03-29 21:43:40 -03:00
view_year - - ;
2020-08-19 10:29:43 -03:00
}
2021-03-29 21:43:40 -03:00
calendar - > update_tiles ( view_year , view_month ) ;
2020-08-19 10:29:43 -03:00
} ) ;
2022-07-11 14:32:29 -03:00
auto next_date_action = GUI : : Action : : create ( { } , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/go-forward.png " sv ) ) , [ & ] ( const GUI : : Action & ) {
2021-03-29 21:43:40 -03:00
unsigned view_month = calendar - > view_month ( ) ;
unsigned view_year = calendar - > view_year ( ) ;
if ( calendar - > mode ( ) = = GUI : : Calendar : : Month ) {
view_month + + ;
if ( calendar - > view_month ( ) = = 12 ) {
view_month = 1 ;
view_year + + ;
2020-08-19 10:29:43 -03:00
}
} else {
2021-03-29 21:43:40 -03:00
view_year + + ;
2020-08-19 10:29:43 -03:00
}
2021-03-29 21:43:40 -03:00
calendar - > update_tiles ( view_year , view_month ) ;
2020-08-19 10:29:43 -03:00
} ) ;
2022-07-11 14:32:29 -03:00
auto add_event_action = GUI : : Action : : create ( " &Add Event " , { } , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/add-event.png " sv ) ) , [ & ] ( const GUI : : Action & ) {
2021-03-29 21:43:40 -03:00
AddEventDialog : : show ( calendar - > selected_date ( ) , window ) ;
2020-08-19 10:29:43 -03:00
} ) ;
2022-07-11 14:32:29 -03:00
auto jump_to_action = GUI : : Action : : create ( " Jump to &Today " , { } , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/calendar-date.png " sv ) ) , [ & ] ( const GUI : : Action & ) {
2021-03-29 21:43:40 -03:00
calendar - > set_selected_date ( Core : : DateTime : : now ( ) ) ;
calendar - > update_tiles ( Core : : DateTime : : now ( ) . year ( ) , Core : : DateTime : : now ( ) . month ( ) ) ;
2020-08-19 10:29:43 -03:00
} ) ;
2022-07-11 14:32:29 -03:00
auto view_month_action = GUI : : Action : : create_checkable ( " &Month View " , { Mod_Ctrl , KeyCode : : Key_1 } , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/calendar-month-view.png " sv ) ) , [ & ] ( const GUI : : Action & ) {
2021-03-29 21:43:40 -03:00
if ( calendar - > mode ( ) = = GUI : : Calendar : : Year )
calendar - > toggle_mode ( ) ;
} ) ;
view_month_action - > set_checked ( true ) ;
2020-08-19 10:29:43 -03:00
2022-07-11 14:32:29 -03:00
auto view_year_action = GUI : : Action : : create_checkable ( " &Year View " , { Mod_Ctrl , KeyCode : : Key_2 } , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/icon-view.png " sv ) ) , [ & ] ( const GUI : : Action & ) {
2021-03-29 21:43:40 -03:00
if ( calendar - > mode ( ) = = GUI : : Calendar : : Month )
calendar - > toggle_mode ( ) ;
} ) ;
2020-08-19 10:29:43 -03:00
2021-03-29 21:43:40 -03:00
auto view_type_action_group = make < GUI : : ActionGroup > ( ) ;
view_type_action_group - > set_exclusive ( true ) ;
view_type_action_group - > add_action ( * view_month_action ) ;
view_type_action_group - > add_action ( * view_year_action ) ;
2022-09-19 17:50:01 -03:00
auto default_view = Config : : read_string ( " Calendar " sv , " View " sv , " DefaultView " sv , " Month " sv ) ;
if ( default_view = = " Year " )
view_year_action - > set_checked ( true ) ;
2021-03-29 21:43:40 -03:00
2022-07-17 18:29:44 -03:00
auto open_settings_action = GUI : : Action : : create ( " &Settings " , { } , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/app-settings.png " sv ) ) , [ & ] ( GUI : : Action const & ) {
GUI : : Process : : spawn_or_show_error ( window , " /bin/CalendarSettings " sv ) ;
} ) ;
2022-01-04 14:14:56 -03:00
( void ) TRY ( toolbar - > try_add_action ( prev_date_action ) ) ;
( void ) TRY ( toolbar - > try_add_action ( next_date_action ) ) ;
TRY ( toolbar - > try_add_separator ( ) ) ;
( void ) TRY ( toolbar - > try_add_action ( jump_to_action ) ) ;
( void ) TRY ( toolbar - > try_add_action ( add_event_action ) ) ;
TRY ( toolbar - > try_add_separator ( ) ) ;
( void ) TRY ( toolbar - > try_add_action ( view_month_action ) ) ;
( void ) TRY ( toolbar - > try_add_action ( view_year_action ) ) ;
2022-07-17 18:29:44 -03:00
( void ) TRY ( toolbar - > try_add_action ( open_settings_action ) ) ;
2021-03-29 21:43:40 -03:00
calendar - > on_tile_doubleclick = [ & ] {
AddEventDialog : : show ( calendar - > selected_date ( ) , window ) ;
2020-08-19 10:29:43 -03:00
} ;
2021-03-29 21:43:40 -03:00
calendar - > on_month_click = [ & ] {
view_month_action - > set_checked ( true ) ;
2020-08-19 10:29:43 -03:00
} ;
2020-03-10 18:41:48 -03:00
2021-07-21 16:21:03 -03:00
auto & file_menu = window - > add_menu ( " &File " ) ;
2022-07-11 14:32:29 -03:00
file_menu . add_action ( GUI : : Action : : create ( " &Add Event " , { Mod_Ctrl | Mod_Shift , Key_E } , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/add-event.png " sv ) ) ,
2020-04-09 19:05:48 -03:00
[ & ] ( const GUI : : Action & ) {
2021-03-29 21:43:40 -03:00
AddEventDialog : : show ( calendar - > selected_date ( ) , window ) ;
2020-04-09 19:05:48 -03:00
} ) ) ;
2022-07-17 18:29:44 -03:00
file_menu . add_action ( open_settings_action ) ;
2020-04-09 19:05:48 -03:00
2022-01-04 14:14:56 -03:00
TRY ( file_menu . try_add_separator ( ) ) ;
2020-08-19 10:29:43 -03:00
2022-01-04 14:14:56 -03:00
TRY ( file_menu . try_add_action ( GUI : : CommonActions : : make_quit_action ( [ ] ( auto & ) {
2020-07-04 11:52:01 -03:00
GUI : : Application : : the ( ) - > quit ( ) ;
2022-01-04 14:14:56 -03:00
} ) ) ) ;
2020-04-09 19:05:48 -03:00
2022-01-04 14:14:56 -03:00
auto view_menu = TRY ( window - > try_add_menu ( " &View " ) ) ;
TRY ( view_menu - > try_add_action ( * view_month_action ) ) ;
TRY ( view_menu - > try_add_action ( * view_year_action ) ) ;
2021-03-29 21:43:40 -03:00
2022-01-04 14:14:56 -03:00
auto help_menu = TRY ( window - > try_add_menu ( " &Help " ) ) ;
TRY ( help_menu - > try_add_action ( GUI : : CommonActions : : make_about_action ( " Calendar " , app_icon , window ) ) ) ;
2020-08-18 12:08:54 -03:00
2020-08-19 10:29:43 -03:00
window - > show ( ) ;
2020-07-04 09:05:19 -03:00
app - > exec ( ) ;
2022-01-04 14:14:56 -03:00
return 0 ;
2020-03-10 18:41:48 -03:00
}