[gnome-music] Use filename of tracks as title if not available



commit fd17bfae7076b880a604fa3ddf14a9f8ee2cb6bf
Author: Arnel A. Borja <arnelborja src gnome org>
Date:   Thu Jul 4 19:48:23 2013 +0800

    Use filename of tracks as title if not available
    
    https://bugzilla.gnome.org/show_bug.cgi?id=701427

 src/albumArtCache.js |   25 +++++++++++++++++++++++++
 src/player.js        |   11 +----------
 src/view.js          |   13 +------------
 src/widgets.js       |   19 ++++++++++---------
 4 files changed, 37 insertions(+), 31 deletions(-)
---
diff --git a/src/albumArtCache.js b/src/albumArtCache.js
index 4fb1dc1..4599ca6 100644
--- a/src/albumArtCache.js
+++ b/src/albumArtCache.js
@@ -29,9 +29,34 @@ const Regex = GLib.Regex;
 const Path = GLib.Path;
 const Grl = imports.gi.Grl;
 
+const Gettext = imports.gettext;
+const _ = imports.gettext.gettext;
+
 const Grilo = imports.grilo;
 const grilo = Grilo.grilo;
 
+const InvalidChars = /[()<>\[\]{}_! #$^&*+=|\\\/\"'?~]/g;
+const ReduceSpaces = /\t|\s+/g;
+
+function getMediaTitle(media, escaped=false) {
+    let title = media.get_title();
+    if (title)
+        return title;
+
+    let uri = media.get_url();
+    if (!uri)
+        return _("Untitled");
+
+    let file = Gio.File.new_for_path(uri),
+        basename = file.get_basename();
+
+    title = GLib.uri_unescape_string(basename, null);
+    if (escaped)
+        return GLib.markup_escape_text(title, -1);
+
+    return title;
+}
+
 const AlbumArtCache = new Lang.Class({
     Name: "AlbumArtCache",
     Extends: GLib.Object,
diff --git a/src/player.js b/src/player.js
index b763c52..3103072 100644
--- a/src/player.js
+++ b/src/player.js
@@ -322,16 +322,7 @@ const Player = new Lang.Class({
                 }
             }));
 
-        if (media.get_title() != null) {
-            this.titleLabel.set_label(media.get_title());
-        } else {
-            let url = media.get_url(),
-                file = GLib.File.new_for_path(url),
-                basename = file.get_basename(),
-                toShow = GLib.Uri.unescape_string(basename, null);
-
-            this.titleLabel.set_label(toShow);
-        }
+        this.titleLabel.set_label(AlbumArtCache.getMediaTitle(media));
 
         if (media.get_artist() != null)
             this.artistLabel.set_label(media.get_artist());
diff --git a/src/view.js b/src/view.js
index 647b0dd..73c937e 100644
--- a/src/view.js
+++ b/src/view.js
@@ -45,11 +45,6 @@ const nowPlayingIconName = 'media-playback-start-symbolic';
 const errorIconName = 'dialog-error-symbolic';
 const starIconName = 'starred-symbolic';
 
-function extractFileName(uri) {
-    var exp = /^.*[\\\/]|[.][^.]*$/g;
-    return unescape(uri.replace(exp, ''));
-}
-
 const albumArtCache = AlbumArtCache.AlbumArtCache.getDefault();
 const grilo = Grilo.grilo;
 
@@ -212,9 +207,6 @@ const ViewContainer = new Lang.Class({
                 artist = item.get_author();
             if (item.get_string(Grl.METADATA_KEY_ARTIST) != null)
                 artist = item.get_string(Grl.METADATA_KEY_ARTIST)
-            if ((item.get_title() == null) && (item.get_url() != null)) {
-                item.set_title (extractFileName(item.get_url()));
-            }
             try{
                 if (item.get_url())
                     this.player.discoverer.discover_uri(item.get_url());
@@ -371,9 +363,6 @@ const Songs = new Lang.Class({
         if (item != null) {
             this._offset += 1;
             var iter = this._model.append();
-            if ((item.get_title() == null) && (item.get_url() != null)) {
-                item.set_title (extractFileName(item.get_url()));
-            }
             try{
                 if (item.get_url())
                     this.player.discoverer.discover_uri(item.get_url());
@@ -415,7 +404,7 @@ const Songs = new Lang.Class({
             titleRenderer.yalign = 0.5;
             titleRenderer.height = 48;
             titleRenderer.ellipsize = Pango.EllipsizeMode.END;
-            titleRenderer.text = item.get_title();
+            titleRenderer.text = AlbumArtCache.getMediaTitle(item);
         }))
         let starRenderer = new Gtk.CellRendererPixbuf({xpad: 32});
         listWidget.add_renderer(starRenderer,Lang.bind(this,function (col,cell,model,iter) {
diff --git a/src/widgets.js b/src/widgets.js
index 122a45f..4109f07 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -132,7 +132,8 @@ const AlbumWidget = new Lang.Class({
                 if (this.model.get_value(iter, 7) != errorIconName) {
                     if (this.iterToClean && this.player.playlistId == this.album){
                         let item = this.model.get_value(this.iterToClean, 5);
-                        this.model.set_value(this.iterToClean, 0, item.get_title());
+                        let title = AlbumArtCache.getMediaTitle(item);
+                        this.model.set_value(this.iterToClean, 0, title);
                         // Hide now playing icon
                         this.model.set_value(this.iterToClean, 6, false);
                     }
@@ -249,7 +250,7 @@ const AlbumWidget = new Lang.Class({
                     tracks.push(track);
                     duration = duration + track.get_duration();
                     let iter = this.model.append();
-                    let escapedTitle = GLib.markup_escape_text(track.get_title(), -1);
+                    let escapedTitle = AlbumArtCache.getMediaTitle(track, true);
                     try{
                         this.player.discoverer.discover_uri(track.get_url());
                         this.model.set(iter,
@@ -323,7 +324,7 @@ const AlbumWidget = new Lang.Class({
         do{
             let song = playlist.get_value(iter, 5);
 
-            let escapedTitle = GLib.markup_escape_text(song.get_title(), -1);
+            let escapedTitle = AlbumArtCache.getMediaTitle(song, true);
             if (song == currentSong){
                 title = "<b>" + escapedTitle + "</b>";
                 iconVisible = true;
@@ -413,7 +414,7 @@ const ArtistAlbums = new Lang.Class({
             if (!songWidget.can_be_played)
                 continue;
 
-            let escapedTitle = GLib.markup_escape_text(song.get_title(), -1);
+            let escapedTitle = AlbumArtCache.getMediaTitle(song, true);
             if (song == currentSong){
                 songWidget.nowPlayingSign.show();
                 songWidget.title.set_markup("<b>" + escapedTitle + "</b>");
@@ -436,7 +437,7 @@ const ArtistAlbums = new Lang.Class({
         do{
             let song = this.model.get_value(iter, 5);
             let songWidget = song.songWidget;
-            let escapedTitle = GLib.markup_escape_text(song.get_title(), -1);
+            let escapedTitle = AlbumArtCache.getMediaTitle(song, true);
             if (songWidget.can_be_played)
                 songWidget.nowPlayingSign.hide();
             songWidget.title.set_markup("<span>" + escapedTitle + "</span>");
@@ -568,8 +569,8 @@ const ArtistAlbumWidget = new Lang.Class({
                     var songWidget = ui.get_object("eventbox1");
                     this.songs.push(songWidget);
                     ui.get_object("num").set_markup("<span 
color='grey'>"+this.songs.length.toString()+"</span>");
-                    if (track.get_title() != null)
-                        ui.get_object("title").set_text(track.get_title());
+                    let title = AlbumArtCache.getMediaTitle(track);
+                    ui.get_object('title').set_text(title);
                     //var songWidget = ui.get_object("duration").set_text(track.get_title());
                     ui.get_object("title").set_alignment(0.0, 0.5);
                     this.ui.get_object("grid1").attach(songWidget,
@@ -585,7 +586,7 @@ const ArtistAlbumWidget = new Lang.Class({
                         this.player.discoverer.discover_uri(track.get_url());
                         model.set(iter,
                             [0, 1, 2, 3, 4, 5],
-                            [ track.get_title(), "", "", false, nowPlayingIconName, track]);
+                            [ title, '', '', false, nowPlayingIconName, track]);
                         songWidget.nowPlayingSign = ui.get_object("image1");
                         songWidget.nowPlayingSign.set_from_icon_name(nowPlayingIconName, 
Gtk.IconSize.SMALL_TOOLBAR);
                         songWidget.nowPlayingSign.set_no_show_all("true");
@@ -598,7 +599,7 @@ const ArtistAlbumWidget = new Lang.Class({
                         log("failed to discover url " + track.get_url());
                         this.model.set(iter,
                             [0, 1, 2, 3, 4, 5],
-                            [ track.get_title(), "", "", true, errorIconName, track ]);
+                            [ title, '', '', true, errorIconName, track ]);
                         songWidget.nowPlayingSign = ui.get_object("image1");
                         songWidget.nowPlayingSign.set_from_icon_name(errorIconName, 
Gtk.IconSize.SMALL_TOOLBAR);
                         songWidget.nowPlayingSign.set_alignment(0.0,0.6);


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