1 /*****************************************************************************
2 * selector.cpp : Playlist source selector
3 ****************************************************************************
4 * Copyright (C) 2006-2009 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
31 #include "components/playlist/selector.hpp"
32 #include "playlist_item.hpp"
35 #include <QVBoxLayout>
36 #include <QHeaderView>
39 #include <vlc_playlist.h>
40 #include <vlc_services_discovery.h>
42 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
43 : QTreeWidget( p ), p_intf(_p_intf)
45 setIconSize( QSize( 24,24 ) );
46 // view->setAlternatingRowColors( true );
49 setRootIsDecorated( false );
50 // model = new PLModel( THEPL, p_intf, THEPL->p_root_category, 1, this );
51 // view->setModel( model );
52 viewport()->setAcceptDrops(true);
53 setDropIndicatorShown(true);
54 invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
57 CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
58 this, setSource( QTreeWidgetItem *) );
59 /* I believe this is unnecessary, seeing
60 QStyle::SH_ItemView_ActivateItemOnSingleClick
61 CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
62 this, setSource( QTreeWidgetItem *) ); */
64 /* select the first item */
65 // view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
68 void PLSelector::setSource( QTreeWidgetItem *item )
74 int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
78 assert( ( i_type == PL_TYPE || i_type == ML_TYPE || i_type == SD_TYPE ) );
79 if( i_type == SD_TYPE )
81 QString qs = item->data( 0, NAME_ROLE ).toString();
82 if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) ) )
84 playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
86 playlist_item_t *pl_item =
87 THEPL->p_root_category->pp_children[THEPL->p_root_category->i_children-1];
88 item->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( pl_item ) );
90 emit activated( pl_item );
95 if( i_type == SD_TYPE )
96 msg_Dbg( p_intf, "SD already loaded, reloading" );
98 playlist_item_t *pl_item =
99 item->data( 0, PPL_ITEM_ROLE ).value<playlist_item_t *>();
101 emit activated( pl_item );
104 void PLSelector::createItems()
106 QTreeWidgetItem *pl = new QTreeWidgetItem( this );
107 pl->setText( 0, qtr( "Playlist" ) );
108 pl->setData( 0, TYPE_ROLE, PL_TYPE );
109 pl->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_local_category ) );
110 /* QTreeWidgetItem *empty = new QTreeWidgetItem( view );
111 empty->setFlags(Qt::NoItemFlags); */
113 QTreeWidgetItem *lib = new QTreeWidgetItem( this );
114 lib->setText( 0, qtr( "Library" ) );
115 lib->setData( 0, TYPE_ROLE, ML_TYPE );
116 lib->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_ml_category ) );
118 /* QTreeWidgetItem *empty2 = new QTreeWidgetItem( view );
119 empty2->setFlags(Qt::NoItemFlags);*/
121 QTreeWidgetItem *sds = new QTreeWidgetItem( this );
122 sds->setExpanded( true );
123 sds->setText( 0, qtr( "Libraries" ) );
124 sds->setFlags( sds->flags() & ~Qt::ItemIsDropEnabled );
126 char **ppsz_longnames;
127 char **ppsz_names = vlc_sd_GetNames( &ppsz_longnames );
131 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
132 QTreeWidgetItem *sd_item;
133 for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
135 sd_item = new QTreeWidgetItem( QStringList( *ppsz_longname ) );
136 sd_item->setData( 0, TYPE_ROLE, SD_TYPE );
137 sd_item->setData( 0, NAME_ROLE, qfu( *ppsz_name ) );
138 sd_item->setFlags( sd_item->flags() & ~Qt::ItemIsDropEnabled );
139 sds->addChild( sd_item );
143 QStringList PLSelector::mimeTypes() const
146 types << "vlc/qt-playlist-item";
150 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
151 const QMimeData * data, Qt::DropAction action )
153 if( !parent ) return false;
155 QVariant type = parent->data( 0, TYPE_ROLE );
156 if( type == QVariant() ) return false;
157 int i_type = type.toInt();
158 if( i_type != PL_TYPE && i_type != ML_TYPE ) return false;
159 bool to_pl = i_type == PL_TYPE;
161 if( data->hasFormat( "vlc/qt-playlist-item" ) )
163 QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
164 QDataStream stream( &encodedData, QIODevice::ReadOnly );
165 playlist_Lock( THEPL );
166 while( !stream.atEnd() )
169 stream.readRawData( (char*)&item, sizeof(PLItem*) );
170 input_item_t *pl_input =item->inputItem();
171 playlist_AddExt ( THEPL,
172 pl_input->psz_uri, pl_input->psz_name,
173 PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
174 pl_input->i_duration,
175 pl_input->i_options, pl_input->ppsz_options, pl_input->optflagc,
178 playlist_Unlock( THEPL );
183 PLSelector::~PLSelector()