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




commit 0e86097d6808f1a92254e24e268e87155224d15d
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 | 62 ++++++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 59 insertions(+), 20 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..89a3ace8 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..b9214f13 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,15 +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)
+
+        const copyFiles = function (obj, res) {
+            const fileInfos = obj.next_files_finish(res);
+            if (fileInfos.length) {
+                fileInfos.forEach(info => {
+                    const src = oldDir.get_child(info.get_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(info.get_name()));
+
+                    src.copy_async(dest, Gio.FileCopyFlags.BACKUP, GLib.PRIORITY_LOW, null, null, (obj, res) 
=> {
+                        obj.copy_finish(res);
+                        obj.trash_async(GLib.PRIORITY_LOW, null, null);
+                        this.dirMonitor.emit_event(dest, src, Gio.FileMonitorEvent.MOVED_IN);
+                    });
+
+                });
+                fileEnumerator.next_files_async(5, GLib.PRIORITY_LOW, null, copyFiles.bind(this));
+            } else {
+                fileEnumerator.close(null);
+                oldDir.delete_async(GLib.PRIORITY_LOW, null, (obj, res) => {
+                    try {
+                        obj.delete_finish(res);
+                    } catch (e) {
+                        log('Failed to remove the old Recordings directory. Ignore if you\'re using 
flatpak');
+                        log(e);
+                    }
+                });
+            }
+        }
+        fileEnumerator.next_files_async(5, GLib.PRIORITY_LOW, null, copyFiles.bind(this))
     }
 
-    _enumerateDirectory(obj, res) {
+   _enumerateDirectory(obj, res) {
         this._enumerator = obj.enumerate_children_finish(res);
         if (this._enumerator === null) {
             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 +95,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]