summaryrefslogtreecommitdiff
path: root/ui/src/tracker/event_handlers.cpp
blob: 78eb82be438bf3885177b9b1acb2fe53cef69562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "turns/lang/messages.hpp"
#include "turns/ui/tracker.hpp"

#include <sigc++/functors/mem_fun.h>

#include <glibmm/i18n.h>
#include <glibmm/refptr.h>
#include <glibmm/ustring.h>

#include <giomm/asyncresult.h>
#include <giomm/error.h>

#include <gtkmm/filedialog.h>

#include <nlohmann/json.hpp>

#include <cstddef>
#include <exception>
#include <format>
#include <string_view>

namespace turns::ui
{

  auto Tracker::on_load_content_done(Glib::RefPtr<Gio::AsyncResult> result) -> void
  {
    set_sensitive();
    char * data{};
    auto size = std::size_t{};

    try
    {
      if (!m_file->load_contents_finish(result, data, size, m_file_etag))
      {
        m_file.reset();
        m_file_etag.clear();
        return;
      }

      m_turn_order->load(nlohmann::json::parse(std::string_view{data, size}));
    }
    catch (std::exception const & e)
    {
      return show_error(e);
    }

    auto name = m_file->get_basename();
    show_toast(std::vformat(_(lang::successfully_opened_format), std::make_format_args(name)));
    set_title(std::format("{} - {}", _(lang::turns), name));
  }

  auto Tracker::on_replace_content_done(Glib::RefPtr<Gio::AsyncResult> result) -> void
  {
    set_sensitive();

    try
    {
      m_file->replace_contents_finish(result, m_file_etag);
    }
    catch (Gio::Error const & e)
    {
      return show_error(e);
    }

    auto name = m_file->get_basename();
    show_toast(std::vformat(_(lang::successfully_saved_format), std::make_format_args(name)));
    set_title(std::format("{} - {}", _(lang::turns), name));
  }

  auto Tracker::on_open_response(Glib::RefPtr<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> dialog) -> void
  {
    try
    {
      m_file = dialog->open_finish(result);
    }
    catch (std::exception const & e)
    {
      return show_error(e);
    }

    m_file->load_contents_async(sigc::mem_fun(*this, &Tracker::on_load_content_done));
    set_sensitive(false);
  }

  auto Tracker::on_save_response(Glib::RefPtr<Gio::AsyncResult> result, Glib::RefPtr<Gtk::FileDialog> dialog) -> void
  {
    try
    {
      m_file = dialog->save_finish(result);
    }
    catch (std::exception const & e)
    {
      show_error(e);
    }

    start_replace_content();
    set_sensitive(false);
  }

  auto Tracker::on_settings_changed(Glib::ustring) -> void
  {
    update_colors();
  }

}  // namespace turns::ui