Clean up Automation4::ScriptManager

Originally committed to SVN as r5642.
This commit is contained in:
Thomas Goyne 2011-09-28 19:48:58 +00:00
parent eec3d64221
commit 8ba559b7f7
2 changed files with 28 additions and 92 deletions

View file

@ -355,76 +355,31 @@ namespace Automation4 {
} }
// ScriptManager // ScriptManager
/// @brief DOCME
///
ScriptManager::ScriptManager()
{
// do nothing...?
}
/// @brief DOCME
///
ScriptManager::~ScriptManager() ScriptManager::~ScriptManager()
{ {
RemoveAll(); RemoveAll();
} }
/// @brief DOCME
/// @param script
/// @return
///
void ScriptManager::Add(Script *script) void ScriptManager::Add(Script *script)
{ {
for (std::vector<Script*>::iterator i = scripts.begin(); i != scripts.end(); ++i) { if (find(scripts.begin(), scripts.end(), script) == scripts.end())
if (script == *i) return; scripts.push_back(script);
}
scripts.push_back(script);
} }
/// @brief DOCME
/// @param script
/// @return
///
void ScriptManager::Remove(Script *script) void ScriptManager::Remove(Script *script)
{ {
for (std::vector<Script*>::iterator i = scripts.begin(); i != scripts.end(); ++i) { std::vector<Script*>::iterator i = find(scripts.begin(), scripts.end(), script);
if (script == *i) { if (i != scripts.end()) {
delete *i; delete *i;
scripts.erase(i); scripts.erase(i);
return;
}
} }
} }
/// @brief DOCME
///
void ScriptManager::RemoveAll() void ScriptManager::RemoveAll()
{ {
for (std::vector<Script*>::iterator i = scripts.begin(); i != scripts.end(); ++i) { delete_clear(scripts);
delete *i;
}
scripts.clear();
} }
/// @brief DOCME
/// @return
///
const std::vector<Script*>& ScriptManager::GetScripts() const
{
return scripts;
}
/// @brief DOCME
/// @return
///
const std::vector<cmd::Command*>& ScriptManager::GetMacros() const std::vector<cmd::Command*>& ScriptManager::GetMacros()
{ {
macros.clear(); macros.clear();
@ -437,20 +392,12 @@ namespace Automation4 {
// AutoloadScriptManager // AutoloadScriptManager
AutoloadScriptManager::AutoloadScriptManager(wxString const& path)
: path(path)
/// @brief DOCME
/// @param _path
///
AutoloadScriptManager::AutoloadScriptManager(const wxString &_path)
: path(_path)
{ {
Reload(); Reload();
} }
/// @brief DOCME
///
void AutoloadScriptManager::Reload() void AutoloadScriptManager::Reload()
{ {
RemoveAll(); RemoveAll();
@ -496,7 +443,8 @@ namespace Automation4 {
slots.push_back(c->ass->AddFileOpenListener(&LocalScriptManager::Reload, this)); slots.push_back(c->ass->AddFileOpenListener(&LocalScriptManager::Reload, this));
} }
void LocalScriptManager::Reload() { void LocalScriptManager::Reload()
{
RemoveAll(); RemoveAll();
wxString local_scripts = context->ass->GetScriptInfo("Automation Scripts"); wxString local_scripts = context->ass->GetScriptInfo("Automation Scripts");
@ -525,7 +473,6 @@ namespace Automation4 {
wxFileName sfname(trimmed); wxFileName sfname(trimmed);
sfname.MakeAbsolute(basepath); sfname.MakeAbsolute(basepath);
if (sfname.FileExists()) { if (sfname.FileExists()) {
wxString err;
Add(Automation4::ScriptFactory::CreateFromFile(sfname.GetFullPath(), true)); Add(Automation4::ScriptFactory::CreateFromFile(sfname.GetFullPath(), true));
} else { } else {
wxLogWarning("Automation Script referenced could not be found.\nFilename specified: %c%s\nSearched relative to: %s\nResolved filename: %s", wxLogWarning("Automation Script referenced could not be found.\nFilename specified: %c%s\nSearched relative to: %s\nResolved filename: %s",
@ -534,7 +481,8 @@ namespace Automation4 {
} }
} }
void LocalScriptManager::OnSubtitlesSave() { void LocalScriptManager::OnSubtitlesSave()
{
// Store Automation script data // Store Automation script data
// Algorithm: // Algorithm:
// 1. If script filename has Automation Base Path as a prefix, the path is relative to that (ie. "$") // 1. If script filename has Automation Base Path as a prefix, the path is relative to that (ie. "$")
@ -614,6 +562,7 @@ namespace Automation4 {
if (log_errors) if (log_errors)
wxLogError(_("The file was not recognised as an Automation script: %s"), filename); wxLogError(_("The file was not recognised as an Automation script: %s"), filename);
return new UnknownScript(filename); return new UnknownScript(filename);
} }
@ -661,7 +610,6 @@ namespace Automation4 {
return fnfilter; return fnfilter;
} }
// UnknownScript // UnknownScript
UnknownScript::UnknownScript(wxString const& filename) UnknownScript::UnknownScript(wxString const& filename)
: Script(filename) : Script(filename)

View file

@ -217,30 +217,25 @@ namespace Automation4 {
virtual std::vector<SubtitleFormat*> GetFormats() const=0; virtual std::vector<SubtitleFormat*> GetFormats() const=0;
}; };
/// DOCME /// A manager of loaded automation scripts
/// @class ScriptManager
/// @brief DOCME
///
/// DOCME
class ScriptManager { class ScriptManager {
private:
/// DOCME
std::vector<Script*> scripts; std::vector<Script*> scripts;
/// DOCME
std::vector<cmd::Command*> macros; std::vector<cmd::Command*> macros;
public: public:
ScriptManager(); /// Deletes all scripts managed
virtual ~ScriptManager(); // Deletes all scripts managed virtual ~ScriptManager();
void Add(Script *script); // Add a script to the manager. The ScriptManager takes owvership of the script and will automatically delete it. /// Add a script to the manager. The ScriptManager takes ownership of the script and will automatically delete it.
void Remove(Script *script); // Remove a script from the manager, and delete the Script object. void Add(Script *script);
void RemoveAll(); // Deletes all scripts managed /// Remove a script from the manager, and delete the Script object.
void Remove(Script *script);
/// Deletes all scripts managed
void RemoveAll();
/// Reload all scripts mananaged
virtual void Reload() = 0; virtual void Reload() = 0;
const std::vector<Script*>& GetScripts() const; /// Get all managed scripts (both loaded and invalid)
const std::vector<Script*>& GetScripts() const { return scripts; }
const std::vector<cmd::Command*>& GetMacros(); const std::vector<cmd::Command*>& GetMacros();
// No need to have getters for the other kinds of features, I think. // No need to have getters for the other kinds of features, I think.
@ -258,18 +253,11 @@ namespace Automation4 {
void Reload(); void Reload();
}; };
/// DOCME /// Manager for scripts in the autoload directory
/// @class AutoloadScriptManager
/// @brief DOCME
///
/// DOCME
class AutoloadScriptManager : public ScriptManager { class AutoloadScriptManager : public ScriptManager {
private:
/// DOCME
wxString path; wxString path;
public: public:
AutoloadScriptManager(const wxString &_path); AutoloadScriptManager(wxString const& path);
void Reload(); void Reload();
}; };