add Autosave continuous backups by Andras Reuss and Andrew-R
authorGood Guy <[email protected]>
Thu, 11 Feb 2021 18:29:04 +0000 (11:29 -0700)
committerGood Guy <[email protected]>
Thu, 11 Feb 2021 18:29:04 +0000 (11:29 -0700)
cinelerra-5.1/cinelerra/appearanceprefs.C
cinelerra-5.1/cinelerra/appearanceprefs.h
cinelerra-5.1/cinelerra/mwindow.C
cinelerra-5.1/cinelerra/mwindow.h
cinelerra-5.1/cinelerra/preferences.C
cinelerra-5.1/cinelerra/preferences.h

index 71bef32121dc69e8f88f904b2d8cfb4ecab0d57e..c80ee26bef23c2a00ff0557087ca65fdbdc33700 100644 (file)
@@ -232,7 +232,9 @@ void AppearancePrefs::create_objects()
        UseUnsafeGUI *unsafe_gui = new UseUnsafeGUI(pwindow, x, y);
        add_subwindow(unsafe_gui);
        y += unsafe_gui->get_h() + ys5;
-
+       OngoingBackups *ongoing_backups = new OngoingBackups(pwindow, x, y);
+       add_subwindow(ongoing_backups);
+       y += ongoing_backups->get_h() + ys5;
 
        x = get_w() / 3 + xs30;
        y = y1;
@@ -672,6 +674,20 @@ int UseUnsafeGUI::handle_event()
        return 1;
 }
 
+OngoingBackups::OngoingBackups(PreferencesWindow *pwindow, int x, int y)
+ : BC_CheckBox(x, y, pwindow->thread->preferences->ongoing_backups,
+       _("Autosave continuous backups"))
+{
+       this->pwindow = pwindow;
+       set_tooltip(_("When you stop Cinelerra, all but the newest 50 will be deleted but you risk \n running out of disk space if you do a lot of work without restarting."));
+}
+
+int OngoingBackups::handle_event()
+{
+       pwindow->thread->preferences->ongoing_backups = get_value();
+       return 1;
+}
+
 BD_WarnRoot::BD_WarnRoot(PreferencesWindow *pwindow, int x, int y)
  : BC_CheckBox(x, y, pwindow->thread->preferences->bd_warn_root,
        _("Create Bluray warns if not root"))
index 5bde37be400496afceb760aee7137a71288a4dce..fc40b04b81358c49a8764f11a93295ff6155bd57 100644 (file)
@@ -286,6 +286,14 @@ public:
        PreferencesWindow *pwindow;
 };
 
+class OngoingBackups: public BC_CheckBox
+{
+public:
+       OngoingBackups(PreferencesWindow *pwindow, int x, int y);
+       int handle_event();
+       PreferencesWindow *pwindow;
+};
+
 class BD_WarnRoot : public BC_CheckBox
 {
 public:
index 04549fdedc984278e8ac6a5dfc8176c1fe7ba69b..e63b7c54a8c629284fe682ad1822aa3831eddf0f 100644 (file)
@@ -296,6 +296,7 @@ MWindow::~MWindow()
        plugin_gui_lock->unlock();
        hide_keyframe_guis();
        clean_indexes();
+       clean_backups();
        save_defaults();
 // Give up and go to a movie
 //  cant run valgrind if this is used
@@ -984,6 +985,58 @@ void MWindow::init_preferences()
        YUV::yuv.yuv_set_colors(preferences->yuv_color_space, preferences->yuv_color_range);
 }
 
+void MWindow::clean_backups()
+{
+    FileSystem fs;
+    int total_excess;
+    long oldest = 0;
+    int oldest_item = -1;
+    char string[BCTEXTLEN];
+
+// Delete extra backups
+    fs.set_filter("backup*.prev_*");
+    fs.complete_path(preferences->index_directory);
+    fs.update(preferences->index_directory);
+
+    // set to 50 for now
+    // total_excess = fs.dir_list.total - preferences->index_count;
+    total_excess = fs.dir_list.total - 50;
+    printf("Total excess of backups: %i \n", total_excess);
+
+//printf("MWindow::clean_backups 1 %d\n", fs.dir_list.total);
+
+    while(total_excess > 0)
+    {
+// Get oldest
+       for(int i = 0; i < fs.dir_list.total; i++)
+       {
+           fs.join_names(string, preferences->index_directory, fs.dir_list[i]->name);
+
+           if(i == 0 || fs.get_date(string) <= oldest)
+           {
+               oldest = fs.get_date(string);
+               oldest_item = i;
+           }
+       }
+
+       if(oldest_item >= 0)
+       {
+// Remove backup file
+           fs.join_names(string,
+               preferences->index_directory,
+               fs.dir_list[oldest_item]->name);
+//printf("MWindow::clean_backups 1 %s\n", string);
+           if(remove(string))
+               perror("delete_backups");
+           delete fs.dir_list[oldest_item];
+           fs.dir_list.remove_number(oldest_item);
+
+       }
+
+       total_excess--;
+    }
+}
+
 void MWindow::clean_indexes()
 {
        FileSystem fs;
@@ -4270,6 +4323,25 @@ void MWindow::get_backup_path(char *path, int len)
        cp += snprintf(cp, ep-cp, idx ? BACKUPn_FILE : BACKUP_FILE, idx);
 }
 
