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.
This commit is contained in:
Thomas Goyne 2013-02-01 07:28:57 -08:00
parent 47c36c9033
commit d55949d9c1
2 changed files with 21 additions and 12 deletions

View file

@ -21,6 +21,7 @@
#include "ass_dialogue.h" #include "ass_dialogue.h"
#include "ass_file.h" #include "ass_file.h"
#include "ass_style.h" #include "ass_style.h"
#include "charset_detect.h"
#include "compat.h" #include "compat.h"
#include "command/command.h" #include "command/command.h"
#include "include/aegisub/context.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; 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 // Make sure that file isn't actually a timecode file
try { if (charset != "binary") {
TextFileReader testSubs(filename, charset); try {
std::string cur = testSubs.ReadLineFromFile(); TextFileReader testSubs(filename, charset);
if (boost::starts_with(cur, "# timecode")) { std::string cur = testSubs.ReadLineFromFile();
context->videoController->LoadTimecodes(filename); if (boost::starts_with(cur, "# timecode")) {
return; 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); const SubtitleFormat *reader = SubtitleFormat::GetReader(filename);

View file

@ -74,7 +74,7 @@ public:
/// @brief Load from a file /// @brief Load from a file
/// @param file File name /// @param file File name
/// @param charset Character set of file or empty to autodetect /// @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 /// @brief Save to a file
/// @param file Path to save to /// @param file Path to save to