Exemple #1
0
void FrameMain::LoadSubtitles(wxString const& filename, wxString const& charset) {
	if (context->ass->loaded) {
		if (TryToCloseSubs() == wxCANCEL) return;
	}

	try {
		// Make sure that file isn't actually a timecode file
		try {
			TextFileReader testSubs(filename, charset);
			wxString cur = testSubs.ReadLineFromFile();
			if (cur.Left(10) == "# 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
		}

		context->ass->Load(filename, charset);

		wxFileName file(filename);
		StandardPaths::SetPathValue("?script", file.GetPath());
		config::mru->Add("Subtitle", STD_STR(filename));
		OPT_SET("Path/Last/Subtitles")->SetString(STD_STR(file.GetPath()));

		// Save backup of file
		if (context->ass->CanSave() && OPT_GET("App/Auto/Backup")->GetBool()) {
			if (file.FileExists()) {
				wxString path = lagi_wxString(OPT_GET("Path/Auto/Backup")->GetString());
				if (path.empty()) path = file.GetPath();
				wxFileName dstpath(StandardPaths::DecodePath(path + "/"));
				if (!dstpath.DirExists())
					wxMkdir(dstpath.GetPath());

				dstpath.SetFullName(file.GetName() + ".ORIGINAL." + file.GetExt());

				wxCopyFile(file.GetFullPath(), dstpath.GetFullPath(), true);
			}
		}
	}
	catch (agi::FileNotFoundError const&) {
		wxMessageBox(filename + " not found.", "Error", wxOK | wxICON_ERROR | wxCENTER, this);
		config::mru->Remove("Subtitle", STD_STR(filename));
		return;
	}
	catch (agi::Exception const& err) {
		wxMessageBox(lagi_wxString(err.GetChainedMessage()), "Error", wxOK | wxICON_ERROR | wxCENTER, this);
	}
	catch (...) {
		wxMessageBox("Unknown error", "Error", wxOK | wxICON_ERROR | wxCENTER, this);
		return;
	}
}
///////////////////////
// Unhandled exception
void AegisubApp::OnUnhandledException() {
	// Attempt to recover file
	wxFileName origfile(AssFile::top->filename);
	wxString path = Options.AsText(_T("Auto recovery path"));
	if (path.IsEmpty()) path = folderName;
	wxFileName dstpath(path);
	if (!dstpath.IsAbsolute()) path = AegisubApp::folderName + path;
	path += _T("/");
	dstpath.Assign(path);
	if (!dstpath.DirExists()) wxMkdir(path);
	wxString filename = folderName + origfile.GetName() + _T(".RECOVER.ass");
	AssFile::top->Save(filename,false,false);

	// Inform user of crash
	wxMessageBox(_T("Aegisub has encountered an unhandled exception error and will terminate now. The subtitles you were working on were saved to \"") + filename + _T("\", but they might be corrupt."), _T("Unhandled exception"), wxOK | wxICON_ERROR, NULL);
}