]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/components/playlist/selector.cpp
Qt, selector: cleanup
[vlc] / modules / gui / qt4 / components / playlist / selector.cpp
index 42076f936359d971aacdc4eb15d3ba1aff7a4604..5b53c3bc67cb4c001c0952e67c4953ab35963d05 100644 (file)
 # include "config.h"
 #endif
 
-#include <assert.h>
-
-#include "components/playlist/selector.hpp"
-#include "playlist_item.hpp"
 #include "qt4.hpp"
+#include "components/playlist/selector.hpp"
+#include "playlist_model.hpp"                /* plMimeData */
+#include "input_manager.hpp"                 /* MainInputManager, for podcast */
 
-#include <QVBoxLayout>
-#include <QHeaderView>
+#include <QInputDialog>
+#include <QMessageBox>
 #include <QMimeData>
+#include <QDragMoveEvent>
+#include <QTreeWidgetItem>
+#include <QHBoxLayout>
+#include <QPainter>
 
 #include <vlc_playlist.h>
 #include <vlc_services_discovery.h>
 
+void SelectorActionButton::paintEvent( QPaintEvent *event )
+{
+    QPainter p( this );
+    QColor color = palette().color( QPalette::HighlightedText );
+    color.setAlpha( 80 );
+    if( underMouse() )
+        p.fillRect( rect(), color );
+    p.setPen( color );
+    int frame = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
+    p.drawLine( rect().topLeft() + QPoint( 0, frame ),
+                rect().bottomLeft() - QPoint( 0, frame ) );
+    QVLCFramelessButton::paintEvent( event );
+}
+
+PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
+    : qitem(i), lblAction( NULL)
+{
+    layout = new QHBoxLayout( this );
+    layout->setContentsMargins(0,0,0,0);
+    layout->addSpacing( 3 );
+
+    lbl = new QVLCElidingLabel( text );
+    layout->addWidget(lbl, 1);
+
+    int height = qMax( 22, fontMetrics().height() + 8 );
+    setMinimumHeight( height );
+}
+
+void PLSelItem::addAction( ItemAction act, const QString& tooltip )
+{
+    if( lblAction ) return; //might change later
+
+    QIcon icon;
+
+    switch( act )
+    {
+    case ADD_ACTION:
+        icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
+    case RM_ACTION:
+        icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
+    }
+
+    lblAction = new SelectorActionButton();
+    lblAction->setIcon( icon );
+    lblAction->setMinimumWidth( lblAction->sizeHint().width() + 6 );
+
+    if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
+
+    layout->addWidget( lblAction, 0 );
+    lblAction->hide();
+
+    CONNECT( lblAction, clicked(), this, triggerAction() );
+}
+
+
 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
            : QTreeWidget( p ), p_intf(_p_intf)
 {
+    /* Properties */
+    setFrameStyle( QFrame::NoFrame );
+    viewport()->setAutoFillBackground( false );
     setIconSize( QSize( 24,24 ) );
-//    view->setAlternatingRowColors( true );
-    setIndentation( 10 );
-    header()->hide();
-    setRootIsDecorated( false );
-//    model = new PLModel( THEPL, p_intf, THEPL->p_root_category, 1, this );
-//    view->setModel( model );
+    setIndentation( 12 );
+    setHeaderHidden( true );
+    setRootIsDecorated( true );
+    setAlternatingRowColors( false );
+
+    /* drops */
     viewport()->setAcceptDrops(true);
     setDropIndicatorShown(true);
     invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
 
+    /* Podcasts */
+    podcastsParent = NULL;
+    podcastsParentId = -1;
+
+    /* Podcast connects */
+    CONNECT( THEMIM, playlistItemAppended( int, int ),
+             this, plItemAdded( int, int ) );
+    CONNECT( THEMIM, playlistItemRemoved( int ),
+             this, plItemRemoved( int ) );
+    DCONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
+              this, inputItemUpdate( input_item_t * ) );
+
     createItems();
     CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
              this, setSource( QTreeWidgetItem *) );
