Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/gui/gsc_info_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,14 @@ void GscInfoWindow::on_view_output_button_clicked()

win->set_text_from_command(_("Smartctl Output"), output);

// Set text content for saving as .txt
if (auto p = this->drive_->get_property_repository().lookup_property("smartctl/output"); !p.empty()) {
const std::string text_output = p.get_value<std::string>();
if (!text_output.empty()) {
win->set_text_contents(text_output);
}
}

const std::string filename = drive_->get_save_filename();
if (!filename.empty())
win->set_save_filename(filename);
Expand Down Expand Up @@ -910,17 +918,18 @@ void GscInfoWindow::on_save_info_button_clicked()
case Gtk::RESPONSE_ACCEPT:
{
hz::fs::path file;
bool txt_selected = false;
#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());
txt_selected = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog.get())) == txt_filter->gobj();
#else
file = hz::fs_path_from_string(dialog.get_filename()); // in fs encoding
last_dir = dialog.get_current_folder(); // save for the future
txt_selected = dialog.get_filter() == txt_filter;
#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");
}
Expand Down
32 changes: 30 additions & 2 deletions src/gui/gsc_text_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ class GscTextWindow : public AppBuilderWidget<GscTextWindow<InstanceSwitch>, Ins
}


/// Set alternative text content for saving as .txt file
void set_text_contents(const std::string& text_contents)
{
text_contents_ = text_contents;
}


protected:


Expand Down Expand Up @@ -163,6 +170,14 @@ class GscTextWindow : public AppBuilderWidget<GscTextWindow<InstanceSwitch>, Ins
specific_filter->add_pattern("*.json");
specific_filter->add_pattern("*.txt");

Glib::RefPtr<Gtk::FileFilter> json_filter = Gtk::FileFilter::create();
json_filter->set_name(_("JSON Files"));
json_filter->add_pattern("*.json");

Glib::RefPtr<Gtk::FileFilter> txt_filter = Gtk::FileFilter::create();
txt_filter->set_name(_("Text Files"));
txt_filter->add_pattern("*.txt");

Glib::RefPtr<Gtk::FileFilter> all_filter = Gtk::FileFilter::create();
all_filter->set_name(_("All Files"));
all_filter->add_pattern("*");
Expand All @@ -175,6 +190,8 @@ class GscTextWindow : public AppBuilderWidget<GscTextWindow<InstanceSwitch>, Ins
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())
Expand All @@ -196,6 +213,8 @@ class GscTextWindow : public AppBuilderWidget<GscTextWindow<InstanceSwitch>, Ins
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())
Expand All @@ -213,21 +232,28 @@ class GscTextWindow : public AppBuilderWidget<GscTextWindow<InstanceSwitch>, Ins
case Gtk::RESPONSE_ACCEPT:
{
hz::fs::path file;
bool txt_selected = false;
#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());
txt_selected = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog.get())) == txt_filter->gobj();
#else
file = hz::fs_path_from_string(dialog.get_filename()); // in fs encoding
last_dir = dialog.get_current_folder(); // save for the future
txt_selected = dialog.get_filter() == txt_filter;
#endif
rconfig::set_data("gui/drive_data_open_save_dir", last_dir);

if (file.extension() != ".json" && file.extension() != ".txt") {
file += ".json";
file += (txt_selected ? ".txt" : ".json");
}

bool save_txt = txt_selected || file.extension() == ".txt";

std::string text;
if (std::holds_alternative<std::string>(contents_)) {
if (save_txt && !text_contents_.empty()) {
text = text_contents_;
} else if (std::holds_alternative<std::string>(contents_)) {
text = std::get<std::string>(contents_);
} else {
text = std::get<Glib::ustring>(contents_);
Expand Down Expand Up @@ -269,6 +295,8 @@ class GscTextWindow : public AppBuilderWidget<GscTextWindow<InstanceSwitch>, Ins

std::string save_filename_; ///< Default filename for Save As

std::string text_contents_; ///< Alternative text content for saving as .txt file

};


Expand Down