[vlc-devel] [PATCH] Added playlist total duration.

Olafs Vandāns lunaroverlord at gmail.com
Thu Nov 15 09:01:22 CET 2012


playlist_GetNodeDuration(node) calculates the duration of a playlist
node by summing the durations of its children. This is employed in Qt,
in PLSelector::updateTotalDuration to show the total playing time in the
treeView control with the "Playlist" option. This function is called
when items are added (::plItemAdded), removed(plItemRemoved) or
updated (::inputItemUpdate). This isn't done for the macosx gui.
---
 include/vlc_playlist.h                           |    4 +++
 modules/gui/qt4/components/playlist/selector.cpp |   39 ++++++++++++++++++++--
 modules/gui/qt4/components/playlist/selector.hpp |    4 +++
 src/libvlccore.sym                               |    1 +
 src/playlist/item.c                              |   14 ++++++++
 5 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/include/vlc_playlist.h b/include/vlc_playlist.h
index ea269a2..2bb39a8 100644
--- a/include/vlc_playlist.h
+++ b/include/vlc_playlist.h
@@ -283,6 +283,10 @@ VLC_API int playlist_Control( playlist_t *p_playlist, int i_query, bool b_locked
  */
 VLC_API input_thread_t * playlist_CurrentInput( playlist_t *p_playlist ) VLC_USED;
 
+/** Get the duration of all items in a node.
+ */
+VLC_API mtime_t playlist_GetNodeDuration( playlist_item_t * );
+
 /** Clear the playlist
  * \param b_locked TRUE if playlist is locked when entering this function
  */
diff --git a/modules/gui/qt4/components/playlist/selector.cpp b/modules/gui/qt4/components/playlist/selector.cpp
index e948eca..863fcc5 100644
--- a/modules/gui/qt4/components/playlist/selector.cpp
+++ b/modules/gui/qt4/components/playlist/selector.cpp
@@ -188,13 +188,42 @@ PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
     return item;
 }
 
+/*
+ * Reads and updates the playlist's duration as [xx:xx] after the label in the tree
+ * item - the treeview item to get the duration for
+ * prefix - the string to use before the time (should be the category name)
+ */
+void PLSelector::updateTotalDuration( PLSelItem* item, const char* prefix )
+{
+    /* Getting  the playlist */
+    QVariant playlistVariant = item->treeItem()->data( 0, PL_ITEM_ROLE );
+    playlist_item_t* node = playlistVariant.value<playlist_item_t*>();
+
+    /* Formatting time */
+    QString qs_timeLabel( prefix );
+    mtime_t mt_duration = playlist_GetNodeDuration( node );
+    int i_seconds = mt_duration / 1000000;
+    int i_minutes = i_seconds / 60;
+    i_seconds = i_seconds % 60;
+    if( i_minutes >= 60 )
+    {
+        int i_hours = i_minutes / 60;
+        i_minutes = i_minutes % 60;
+        qs_timeLabel += QString(" [%1:%2:%3]").arg( i_hours ).arg( i_minutes, 2, 10, QChar('0') ).arg( i_seconds, 2, 10, QChar('0') );
+    }
+    else
+        qs_timeLabel += QString( " [%1:%2]").arg( i_minutes, 2, 10, QChar('0') ).arg( i_seconds, 2, 10, QChar('0') );
+
+    item->setText( qs_timeLabel );
+}
+
 void PLSelector::createItems()
 {
     /* PL */
-    PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, N_("Playlist"), true ),
+    playlistItem = putPLData( addItem( PL_ITEM_TYPE, N_("Playlist"), true ),
                               THEPL->p_playing );
-    pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
-    setCurrentItem( pl->treeItem() );
+    playlistItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
+    setCurrentItem( playlistItem->treeItem() );
 
     /* ML */
     PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, N_("Media Library"), true ),
@@ -426,6 +455,7 @@ void PLSelector::dragMoveEvent ( QDragMoveEvent * event )
 
 void PLSelector::plItemAdded( int item, int parent )
 {
+    updateTotalDuration(playlistItem, "Playlist");
     if( parent != podcastsParentId || podcastsParent == NULL ) return;
 
     playlist_Lock( THEPL );
@@ -458,6 +488,7 @@ void PLSelector::plItemAdded( int item, int parent )
 
 void PLSelector::plItemRemoved( int id )
 {
+    updateTotalDuration(playlistItem, "Playlist");
     if( !podcastsParent ) return;
 
     int c = podcastsParent->childCount();
@@ -477,6 +508,8 @@ void PLSelector::plItemRemoved( int id )
 
 void PLSelector::inputItemUpdate( input_item_t *arg )
 {
+    updateTotalDuration(playlistItem, "Playlist");
+
     if( podcastsParent == NULL )
         return;
 
diff --git a/modules/gui/qt4/components/playlist/selector.hpp b/modules/gui/qt4/components/playlist/selector.hpp
index 68db810..7ec7639 100644
--- a/modules/gui/qt4/components/playlist/selector.hpp
+++ b/modules/gui/qt4/components/playlist/selector.hpp
@@ -133,6 +133,10 @@ private:
             bool drop = false, QTreeWidgetItem* parentItem = 0 );
     PLSelItem * addPodcastItem( playlist_item_t *p_item );
 
+    PLSelItem* playlistItem;
+
+    void updateTotalDuration(PLSelItem*, const char*);
+
     inline PLSelItem * itemWidget( QTreeWidgetItem * );
 
     intf_thread_t    *p_intf;
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index a3cfbd4..652a022 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -328,6 +328,7 @@ playlist_DeleteFromInput
 playlist_Export
 playlist_GetNextLeaf
 playlist_GetPrevLeaf
+playlist_GetNodeDuration
 playlist_Import
 playlist_IsServicesDiscoveryLoaded
 playlist_ItemGetById
diff --git a/src/playlist/item.c b/src/playlist/item.c
index 2b99dce..476a797 100644
--- a/src/playlist/item.c
+++ b/src/playlist/item.c
@@ -716,6 +716,20 @@ void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
     var_SetAddress( p_playlist, "playlist-item-append", &add );
 }
 
+/**
+ * Get the duration of all items in a node.
+ */
+mtime_t playlist_GetNodeDuration( playlist_item_t* node )
+{
+    mtime_t mt_duration = 0;
+
+    if( node->i_children != -1 )
+        for( int i = 0; i < node->i_children; i++ )
+            mt_duration += input_item_GetDuration( node->pp_children[i]->p_input );
+
+    return mt_duration;
+}
+
 /***************************************************************************
  * The following functions are local
  ***************************************************************************/
-- 
1.7.10.4




More information about the vlc-devel mailing list