+    CONNECT( this, itemClicked( QTreeWidgetItem *, int ),
+             this, setSource( QTreeWidgetItem *) );
+
     /* I believe this is unnecessary, seeing
        QStyle::SH_ItemView_ActivateItemOnSingleClick
         CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
@@ -65,6 +141,108 @@ PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
 //  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
 }
 
+PLSelector::~PLSelector()
+{
+    if( podcastsParent )
+    {
+        int c = podcastsParent->childCount();
+        for( int i = 0; i < c; i++ )
+        {
+            QTreeWidgetItem *item = podcastsParent->child(i);
+            input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
+            vlc_gc_decref( p_input );
+        }
+    }
+}
+
+PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
+{
+    item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
+    item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
+    return item;
+}
+
+PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
+{
+    item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
+/*    item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
+    item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
+    return item;
+}
+
+void PLSelector::createItems()
+{
+    /* PL */
+    PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, "Playlist", true ),
+                              THEPL->p_playing );
+    pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
+
+    /* ML */
+    PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, "Media Library", true ),
+                              THEPL->p_media_library );
+    ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
+
+    /* SD nodes */
+    QTreeWidgetItem *mycomp = addItem( CATEGORY_TYPE, "My Computer" )->treeItem();
+    QTreeWidgetItem *devices = addItem( CATEGORY_TYPE, "Devices" )->treeItem();
+    QTreeWidgetItem *lan = addItem( CATEGORY_TYPE, "Local Network" )->treeItem();
+    QTreeWidgetItem *internet = addItem( CATEGORY_TYPE, "Internet" )->treeItem();
+
+    /* SD subnodes */
+    char **ppsz_longnames;
+    int *p_categories;
+    char **ppsz_names = vlc_sd_GetNames( THEPL, &ppsz_longnames, &p_categories );
+    if( !ppsz_names )
+        return;
+
+    char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
+    int *p_category = p_categories;
+    for( ; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++ )
+    {
+        //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
+
+        PLSelItem *selItem;
+        switch( *p_category )
+        {
+        case SD_CAT_INTERNET:
+            {
+            selItem = addItem( SD_TYPE, *ppsz_longname, false, internet );
+            if( !strncmp( *ppsz_name, "podcast", 7 ) )
+            {
+                selItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
+                selItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
+                CONNECT( selItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
+                podcastsParent = selItem->treeItem();
+            }
+            }
+            break;
+        case SD_CAT_DEVICES:
+            selItem = addItem( SD_TYPE, *ppsz_longname, false, devices );
+            break;
+        case SD_CAT_LAN:
+            selItem = addItem( SD_TYPE, *ppsz_longname, false, lan );
+            break;
+        case SD_CAT_MYCOMPUTER:
+            selItem = addItem( SD_TYPE, *ppsz_longname, false, mycomp );
+            break;
+        default:
+            selItem = addItem( SD_TYPE, *ppsz_longname );
+        }
+
+        putSDData( selItem, *ppsz_name, *ppsz_longname );
+        free( *ppsz_name );
+        free( *ppsz_longname );
+    }
+    free( ppsz_names );
+    free( ppsz_longnames );
+    free( p_categories );
+
+    if( mycomp->childCount() == 0 ) delete mycomp;
+    if( devices->childCount() == 0 ) delete devices;
+    if( lan->childCount() == 0 ) delete lan;
+    if( internet->childCount() == 0 ) delete internet;
+}
+
 void PLSelector::setSource( QTreeWidgetItem *item )
 {
     if( !item )
@@ -72,118 +250,253 @@ void PLSelector::setSource( QTreeWidgetItem *item )
 
     bool b_ok;
     int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
-    if( !b_ok )
+    if( !b_ok || i_type == CATEGORY_TYPE )
         return;
 
-    assert( ( i_type == PL_TYPE || i_type == ML_TYPE || i_type == SD_TYPE ) );
+    bool sd_loaded;
     if( i_type == SD_TYPE )
     {
         QString qs = item->data( 0, NAME_ROLE ).toString();
-        if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) ) )
-        {
+        sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
+        if( !sd_loaded )
             playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
-#warning FIXME
-            playlist_item_t *pl_item =
-                    THEPL->p_root_category->pp_children[THEPL->p_root_category->i_children-1];
-            item->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( pl_item ) );
+    }
 
-            emit activated( pl_item );
-            return;
+    /* */
+    playlist_Lock( THEPL );
+    playlist_item_t *pl_item = NULL;
+
+    if( i_type == SD_TYPE )
+    {
+        /* Find the right item for the SD */
+        pl_item = playlist_ChildSearchName( THEPL->p_root,
+                      qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
+
+        /* Podcasts */
+        if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
+        {
+            if( pl_item && !sd_loaded )
+            {
+                podcastsParentId = pl_item->i_id;
+                for( int i=0; i < pl_item->i_children; i++ )
+                    addPodcastItem( pl_item->pp_children[i] );
+            }
+            pl_item = NULL; //to prevent activating it
         }
     }
+    else
+        pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
 
-    if( i_type == SD_TYPE )
-        msg_Dbg( p_intf, "SD already loaded, reloading" );
+    playlist_Unlock( THEPL );
 
-    playlist_item_t *pl_item =
-            item->data( 0, PPL_ITEM_ROLE ).value<playlist_item_t *>();
+    /* */
     if( pl_item )
-            emit activated( pl_item );
+        emit activated( pl_item );
 }
 
