mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 05:07:34 +00:00
Calendar: Implement saving, loading, and displaying of calendars
The user can now save, load, and view calendars. A calendar is made up of an array of events which are saved in a JSON file. In the future we should implement the iCalendar standard instead of using a custom format.
This commit is contained in:
parent
db3e1b128c
commit
1b5b1e4153
17 changed files with 696 additions and 176 deletions
|
@ -5,9 +5,12 @@
|
|||
*/
|
||||
|
||||
#include "AddEventDialog.h"
|
||||
#include <Applications/Calendar/CalendarWindowGML.h>
|
||||
#include "CalendarWidget.h"
|
||||
#include <LibConfig/Client.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibFileSystem/FileSystem.h>
|
||||
#include <LibFileSystemAccessClient/Client.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/ActionGroup.h>
|
||||
#include <LibGUI/Application.h>
|
||||
|
@ -16,24 +19,42 @@
|
|||
#include <LibGUI/Icon.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/Menubar.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibGUI/Process.h>
|
||||
#include <LibGUI/Toolbar.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Font/FontDatabase.h>
|
||||
#include <LibGfx/Palette.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
TRY(Core::System::pledge("stdio recvfd sendfd rpath proc exec unix"));
|
||||
TRY(Core::System::pledge("stdio recvfd sendfd rpath wpath cpath proc exec unix"));
|
||||
|
||||
auto app = TRY(GUI::Application::create(arguments));
|
||||
|
||||
StringView filename;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(filename, "File to read from", "file", Core::ArgsParser::Required::No);
|
||||
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (!filename.is_empty()) {
|
||||
if (!FileSystem::exists(filename) || FileSystem::is_directory(filename)) {
|
||||
warnln("File does not exist or is a directory: {}", filename);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
Config::pledge_domain("Calendar");
|
||||
Config::monitor_domain("Calendar");
|
||||
|
||||
TRY(Core::System::pledge("stdio recvfd sendfd rpath proc exec"));
|
||||
TRY(Core::System::pledge("stdio recvfd sendfd rpath wpath cpath proc exec unix"));
|
||||
TRY(Core::System::unveil("/etc/timezone", "r"));
|
||||
TRY(Core::System::unveil("/res", "r"));
|
||||
TRY(Core::System::unveil("/bin/CalendarSettings", "x"));
|
||||
TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
|
||||
TRY(Core::System::unveil(nullptr, nullptr));
|
||||
|
||||
auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-calendar"sv));
|
||||
|
@ -42,92 +63,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->resize(600, 480);
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
auto main_widget = TRY(window->set_main_widget<GUI::Widget>());
|
||||
TRY(main_widget->load_from_gml(calendar_window_gml));
|
||||
|
||||
auto toolbar = main_widget->find_descendant_of_type_named<GUI::Toolbar>("toolbar");
|
||||
auto calendar = main_widget->find_descendant_of_type_named<GUI::Calendar>("calendar");
|
||||
|
||||
auto prev_date_action = GUI::Action::create({}, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"sv)), [&](const GUI::Action&) {
|
||||
calendar->show_previous_date();
|
||||
});
|
||||
|
||||
auto next_date_action = GUI::Action::create({}, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"sv)), [&](const GUI::Action&) {
|
||||
calendar->show_next_date();
|
||||
});
|
||||
|
||||
auto add_event_action = GUI::Action::create("&Add Event", {}, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/add-event.png"sv)), [&](const GUI::Action&) {
|
||||
AddEventDialog::show(calendar->selected_date(), window);
|
||||
});
|
||||
|
||||
auto jump_to_action = GUI::Action::create("Jump to &Today", {}, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/calendar-date.png"sv)), [&](const GUI::Action&) {
|
||||
calendar->set_selected_date(Core::DateTime::now());
|
||||
calendar->update_tiles(Core::DateTime::now().year(), Core::DateTime::now().month());
|
||||
});
|
||||
|
||||
auto view_month_action = GUI::Action::create_checkable("&Month View", { Mod_Ctrl, KeyCode::Key_1 }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/calendar-month-view.png"sv)), [&](const GUI::Action&) {
|
||||
if (calendar->mode() == GUI::Calendar::Year)
|
||||
calendar->toggle_mode();
|
||||
});
|
||||
view_month_action->set_checked(true);
|
||||
|
||||
auto view_year_action = GUI::Action::create_checkable("&Year View", { Mod_Ctrl, KeyCode::Key_2 }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/icon-view.png"sv)), [&](const GUI::Action&) {
|
||||
if (calendar->mode() == GUI::Calendar::Month)
|
||||
calendar->toggle_mode();
|
||||
});
|
||||
|
||||
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);
|
||||
auto default_view = Config::read_string("Calendar"sv, "View"sv, "DefaultView"sv, "Month"sv);
|
||||
if (default_view == "Year")
|
||||
view_year_action->set_checked(true);
|
||||
|
||||
auto open_settings_action = GUI::Action::create("Calendar &Settings", {}, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-settings.png"sv)), [&](GUI::Action const&) {
|
||||
GUI::Process::spawn_or_show_error(window, "/bin/CalendarSettings"sv);
|
||||
});
|
||||
|
||||
(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));
|
||||
(void)TRY(toolbar->try_add_action(open_settings_action));
|
||||
|
||||
calendar->on_tile_doubleclick = [&] {
|
||||
AddEventDialog::show(calendar->selected_date(), window);
|
||||
};
|
||||
|
||||
calendar->on_month_click = [&] {
|
||||
view_month_action->set_checked(true);
|
||||
};
|
||||
|
||||
auto& file_menu = window->add_menu("&File"_short_string);
|
||||
file_menu.add_action(GUI::Action::create("&Add Event", { Mod_Ctrl | Mod_Shift, Key_E }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/add-event.png"sv)),
|
||||
[&](const GUI::Action&) {
|
||||
AddEventDialog::show(calendar->selected_date(), window);
|
||||
}));
|
||||
file_menu.add_action(open_settings_action);
|
||||
|
||||
TRY(file_menu.try_add_separator());
|
||||
|
||||
TRY(file_menu.try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
||||
GUI::Application::the()->quit();
|
||||
})));
|
||||
|
||||
auto view_menu = TRY(window->try_add_menu("&View"_short_string));
|
||||
TRY(view_menu->try_add_action(*view_month_action));
|
||||
TRY(view_menu->try_add_action(*view_year_action));
|
||||
|
||||
auto help_menu = TRY(window->try_add_menu("&Help"_short_string));
|
||||
TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(window)));
|
||||
TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Calendar", app_icon, window)));
|
||||
auto calendar_widget = TRY(Calendar::CalendarWidget::create(window));
|
||||
window->set_main_widget(calendar_widget);
|
||||
|
||||
window->show();
|
||||
|
||||
if (!filename.is_empty()) {
|
||||
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, filename);
|
||||
if (!response.is_error()) {
|
||||
calendar_widget->load_file(response.release_value());
|
||||
}
|
||||
}
|
||||
|
||||
app->exec();
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue