Ejemplo n.º 1
0
        void execute(model_type& model, abstract_window_type& parent)
        const
        {
            file_property_dialog_type dialog{ parent, m_message_catalog };

            dialog.set_company_name(model.timetable().company_name());
            dialog.set_line_name(model.timetable().line_name());
            dialog.set_note(model.timetable().note());
            if (model.has_path())
                dialog.set_file_name(model.path().template string<string_type>());

            dialog.do_modal();
            if (dialog.result() != dialog_type::result_type::accepted)
                return;

            model.timetable().set_company_name(dialog.company_name());
            model.timetable().set_line_name(dialog.line_name());
            model.timetable().set_note(dialog.note());
        }
        void operator()(
            model_type&                                                           model,
            const tetengo2::stdalt::optional<tetengo2::stdalt::filesystem::path>& given_path,
            abstract_window_type&                                                 parent) const
        {
            if (!m_ask_file_path && !model.has_path() && !given_path)
                return;

            if (!m_confirm_file_save(parent))
                return;

            tetengo2::stdalt::filesystem::path path{};
            if (given_path)
            {
                path = *given_path;
            }
            else if (m_ask_file_path)
            {
                file_open_dialog_type dialog{ m_message_catalog.get(TETENGO2_TEXT("Dialog:FileOpenSave:Open")),
                                              make_file_filters(),
                                              parent };
                const auto            ok = dialog.do_modal();
                if (!ok)
                    return;

                path = dialog.result();
            }
            else
            {
                assert(model.has_path());
                path = model.path();
            }

            std::ifstream input_stream{ path, std::ios_base::binary };
            if (!input_stream)
            {
                create_cant_open_file_message_box(path, parent)->do_modal();
                return;
            }

            reader_selector_type reader_selector{ reader_set_type::create_readers(
                parent, path.template string<typename string_type::value_type>(), m_message_catalog) };
            const auto           first = tetengo2::iterator::make_observable_forward_iterator(
                boost::spirit::make_default_multi_pass(std::istreambuf_iterator<char>{ input_stream }));
            const auto last = tetengo2::iterator::make_observable_forward_iterator(
                boost::spirit::make_default_multi_pass(std::istreambuf_iterator<char>{}));
            auto error = reader_error_type::none;
            auto p_timetable = reader_selector.read(first, last, error);
            if (!p_timetable)
            {
                switch (error)
                {
                case reader_error_type::canceled:
                    break; // Do nothing.
                case reader_error_type::corrupted:
                    create_file_broken_message_box(path, parent)->do_modal();
                    break;
                case reader_error_type::unsupported:
                    create_unsupported_format_file_message_box(path, parent)->do_modal();
                    break;
                default:
                    assert(false);
                    BOOST_THROW_EXCEPTION(std::logic_error("Unknown reader error."));
                }
                return;
            }

            model.reset_timetable(std::move(p_timetable), path);
        }