-void PLSelector::createItems()
+PLSelItem * PLSelector::addItem (
+    SelectorItemType type, const char* str, bool drop,
+    QTreeWidgetItem* parentItem )
 {
-    QTreeWidgetItem *pl = new QTreeWidgetItem( this );
-    pl->setText( 0, qtr( "Playlist" ) );
-    pl->setData( 0, TYPE_ROLE, PL_TYPE );
-    pl->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_local_category ) );
-/*  QTreeWidgetItem *empty = new QTreeWidgetItem( view );
-    empty->setFlags(Qt::NoItemFlags); */
+  QTreeWidgetItem *item = parentItem ?
+      new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
 
-    QTreeWidgetItem *lib = new QTreeWidgetItem( this );
-    lib->setText( 0, qtr( "Library" ) );
-    lib->setData( 0, TYPE_ROLE, ML_TYPE );
-    lib->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_ml_category ) );
+  PLSelItem *selItem = new PLSelItem( item, qtr( str ) );
+  setItemWidget( item, 0, selItem );
+  item->setData( 0, TYPE_ROLE, (int)type );
+  if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
 
-/*  QTreeWidgetItem *empty2 = new QTreeWidgetItem( view );
-    empty2->setFlags(Qt::NoItemFlags);*/
+  return selItem;
+}
 
-    QTreeWidgetItem *sds = new QTreeWidgetItem( this );
-    sds->setExpanded( true );
-    sds->setText( 0, qtr( "Libraries" ) );
-    sds->setFlags( sds->flags() & ~Qt::ItemIsDropEnabled );
+PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
+{
+    vlc_gc_incref( p_item->p_input );
 
-    char **ppsz_longnames;
-    char **ppsz_names = vlc_sd_GetNames( &ppsz_longnames );
-    if( !ppsz_names )
-        return;
+    char *psz_name = input_item_GetName( p_item->p_input );
+    PLSelItem *item = addItem( PL_ITEM_TYPE,  psz_name, false, podcastsParent );
+    free( psz_name );
 
-    char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
-    QTreeWidgetItem *sd_item;
-    for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
-    {
-        sd_item = new QTreeWidgetItem( QStringList( qfu(*ppsz_longname) ) );
-        sd_item->setData( 0, TYPE_ROLE, SD_TYPE );
-        sd_item->setData( 0, NAME_ROLE, qfu( *ppsz_name ) );
-        sd_item->setFlags( sd_item->flags() & ~Qt::ItemIsDropEnabled );
-        sds->addChild( sd_item );
-        free( *ppsz_name );
-        free( *ppsz_longname );
-    }
-    free( ppsz_names );
-    free( ppsz_longnames );
+    item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
+    item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
+    item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
+    item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
+    CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
+    return item;
 }
 
 QStringList PLSelector::mimeTypes() const
 {
     QStringList types;
-    types << "vlc/qt-playlist-item";
+    types << "vlc/qt-input-items";
     return types;
 }
 
 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
