From cf7ad4645eddcc55176670665de03a7ca4aaf48c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Filip=20Ros=C3=A9en?= <filip@videolabs.io>
Date: Mon, 18 Jul 2016 15:01:29 +0200
Subject: [PATCH 1/1] gui/qt: fix #17184 (missing entries in context-menu)

This patch fixes the regression where certain entities on the
context-menu when dealing with the playlist are not displayed if you
right-click somewhere where there is no item:

    - https://trac.videolan.org/vlc/ticket/17184

Actions that are not bound to a single entity are now displayed
correctly (vlc 2.2.4 has been used as reference), with two minor
differences:

    * ACTION_PAUSE will now be shown if the current item is being
      played, and;

    * ACTION_PLAY will not show if the item is already playing.

refs #17184
---
 .../gui/qt/components/playlist/playlist_model.cpp  | 129 +++++++++++++--------
 1 file changed, 83 insertions(+), 46 deletions(-)

diff --git a/modules/gui/qt/components/playlist/playlist_model.cpp b/modules/gui/qt/components/playlist/playlist_model.cpp
index 3f03209..863a380 100644
--- a/modules/gui/qt/components/playlist/playlist_model.cpp
+++ b/modules/gui/qt/components/playlist/playlist_model.cpp
@@ -1001,57 +1001,94 @@ bool PLModel::action( QAction *action, const QModelIndexList &indexes )
 
 bool PLModel::isSupportedAction( actions action, const QModelIndex &index ) const
 {
-    if( !index.isValid() )
-        return false;
+    vlc_playlist_locker pl_lock ( THEPL );
 
-    const PLItem *item = getItem( index );
+    bool const b_readonly = THEPL->p_root->i_flags & PLAYLIST_RO_FLAG;
+    AbstractPLItem * item = VLCModel::getItem( index );
 
-    switch ( action )
-    {
-    case ACTION_ADDTOPLAYLIST:
-        /* Only if we are not already in Current Playing */
-        if ( getPLRootType() == ROOTTYPE_CURRENT_PLAYING ) return false;
-        if( index != rootIndex() )
-            return ( item->id( PLAYLIST_ID ) != THEPL->p_playing->i_id );
-    case ACTION_SORT:
-        return rowCount() && !item->readOnly();
-    case ACTION_PLAY:
+    switch( action )
     {
-        PL_LOCK;
-        bool b_ret = !isCurrent( index ) || playlist_Status(THEPL) == PLAYLIST_PAUSED;
-        PL_UNLOCK;
-        return b_ret;
-    }
-    case ACTION_PAUSE:
-    {
-        PL_LOCK;
-        bool b_ret = isCurrent( index ) && playlist_Status(THEPL) == PLAYLIST_RUNNING;
-        PL_UNLOCK;
-        return b_ret;
-    }
-    case ACTION_STREAM:
-    case ACTION_SAVE:
-    case ACTION_INFO:
-        return index != rootIndex();
-    case ACTION_REMOVE:
-        return index != rootIndex() && !item->readOnly();
-    case ACTION_EXPLORE:
+        case ACTION_ADDTOPLAYLIST:
+        {
+            if( getPLRootType() == ROOTTYPE_CURRENT_PLAYING )
+                return false;
+
+            if( index == rootIndex() )
+                return false;
+
+            return item && item->id( PLAYLIST_ID ) != THEPL->p_playing->i_id;
+        }
+        case ACTION_SORT:
+        {
+           return rowCount() && b_readonly;
+        }
+        case ACTION_PLAY:
+        {
+            if( playlist_Status( THEPL ) == PLAYLIST_RUNNING
+              && isCurrent( index ) )
+            {
+                return false;
+            }
+
+            return index.isValid();
+        }
+        case ACTION_PAUSE:
+        {
+            if( !isCurrent( index ) )
+                return false;
+
+            return playlist_Status( THEPL ) == PLAYLIST_RUNNING;
+        }
+        case ACTION_STREAM:
+        case ACTION_SAVE:
+        case ACTION_INFO:
+        {
+            return index != rootIndex();
+        }
+        case ACTION_REMOVE:
+        {
+            if( index == rootIndex() )
+                return false;
+
+            return item && !item->readOnly();
+        }
+        case ACTION_EXPLORE:
+        {
             return getURI( index ).startsWith( "file://" );
-    case ACTION_CREATENODE:
-            return ( isTree() && !item->readOnly() );
-    case ACTION_RENAMENODE:
-            return ( index != rootIndex() ) && !isLeaf( index ) && !item->readOnly();
-    case ACTION_CLEAR:
-            return rowCount() && !item->readOnly();
-    case ACTION_ENQUEUEFILE:
-    case ACTION_ENQUEUEDIR:
-    case ACTION_ENQUEUEGENERIC:
-        return !item->readOnly();
-    case ACTION_SAVETOPLAYLIST:
-        return rowCount() > 0;
-    default:
-        return false;
+        }
+        case ACTION_CREATENODE:
+        {
+            return isTree() && !b_readonly;
+        }
+        case ACTION_RENAMENODE:
+        {
+            if( index == rootIndex() || item == NULL || item->readOnly() )
+                return false;
+
+            input_item_t* p_iitem = item->inputItem();
+
+            if( p_iitem == NULL )
+                return false;
+
+            return p_iitem->i_type == ITEM_TYPE_NODE
+                || p_iitem->i_type == ITEM_TYPE_DIRECTORY;
+        }
+        case ACTION_CLEAR:
+        {
+            return rowCount() && !b_readonly;
+        }
+        case ACTION_ENQUEUEGENERIC:
+        case ACTION_ENQUEUEFILE:
+        case ACTION_ENQUEUEDIR:
+        {
+            return !b_readonly;
+        }
+        case ACTION_SAVETOPLAYLIST:
+        {
+            return rowCount();
+        }
     }
+
     return false;
 }
 
-- 
2.9.0

