From b027124a87071401713f3b1dd9a28a8a56b90617 Mon Sep 17 00:00:00 2001 From: faheem Date: Tue, 2 Mar 2010 16:12:07 +0000 Subject: [PATCH] Gets title string from filename if it cannot be obtained from tags --- nowplaying.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/nowplaying.c b/nowplaying.c index ff0f811..8037e73 100644 --- a/nowplaying.c +++ b/nowplaying.c @@ -28,26 +28,49 @@ static void sig_handler( int sig G_GNUC_UNUSED ) } } +static gchar* filename_from_object_id( + const gchar *object_id) +{ + /* TODO: Strip off the extension */ + g_return_val_if_fail (object_id, NULL); + + gchar *path_uri = g_filename_display_basename( object_id ); /* Remove "localtagfs::music/songs". Pointer arith. would probably work, but I don't wan't to hardcode anything. */ + gchar *path_unescaped = g_uri_unescape_string( path_uri, NULL ); /* Make the string look normal */ + gchar *ret_filename = g_filename_display_basename( path_unescaped ); /* Get the filename w/out path */ + + g_free (path_uri); + g_free (path_unescaped); + + return ret_filename ? ret_filename : NULL; +} + static void metadata_callback( const MafwRenderer *self G_GNUC_UNUSED, - const gchar *object_id G_GNUC_UNUSED, + const gchar *object_id, GHashTable *metadata, gconstpointer user_data G_GNUC_UNUSED, const GError *error G_GNUC_UNUSED ) { + gchar *title = NULL; const gchar *artist = NULL; - const gchar *title = NULL; HildonNotification *n = NULL; - artist = g_value_get_string( mafw_metadata_first( metadata, MAFW_METADATA_KEY_ARTIST ) ); - title = g_value_get_string( mafw_metadata_first( metadata, MAFW_METADATA_KEY_TITLE ) ); + /* First: Try to get title and artist strings from tags: */ + title = g_value_dup_string( mafw_metadata_first ( metadata, MAFW_METADATA_KEY_TITLE ) ); + artist = g_value_get_string( mafw_metadata_first ( metadata, MAFW_METADATA_KEY_ARTIST ) ); - if ( !artist || *artist == '\0' ) /* || !strcmp (artist, "")) */ { - artist = dgettext( "mediaplayer", "mp_li_unknown_artist" ); + /* Secondly: If getting the title from the tags fails, fallback to the filename, instead: */ + if ( !title || *title == '\0' ) /* || !strcmp (title, "")) */ { + title = filename_from_object_id ( object_id ); } + /* Thirdly: If above fails, we just resort to "(unknown song)" */ if ( !title || *title == '\0' ) /* || !strcmp (title, "")) */ { - title = dgettext( "mediaplayer", "mp_li_unknown_song" ); + title = g_strdup( dgettext( "mediaplayer", "mp_li_unknown_song" ) ); + } + + if ( !artist || *artist == '\0' ) /* || !strcmp (artist, "")) */ { + artist = g_dgettext( "mediaplayer", "mp_li_unknown_artist" ); } n = hildon_notification_new( artist, title, "tasklaunch_media_player", "media" ); @@ -62,6 +85,8 @@ static void metadata_callback( notify_notification_set_timeout( NOTIFY_NOTIFICATION( n ), 4500 /* 4.5s */ ); notify_notification_show( NOTIFY_NOTIFICATION( n ), NULL ); + + g_free ( title ); } static void state_changed_cb( -- 1.7.9.5