diff options
Diffstat (limited to 'ui/src')
| -rw-r--r-- | ui/src/windows/tracker.cpp | 83 |
1 files changed, 73 insertions, 10 deletions
diff --git a/ui/src/windows/tracker.cpp b/ui/src/windows/tracker.cpp index 5afadc5..6c248fe 100644 --- a/ui/src/windows/tracker.cpp +++ b/ui/src/windows/tracker.cpp @@ -20,6 +20,8 @@ #include <nlohmann/json.hpp> #include <format> +#include <stdexcept> +#include <string_view> #include <utility> namespace turns::ui::windows @@ -82,6 +84,12 @@ namespace turns::ui::windows // clang-format on } + auto tracker::do_save() -> void + { + m_buffer = m_turn_order->serialize().dump(2); + m_file->replace_contents_async(sigc::mem_fun(*this, &tracker::handle_save_done), m_buffer, m_file_tag); + } + auto tracker::handle_add_participant() -> void { auto [lifeline, dialog] = editor_for(nullptr); @@ -104,11 +112,62 @@ namespace turns::ui::windows dialog->present(this); } + auto tracker::handle_open() -> void + { + auto filters = Gio::ListStore<Gtk::FileFilter>::create(); + auto filter = Gtk::FileFilter::create(); + filter->set_name(_("Turns Files")); + filter->add_pattern("*.trns"); + filters->append(filter); + + auto dialog = Gtk::FileDialog::create(); + dialog->set_filters(filters); + dialog->open(sigc::bind(sigc::mem_fun(*this, &tracker::handle_open_response), dialog)); + } + + auto tracker::handle_open_done(Glib::RefPtr<Gio::AsyncResult> result) -> void + try + { + set_sensitive(); + char * data{}; + auto size = std::size_t{}; + if (!m_file->load_contents_finish(result, data, size, m_file_tag)) + { + m_file.reset(); + m_file_tag.clear(); + return; + } + auto deserialized = nlohmann::json::parse(std::string_view{data, size}); + m_turn_order->load(deserialized); + auto name = m_file->get_basename(); + auto message = std::vformat(_(lang::successfully_opened_format), std::make_format_args(name)); + auto toast = adw_toast_new(message.c_str()); + adw_toast_overlay_add_toast(m_overlay, toast); + set_title(std::format("{} - {}", _(lang::turns), name)); + } + catch (std::exception const & e) + { + show_error_toast(e); + } + + auto tracker::handle_open_response(Glib::RefPtr<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> dialog) -> void + try + { + m_file = dialog->open_finish(result); + m_file->load_contents_async(sigc::mem_fun(*this, &tracker::handle_open_done)); + set_sensitive(false); + } + catch (std::exception const & e) + { + set_sensitive(); + show_error_toast(e); + } + auto tracker::handle_save(bool force_ask) -> void { if (m_file && !force_ask) { - m_file->replace_contents_async(sigc::mem_fun(*this, &tracker::handle_save_done), m_turn_order->serialize().dump(2), m_file_tag); + do_save(); } else { @@ -145,27 +204,21 @@ namespace turns::ui::windows } catch (Gio::Error const & e) { - auto error = e.what(); - auto message = std::vformat(_(lang::saving_failed_format), std::make_format_args(error)); - auto toast = adw_toast_new(message.c_str()); - adw_toast_overlay_add_toast(m_overlay, toast); + show_error_toast(e); } auto tracker::handle_save_response(Glib::RefPtr<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> dialog) -> void try { m_file = dialog->save_finish(result); - m_file->replace_contents_async(sigc::mem_fun(*this, &tracker::handle_save_done), m_turn_order->serialize().dump(2), m_file_tag); + do_save(); set_sensitive(false); } catch (Gtk::DialogError const & e) { if (e.code() == Gtk::DialogError::FAILED) { - auto error = e.what(); - auto message = std::vformat(_(lang::saving_failed_format), std::make_format_args(error)); - auto toast = adw_toast_new(message.c_str()); - adw_toast_overlay_add_toast(m_overlay, toast); + show_error_toast(e); } } @@ -249,9 +302,11 @@ namespace turns::ui::windows // win.delete // win.edit + // win.open { add_action_with_parameter("delete", Glib::VARIANT_TYPE_INT32, sigc::mem_fun(*this, &tracker::handle_delete_participant)); add_action_with_parameter("edit", Glib::VARIANT_TYPE_INT32, sigc::mem_fun(*this, &tracker::handle_edit_participant)); + add_action("open", sigc::mem_fun(*this, &tracker::handle_open)); } // win.save @@ -275,6 +330,14 @@ namespace turns::ui::windows } } + auto tracker::show_error_toast(std::exception const & e) -> void + { + auto error = e.what(); + auto message = std::vformat(_(lang::saving_failed_format), std::make_format_args(error)); + auto toast = adw_toast_new(message.c_str()); + adw_toast_overlay_add_toast(m_overlay, toast); + } + auto tracker::update_subtitle() -> void { if (m_turn_order->is_empty()) |
