[gnome-sound-recorder] Simplify how we build record filenames



commit 2802cce3875c75ca28e484c20e0b7b7994ef5fda
Author: Cosimo Cecchi <cosimo endlessm com>
Date:   Thu Jan 16 15:27:04 2014 -0800

    Simplify how we build record filenames
    
    Refactor code dealing with save directory and filenames to be simpler.
    
    Signed-off-by: Meg Ford <meg387 gmail com>

 src/fileUtil.js   |   36 ------------------------------------
 src/info.js       |   16 +++++++---------
 src/listview.js   |   22 ++++++++++------------
 src/main.js       |    7 +++++++
 src/mainWindow.js |   24 +++++++++---------------
 src/play.js       |   14 ++++++--------
 src/record.js     |   26 ++++----------------------
 7 files changed, 43 insertions(+), 102 deletions(-)
---
diff --git a/src/fileUtil.js b/src/fileUtil.js
index f03c173..5e7a51b 100644
--- a/src/fileUtil.js
+++ b/src/fileUtil.js
@@ -34,42 +34,6 @@ const Listview = imports.listview;
 const MainWindow = imports.mainWindow;
 const Record = imports.record;
 
-const FileUtil = new Lang.Class({
-    Name: "FileUtil",
-        
-   rename: function(fileName, newFileName) { 
-       let fileToRename = Gio.file_new_for_path(fileName);
-       fileToRename.set_display_name_async(newFileName, GLib.PRIORITY_DEFAULT, null, null);
-    },
-    
-    loadFile: function(text) { 
-        this._text = text;
-        this._buildFileName = new Record.BuildFileName();
-        let path = this._buildFileName.buildPath();
-        path.push(text);
-        let fileNav = GLib.build_filenamev(path);
-       
-        return fileNav;
-    },
-    
-    deleteFile: function(fileNav) {
-        this._fileNav = fileNav;
-        this._fileToDelete = Gio.file_new_for_path(this._fileNav);       
-        this._fileToDelete.delete_async(GLib.PRIORITY_DEFAULT, null, Lang.bind(this, 
this._deleteFileCallback));
-    }, 
-    
-    _deleteFileCallback: function(obj, res) {
-        this._deleted = obj.delete_finish(res);        
-    },
-    
-    getDirPath: function() {
-        let path = MainWindow.path;
-        let dirName = GLib.build_filenamev(path);
-        let dir = Gio.file_new_for_path(dirName);
-        return dir;
-    }      
-});
-    
 const _OFFSET_STEP = 10;
 let CurrentEndIdx;
 
diff --git a/src/info.js b/src/info.js
index 35076a6..0b8c6ec 100644
--- a/src/info.js
+++ b/src/info.js
@@ -27,7 +27,6 @@ const Gtk = imports.gi.Gtk;
 const _ = imports.gettext.gettext;
 const C_ = imports.gettext.pgettext;
 
-const FileUtil = imports.fileUtil;
 const MainWindow = imports.mainWindow;
 
 const _FILE_NAME_ENTRY_TIMEOUT = 600;
