Move autosave logic to SubsController

This commit is contained in:
Thomas Goyne 2013-01-30 06:40:02 -08:00
parent 74028b9bcd
commit a9467836f9
4 changed files with 33 additions and 32 deletions

View file

@ -82,7 +82,6 @@
#include <wx/sysopt.h>
enum {
ID_APP_TIMER_AUTOSAVE = 12001,
ID_APP_TIMER_STATUSCLEAR = 12002
};
@ -92,8 +91,6 @@ enum {
#define StartupLog(a) LOG_I("frame_main/init") << a
#endif
static void autosave_timer_changed(wxTimer *timer);
wxDEFINE_EVENT(FILE_LIST_DROPPED, wxThreadEvent);
static void get_files_to_load(wxArrayString const& list, std::string &subs, std::string &audio, std::string &video) {
@ -272,12 +269,6 @@ FrameMain::FrameMain (wxArrayString args)
StartupLog("Complete context initialization");
context->videoController->SetContext(context.get());
StartupLog("Set up Auto Save");
AutoSave.SetOwner(this, ID_APP_TIMER_AUTOSAVE);
autosave_timer_changed(&AutoSave);
OPT_SUB("App/Auto/Save", autosave_timer_changed, &AutoSave);
OPT_SUB("App/Auto/Save Every Seconds", autosave_timer_changed, &AutoSave);
StartupLog("Set up drag/drop target");
SetDropTarget(new AegisubFileDropTarget(this));
@ -550,15 +541,7 @@ bool FrameMain::LoadList(wxArrayString list) {
return loaded_any;
}
static void autosave_timer_changed(wxTimer *timer) {
int freq = OPT_GET("App/Auto/Save Every Seconds")->GetInt();
if (freq > 0 && OPT_GET("App/Auto/Save")->GetBool())
timer->Start(freq * 1000);
else
timer->Stop();
}
BEGIN_EVENT_TABLE(FrameMain, wxFrame)
EVT_TIMER(ID_APP_TIMER_AUTOSAVE, FrameMain::OnAutoSave)
EVT_TIMER(ID_APP_TIMER_STATUSCLEAR, FrameMain::OnStatusClear)
EVT_CLOSE(FrameMain::OnCloseWindow)
@ -586,18 +569,6 @@ void FrameMain::OnCloseWindow(wxCloseEvent &event) {
Destroy();
}
void FrameMain::OnAutoSave(wxTimerEvent &) try {
auto fn = context->subsController->AutoSave();
if (!fn.empty())
StatusTimeout(wxString::Format(_("File backup saved as \"%s\"."), fn.wstring()));
}
catch (const agi::Exception& err) {
StatusTimeout(to_wx("Exception when attempting to autosave file: " + err.GetMessage()));
}
catch (...) {
StatusTimeout("Unhandled exception when attempting to autosave file.");
}
void FrameMain::OnStatusClear(wxTimerEvent &) {
SetStatusText("",1);
}

View file

@ -76,7 +76,6 @@ class FrameMain: public wxFrame {
bool showVideo; ///< Is the video display shown?
bool showAudio; ///< Is the audio display shown?
wxTimer AutoSave; ///< Autosave timer
wxTimer StatusClear; ///< Status bar timeout timer
/// Block video loading; used when both video and subtitles are opened at
/// the same time, so that the video associated with the subtitles (if any)
@ -94,8 +93,6 @@ class FrameMain: public wxFrame {
void OnKeyDown(wxKeyEvent &event);
void OnMouseWheel(wxMouseEvent &evt);
/// @brief Autosave the currently open file, if any
void OnAutoSave(wxTimerEvent &event);
void OnStatusClear(wxTimerEvent &event);
void OnCloseWindow (wxCloseEvent &event);

View file

@ -23,7 +23,9 @@
#include "ass_style.h"
#include "compat.h"
#include "command/command.h"
#include "frame_main.h"
#include "include/aegisub/context.h"
#include "main.h"
#include "options.h"
#include "subtitle_format.h"
#include "text_file_reader.h"
@ -37,6 +39,16 @@
#include <boost/format.hpp>
#include <wx/msgdlg.h>
namespace {
void autosave_timer_changed(wxTimer *timer) {
int freq = OPT_GET("App/Auto/Save Every Seconds")->GetInt();
if (freq > 0 && OPT_GET("App/Auto/Save")->GetBool())
timer->Start(freq * 1000);
else
timer->Stop();
}
}
struct SubsController::UndoInfo {
AssFile file;
wxString undo_description;
@ -51,6 +63,23 @@ SubsController::SubsController(agi::Context *context)
, saved_commit_id(0)
, autosaved_commit_id(0)
{
autosave_timer_changed(&autosave_timer);
OPT_SUB("App/Auto/Save", autosave_timer_changed, &autosave_timer);
OPT_SUB("App/Auto/Save Every Seconds", autosave_timer_changed, &autosave_timer);
autosave_timer.Bind(wxEVT_TIMER, [=](wxTimerEvent&) {
try {
auto fn = AutoSave();
if (!fn.empty())
wxTheApp->frame->StatusTimeout(wxString::Format(_("File backup saved as \"%s\"."), fn.wstring()));
}
catch (const agi::Exception& err) {
wxTheApp->frame->StatusTimeout(to_wx("Exception when attempting to autosave file: " + err.GetMessage()));
}
catch (...) {
wxTheApp->frame->StatusTimeout("Unhandled exception when attempting to autosave file.");
}
});
}
void SubsController::Load(agi::fs::path const& filename, std::string const& charset) {

View file

@ -20,6 +20,7 @@
#include <boost/container/list.hpp>
#include <boost/filesystem/path.hpp>
#include <set>
#include <wx/timer.h>
class AssEntry;
class AssFile;
@ -42,6 +43,9 @@ class SubsController {
/// Last autosaved version of this file
int autosaved_commit_id;
/// Timer for triggering autosaves
wxTimer autosave_timer;
/// A new file has been opened (filename)
agi::signal::Signal<agi::fs::path> FileOpen;
/// The file is about to be saved