summaryrefslogtreecommitdiff
path: root/ui/src/windows/tracker.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ui/src/windows/tracker.cpp')
-rw-r--r--ui/src/windows/tracker.cpp64
1 files changed, 62 insertions, 2 deletions
diff --git a/ui/src/windows/tracker.cpp b/ui/src/windows/tracker.cpp
index 8add81f..5571aca 100644
--- a/ui/src/windows/tracker.cpp
+++ b/ui/src/windows/tracker.cpp
@@ -5,12 +5,19 @@
#include "turns/lang/messages.hpp"
#include "turns/ui/windows/participant_editor.hpp"
+#include <sigc++/bind.h>
#include <sigc++/functors/mem_fun.h>
#include <glibmm/binding.h>
#include <glibmm/i18n.h>
+#include <giomm/liststore.h>
+
+#include <gtkmm/error.h>
+#include <gtkmm/filedialog.h>
+
#include <adwaita.h>
+#include <nlohmann/json.hpp>
#include <format>
#include <utility>
@@ -43,6 +50,7 @@ namespace turns::ui::windows
, m_adw{ADW_APPLICATION_WINDOW(gobj())}
, m_controls{builder->get_widget<Gtk::Revealer>("controls")}
, m_empty(builder->get_widget<Gtk::Widget>("empty"))
+ , m_overlay{ADW_TOAST_OVERLAY(builder->get_widget<Gtk::Widget>("overlay")->gobj())}
, m_stack{builder->get_widget<Gtk::Stack>("stack")}
, m_start{builder->get_widget<Gtk::Button>("start")}
, m_title(ADW_WINDOW_TITLE(builder->get_widget<Gtk::Widget>("title")->gobj()))
@@ -92,6 +100,48 @@ namespace turns::ui::windows
dialog->present(this);
}
+ auto tracker::handle_save() -> 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_initial_name(m_file_name.value_or(_(lang::new_turn_order_file_name)));
+ dialog->set_filters(filters);
+ dialog->save(*this, sigc::bind(sigc::mem_fun(*this, &tracker::handle_save_finish), dialog));
+ }
+
+ auto tracker::handle_save_finish(Glib::RefPtr<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> dialog) -> void
+ try
+ {
+ auto file = dialog->save_finish(result);
+ file->replace_contents(m_turn_order->serialize().dump(2), m_file_tag, m_file_tag);
+ auto name = file->get_basename();
+ auto message = std::vformat(_(lang::successfully_saved_format), std::make_format_args(name));
+ auto toast = adw_toast_new(message.c_str());
+ adw_toast_overlay_add_toast(m_overlay, toast);
+ }
+ 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);
+ }
+ }
+ 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);
+ }
+
auto tracker::handle_stop() -> void
{
auto dialog = ADW_ALERT_DIALOG(adw_alert_dialog_new("Stop turn order", "Do you want to clear the turn order?"));
@@ -174,6 +224,16 @@ namespace turns::ui::windows
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));
}
+
+ // win.save
+ // depends-on: turn_order:is_empty == false
+ {
+ auto action = add_action("save", sigc::mem_fun(*this, &tracker::handle_save));
+
+ Glib::Binding::bind_property(m_turn_order->is_empty(),
+ action->property_enabled(),
+ Glib::Binding::Flags::SYNC_CREATE | Glib::Binding::Flags::INVERT_BOOLEAN);
+ }
}
auto tracker::update_subtitle() -> void
@@ -185,8 +245,8 @@ namespace turns::ui::windows
else
{
auto round_number = m_turn_order->round_number() + 1;
- m_subtitle = round_number == 0 ? "" : std::vformat(_("Round {}"), std::make_format_args(round_number));
+ m_subtitle = round_number == 0 ? "" : std::vformat(_(lang::round_number), std::make_format_args(round_number));
}
}
-} // namespace turns::app::windows
+} // namespace turns::ui::windows