mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:37:35 +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
63
Userland/Applications/Calendar/EventManager.cpp
Normal file
63
Userland/Applications/Calendar/EventManager.cpp
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright (c) 2023, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "EventManager.h"
|
||||
#include <AK/JsonParser.h>
|
||||
#include <LibConfig/Client.h>
|
||||
#include <LibFileSystemAccessClient/Client.h>
|
||||
|
||||
namespace Calendar {
|
||||
|
||||
EventManager::EventManager()
|
||||
{
|
||||
}
|
||||
|
||||
OwnPtr<EventManager> EventManager::create()
|
||||
{
|
||||
return adopt_own(*new EventManager());
|
||||
}
|
||||
|
||||
ErrorOr<void> EventManager::add_event(JsonObject event)
|
||||
{
|
||||
TRY(m_events.append(move(event)));
|
||||
set_dirty(true);
|
||||
on_events_change();
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void EventManager::set_events(JsonArray events)
|
||||
{
|
||||
m_events = move(events);
|
||||
on_events_change();
|
||||
}
|
||||
|
||||
ErrorOr<void> EventManager::save(FileSystemAccessClient::File& file)
|
||||
{
|
||||
set_filename(file.filename());
|
||||
set_dirty(false);
|
||||
|
||||
auto stream = file.release_stream();
|
||||
TRY(stream->write_some(m_events.to_deprecated_string().bytes()));
|
||||
stream->close();
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<void> EventManager::load_file(FileSystemAccessClient::File& file)
|
||||
{
|
||||
set_filename(file.filename());
|
||||
set_dirty(false);
|
||||
|
||||
auto content = TRY(file.stream().read_until_eof());
|
||||
auto events = TRY(AK::JsonParser(content).parse());
|
||||
|
||||
set_events(events.as_array());
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue