From d55949d9c1ad64c34be7732d3b2632100f997be8 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Fri, 1 Feb 2013 07:28:57 -0800 Subject: [PATCH] Only perform charset detection once when opening subtitles Relying on TextFileReader to do the charset detection results in the user being prompted to pick a charset twice when it can't be auto-detected, since the result from trying to open the subtitles as timecodes was not being reused. Closes #1512. --- aegisub/src/subs_controller.cpp | 31 ++++++++++++++++++++----------- aegisub/src/subs_controller.h | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/aegisub/src/subs_controller.cpp b/aegisub/src/subs_controller.cpp index f4fc5c724..b835437da 100644 --- a/aegisub/src/subs_controller.cpp +++ b/aegisub/src/subs_controller.cpp @@ -21,6 +21,7 @@ #include "ass_dialogue.h" #include "ass_file.h" #include "ass_style.h" +#include "charset_detect.h" #include "compat.h" #include "command/command.h" #include "include/aegisub/context.h" @@ -81,21 +82,29 @@ SubsController::SubsController(agi::Context *context) }); } -void SubsController::Load(agi::fs::path const& filename, std::string const& charset) { +void SubsController::Load(agi::fs::path const& filename, std::string charset) { if (TryToClose() == wxCANCEL) return; + // TextFileReader does this automatically, but relying on that results in + // the user being prompted twice if it can't be auto-detected, since we + // open the file twice below. + if (charset.empty()) + charset = CharSetDetect::GetEncoding(filename); + // Make sure that file isn't actually a timecode file - try { - TextFileReader testSubs(filename, charset); - std::string cur = testSubs.ReadLineFromFile(); - if (boost::starts_with(cur, "# timecode")) { - context->videoController->LoadTimecodes(filename); - return; + if (charset != "binary") { + try { + TextFileReader testSubs(filename, charset); + std::string cur = testSubs.ReadLineFromFile(); + if (boost::starts_with(cur, "# timecode")) { + context->videoController->LoadTimecodes(filename); + return; + } + } + catch (...) { + // if trying to load the file as timecodes fails it's fairly + // safe to assume that it is in fact not a timecode file } - } - catch (...) { - // if trying to load the file as timecodes fails it's fairly - // safe to assume that it is in fact not a timecode file } const SubtitleFormat *reader = SubtitleFormat::GetReader(filename); diff --git a/aegisub/src/subs_controller.h b/aegisub/src/subs_controller.h index 22b820171..04e1e6c45 100644 --- a/aegisub/src/subs_controller.h +++ b/aegisub/src/subs_controller.h @@ -74,7 +74,7 @@ public: /// @brief Load from a file /// @param file File name /// @param charset Character set of file or empty to autodetect - void Load(agi::fs::path const& file, std::string const& charset=""); + void Load(agi::fs::path const& file, std::string charset=""); /// @brief Save to a file /// @param file Path to save to