@@ -37,9 +36,9 @@ const InfoDialog = new Lang.Class({
 
     _init: function(fileNav) {
         let fileName = fileNav;
-        
-        this.nav = MainWindow.fileUtil.loadFile(fileName.fileName);
-        
+
+        this._file = Gio.File.new_for_uri(fileNav.uri);
+
         this.widget = new Gtk.Dialog ({ resizable: false,
                                         modal: true,
                                         destroy_with_parent: true,
@@ -144,8 +143,7 @@ const InfoDialog = new Lang.Class({
         grid.attach_next_to(this._fileNameEntry, this._name, Gtk.PositionType.RIGHT, 2, 1);
 
         // Source value
-        let uri = GLib.filename_to_uri(this.nav, null);
-        let sourceLink = Gio.file_new_for_uri(uri).get_parent();
+        let sourceLink = this._file.get_parent();
         let sourcePath = sourceLink.get_path();
 
         this._sourceData = new Gtk.LinkButton({ label: sourcePath,
@@ -177,9 +175,9 @@ const InfoDialog = new Lang.Class({
     },
     
     onDoneClicked: function() {
-        let newFileName = this._fileNameEntry.get_text(); 
-        MainWindow.fileUtil.rename(this.nav, newFileName);
-        this.widget.destroy(); 
+        let newFileName = this._fileNameEntry.get_text();
+        this._file.set_display_name_async(newFileName, GLib.PRIORITY_DEFAULT, null, null);
+        this.widget.destroy();
     },
     
     onCancelClicked: function() {
diff --git a/src/listview.js b/src/listview.js
index c222cd9..cf9d938 100644
--- a/src/listview.js
+++ b/src/listview.js
@@ -82,18 +82,18 @@ const Listview = new Lang.Class({
     _init: function() {
         stopVal = EnumeratorState.ACTIVE;
         allFilesInfo = [];
-    },    
-        
+
+        // Save a reference to the savedir to quickly access it
+        this._saveDir = Gio.Application.get_default().saveDir;
+    },
+
     monitorListview: function() {
-        let dir = MainWindow.fileUtil.getDirPath(); 
-        this.dirMonitor = dir.monitor_directory(Gio.FileMonitorFlags.NONE, null);
-        this.dirMonitor.connect('changed', this._onDirChanged);      
+        this.dirMonitor = this._saveDir.monitor_directory(Gio.FileMonitorFlags.NONE, null);
+        this.dirMonitor.connect('changed', this._onDirChanged);
     },
             
     enumerateDirectory: function() {
-        let dir = MainWindow.fileUtil.getDirPath();    
-      
-        dir.enumerate_children_async('standard::name,time::created,time::modified',
+        this._saveDir.enumerate_children_async('standard::name,time::created,time::modified',
                                      Gio.FileQueryInfoFlags.NONE,
                                      GLib.PRIORITY_LOW, 
                                      null, Lang.bind(this, 
@@ -120,10 +120,8 @@ const Listview = new Lang.Class({
                         files.forEach(Lang.bind(this,
                             function(file) {
                                 let returnedName = file.get_attribute_as_string("standard::name");
-                                let buildFileName = new Record.BuildFileName();
-                                let initialFileName = buildFileName.buildPath();
-                                initialFileName.push(returnedName);
-                                let finalFileName = GLib.build_filenamev(initialFileName);
+                                let finalFileName = GLib.build_filenamev([this._saveDir.get_path(),
+                                                                          returnedName]);
                                 let fileUri = GLib.filename_to_uri(finalFileName, null);
                                 let timeVal = file.get_modification_time();
                                 let date = GLib.DateTime.new_from_timeval_local(timeVal);
diff --git a/src/main.js b/src/main.js
index 165d03d..f08e260 100644
--- a/src/main.js
+++ b/src/main.js
@@ -93,6 +93,13 @@ const Application = new Lang.Class({
         log(_("Sound Recorder started"));
         Gst.init(null, 0);
         this._initAppMenu();
+
+        /* Translators: "Recordings" here refers to the name of the directory where the application places 
files */
+        let path = GLib.build_filenamev([GLib.get_home_dir(), _("Recordings")]);
+
+        // Ensure Recordings directory
+        GLib.mkdir_with_parents(path, 0755);
+        this.saveDir = Gio.file_new_for_path(path);
     },
 
     vfunc_activate: function() {
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 2d40fb6..9a1bdd3 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -40,13 +40,11 @@ const Waveform = imports.waveform;
 
 let activeProfile = null;
 let audioProfile = null;
-let fileUtil = null;
 let grid = null;
 let groupGrid;
 let list = null;
 let loadMoreButton = null;
 let offsetController = null;
-let path = null;
 let play = null;
 let previousSelRow = null;
 let recordPipeline = null;
@@ -90,10 +88,6 @@ const MainWindow = new Lang.Class({
 
      _init: function(params) {
         audioProfile = new AudioProfile.AudioProfile();
-        this._buildFileName = new Record.BuildFileName()
-        path = this._buildFileName.buildPath();
-        this._buildFileName.ensureDirectory(path);
-        fileUtil = new FileUtil.FileUtil();
         offsetController = new FileUtil.OffsetController;
         view = new MainView();
         play = new Play.Play();
@@ -186,7 +180,6 @@ const MainView = new Lang.Class({
     },        
     
     _addListviewPage: function(name) {
-        fileUtil = new FileUtil.FileUtil();
         list = new Listview.Listview();
         list.setListTypeNew();
         list.enumerateDirectory();
@@ -656,9 +649,9 @@ const MainView = new Lang.Class({
                         child.sensitive = true;
                 }));
         }
-    },    
-    
-    _getFileNameFromRow: function(selected) {
+    },
+
+    _getFileFromRow: function(selected) {
         this._selected = selected;
         let fileForAction = null;
         let rowWidget = this._selected.get_child(this.fileName);
@@ -667,7 +660,8 @@ const MainView = new Lang.Class({
             
                 if (child.name == "FileNameLabel") {
                     let name = child.get_text();
-                    fileForAction = fileUtil.loadFile(name);
+                    let application = Gio.Application.get_default();
+                    fileForAction = application.saveDir.get_child_for_display_name(name);
                 }
              }));
              
@@ -676,14 +670,14 @@ const MainView = new Lang.Class({
     
     _deleteFile: function(selected) {
         this._selected = selected;
-        let fileToDelete = this._getFileNameFromRow(this._selected);
-        fileUtil.deleteFile(fileToDelete);
+        let fileToDelete = this._getFileFromRow(this._selected);
+        fileToDelete.delete_async(GLib.PRIORITY_DEFAULT, null, null);
     },
     
     loadPlay: function(selected) {
         this._selected = selected;
-        let fileToPlay = this._getFileNameFromRow(this._selected);
-        
+        let fileToPlay = this._getFileFromRow(this._selected);
+
         return fileToPlay;
     }, 
     
diff --git a/src/play.js b/src/play.js
index b425359..764967f 100644
--- a/src/play.js
+++ b/src/play.js
@@ -43,10 +43,9 @@ const _TENTH_SEC = 100000000;
  
  const Play = new Lang.Class({
     Name: "Play",
-           
-    _playPipeline: function(fileName) {
-        this._fileName = this._fileToPlay;
-        let uri = GLib.filename_to_uri(this._fileName, null);       
+
+    _playPipeline: function() {
+        let uri = this._fileToPlay.get_uri();
         this.play = Gst.ElementFactory.make("playbin", "play");
         this.play.set_property("uri", uri);
         this.sink = Gst.ElementFactory.make("pulsesink", "sink");
@@ -62,13 +61,12 @@ const _TENTH_SEC = 100000000;
                 }
             }));
     },
-            
-    startPlaying: function(fileName) {
-        this._fileName = fileName;
+
+    startPlaying: function() {
         this.baseTime = 0;
         
         if (!this.play || this.playState == PipelineStates.STOPPED ) {
-            this._playPipeline(this._fileName);
+            this._playPipeline();
         }
             
         if (this.playState == PipelineStates.PAUSED) {
diff --git a/src/record.js b/src/record.js
index 7a3b92b..eae6c59 100644
--- a/src/record.js
+++ b/src/record.js
@@ -264,33 +264,15 @@ const Record = new Lang.Class({
 const BuildFileName = new Lang.Class({ 
     Name: 'BuildFileName',
 
-    buildPath: function() {
-        let initialFileName = [];
-        initialFileName.push(GLib.get_home_dir());
-        /* Translators: "Recordings" here refers to the name of the directory where the application places 
files */
-        initialFileName.push(_("Recordings"));
-        
-        return initialFileName;
-    },
-    
-    ensureDirectory: function(name) {
-        this._name = name;    
-        let dirName = GLib.build_filenamev(this._name);
-        let namedDir = GLib.mkdir_with_parents(dirName, 0775);
-    },
-    
     buildInitialFilename: function() {
         let fileExtensionName = MainWindow.audioProfile.fileExtensionReturner();
-        let dir = this.buildPath();
+        let dir = Gio.Application.get_default().saveDir;
         this.dateTime = GLib.DateTime.new_now_local();
         /* Translators: "Audio from %Y-%m-%d %H:%M:%S" is the default name assigned to a file created
             by the application (for example, "Audio from 2013-10-05 13:25:21.ogg"). */
-        let dateTimeString = this.dateTime.format(_("Audio from %Y-%m-%d %H:%M:%S"));  
-         dir.push(dateTimeString + fileExtensionName);
-        // Use GLib.build_filenamev to work around missing vararg functions.
-        let title = GLib.build_filenamev(dir);
-        let file = Gio.file_new_for_path(title);
-        
+        let dateTimeString = this.dateTime.format(_("Audio from %Y-%m-%d %H:%M:%S"));
+        let file = dir.get_child_for_display_name(dateTimeString + fileExtensionName);
+
         return file;
     },
     


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