Gets title string from filename if it cannot be obtained from tags
[nowplayingd] / nowplaying.c
index 8dfbe8d..8037e73 100644 (file)
@@ -28,33 +28,49 @@ static void sig_handler( int sig G_GNUC_UNUSED )
        }
 }
 
-static gboolean close_notification( gpointer n )
+static gchar* filename_from_object_id(
+       const gchar *object_id)
 {
-       notify_notification_close( NOTIFY_NOTIFICATION( n ), NULL );
+       /* TODO: Strip off the extension */
+       g_return_val_if_fail (object_id, NULL);
 
-       return FALSE;
+       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" );
@@ -66,9 +82,11 @@ static void metadata_callback(
                "open_mp_now_playing",
                G_TYPE_NONE, NULL,
                -1 );
-               
+
+       notify_notification_set_timeout( NOTIFY_NOTIFICATION( n ), 4500 /* 4.5s */ );
        notify_notification_show( NOTIFY_NOTIFICATION( n ), NULL );
-       g_timeout_add_seconds( 5, close_notification, n );
+
+       g_free ( title );
 }
 
 static void state_changed_cb(
@@ -89,6 +107,20 @@ static void state_changed_cb(
   }
 }
 
+static void renderer_removed_cb(
+       const MafwRegistry *registry2 G_GNUC_UNUSED,
+       GObject *renderer,
+       gconstpointer user_data G_GNUC_UNUSED)
+{
+       if ( MAFW_IS_RENDERER( renderer ) ) {
+               const gchar *name = mafw_extension_get_name( MAFW_EXTENSION( renderer ) );
+
+               if ( !strcmp( name, WANTED_RENDERER ) ) {
+                       g_signal_handlers_disconnect_by_func( renderer, G_CALLBACK( state_changed_cb ), NULL );
+               }
+       }
+}
+
 static void renderer_added_cb(
        const MafwRegistry *registry2 G_GNUC_UNUSED,
        GObject *renderer,
@@ -103,6 +135,18 @@ static void renderer_added_cb(
        }
 }
 
+static void mafw_deinit( void )
+{
+    GList *renderers = NULL;
+
+       g_signal_handlers_disconnect_by_func( registry, G_CALLBACK( renderer_removed_cb ), NULL );
+       renderers = mafw_registry_get_renderers( registry );
+       while ( renderers ) {
+               renderer_removed_cb( registry, G_OBJECT( renderers->data ), NULL );
+               renderers = g_list_next( renderers );
+       }
+}
+
 static void mafw_init( void )
 {
     GList *renderers = NULL;
@@ -140,8 +184,12 @@ int main (void)
 
        g_main_loop_unref( loop );
 
+       mafw_deinit();
+
        mafw_shared_deinit();
 
+       notify_uninit();
+
        return EXIT_SUCCESS;
 }