Skip to content

Commit

Permalink
improving resilience when losing access to open document (#2120)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuspen committed Nov 18, 2023
1 parent c901132 commit cedfdb6
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 135 deletions.
193 changes: 97 additions & 96 deletions src/ct/ct_actions_export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ void CtActions::export_to_txt_auto(const std::string& dir, bool overwrite, bool
void CtActions::_export_print(bool save_to_pdf, const fs::path& auto_path, bool auto_overwrite)
{
CtExporting export_type;
if (!auto_path.empty()) {
if (not auto_path.empty()) {
export_type = CtExporting::ALL_TREE;
}
else {
if (!_is_there_selected_node_or_error()) return;
if (not _is_there_selected_node_or_error()) return;
export_type = CtDialogs::selnode_selnodeandsub_alltree_dialog(*_pCtMainWin, true, &_export_options.include_node_name,
&_export_options.new_node_page, nullptr, nullptr);
}
Expand All @@ -210,9 +210,9 @@ void CtActions::_export_print(bool save_to_pdf, const fs::path& auto_path, bool
CtExport2Pdf{_pCtMainWin}.node_and_subnodes_export_print(pdf_filepath, _pCtMainWin->curr_tree_iter(), _export_options);
}
else if (export_type == CtExporting::ALL_TREE) {
if (!auto_path.empty()) {
if (not auto_path.empty()) {
pdf_filepath = auto_path / (_pCtMainWin->get_ct_storage()->get_file_name().string() + ".pdf");
if (!auto_overwrite && fs::is_regular_file(pdf_filepath)) {
if (not auto_overwrite && fs::is_regular_file(pdf_filepath)) {
spdlog::info("pdf exists and overwrite is off, export is stopped");
return;
}
Expand All @@ -224,7 +224,7 @@ void CtActions::_export_print(bool save_to_pdf, const fs::path& auto_path, bool
CtExport2Pdf{_pCtMainWin}.tree_export_print(pdf_filepath, _pCtMainWin->get_tree_store().get_ct_iter_first(), _export_options);
}
else if (export_type == CtExporting::SELECTED_TEXT) {
if (!_is_there_text_selection_or_error()) return;
if (not _is_there_text_selection_or_error()) return;
Gtk::TextIter iter_start, iter_end;
_curr_buffer()->get_selection_bounds(iter_start, iter_end);

Expand All @@ -246,126 +246,127 @@ void CtActions::_export_print(bool save_to_pdf, const fs::path& auto_path, bool
void CtActions::_export_to_html(const fs::path& auto_path, bool auto_overwrite)
{
CtExporting export_type;
if (!auto_path.empty())
{
if (not auto_path.empty()) {
export_type = CtExporting::ALL_TREE;
}
else
{
if (!_is_there_selected_node_or_error()) return;
export_type = CtDialogs::selnode_selnodeandsub_alltree_dialog(*_pCtMainWin, true, &_export_options.include_node_name,
nullptr, &_export_options.index_in_page, &_export_options.single_file);
else {
if (not _is_there_selected_node_or_error()) return;
export_type = CtDialogs::selnode_selnodeandsub_alltree_dialog(
*_pCtMainWin, true, &_export_options.include_node_name,
nullptr, &_export_options.index_in_page, &_export_options.single_file);
}
if (export_type == CtExporting::NONESAVE) return;

CtExport2Html export2html(_pCtMainWin);
CtExport2Html export2html{_pCtMainWin};
fs::path ret_html_path;
if (export_type == CtExporting::CURRENT_NODE)
{
std::string folder_name = CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter());
if (export2html.prepare_html_folder("", folder_name, false, ret_html_path))
export2html.node_export_to_html(_pCtMainWin->curr_tree_iter(), _export_options, "", -1, -1);
}
else if (export_type == CtExporting::CURRENT_NODE_AND_SUBNODES)
{
std::string folder_name = CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter());
if (export2html.prepare_html_folder("", folder_name, false, ret_html_path)) {
if (_export_options.single_file) {
export2html.nodes_all_export_to_single_html(false, _export_options);
} else {
export2html.nodes_all_export_to_multiple_html(false, _export_options);
try {
if (export_type == CtExporting::CURRENT_NODE) {
std::string folder_name = CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter());
if (export2html.prepare_html_folder("", folder_name, false, ret_html_path)) {
export2html.node_export_to_html(_pCtMainWin->curr_tree_iter(), _export_options, "", -1, -1);
}
}
}
else if (export_type == CtExporting::ALL_TREE)
{
fs::path folder_name = _pCtMainWin->get_ct_storage()->get_file_name();
if (export2html.prepare_html_folder(auto_path, folder_name, auto_overwrite, ret_html_path)) {
if (_export_options.single_file) {
export2html.nodes_all_export_to_single_html(true, _export_options);
} else {
export2html.nodes_all_export_to_multiple_html(true, _export_options);
else if (export_type == CtExporting::CURRENT_NODE_AND_SUBNODES) {
std::string folder_name = CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter());
if (export2html.prepare_html_folder("", folder_name, false, ret_html_path)) {
if (_export_options.single_file) {
export2html.nodes_all_export_to_single_html(false, _export_options);
}
else {
export2html.nodes_all_export_to_multiple_html(false, _export_options);
}
}
}
}
else if (export_type == CtExporting::SELECTED_TEXT)
{
if (!_is_there_text_selection_or_error()) return;
Gtk::TextIter iter_start, iter_end;
_curr_buffer()->get_selection_bounds(iter_start, iter_end);
else if (export_type == CtExporting::ALL_TREE) {
fs::path folder_name = _pCtMainWin->get_ct_storage()->get_file_name();
if (export2html.prepare_html_folder(auto_path, folder_name, auto_overwrite, ret_html_path)) {
if (_export_options.single_file) {
export2html.nodes_all_export_to_single_html(true, _export_options);
}
else {
export2html.nodes_all_export_to_multiple_html(true, _export_options);
}
}
}
else if (export_type == CtExporting::SELECTED_TEXT) {
if (not _is_there_text_selection_or_error()) return;
Gtk::TextIter iter_start, iter_end;
_curr_buffer()->get_selection_bounds(iter_start, iter_end);

std::string folder_name = CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter());
if (export2html.prepare_html_folder("", folder_name, false, ret_html_path))
export2html.node_export_to_html(_pCtMainWin->curr_tree_iter(), _export_options, "", iter_start.get_offset(), iter_end.get_offset());
std::string folder_name = CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter());
if (export2html.prepare_html_folder("", folder_name, false, ret_html_path)) {
export2html.node_export_to_html(_pCtMainWin->curr_tree_iter(), _export_options, "", iter_start.get_offset(), iter_end.get_offset());
}
}
if (not ret_html_path.empty() and auto_path.empty()) {
fs::open_folderpath(ret_html_path, _pCtConfig);
}
}
if (!ret_html_path.empty() && auto_path.empty()) {
fs::open_folderpath(ret_html_path, _pCtConfig);
catch (std::exception& e) {
spdlog::error(e.what());
CtDialogs::error_dialog(e.what(), *_pCtMainWin);
}
}

// Export To Plain Text Multiple (or single) Files
void CtActions::_export_to_txt(const fs::path& auto_path, bool auto_overwrite)
{
CtExporting export_type;
if (!auto_path.empty())
{
if (not auto_path.empty()) {
_export_options.include_node_name = true;
export_type = CtExporting::ALL_TREE;
}
else
{
if (!_is_there_selected_node_or_error()) return;
else {
if (not _is_there_selected_node_or_error()) return;
export_type = CtDialogs::selnode_selnodeandsub_alltree_dialog(*_pCtMainWin, true, &_export_options.include_node_name, nullptr, nullptr, &_export_options.single_file);
}
if (export_type == CtExporting::NONESAVE) return;

if (export_type == CtExporting::CURRENT_NODE)
{
fs::path txt_filepath = CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter());
txt_filepath = _get_txt_filepath("", txt_filepath);
if (txt_filepath.empty()) return;
CtExport2Txt(_pCtMainWin).node_export_to_txt(_pCtMainWin->curr_tree_iter(), txt_filepath, _export_options, -1, -1);
}
else if (export_type == CtExporting::CURRENT_NODE_AND_SUBNODES)
{
if (_export_options.single_file)
{
fs::path txt_filepath = _get_txt_filepath("", _pCtMainWin->get_ct_storage()->get_file_name());
if (txt_filepath.empty()) return;
CtExport2Txt(_pCtMainWin).nodes_all_export_to_txt(false, "", txt_filepath, _export_options);
try {
if (export_type == CtExporting::CURRENT_NODE) {
fs::path txt_filepath = CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter());
txt_filepath = _get_txt_filepath("", txt_filepath);
if (txt_filepath.empty()) return;
CtExport2Txt{_pCtMainWin}.node_export_to_txt(_pCtMainWin->curr_tree_iter(), txt_filepath, _export_options, -1, -1);
}
else
{
fs::path folder_path = _get_txt_folder("", CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter()), false);
if (folder_path.empty()) return;
CtExport2Txt(_pCtMainWin).nodes_all_export_to_txt(false, folder_path, "", _export_options);
else if (export_type == CtExporting::CURRENT_NODE_AND_SUBNODES) {
if (_export_options.single_file) {
fs::path txt_filepath = _get_txt_filepath("", _pCtMainWin->get_ct_storage()->get_file_name());
if (txt_filepath.empty()) return;
CtExport2Txt{_pCtMainWin}.nodes_all_export_to_txt(false, "", txt_filepath, _export_options);
}
else {
fs::path folder_path = _get_txt_folder("", CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter()), false);
if (folder_path.empty()) return;
CtExport2Txt{_pCtMainWin}.nodes_all_export_to_txt(false, folder_path, "", _export_options);
}
}
}
else if (export_type == CtExporting::ALL_TREE)
{
if (_export_options.single_file)
{
fs::path txt_filepath = _get_txt_filepath(auto_path, _pCtMainWin->get_ct_storage()->get_file_name());
if (txt_filepath.empty()) return;
CtExport2Txt(_pCtMainWin).nodes_all_export_to_txt(true, "", txt_filepath, _export_options);
else if (export_type == CtExporting::ALL_TREE) {
if (_export_options.single_file) {
fs::path txt_filepath = _get_txt_filepath(auto_path, _pCtMainWin->get_ct_storage()->get_file_name());
if (txt_filepath.empty()) return;
CtExport2Txt{_pCtMainWin}.nodes_all_export_to_txt(true, "", txt_filepath, _export_options);
}
else {
auto folder_path = _get_txt_folder(auto_path, _pCtMainWin->get_ct_storage()->get_file_name(), auto_overwrite);
if (folder_path.empty()) return;
CtExport2Txt{_pCtMainWin}.nodes_all_export_to_txt(true, folder_path, "", _export_options);
}
}
else
{
auto folder_path = _get_txt_folder(auto_path, _pCtMainWin->get_ct_storage()->get_file_name(), auto_overwrite);
if (folder_path.empty()) return;
CtExport2Txt(_pCtMainWin).nodes_all_export_to_txt(true, folder_path, "", _export_options);
else if (export_type == CtExporting::SELECTED_TEXT) {
if (not _is_there_text_selection_or_error()) return;
Gtk::TextIter iter_start, iter_end;
_curr_buffer()->get_selection_bounds(iter_start, iter_end);

fs::path txt_filepath = CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter());
txt_filepath = _get_txt_filepath("", txt_filepath);
if (txt_filepath.empty()) return;
CtExport2Txt{_pCtMainWin}.node_export_to_txt(_pCtMainWin->curr_tree_iter(), txt_filepath, _export_options, iter_start.get_offset(), iter_end.get_offset());
}
}
else if (export_type == CtExporting::SELECTED_TEXT)
{
if (!_is_there_text_selection_or_error()) return;
Gtk::TextIter iter_start, iter_end;
_curr_buffer()->get_selection_bounds(iter_start, iter_end);

fs::path txt_filepath = CtMiscUtil::get_node_hierarchical_name(_pCtMainWin->curr_tree_iter());
txt_filepath = _get_txt_filepath("", txt_filepath);
if (txt_filepath.empty()) return;
CtExport2Txt(_pCtMainWin).node_export_to_txt(_pCtMainWin->curr_tree_iter(), txt_filepath, _export_options, iter_start.get_offset(), iter_end.get_offset());
catch (std::exception& e) {
spdlog::error(e.what());
CtDialogs::error_dialog(e.what(), *_pCtMainWin);
}
}

Expand All @@ -378,7 +379,7 @@ fs::path CtActions::_get_pdf_filepath(const fs::path& proposed_name)
args.filter_pattern = {"*.pdf"};

fs::path filename = CtDialogs::file_save_as_dialog(_pCtMainWin, args);
if (!filename.empty())
if (not filename.empty())
{
if (filename.extension() != ".pdf") filename += ".pdf";
_pCtConfig->pickDirExport = filename.parent_path().string();
Expand All @@ -405,7 +406,7 @@ fs::path CtActions::_get_txt_filepath(const fs::path& dir_place, const fs::path&
filename = dir_place / proposed_name.string();
}

if (!filename.empty())
if (not filename.empty())
{
if (filename.extension() != ".txt") filename += ".txt";
_pCtConfig->pickDirExport = filename.parent_path().string();
Expand Down
Loading

0 comments on commit cedfdb6

Please sign in to comment.