[the-board] [things] Change Sound thing to be URI-based instead of path-based



commit 2b2f94858f0740c714e2e47e84adafa4ff39c4f4
Author: Lucas Rocha <lucasr lucasr org>
Date:   Thu Jul 7 23:56:14 2011 +0100

    [things] Change Sound thing to be URI-based instead of path-based
    
    More future-proof and leads to cleaner code.

 src/js/ui/things/sound.js  |   49 +++++++++++++++++++++--------------------
 src/tb/tb-sound-player.c   |   42 ++++++++++++++++++------------------
 src/tb/tb-sound-recorder.c |   51 +++++++++++++++++++++++++------------------
 3 files changed, 76 insertions(+), 66 deletions(-)
---
diff --git a/src/js/ui/things/sound.js b/src/js/ui/things/sound.js
index 6bdc342..a13d669 100644
--- a/src/js/ui/things/sound.js
+++ b/src/js/ui/things/sound.js
@@ -275,20 +275,20 @@ SoundThing.prototype = {
         // loading state of the sound
     },
 
-    _updateSoundFilename : function(soundFilename, fromState) {
-        if (this._soundFilename == soundFilename) {
+    _updateURI : function(uri, fromState) {
+        if (this._uri == uri) {
             return;
         }
 
         this._maybeDeleteVoiceFile();
 
-        this._soundFilename = soundFilename;
+        this._uri = uri;
 
-        if (this._soundFilename) {
+        if (this._uri) {
             this._updateSpinner();
 
             // start loading the new sound file
-            this._player.filename = this._soundFilename;
+            this._player.uri = this._uri;
 
             //this._player.playing = false;
 
@@ -346,7 +346,7 @@ SoundThing.prototype = {
             return;
         }
 
-        let filename = chooser.get_filename();
+        let uri = chooser.get_uri();
 
         // Destroy dialog first, then set sound
         chooser.destroy();
@@ -355,8 +355,8 @@ SoundThing.prototype = {
         // because the chooser grabs key focus
         this._captionLabel.clutterText.grab_key_focus();
 
-        this._updateSoundFilename(filename,
-                                  false /* not from state*/);
+        this._updateURI(uri,
+                        false /* not from state*/);
     },
 
     _ensureTargetVoiceDir : function() {
@@ -370,7 +370,7 @@ SoundThing.prototype = {
         }
     },
 
-    _generateVoiceFilename : function() {
+    _generateVoiceFileURI : function() {
         this._ensureTargetVoiceDir();
 
         let now = new Date();
@@ -385,7 +385,9 @@ SoundThing.prototype = {
                        now.getMilliseconds() +
                        ".ogg";
 
-        return this._targetVoicePath + filename;
+        let file = GIO.file_new_for_path(this._targetVoicePath + filename);
+
+        return file.get_uri();
     },
 
     _startVoiceRecording : function() {
@@ -393,14 +395,14 @@ SoundThing.prototype = {
             return;
         }
 
-        let filename = this._generateVoiceFilename();
+        let uri = this._generateVoiceFileURI();
 
-        if (!filename) {
+        if (!uri) {
             return;
         }
 
         this._recorder =
-            new TheBoard.SoundRecorder({ filename: filename });
+            new TheBoard.SoundRecorder({ uri: uri });
 
         this._recorder.connect("notify::duration",
                                Lang.bind(this,
@@ -443,7 +445,7 @@ SoundThing.prototype = {
 
         this._setSpoolsAnimating(false);
 
-        let soundFilename = this._recorder.filename;
+        let uri = this._recorder.uri;
 
         this._recorder.stop();
         delete this._recorder;
@@ -456,8 +458,8 @@ SoundThing.prototype = {
         this._captionLabel.text = this._textBeforeRecording;
         delete this._textBeforeRecording;
 
-        this._updateSoundFilename(soundFilename,
-                                  true /* avoid autoplaying */);
+        this._updateURI(uri,
+                        true /* avoid autoplaying */);
 
         this.emit("save");
 
@@ -474,13 +476,13 @@ SoundThing.prototype = {
     },
 
     _maybeDeleteVoiceFile : function() {
-        if (!this._soundFilename) {
+        if (!this._uri) {
             return;
         }
 
         let targetDir = GIO.file_new_for_path(this._targetVoicePath);
 
-        let soundFile = GIO.file_new_for_path(this._soundFilename);
+        let soundFile = GIO.file_new_for_path(this._uri);
         let parentDir = soundFile.get_parent();
 
         if (targetDir.equal(parentDir)) {
@@ -522,7 +524,7 @@ SoundThing.prototype = {
         let actorsToShow = [];
         let actorsToHide = [];
 
-        let showTimeLabel = this._recorder || this._soundFilename;
+        let showTimeLabel = this._recorder || this._uri;
 
         if (showTimeLabel) {
             actorsToShow.push(this._timeLabel);
@@ -531,7 +533,7 @@ SoundThing.prototype = {
         }
 
         let showPlaybackBox =
-            !this._recorder && this._soundFilename &&
+            !this._recorder && this._uri &&
             (this.hover || this.active);
 
         if (showPlaybackBox) {
@@ -666,12 +668,11 @@ SoundThing.prototype = {
     },
 
     loadState : function(state) {
-        if ('soundFilename' in state) {
+        if ('uri' in state) {
             let fromState = 'width' in state &&
                             'height' in state;
 
-            this._updateSoundFilename(state.soundFilename,
-                                      fromState);
+            this._updateURI(state.uri, fromState);
         }
 
         if ('text' in state) {
@@ -680,7 +681,7 @@ SoundThing.prototype = {
     },
 
     getState : function() {
-        return { soundFilename: this._soundFilename,
+        return { uri: this._uri,
                  text: this._captionLabel.text };
     },
 
diff --git a/src/tb/tb-sound-player.c b/src/tb/tb-sound-player.c
index 8f4d3fa..9616213 100644
--- a/src/tb/tb-sound-player.c
+++ b/src/tb/tb-sound-player.c
@@ -20,7 +20,7 @@ enum
   PROP_STATE,
   PROP_PROGRESS,
   PROP_DURATION,
-  PROP_FILENAME
+  PROP_URI
 };
 
 struct _TbSoundPlayerPrivate
@@ -28,7 +28,7 @@ struct _TbSoundPlayerPrivate
   GstElement            *pipeline;
   GstBus                *bus;
   TbSoundPlayerState     state;
-  char                  *filename;
+  char                  *uri;
   gboolean               playing;
   GstState               stacked_state;
   gdouble                stacked_progress;
@@ -61,8 +61,8 @@ tb_sound_player_set_state (TbSoundPlayer      *player,
 }
 
 static void
-tb_sound_player_set_filename (TbSoundPlayer *player,
-                              const char    *filename)
+tb_sound_player_set_uri (TbSoundPlayer *player,
+                              const char    *uri)
 {
   TbSoundPlayerPrivate *priv;
 
@@ -70,19 +70,19 @@ tb_sound_player_set_filename (TbSoundPlayer *player,
 
   priv = TB_SOUND_PLAYER_GET_PRIVATE (player);
 
-  if (priv->filename &&
-      !strcmp (priv->filename, filename))
+  if (priv->uri &&
+      !strcmp (priv->uri, uri))
     return;
 
-  g_free (priv->filename);
-  priv->filename = g_strdup (filename);
+  g_free (priv->uri);
+  priv->uri = g_strdup (uri);
 
   if (priv->pipeline)
     tb_sound_player_destroy_pipeline (player);
 
   tb_sound_player_ensure_pipeline (player);
 
-  g_object_notify (G_OBJECT (player), "filename");
+  g_object_notify (G_OBJECT (player), "uri");
 }
 
 static void
@@ -412,7 +412,7 @@ tb_sound_player_ensure_pipeline (TbSoundPlayer *player)
   if (priv->pipeline)
     return TRUE;
 
-  if (priv->filename == NULL)
+  if (priv->uri == NULL)
     {
       tb_sound_player_set_state (player, TB_SOUND_PLAYER_STATE_ERROR);
       return FALSE;
@@ -420,8 +420,8 @@ tb_sound_player_ensure_pipeline (TbSoundPlayer *player)
 
   error = NULL;
 
-  pipeline_desc = g_strdup_printf("playbin uri=\"file://%s\"",
-                                  priv->filename);
+  pipeline_desc = g_strdup_printf("playbin uri=\"%s\"",
+                                  priv->uri);
 
   priv->pipeline = gst_parse_launch (pipeline_desc, &error);
 
@@ -574,8 +574,8 @@ tb_sound_player_get_property (GObject    *gobject,
       g_value_set_double (value, priv->duration);
       break;
 
-    case PROP_FILENAME:
-      g_value_set_string (value, priv->filename);
+    case PROP_URI:
+      g_value_set_string (value, priv->uri);
       break;
 
     default:
@@ -608,8 +608,8 @@ tb_sound_player_set_property (GObject      *gobject,
                                     g_value_get_double (value));
       break;
 
-    case PROP_FILENAME:
-      tb_sound_player_set_filename (player,
+    case PROP_URI:
+      tb_sound_player_set_uri (player,
                                     g_value_get_string (value));
       break;
 
@@ -674,10 +674,10 @@ tb_sound_player_class_init (TbSoundPlayerClass *klass)
 
   g_object_class_install_property
                  (gobject_class,
-                  PROP_FILENAME,
-                  g_param_spec_string ("filename",
-                                       "Filename",
-                                       "Filename to save sound to",
+                  PROP_URI,
+                  g_param_spec_string ("uri",
+                                       "URI",
+                                       "URI to play sound from",
                                        NULL,
                                        G_PARAM_READWRITE |
                                        G_PARAM_CONSTRUCT));
@@ -692,7 +692,7 @@ tb_sound_player_init (TbSoundPlayer *player)
 
   player->priv->state = TB_SOUND_PLAYER_STATE_UNKNOWN;
   player->priv->playing = FALSE;
-  player->priv->filename = NULL;
+  player->priv->uri = NULL;
   player->priv->pipeline = NULL;
   player->priv->bus = NULL;
   player->priv->stacked_progress = 0.0;
diff --git a/src/tb/tb-sound-recorder.c b/src/tb/tb-sound-recorder.c
index 83f78cd..8a0b08f 100644
--- a/src/tb/tb-sound-recorder.c
+++ b/src/tb/tb-sound-recorder.c
@@ -1,5 +1,6 @@
 #include <glib.h>
 #include <glib-object.h>
+#include <gio/gio.h>
 #include <gst/gst.h>
 
 #include "tb-enum-types.h"
@@ -15,7 +16,7 @@ enum
   PROP_0,
 
   PROP_STATE,
-  PROP_FILENAME,
+  PROP_URI,
   PROP_DURATION
 };
 
@@ -24,7 +25,7 @@ struct _TbSoundRecorderPrivate
   GstElement            *pipeline;
   GstBus                *bus;
   TbSoundRecorderState   state;
-  char                  *filename;
+  char                  *uri;
   gint                   duration;
   guint                  tick_timeout_id;
 };
@@ -50,27 +51,27 @@ tb_sound_recorder_set_state (TbSoundRecorder      *recorder,
 }
 
 static void
-tb_sound_recorder_set_filename (TbSoundRecorder *recorder,
-                                const char      *filename)
+tb_sound_recorder_set_uri (TbSoundRecorder *recorder,
+                                const char      *uri)
 {
   TbSoundRecorderPrivate *priv;
 
   g_return_if_fail (TB_IS_SOUND_RECORDER (recorder));
-  g_return_if_fail (filename != NULL);
+  g_return_if_fail (uri != NULL);
 
   priv = TB_SOUND_RECORDER_GET_PRIVATE (recorder);
 
-  if (priv->filename &&
-      !strcmp (priv->filename, filename))
+  if (priv->uri &&
+      !strcmp (priv->uri, uri))
     return;
 
-  g_free (priv->filename);
-  priv->filename = g_strdup (filename);
+  g_free (priv->uri);
+  priv->uri = g_strdup (uri);
 
   if (priv->pipeline)
     tb_sound_recorder_destroy_pipeline (recorder);
 
-  g_object_notify (G_OBJECT (recorder), "filename");
+  g_object_notify (G_OBJECT (recorder), "uri");
 }
 
 static void
@@ -240,15 +241,17 @@ static gboolean
 tb_sound_recorder_ensure_pipeline (TbSoundRecorder *recorder)
 {
   TbSoundRecorderPrivate *priv;
+  GFile *file;
   GError *error;
   gchar *pipeline_desc;
+  gchar *file_path;
 
   priv = TB_SOUND_RECORDER_GET_PRIVATE (recorder);
 
   if (priv->pipeline)
     return TRUE;
 
-  if (priv->filename == NULL)
+  if (priv->uri == NULL)
     {
       tb_sound_recorder_set_state (recorder, TB_SOUND_RECORDER_STATE_ERROR);
       return FALSE;
@@ -256,13 +259,19 @@ tb_sound_recorder_ensure_pipeline (TbSoundRecorder *recorder)
 
   error = NULL;
 
+  file = g_file_new_for_uri (priv->uri);
+  file_path = g_file_get_path (file);
+
   pipeline_desc = g_strdup_printf("autoaudiosrc name=source ! "
                                   "audioconvert ! "
                                   "audioresample ! "
                                   "vorbisenc ! "
                                   "oggmux ! "
                                   "filesink location=\"%s\"",
-                                  priv->filename);
+                                  file_path);
+
+  g_object_unref (file);
+  g_free (file_path);
 
   priv->pipeline = gst_parse_launch (pipeline_desc, &error);
 
@@ -338,8 +347,8 @@ tb_sound_recorder_get_property (GObject    *gobject,
       g_value_set_enum (value, priv->state);
       break;
 
-    case PROP_FILENAME:
-      g_value_set_string (value, priv->filename);
+    case PROP_URI:
+      g_value_set_string (value, priv->uri);
       break;
 
     case PROP_DURATION:
@@ -364,8 +373,8 @@ tb_sound_recorder_set_property (GObject      *gobject,
 
   switch (prop_id)
     {
-    case PROP_FILENAME:
-      tb_sound_recorder_set_filename (TB_SOUND_RECORDER (gobject),
+    case PROP_URI:
+      tb_sound_recorder_set_uri (TB_SOUND_RECORDER (gobject),
                                       g_value_get_string (value));
       break;
 
@@ -399,10 +408,10 @@ tb_sound_recorder_class_init (TbSoundRecorderClass *klass)
 
   g_object_class_install_property
                  (gobject_class,
-                  PROP_FILENAME,
-                  g_param_spec_string ("filename",
-                                       "Filename",
-                                       "Filename to save sound to",
+                  PROP_URI,
+                  g_param_spec_string ("uri",
+                                       "URI",
+                                       "URI to save sound to",
                                        NULL,
                                        G_PARAM_READWRITE |
                                        G_PARAM_CONSTRUCT));
@@ -427,7 +436,7 @@ tb_sound_recorder_init (TbSoundRecorder *recorder)
   recorder->priv = TB_SOUND_RECORDER_GET_PRIVATE (recorder);
 
   recorder->priv->state = TB_SOUND_RECORDER_STATE_UNKNOWN;
-  recorder->priv->filename = NULL;
+  recorder->priv->uri = NULL;
   recorder->priv->pipeline = NULL;
   recorder->priv->bus = NULL;
   recorder->priv->duration = 0;



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