diff --git a/aegisub/src/frame_main.cpp b/aegisub/src/frame_main.cpp index 849ce44b5..2e8c749f5 100644 --- a/aegisub/src/frame_main.cpp +++ b/aegisub/src/frame_main.cpp @@ -82,7 +82,6 @@ #include 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); } diff --git a/aegisub/src/frame_main.h b/aegisub/src/frame_main.h index 58f38bf50..c75b95609 100644 --- a/aegisub/src/frame_main.h +++ b/aegisub/src/frame_main.h @@ -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); diff --git a/aegisub/src/subs_controller.cpp b/aegisub/src/subs_controller.cpp index c04afacc1..7a92f0be0 100644 --- a/aegisub/src/subs_controller.cpp +++ b/aegisub/src/subs_controller.cpp @@ -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 #include +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) { diff --git a/aegisub/src/subs_controller.h b/aegisub/src/subs_controller.h index 7a6417426..22b820171 100644 --- a/aegisub/src/subs_controller.h +++ b/aegisub/src/subs_controller.h @@ -20,6 +20,7 @@ #include #include #include +#include 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 FileOpen; /// The file is about to be saved