[gnome-sound-recorder/bilelmoussaoui/copy-old-files] move recordings directory into xdg-data



commit 180c24ee795a5a1c391d81b94215737e7b3048f0
Author: Bilal Elmoussaoui <bil elmoussaoui gmail com>
Date:   Sun Aug 9 03:26:46 2020 +0200

    move recordings directory into xdg-data
    
    this also copies the old files to the new location async and
    they appear as $oldName followed by a suffix to differentiate them
    
    Note: currently we don't regenerate waveform data for those files
    this will come in a next merge request
    
    Fixes #20 #30

 po/POTFILES.in       |  1 +
 src/application.js   | 16 ++++++------
 src/recordingList.js | 69 ++++++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 67 insertions(+), 19 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index fa54f7c7..6c267014 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -8,6 +8,7 @@ data/ui/window.ui
 src/application.js
 src/main.js
 src/recorder.js
+src/recordingList.js
 src/row.js
 src/utils.js
 src/waveform.js
diff --git a/src/application.js b/src/application.js
index 079e5656..22a27da2 100644
--- a/src/application.js
+++ b/src/application.js
@@ -21,7 +21,7 @@
 const { Gdk, Gio, GLib, GObject, Gst, Gtk, Handy } = imports.gi;
 
 /* Translators: "Recordings" here refers to the name of the directory where the application places files */
-var RecordingsDir = Gio.file_new_for_path(GLib.build_filenamev([GLib.get_home_dir(), _('Recordings')]));
+var RecordingsDir = Gio.file_new_for_path(GLib.build_filenamev([GLib.get_user_data_dir(), pkg.name]));
 var CacheDir = Gio.file_new_for_path(GLib.build_filenamev([GLib.get_user_cache_dir(), pkg.name]));
 var Settings = new Gio.Settings({ schema: pkg.name });
 
@@ -127,20 +127,18 @@ var Application = GObject.registerClass(class Application extends Gtk.Applicatio
 
     vfunc_startup() {
         super.vfunc_startup();
-
         this._loadStyleSheet();
         Gtk.IconTheme.get_default().add_resource_path('/org/gnome/SoundRecorder/icons/');
         log(_('Sound Recorder started'));
         Handy.init();
         Gst.init(null);
-        this._initAppMenu();
-        this.ensureDirectory();
-    }
 
-    ensureDirectory() {
-        // Ensure Recordings directory
-        GLib.mkdir_with_parents(RecordingsDir.get_path(), 0o0755);
-        GLib.mkdir_with_parents(CacheDir.get_path(), 0o0755);
+        if (!RecordingsDir.query_exists(null))
+            RecordingsDir.make_directory_with_parents(null);
+        if (!CacheDir.query_exists(null))
+            CacheDir.make_directory_with_parents(null);
+
+        this._initAppMenu();
     }
 
     vfunc_activate() {
diff --git a/src/recordingList.js b/src/recordingList.js
index df7e05cd..a17bc05e 100644
--- a/src/recordingList.js
+++ b/src/recordingList.js
@@ -9,16 +9,11 @@ var RecordingList = new GObject.registerClass(class RecordingList extends Gio.Li
         super._init({ });
 
         // Monitor Direcotry actions
-        let dirMonitor = RecordingsDir.monitor_directory(Gio.FileMonitorFlags.WATCH_MOVES, null);
-        dirMonitor.connect('changed', (_dirMonitor, file1, file2, eventType) => {
-            let index = this.getIndex(file1);
+        this.dirMonitor = RecordingsDir.monitor_directory(Gio.FileMonitorFlags.WATCH_MOVES, null);
+        this.dirMonitor.connect('changed', (_dirMonitor, file1, file2, eventType) => {
+            const index = this.getIndex(file1);
 
             switch (eventType) {
-            case Gio.FileMonitorEvent.DELETED:
-                if (RecordingsDir.equal(file1))
-                    Gio.Application.get_default().ensureDirectory();
-
-                break;
             case Gio.FileMonitorEvent.MOVED_OUT:
                 if (index >= 0)
                     this.remove(index);
@@ -36,6 +31,60 @@ var RecordingList = new GObject.registerClass(class RecordingList extends Gio.Li
             GLib.PRIORITY_LOW,
             null,
             this._enumerateDirectory.bind(this));
+
+        this.copyOldFiles();
+    }
+
+    copyOldFiles() {
+        // Necessary code to move old recordings into the new location for few releases
+        // FIXME: Remove by 3.40/3.42
+        const oldDir = Gio.file_new_for_path(GLib.build_filenamev([GLib.get_home_dir(), _('Recordings')]));
+
+        if (!oldDir.query_exists(null))
+            return;
+
+        const fileEnumerator = oldDir.enumerate_children('standard::name', 
Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
+        let allCopied = true;
+
+        const copyFiles = function (obj, res) {
+            const fileInfos = obj.next_files_finish(res);
+            if (fileInfos.length) {
+                fileInfos.forEach(info => {
+                    const name = info.get_name();
+                    const src = oldDir.get_child(name);
+                    /* Translators: ""%s (Old)"" is the new name assigned to a file moved from
+                        the old recordings location */
+                    const dest = RecordingsDir.get_child(_('%s (Old)').format(name));
+
+                    src.copy_async(dest, Gio.FileCopyFlags.BACKUP, GLib.PRIORITY_LOW, null, null, (objCopy, 
resCopy) => {
+                        try {
+                            objCopy.copy_finish(resCopy);
+                            objCopy.trash_async(GLib.PRIORITY_LOW, null, null);
+                            this.dirMonitor.emit_event(dest, src, Gio.FileMonitorEvent.MOVED_IN);
+                        } catch (e) {
+                            log(`Failed to copy recording ${name} to the new location`);
+                            log(e);
+                            allCopied = false;
+                        }
+                    });
+
+                });
+                fileEnumerator.next_files_async(5, GLib.PRIORITY_LOW, null, copyFiles);
+            } else {
+                fileEnumerator.close(null);
+                if (allCopied) {
+                    oldDir.delete_async(GLib.PRIORITY_LOW, null, (objDelete, resDelete) => {
+                        try {
+                            objDelete.delete_finish(resDelete);
+                        } catch (e) {
+                            log('Failed to remove the old Recordings directory. Ignore if you\'re using 
flatpak');
+                            log(e);
+                        }
+                    });
+                }
+            }
+        }.bind(this);
+        fileEnumerator.next_files_async(5, GLib.PRIORITY_LOW, null, copyFiles);
     }
 
     _enumerateDirectory(obj, res) {
@@ -44,7 +93,7 @@ var RecordingList = new GObject.registerClass(class RecordingList extends Gio.Li
             log('The contents of the Recordings directory were not indexed.');
             return;
         }
-        this._enumerator.next_files_async(20, GLib.PRIORITY_LOW, null, this._onNextFiles.bind(this));
+        this._enumerator.next_files_async(5, GLib.PRIORITY_LOW, null, this._onNextFiles.bind(this));
     }
 
     _onNextFiles(obj, res) {
@@ -55,7 +104,7 @@ var RecordingList = new GObject.registerClass(class RecordingList extends Gio.Li
                 const recording = new Recording(file);
                 this.sortedInsert(recording);
             });
-            this._enumerator.next_files_async(20, GLib.PRIORITY_LOW, null, this._onNextFiles.bind(this));
+            this._enumerator.next_files_async(5, GLib.PRIORITY_LOW, null, this._onNextFiles.bind(this));
         } else {
             this._enumerator.close(null);
         }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]