Move the CanSave logic to the subtitle formats as it's obviously format-specific

Originally committed to SVN as r6625.
This commit is contained in:
Thomas Goyne 2012-03-28 23:58:40 +00:00
parent 0ae8479ffb
commit a03b37bdef
6 changed files with 44 additions and 44 deletions

View file

@ -225,45 +225,9 @@ void AssFile::SaveMemory(std::vector<char> &dst) {
}
}
bool AssFile::CanSave() {
// ASS format?
wxString ext = filename.Lower().Right(4);
if (ext == ".ass") return true;
// Never save texts
if (ext == ".txt") return false;
// Check if it's a known extension
if (!SubtitleFormat::GetWriter(filename)) return false;
// Scan through the lines
AssStyle defstyle;
AssStyle *curstyle;
AssDialogue *curdiag;
for (entryIter cur=Line.begin();cur!=Line.end();cur++) {
// Check style, if anything non-default is found, return false
curstyle = dynamic_cast<AssStyle*>(*cur);
if (curstyle) {
if (curstyle->GetEntryData() != defstyle.GetEntryData()) return false;
}
// Check for attachments, if any is found, return false
if (dynamic_cast<AssAttachment*>(*cur)) return false;
// Check dialog
curdiag = dynamic_cast<AssDialogue*>(*cur);
if (curdiag) {
// Overrides?
curdiag->ParseASSTags();
for (size_t i=0;i<curdiag->Blocks.size();i++) {
if (curdiag->Blocks[i]->GetType() != BLOCK_PLAIN) return false;
}
curdiag->ClearBlocks();
}
}
// Success
return true;
bool AssFile::CanSave() const {
const SubtitleFormat *writer = SubtitleFormat::GetWriter(filename);
return writer && writer->CanSave(this);
}
void AssFile::AddLine(wxString data, int *version, AssAttachment **attach) {

View file

@ -146,7 +146,7 @@ public:
/// Add file name to the MRU list
void AddToRecent(wxString file);
/// Can the file be saved in its current format?
bool CanSave();
bool CanSave() const;
/// @brief Get the list of wildcards supported
/// @param mode 0 = open, 1 = save, 2 = export
static wxString GetWildcardList(int mode);

View file

@ -36,14 +36,17 @@
#include "config.h"
#include "subtitle_format.h"
#ifndef AGI_PRE
#include <wx/intl.h>
#include <wx/choicdlg.h> // Keep this last so wxUSE_CHOICEDLG is set.
#endif
#include "ass_attachment.h"
#include "ass_dialogue.h"
#include "ass_file.h"
#include "subtitle_format.h"
#include "ass_style.h"
#include "subtitle_format_ass.h"
#include "subtitle_format_encore.h"
#include "subtitle_format_microdvd.h"
@ -75,7 +78,28 @@ bool SubtitleFormat::CanWriteFile(wxString const& filename) const {
return GetWriteWildcards().Index(filename.AfterLast('.'), false) != wxNOT_FOUND;
}
/// @brief Ask the user to enter the FPS
bool SubtitleFormat::CanSave(const AssFile *subs) const {
AssStyle defstyle;
for (std::list<AssEntry*>::const_iterator cur = subs->Line.begin(); cur != subs->Line.end(); ++cur) {
// Check style, if anything non-default is found, return false
if (const AssStyle *curstyle = dynamic_cast<const AssStyle*>(*cur)) {
if (curstyle->GetEntryData() != defstyle.GetEntryData())
return false;
}
// Check for attachments, if any is found, return false
if (dynamic_cast<const AssAttachment*>(*cur)) return false;
// Check dialog
if (const AssDialogue *curdiag = dynamic_cast<const AssDialogue*>(*cur)) {
if (curdiag->GetStrippedText() != curdiag->Text)
return false;
}
}
return true;
}
FractionalTime SubtitleFormat::AskForFPS(bool showSMPTE) const {
wxArrayString choices;
bool drop = false;

View file

@ -103,16 +103,22 @@ public:
/// @brief Check if the given file can be read by this format
///
/// Default implement ion simply checks if the file's extension is in the
/// Default implementation simply checks if the file's extension is in the
/// format's wildcard list
virtual bool CanReadFile(wxString const& filename) const;
/// @brief Check if the given file can be written by this format
///
/// Default implement ion simply checks if the file's extension is in the
/// Default implementation simply checks if the file's extension is in the
/// format's wildcard list
virtual bool CanWriteFile(wxString const& filename) const;
/// @brief Check if the given subtitles can be losslessly written by this format
///
/// Default implementation rejects files with attachments, non-default
/// styles, and any overrides
virtual bool CanSave(const AssFile *file) const;
/// Load a subtitle file
/// @param[out] target Destination to read lines into
/// @param filename File to load

View file

@ -48,6 +48,9 @@ public:
wxArrayString GetReadWildcards() const;
wxArrayString GetWriteWildcards() const;
// Naturally the ASS subtitle format can save all Ass files
bool CanSave(const AssFile*) const { return true; }
void ReadFile(AssFile *target, wxString const& filename, wxString const& forceEncoding) const;
void WriteFile(const AssFile *src, wxString const& filename, wxString const& encoding) const;
};

View file

@ -47,6 +47,9 @@ public:
wxArrayString GetReadWildcards() const;
wxArrayString GetWriteWildcards() const;
// TXT format supports so little that it should always require an export
bool CanSave(const AssFile*) const { return false; }
bool CanWriteFile(wxString const& filename) const;
void ReadFile(AssFile *target, wxString const& filename, wxString const& forceEncoding) const;
void WriteFile(const AssFile *src, wxString const& filename, wxString const& encoding) const;