-  const QMimeData * data, Qt::DropAction action )
+    const QMimeData * data, Qt::DropAction action )
 {
     if( !parent ) return false;
 
     QVariant type = parent->data( 0, TYPE_ROLE );
     if( type == QVariant() ) return false;
-    int i_type = type.toInt();
-    if( i_type != PL_TYPE && i_type != ML_TYPE ) return false;
-    bool to_pl = i_type == PL_TYPE;
 
-    if( data->hasFormat( "vlc/qt-playlist-item" ) )
+    int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
+    if( i_truth != IS_PL && i_truth != IS_ML ) return false;
+
+    bool to_pl = ( i_truth == IS_PL );
+
+    const PlMimeData *plMimeData = qobject_cast<const PlMimeData*>( data );
+    if( !plMimeData ) return false;
+
+    QList<input_item_t*> inputItems = plMimeData->inputItems();
+
+    playlist_Lock( THEPL );
+
+    foreach( input_item_t *p_input, inputItems )
     {
-        QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
-        QDataStream stream( &encodedData, QIODevice::ReadOnly );
-        playlist_Lock( THEPL );
-        while( !stream.atEnd() )
+        playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
+        if( !p_item ) continue;
+
+        playlist_NodeAddCopy( THEPL, p_item,
+                              to_pl ? THEPL->p_playing : THEPL->p_media_library,
+                              PLAYLIST_END );
+    }
+
+    playlist_Unlock( THEPL );
+
+    return true;
+}
+
+void PLSelector::dragMoveEvent ( QDragMoveEvent * event )
+{
+    event->setDropAction( Qt::CopyAction );
+    QAbstractItemView::dragMoveEvent( event );
+}
+
+void PLSelector::plItemAdded( int item, int parent )
+{
+    if( parent != podcastsParentId ) return;
+
+    playlist_Lock( THEPL );
+
+    playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
+    if( !p_item ) {
+        playlist_Unlock( THEPL );
+        return;
+    }
+
+    int c = podcastsParent->childCount();
+    for( int i = 0; i < c; i++ )
+    {
+        QTreeWidgetItem *podItem = podcastsParent->child(i);
+        if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
         {
-            PLItem *item;
-            stream.readRawData( (char*)&item, sizeof(PLItem*) );
-            input_item_t *pl_input =item->inputItem();
-            playlist_AddExt ( THEPL,
-                pl_input->psz_uri, pl_input->psz_name,
-                PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
-                pl_input->i_duration,
-                pl_input->i_options, pl_input->ppsz_options, pl_input->optflagc,
-                to_pl, true );
+          //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
+          playlist_Unlock( THEPL );
+          return;
         }
-        playlist_Unlock( THEPL );
     }
-    return true;
+
+    //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
+    addPodcastItem( p_item );
+
+    playlist_Unlock( THEPL );
+
+    podcastsParent->setExpanded( true );
 }
 
-PLSelector::~PLSelector()
+void PLSelector::plItemRemoved( int id )
+{
+    int c = podcastsParent->childCount();
+    for( int i = 0; i < c; i++ )
+    {
+        QTreeWidgetItem *item = podcastsParent->child(i);
+        if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
+        {
+            input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
+            //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
+            vlc_gc_decref( p_input );
+            delete item;
+            return;
+        }
+    }
+}
+
+void PLSelector::inputItemUpdate( input_item_t *arg )
+{
+    int c = podcastsParent->childCount();
+    for( int i = 0; i < c; i++ )
+    {
+        QTreeWidgetItem *item = podcastsParent->child(i);
+        input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
+        if( p_input == arg )
+        {
+            PLSelItem *si = itemWidget( item );
+            char *psz_name = input_item_GetName( p_input );
+            si->setText( qfu( psz_name ) );
+            free( psz_name );
+            return;
+        }
+    }
+}
+
+void PLSelector::podcastAdd( PLSelItem* item )
+{
+    bool ok;
+    QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
+                                         qtr( "Enter URL of the podcast to subscribe to:" ),
+                                         QLineEdit::Normal, QString(), &ok );
+    if( !ok || url.isEmpty() ) return;
+
+    setSource( podcastsParent ); //to load the SD in case it's not loaded
+
+    vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
+        p_intf->p_libvlc, "podcast", FIND_CHILD );
+    if( !p_obj ) return;
+
+    QString request("ADD:");
+    request += url.trimmed();
+    var_SetString( p_obj, "podcast-request", qtu( request ) );
+    vlc_object_release( p_obj );
+}
+
+void PLSelector::podcastRemove( PLSelItem* item )
+{
+    QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
+    question = question.arg( item->text() );
+    QMessageBox::StandardButton res =
+        QMessageBox::question( this, qtr( "Unsubscribe" ), question,
+                               QMessageBox::Ok | QMessageBox::Cancel,
+                               QMessageBox::Cancel );
+    if( res == QMessageBox::Cancel ) return;
+
+    input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
+    if( !input ) return;
+
+    vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
+        p_intf->p_libvlc, "podcast", FIND_CHILD );
+    if( !p_obj ) return;
+
+    QString request("RM:");
+    char *psz_uri = input_item_GetURI( input );
+    request += qfu( psz_uri );
+    var_SetString( p_obj, "podcast-request", qtu( request ) );
+    vlc_object_release( p_obj );
+    free( psz_uri );
+}
+
+PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
+{
+    return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
+}
+
+void PLSelector::drawBranches ( QPainter * painter, const QRect & rect, const QModelIndex & index ) const
 {
+    if( !model()->hasChildren( index ) ) return;
+    QStyleOption option;
+    option.initFrom( this );
+    option.rect = rect.adjusted( rect.width() - indentation(), 0, 0, 0 );
+    style()->drawPrimitive( isExpanded( index ) ?
+                            QStyle::PE_IndicatorArrowDown :
+                            QStyle::PE_IndicatorArrowRight, &option, painter );
 }