+void MWindow::create_timestamped_copy_from_previous_backup(char *previouspath)
+{
+  if (previouspath == nullptr) return;
+  char backup_path[BCTEXTLEN];
+  backup_path[0] = 0;
+  time_t now = time(NULL);
+  struct tm* currenttime = localtime(&now);
+  snprintf(backup_path, sizeof(backup_path), 
+      "%s/%s_%d%.2d%.2d_%.2d%.2d%.2d",
+      File::get_config_path(), BACKUP_FILE1, 
+      currenttime->tm_year + 1900,
+      currenttime->tm_mon + 1,
+      currenttime->tm_mday,
+      currenttime->tm_hour,
+      currenttime->tm_min,
+      currenttime->tm_sec);
+       rename(previouspath, backup_path);
+}
+
 void MWindow::save_backup()
 {
        FileXML file;
@@ -4280,6 +4352,8 @@ void MWindow::save_backup()
        snprintf(backup_path1, sizeof(backup_path1), "%s/%s",
                File::get_config_path(), BACKUP_FILE1);
        get_backup_path(backup_path, sizeof(backup_path));
+       if( preferences->ongoing_backups )
+               create_timestamped_copy_from_previous_backup(backup_path1);
        rename(backup_path, backup_path1);
        edl->save_xml(&file, backup_path);
        file.terminate_string();
index 8f9bfbb74b4a7c3e04d6bf8dfecb756eed60a7e3..c5dde17209371616e592325110322f98cfbf0881 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <stdio.h>
 #include <stdint.h>
+#include <time.h>
 
 #include "apatchgui.h"
 #include "arraylist.h"
@@ -384,6 +385,7 @@ public:
        void crop_video(int mode);
        void update_plugins();
        void get_backup_path(char *path, int len);
+       void create_timestamped_copy_from_previous_backup(char *previouspath);
 // Call after every edit operation
        void save_backup();
        void load_backup();
@@ -853,6 +855,7 @@ public:
        int get_cpus();
 //
        void clean_indexes();
+       void clean_backups();
 //     TimeBomb timebomb;
        SigHandler *sighandler;
        int restart_status;
index e517f2f3dadb72390ef228ee4fc7c80dc493d973..6f4e57c995481e371d6203756fe96dfc78473545 100644 (file)
@@ -94,6 +94,7 @@ Preferences::Preferences()
        memset(&use_hw_dev, 0, sizeof(use_hw_dev));
        warn_indexes = 1;
        unsafe_gui = 0;
+       ongoing_backups = 0;
        warn_fileref = 1;
        bd_warn_root = 1;
        popupmenu_btnup = 1;
@@ -221,6 +222,7 @@ void Preferences::copy_from(Preferences *that)
        strcpy(use_hw_dev, &that->use_hw_dev[0]);
        warn_indexes = that->warn_indexes;
        unsafe_gui = that->unsafe_gui;
+       ongoing_backups = that->ongoing_backups;
        warn_fileref = that->warn_fileref;
        bd_warn_root = that->bd_warn_root;
        popupmenu_btnup = that->popupmenu_btnup;
@@ -372,6 +374,7 @@ int Preferences::load_defaults(BC_Hash *defaults)
        defaults->get("USE_HW_DEV", use_hw_dev);
        warn_indexes = defaults->get("WARN_INDEXES", warn_indexes);
        unsafe_gui = defaults->get("UNSAFE_GUI", unsafe_gui);
+       ongoing_backups = defaults->get("ONGOING_BACKUPS", ongoing_backups);
        warn_fileref = defaults->get("WARN_FILEREF", warn_fileref);
        bd_warn_root = defaults->get("BD_WARN_ROOT", bd_warn_root);
        popupmenu_btnup = defaults->get("POPUPMENU_BTNUP", popupmenu_btnup);
@@ -523,6 +526,7 @@ int Preferences::save_defaults(BC_Hash *defaults)
        defaults->update("USE_HW_DEV", use_hw_dev);
        defaults->update("WARN_INDEXES", warn_indexes);
        defaults->update("UNSAFE_GUI", unsafe_gui);
+       defaults->update("ONGOING_BACKUPS", ongoing_backups);
        defaults->update("WARN_FILEREF", warn_fileref);
        defaults->update("BD_WARN_ROOT", bd_warn_root);
        defaults->update("POPUPMENU_BTNUP", popupmenu_btnup);
index 980e55e0889a4bdd1af208d221d3ad93b44cadcb..e814f4260f8f91d5a52a64af59d3ccedc15a4e22 100644 (file)
@@ -141,6 +141,8 @@ public:
        int yuv_color_range;
 // autocolor asset edit title
        int autocolor_assets;
+// ongoing continuous backups
+       int ongoing_backups;
 // ctrl_toggle && !ctrl_down() clears selection before toggle
        int ctrl_toggle;
 // rectify timeline audio waveform