diff --git a/src/applib/CMakeLists.txt b/src/applib/CMakeLists.txt index ba8b558..86297ff 100644 --- a/src/applib/CMakeLists.txt +++ b/src/applib/CMakeLists.txt @@ -38,6 +38,8 @@ target_sources(applib PRIVATE smartctl_executor.cpp smartctl_executor_gui.h smartctl_executor.h + smartctl_output_export.cpp + smartctl_output_export.h smartctl_parser_types.h smartctl_text_ata_parser.cpp smartctl_text_ata_parser.h diff --git a/src/applib/smartctl_output_export.cpp b/src/applib/smartctl_output_export.cpp new file mode 100644 index 0000000..9d4c2f2 --- /dev/null +++ b/src/applib/smartctl_output_export.cpp @@ -0,0 +1,116 @@ +/****************************************************************************** +License: GNU General Public License v3.0 only +Copyright: + (C) 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup applib +/// \weakgroup applib +/// @{ + +#include "smartctl_output_export.h" + +#include + +#include "nlohmann/json.hpp" +#include "hz/debug.h" +#include "hz/string_algo.h" // string_join +#include "smartctl_parser.h" + + + +bool SmartctlOutputVariants::has(SmartctlOutputFormat format) const +{ + return (format == SmartctlOutputFormat::Json ? json : text).has_value(); +} + + + +const std::string& SmartctlOutputVariants::get(SmartctlOutputFormat format) const +{ + return (format == SmartctlOutputFormat::Json ? json : text).value(); +} + + + +SmartctlOutputVariants smartctl_output_get_variants(std::string_view smartctl_output) +{ + SmartctlOutputVariants variants; + + const auto format = SmartctlParser::detect_output_format(smartctl_output); + if (!format.has_value() || format.value() != SmartctlOutputFormat::Json) { + // Text output, or something unknown (e.g. an error message), which is text as well. + variants.text = std::string(smartctl_output); + return variants; + } + + variants.json = std::string(smartctl_output); + + try { + const nlohmann::json root_node = nlohmann::json::parse(smartctl_output); + const auto smartctl_iter = root_node.find("smartctl"); + if (smartctl_iter != root_node.end() && smartctl_iter->is_object()) { + const auto output_iter = smartctl_iter->find("output"); + if (output_iter != smartctl_iter->end() && output_iter->is_array() && !output_iter->empty()) { + std::vector lines; + for (const auto& entry : *output_iter) { + if (entry.is_string()) { + lines.emplace_back(entry.get()); + } + } + variants.text = hz::string_join(lines, "\n") + "\n"; + } + } + } catch (const nlohmann::json::exception& e) { + debug_out_warn("app", DBG_FUNC_MSG << "Cannot extract text output from JSON: " << e.what() << "\n"); + } + + return variants; +} + + + +std::string smartctl_output_get_file_extension(SmartctlOutputFormat format) +{ + return format == SmartctlOutputFormat::Json ? ".json" : ".txt"; +} + + + +hz::fs::path smartctl_output_set_file_extension(hz::fs::path file, SmartctlOutputFormat format) +{ + const std::string ext = smartctl_output_get_file_extension(format); + if (file.extension() == ".json" || file.extension() == ".txt") { + file.replace_extension(ext); + } else { + file += ext; + } + return file; +} + + + +std::pair smartctl_output_resolve_save_target(hz::fs::path file, + std::optional filter_format, SmartctlOutputFormat default_format, + const SmartctlOutputVariants& variants) +{ + SmartctlOutputFormat format = default_format; + if (filter_format.has_value()) { + format = filter_format.value(); + } else if (file.extension() == ".txt") { + format = SmartctlOutputFormat::Text; + } else if (file.extension() == ".json") { + format = SmartctlOutputFormat::Json; + } + + if (!variants.has(format)) { + format = (format == SmartctlOutputFormat::Json ? SmartctlOutputFormat::Text : SmartctlOutputFormat::Json); + } + + return {smartctl_output_set_file_extension(std::move(file), format), format}; +} + + + +/// @} diff --git a/src/applib/smartctl_output_export.h b/src/applib/smartctl_output_export.h new file mode 100644 index 0000000..be43953 --- /dev/null +++ b/src/applib/smartctl_output_export.h @@ -0,0 +1,71 @@ +/****************************************************************************** +License: GNU General Public License v3.0 only +Copyright: + (C) 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup applib +/// \weakgroup applib +/// @{ + +#ifndef SMARTCTL_OUTPUT_EXPORT_H +#define SMARTCTL_OUTPUT_EXPORT_H + +#include +#include +#include +#include + +#include "hz/fs.h" +#include "smartctl_parser_types.h" + + + +/// Smartctl output, available in JSON and / or text formats. +struct SmartctlOutputVariants { + std::optional json; ///< JSON output, if available + std::optional text; ///< Text (non-JSON) output, if available + + /// Check if the output is available in the specified format + [[nodiscard]] bool has(SmartctlOutputFormat format) const; + + /// Get the output in the specified format. The format must be available. + [[nodiscard]] const std::string& get(SmartctlOutputFormat format) const; +}; + + + +/// Get smartctl output in all the available formats. +/// JSON output contains the text output only if it was obtained with "--json=o" (which we always use); +/// text output is available as is, and has no JSON version. +[[nodiscard]] SmartctlOutputVariants smartctl_output_get_variants(std::string_view smartctl_output); + + + +/// Get the default file extension (including the dot) for the format. +[[nodiscard]] std::string smartctl_output_get_file_extension(SmartctlOutputFormat format); + + + +/// Replace the file name extension with the one corresponding to the format +/// if it's ".json" or ".txt", or append it otherwise. +[[nodiscard]] hz::fs::path smartctl_output_set_file_extension(hz::fs::path file, SmartctlOutputFormat format); + + + +/// Decide which format to save the smartctl output in, and adjust the file name extension accordingly. +/// \param file File name chosen by the user +/// \param filter_format Format of the file filter selected by the user, if the filter is format-specific +/// \param default_format Format to use if neither the filter nor the extension determine it +/// \param variants Available output formats. At least one must be available. +/// \return The file name with the correct extension, and the format to save in. +[[nodiscard]] std::pair smartctl_output_resolve_save_target(hz::fs::path file, + std::optional filter_format, SmartctlOutputFormat default_format, + const SmartctlOutputVariants& variants); + + + +#endif + +/// @} diff --git a/src/applib/tests/CMakeLists.txt b/src/applib/tests/CMakeLists.txt index 69ac7a5..87fb055 100644 --- a/src/applib/tests/CMakeLists.txt +++ b/src/applib/tests/CMakeLists.txt @@ -17,6 +17,7 @@ target_sources(applib_tests PRIVATE test_app_regex.cpp test_fractional_scaling.cpp test_selftest.cpp + test_smartctl_output_export.cpp test_smartctl_parser.cpp test_smartctl_version_parser.cpp ) diff --git a/src/applib/tests/test_smartctl_output_export.cpp b/src/applib/tests/test_smartctl_output_export.cpp new file mode 100644 index 0000000..9725223 --- /dev/null +++ b/src/applib/tests/test_smartctl_output_export.cpp @@ -0,0 +1,148 @@ +/****************************************************************************** +License: BSD Zero Clause License +Copyright: + (C) 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup applib_tests +/// \weakgroup applib_tests +/// @{ + +#include "catch2/catch.hpp" + +#include "applib/smartctl_output_export.h" +#include + + + +namespace { + + const std::string json_with_text = R"json({ + "json_format_version": [1, 0], + "smartctl": { + "version": [7, 4], + "output": [ + "smartctl 7.4 2023-08-01 r5530 [x86_64-linux] (local build)", + "", + "=== START OF INFORMATION SECTION ===" + ] + } +})json"; + + const std::string json_without_text = R"json({ + "json_format_version": [1, 0], + "smartctl": { + "version": [7, 4] + } +})json"; + + const std::string text_output = "smartctl 7.4 2023-08-01 r5530 [x86_64-linux] (local build)\n" + "\n=== START OF INFORMATION SECTION ===\n"; + +} + + + +TEST_CASE("SmartctlOutputVariants", "[app][output_export]") +{ + SECTION("JSON with embedded text output") + { + const auto variants = smartctl_output_get_variants(json_with_text); + REQUIRE(variants.has(SmartctlOutputFormat::Json)); + REQUIRE(variants.has(SmartctlOutputFormat::Text)); + REQUIRE(variants.get(SmartctlOutputFormat::Json) == json_with_text); + REQUIRE(variants.get(SmartctlOutputFormat::Text) == text_output); + } + + SECTION("JSON without embedded text output") + { + const auto variants = smartctl_output_get_variants(json_without_text); + REQUIRE(variants.has(SmartctlOutputFormat::Json)); + REQUIRE_FALSE(variants.has(SmartctlOutputFormat::Text)); + } + + SECTION("Invalid JSON") + { + const auto variants = smartctl_output_get_variants("{ \"smartctl\": "); + REQUIRE(variants.has(SmartctlOutputFormat::Json)); + REQUIRE_FALSE(variants.has(SmartctlOutputFormat::Text)); + } + + SECTION("Text output") + { + const auto variants = smartctl_output_get_variants(text_output); + REQUIRE_FALSE(variants.has(SmartctlOutputFormat::Json)); + REQUIRE(variants.has(SmartctlOutputFormat::Text)); + REQUIRE(variants.get(SmartctlOutputFormat::Text) == text_output); + } + + SECTION("Unknown output is treated as text") + { + const auto variants = smartctl_output_get_variants("Some error message"); + REQUIRE_FALSE(variants.has(SmartctlOutputFormat::Json)); + REQUIRE(variants.get(SmartctlOutputFormat::Text) == "Some error message"); + } +} + + + +TEST_CASE("SmartctlOutputFileExtension", "[app][output_export]") +{ + using F = SmartctlOutputFormat; + REQUIRE(smartctl_output_set_file_extension("a.json", F::Text) == hz::fs::path("a.txt")); + REQUIRE(smartctl_output_set_file_extension("a.txt", F::Json) == hz::fs::path("a.json")); + REQUIRE(smartctl_output_set_file_extension("a.json", F::Json) == hz::fs::path("a.json")); + REQUIRE(smartctl_output_set_file_extension("a", F::Text) == hz::fs::path("a.txt")); + REQUIRE(smartctl_output_set_file_extension("a.log", F::Text) == hz::fs::path("a.log.txt")); + REQUIRE(smartctl_output_set_file_extension("dir.json/a", F::Json) == hz::fs::path("dir.json/a.json")); +} + + + +TEST_CASE("SmartctlOutputSaveTarget", "[app][output_export]") +{ + using F = SmartctlOutputFormat; + const auto both = smartctl_output_get_variants(json_with_text); + const auto json_only = smartctl_output_get_variants(json_without_text); + const auto text_only = smartctl_output_get_variants(text_output); + + SECTION("Format-specific filter overrides the extension") + { + // The default file name has .json extension; selecting the Text filter must save text. + REQUIRE(smartctl_output_resolve_save_target("a.json", F::Text, F::Json, both) + == std::pair(hz::fs::path("a.txt"), F::Text)); + REQUIRE(smartctl_output_resolve_save_target("a.txt", F::Json, F::Json, both) + == std::pair(hz::fs::path("a.json"), F::Json)); + REQUIRE(smartctl_output_resolve_save_target("a", F::Text, F::Json, both) + == std::pair(hz::fs::path("a.txt"), F::Text)); + } + + SECTION("Without a format-specific filter, the extension decides") + { + REQUIRE(smartctl_output_resolve_save_target("a.txt", std::nullopt, F::Json, both) + == std::pair(hz::fs::path("a.txt"), F::Text)); + REQUIRE(smartctl_output_resolve_save_target("a.json", std::nullopt, F::Text, both) + == std::pair(hz::fs::path("a.json"), F::Json)); + } + + SECTION("Without a format-specific filter or a known extension, the default format is used") + { + REQUIRE(smartctl_output_resolve_save_target("a", std::nullopt, F::Json, both) + == std::pair(hz::fs::path("a.json"), F::Json)); + REQUIRE(smartctl_output_resolve_save_target("a", std::nullopt, F::Text, both) + == std::pair(hz::fs::path("a.txt"), F::Text)); + } + + SECTION("Unavailable format falls back to the available one") + { + REQUIRE(smartctl_output_resolve_save_target("a.json", std::nullopt, F::Json, text_only) + == std::pair(hz::fs::path("a.txt"), F::Text)); + REQUIRE(smartctl_output_resolve_save_target("a.txt", F::Text, F::Text, json_only) + == std::pair(hz::fs::path("a.json"), F::Json)); + } +} + + + +/// @} diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 63aa790..250b534 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -27,6 +27,8 @@ target_sources(gsmartcontrol PRIVATE gsc_main_window_iconview.h gsc_preferences_window.cpp gsc_preferences_window.h + gsc_save_output_dialog.cpp + gsc_save_output_dialog.h gsc_startup_settings.h gsc_text_window.h ) diff --git a/src/gui/gsc_executor_error_dialog.cpp b/src/gui/gsc_executor_error_dialog.cpp index 21f9d04..b4eaf90 100644 --- a/src/gui/gsc_executor_error_dialog.cpp +++ b/src/gui/gsc_executor_error_dialog.cpp @@ -93,7 +93,7 @@ void gsc_no_info_dialog_show(const std::string& message, const std::string& sec_ if (response == Gtk::RESPONSE_HELP) { auto win = GscTextWindow::create(); - win->set_text_from_command(output_window_title, output); + win->set_smartctl_output(output_window_title, output); if (!default_save_filename.empty()) win->set_save_filename(default_save_filename); diff --git a/src/gui/gsc_info_window.cpp b/src/gui/gsc_info_window.cpp index 485d907..4a517bf 100644 --- a/src/gui/gsc_info_window.cpp +++ b/src/gui/gsc_info_window.cpp @@ -34,6 +34,7 @@ Copyright: #include "gsc_text_window.h" #include "gsc_info_window.h" #include "gsc_executor_error_dialog.h" +#include "gsc_save_output_dialog.h" #include "gsc_startup_settings.h" @@ -891,7 +892,7 @@ void GscInfoWindow::on_view_output_button_clicked() output = this->drive_->get_basic_output(); } - win->set_text_from_command(_("Smartctl Output"), output); + win->set_smartctl_output(_("Smartctl Output"), output); const std::string filename = drive_->get_save_filename(); if (!filename.empty()) @@ -904,126 +905,11 @@ void GscInfoWindow::on_view_output_button_clicked() void GscInfoWindow::on_save_info_button_clicked() { - static std::string last_dir; - if (last_dir.empty()) { - last_dir = rconfig::get_data("gui/drive_data_open_save_dir"); - } - int result = 0; - - const std::string filename = drive_->get_save_filename(); - - Glib::RefPtr specific_filter = Gtk::FileFilter::create(); - specific_filter->set_name(_("JSON and Text Files")); - specific_filter->add_pattern("*.json"); - specific_filter->add_pattern("*.txt"); - - Glib::RefPtr json_filter = Gtk::FileFilter::create(); - json_filter->set_name(_("JSON Files")); - json_filter->add_pattern("*.json"); - - Glib::RefPtr txt_filter = Gtk::FileFilter::create(); - txt_filter->set_name(_("Text Files")); - txt_filter->add_pattern("*.txt"); - - Glib::RefPtr all_filter = Gtk::FileFilter::create(); - all_filter->set_name(_("All Files")); - all_filter->add_pattern("*"); - -#if GTK_CHECK_VERSION(3, 20, 0) - std::unique_ptr dialog(gtk_file_chooser_native_new( - _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), - &g_object_unref); - - gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog.get()), TRUE); - - gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog.get()), specific_filter->gobj()); - gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog.get()), json_filter->gobj()); - gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog.get()), txt_filter->gobj()); - gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog.get()), all_filter->gobj()); - - if (!last_dir.empty()) - gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog.get()), last_dir.c_str()); - - if (!filename.empty()) - gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog.get()), filename.c_str()); - - result = gtk_native_dialog_run(GTK_NATIVE_DIALOG(dialog.get())); - -#else - Gtk::FileChooserDialog dialog(*this, _("Save Data As..."), - Gtk::FILE_CHOOSER_ACTION_SAVE); - - // Add response buttons the the dialog - dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); - dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT); - - dialog.set_do_overwrite_confirmation(true); - - dialog.add_filter(specific_filter); - dialog.add_filter(json_filter); - dialog.add_filter(txt_filter); - dialog.add_filter(all_filter); - - if (!last_dir.empty()) - dialog.set_current_folder(last_dir); - - if (!filename.empty()) - dialog.set_current_name(filename); - - // Show the dialog and wait for a user response - result = dialog.run(); // the main cycle blocks here -#endif - - // Handle the response - switch (result) { - case Gtk::RESPONSE_ACCEPT: - { - hz::fs::path file; -#if GTK_CHECK_VERSION(3, 20, 0) - file = hz::fs_path_from_string(app_string_from_gchar(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog.get())))); - last_dir = hz::fs_path_to_string(file.parent_path()); -#else - file = hz::fs_path_from_string(dialog.get_filename()); // in fs encoding - last_dir = dialog.get_current_folder(); // save for the future -#endif - rconfig::set_data("gui/drive_data_open_save_dir", last_dir); - - bool txt_selected = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog.get())) == txt_filter->gobj(); - - if (file.extension() != ".json" && file.extension() != ".txt") { - file += (txt_selected ? ".txt" : ".json"); - } - - bool save_txt = txt_selected || file.extension() == ".txt"; - - std::string data = this->drive_->get_full_output(); - if (data.empty()) { - data = this->drive_->get_basic_output(); - } - if (save_txt) { - if (auto p = this->drive_->get_property_repository().lookup_property("smartctl/output"); !p.empty()) { - const std::string text_output = p.get_value(); - if (!text_output.empty()) { - data = text_output; - } - } - } - - const std::error_code ec = hz::fs_file_put_contents(file, data); - if (ec) { - gui_show_error_dialog(_("Cannot save SMART data to file"), ec.message(), this); - } - break; - } - - case Gtk::RESPONSE_CANCEL: case Gtk::RESPONSE_DELETE_EVENT: - // nothing, the dialog is closed already - break; - - default: - debug_out_error("app", DBG_FUNC_MSG << "Unknown dialog response code: " << result << ".\n"); - break; + std::string output = this->drive_->get_full_output(); + if (output.empty()) { + output = this->drive_->get_basic_output(); } + gsc_show_save_smartctl_output_dialog(this, output, drive_->get_save_filename(), SmartctlOutputFormat::Json); } diff --git a/src/gui/gsc_save_output_dialog.cpp b/src/gui/gsc_save_output_dialog.cpp new file mode 100644 index 0000000..907eb43 --- /dev/null +++ b/src/gui/gsc_save_output_dialog.cpp @@ -0,0 +1,199 @@ +/****************************************************************************** +License: GNU General Public License v3.0 only +Copyright: + (C) 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup gsc +/// \weakgroup gsc +/// @{ + +#include +#include +#include +#include +#include + +#include "hz/debug.h" +#include "hz/fs.h" +#include "rconfig/rconfig.h" + +#include "applib/app_gtkmm_tools.h" // app_string_from_gchar +#include "applib/gui_utils.h" // gui_show_error_dialog +#include "applib/smartctl_output_export.h" + +#include "gsc_save_output_dialog.h" + + + +void gsc_show_save_smartctl_output_dialog(Gtk::Window* parent, const std::string& smartctl_output, + const std::string& default_filename, SmartctlOutputFormat preferred_format) +{ + static std::string last_dir; + if (last_dir.empty()) { + last_dir = rconfig::get_data("gui/drive_data_open_save_dir"); + } + + const SmartctlOutputVariants variants = smartctl_output_get_variants(smartctl_output); + const bool has_json = variants.has(SmartctlOutputFormat::Json); + const bool has_text = variants.has(SmartctlOutputFormat::Text); + if (!variants.has(preferred_format)) { + preferred_format = has_json ? SmartctlOutputFormat::Json : SmartctlOutputFormat::Text; + } + + std::string filename; + if (!default_filename.empty()) { + filename = hz::fs_path_to_string(smartctl_output_set_file_extension( + hz::fs_path_from_string(default_filename), preferred_format)); + } + + // The combined filter lets the user choose the format by typing the extension. + Glib::RefPtr specific_filter; + if (has_json && has_text) { + specific_filter = Gtk::FileFilter::create(); + specific_filter->set_name(_("JSON and Text Files")); + specific_filter->add_pattern("*.json"); + specific_filter->add_pattern("*.txt"); + } + + Glib::RefPtr json_filter; + if (has_json) { + json_filter = Gtk::FileFilter::create(); + json_filter->set_name(_("JSON Files")); + json_filter->add_pattern("*.json"); + } + + Glib::RefPtr txt_filter; + if (has_text) { + txt_filter = Gtk::FileFilter::create(); + txt_filter->set_name(_("Text Files")); + txt_filter->add_pattern("*.txt"); + } + + Glib::RefPtr all_filter = Gtk::FileFilter::create(); + all_filter->set_name(_("All Files")); + all_filter->add_pattern("*"); + + int result = 0; + hz::fs::path file; + std::optional filter_format; + + // Get the format of the selected filter, if it's format-specific + auto get_filter_format = [&](GtkFileFilter* selected) -> std::optional { + if (selected != nullptr) { + if (json_filter && selected == json_filter->gobj()) { + return SmartctlOutputFormat::Json; + } + if (txt_filter && selected == txt_filter->gobj()) { + return SmartctlOutputFormat::Text; + } + } + return std::nullopt; + }; + +#if GTK_CHECK_VERSION(3, 20, 0) + std::unique_ptr dialog(gtk_file_chooser_native_new( + _("Save Data As..."), (parent ? parent->gobj() : nullptr), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), + &g_object_unref); + auto* chooser = GTK_FILE_CHOOSER(dialog.get()); + + gtk_file_chooser_set_do_overwrite_confirmation(chooser, TRUE); + + for (const auto& filter : {specific_filter, json_filter, txt_filter, all_filter}) { + if (filter) { + gtk_file_chooser_add_filter(chooser, filter->gobj()); + } + } + + if (!last_dir.empty()) + gtk_file_chooser_set_current_folder(chooser, last_dir.c_str()); + + if (!filename.empty()) + gtk_file_chooser_set_current_name(chooser, filename.c_str()); + + result = gtk_native_dialog_run(GTK_NATIVE_DIALOG(dialog.get())); + + if (result == Gtk::RESPONSE_ACCEPT) { + file = hz::fs_path_from_string(app_string_from_gchar(gtk_file_chooser_get_filename(chooser))); + last_dir = hz::fs_path_to_string(file.parent_path()); + filter_format = get_filter_format(gtk_file_chooser_get_filter(chooser)); + } + +#else + Gtk::FileChooserDialog dialog(_("Save Data As..."), Gtk::FILE_CHOOSER_ACTION_SAVE); + if (parent) { + dialog.set_transient_for(*parent); + } + + // Add response buttons the the dialog + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT); + + dialog.set_do_overwrite_confirmation(true); + + for (const auto& filter : {specific_filter, json_filter, txt_filter, all_filter}) { + if (filter) { + dialog.add_filter(filter); + } + } + + if (!last_dir.empty()) + dialog.set_current_folder(last_dir); + + if (!filename.empty()) + dialog.set_current_name(filename); + + // Show the dialog and wait for a user response + result = dialog.run(); // the main cycle blocks here + + if (result == Gtk::RESPONSE_ACCEPT) { + file = hz::fs_path_from_string(dialog.get_filename()); // in fs encoding + last_dir = dialog.get_current_folder(); // save for the future + filter_format = get_filter_format(dialog.get_filter() ? dialog.get_filter()->gobj() : nullptr); + } +#endif + + // Handle the response + switch (result) { + case Gtk::RESPONSE_ACCEPT: + { + rconfig::set_data("gui/drive_data_open_save_dir", last_dir); + + const auto [target_file, format] = smartctl_output_resolve_save_target( + file, filter_format, preferred_format, variants); + + // The dialog asked about overwriting the chosen file only, not the one with the adjusted extension. + std::error_code exists_ec; + if (target_file != file && hz::fs::exists(target_file, exists_ec)) { + Gtk::MessageDialog confirm_dialog( + Glib::ustring::compose(_("File \"%1\" already exists. Overwrite it?"), hz::fs_path_to_string(target_file.filename())), + false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO, true); + if (parent) { + confirm_dialog.set_transient_for(*parent); + } + if (confirm_dialog.run() != Gtk::RESPONSE_YES) { + break; + } + } + + const std::error_code ec = hz::fs_file_put_contents(target_file, variants.get(format)); + if (ec) { + gui_show_error_dialog(_("Cannot save SMART data to file"), ec.message(), parent); + } + break; + } + + case Gtk::RESPONSE_CANCEL: case Gtk::RESPONSE_DELETE_EVENT: + // nothing, the dialog is closed already + break; + + default: + debug_out_error("app", DBG_FUNC_MSG << "Unknown dialog response code: " << result << ".\n"); + break; + } +} + + + +/// @} diff --git a/src/gui/gsc_save_output_dialog.h b/src/gui/gsc_save_output_dialog.h new file mode 100644 index 0000000..bf92cd1 --- /dev/null +++ b/src/gui/gsc_save_output_dialog.h @@ -0,0 +1,36 @@ +/****************************************************************************** +License: GNU General Public License v3.0 only +Copyright: + (C) 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup gsc +/// \weakgroup gsc +/// @{ + +#ifndef GSC_SAVE_OUTPUT_DIALOG_H +#define GSC_SAVE_OUTPUT_DIALOG_H + +#include +#include + +#include "applib/smartctl_parser_types.h" + + + +/// Show a "Save As" dialog for smartctl output, and save it in the format chosen by the user +/// (JSON or text, depending on availability). The format is chosen by the selected file type, +/// or by the file extension if the file type is not format-specific. +/// \param parent Parent window +/// \param smartctl_output Smartctl output (JSON or text) +/// \param default_filename Suggested file name. Its extension is adjusted to match \c preferred_format. +/// \param preferred_format Format to suggest, if available +void gsc_show_save_smartctl_output_dialog(Gtk::Window* parent, const std::string& smartctl_output, + const std::string& default_filename, SmartctlOutputFormat preferred_format); + + + +#endif + +/// @} diff --git a/src/gui/gsc_text_window.h b/src/gui/gsc_text_window.h index 4f39bb8..3d1b359 100644 --- a/src/gui/gsc_text_window.h +++ b/src/gui/gsc_text_window.h @@ -16,6 +16,7 @@ Copyright: #include #include // GDK_KEY_Escape #include +#include #include #include "hz/debug.h" @@ -25,6 +26,9 @@ Copyright: #include "applib/app_builder_widget.h" #include "applib/app_gtkmm_tools.h" #include "applib/gui_utils.h" // app_apply_fractional_scaling_to_default_size() +#include "applib/smartctl_output_export.h" + +#include "gsc_save_output_dialog.h" @@ -61,6 +65,9 @@ class GscTextWindow : public AppBuilderWidget, Ins Gtk::Button* close_window_button = nullptr; APP_BUILDER_AUTO_CONNECT(close_window_button, clicked); + Gtk::ComboBoxText* view_format_combo = nullptr; + APP_BUILDER_AUTO_CONNECT(view_format_combo, changed); + // Accelerators @@ -83,6 +90,36 @@ class GscTextWindow : public AppBuilderWidget, Ins } + /// Set smartctl output (JSON or text) to display. + /// If both JSON and text formats are available, the user can choose which one to display, + /// and Save As can save in either format. + void set_smartctl_output(const Glib::ustring& title, const std::string& output) + { + smartctl_output_ = output; + smartctl_output_variants_ = smartctl_output_get_variants(output); + displayed_format_ = smartctl_output_variants_->has(SmartctlOutputFormat::Json) + ? SmartctlOutputFormat::Json : SmartctlOutputFormat::Text; + this->contents_ = smartctl_output_variants_->get(displayed_format_); + this->set_text_helper(title, true, true); + + auto* view_format_combo = this->template lookup_widget("view_format_combo"); + if (view_format_combo) { + const bool has_both = smartctl_output_variants_->has(SmartctlOutputFormat::Json) + && smartctl_output_variants_->has(SmartctlOutputFormat::Text); + populating_format_combo_ = true; + view_format_combo->remove_all(); + if (has_both) { + view_format_combo->append(format_combo_json_id, C_("output_format", "JSON")); + view_format_combo->append(format_combo_text_id, C_("output_format", "Text")); + view_format_combo->set_active_id(displayed_format_ == SmartctlOutputFormat::Json + ? format_combo_json_id : format_combo_text_id); + } + populating_format_combo_ = false; + view_format_combo->set_visible(has_both); + } + } + + void set_text(const Glib::ustring& title, const Glib::ustring& contents, bool save_visible = false, bool use_monospace = false) { @@ -95,6 +132,7 @@ class GscTextWindow : public AppBuilderWidget, Ins void set_text_helper(const Glib::ustring& title, bool save_visible = false, bool use_monospace = false) { + title_ = title; this->set_title(title + " - " + default_title_); // something - gsmartcontrol Gtk::TextView* textview = this->template lookup_widget("main_textview"); @@ -108,8 +146,12 @@ class GscTextWindow : public AppBuilderWidget, Ins } if (use_monospace) { - Glib::RefPtr tag = buffer->create_tag(); - tag->property_family() = "Monospace"; + // Reuse the tag, the text may be set multiple times. + Glib::RefPtr tag = buffer->get_tag_table()->lookup("monospace"); + if (!tag) { + tag = buffer->create_tag("monospace"); + tag->property_family() = "Monospace"; + } buffer->apply_tag(tag, buffer->begin(), buffer->end()); } } @@ -153,9 +195,33 @@ class GscTextWindow : public AppBuilderWidget, Ins // ---------- other callbacks + /// Combo change callback + void on_view_format_combo_changed() + { + auto* view_format_combo = this->template lookup_widget("view_format_combo"); + if (populating_format_combo_ || !view_format_combo || !smartctl_output_variants_) { + return; + } + const SmartctlOutputFormat format = (view_format_combo->get_active_id() == format_combo_text_id) + ? SmartctlOutputFormat::Text : SmartctlOutputFormat::Json; + if (format == displayed_format_ || !smartctl_output_variants_->has(format)) { + return; + } + displayed_format_ = format; + this->contents_ = smartctl_output_variants_->get(format); + this->set_text_helper(title_, true, true); + } + + /// Button click callback void on_save_as_button_clicked() { + // Smartctl output can be saved in JSON or text format + if (smartctl_output_variants_) { + gsc_show_save_smartctl_output_dialog(this, smartctl_output_, save_filename_, displayed_format_); + return; + } + static std::string last_dir; if (last_dir.empty()) { last_dir = rconfig::get_data("gui/drive_data_open_save_dir"); @@ -273,6 +339,16 @@ class GscTextWindow : public AppBuilderWidget, Ins std::string save_filename_; ///< Default filename for Save As + Glib::ustring title_; ///< Title passed to set_text_helper() + + std::string smartctl_output_; ///< Smartctl output, if set using set_smartctl_output() + std::optional smartctl_output_variants_; ///< Available formats of smartctl_output_ + SmartctlOutputFormat displayed_format_ = SmartctlOutputFormat::Json; ///< Currently displayed format of smartctl_output_ + bool populating_format_combo_ = false; ///< Ignore combo changes while filling it + + static inline const Glib::ustring format_combo_json_id = "json"; ///< Format combo ID for JSON + static inline const Glib::ustring format_combo_text_id = "text"; ///< Format combo ID for text + }; diff --git a/src/gui/ui/gsc_text_window.glade b/src/gui/ui/gsc_text_window.glade index 371601e..ee63b94 100644 --- a/src/gui/ui/gsc_text_window.glade +++ b/src/gui/ui/gsc_text_window.glade @@ -99,9 +99,20 @@ along with GSmartControl. If not, see . - + True False + + + False + Output format to display and save + + + True + True + 0 + + True