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"
34 #include "../../dialogs_provider.hpp"
36 #include <QVBoxLayout>
37 #include <QHeaderView>
39 #include <QPushButton>
42 #include <vlc_playlist.h>
43 #include <vlc_services_discovery.h>
45 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
46 : QTreeWidget( p ), p_intf(_p_intf)
48 setItemDelegate( new PLSelectorDelegate() );
49 setFrameStyle( QFrame::StyledPanel | QFrame::Plain );
50 setIconSize( QSize( 24,24 ) );
53 setRootIsDecorated( false );
54 setAlternatingRowColors( true );
56 viewport()->setAcceptDrops(true);
57 setDropIndicatorShown(true);
58 invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
61 CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
62 this, setSource( QTreeWidgetItem *) );
63 /* I believe this is unnecessary, seeing
64 QStyle::SH_ItemView_ActivateItemOnSingleClick
65 CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
66 this, setSource( QTreeWidgetItem *) ); */
68 /* select the first item */
69 // view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
72 void PLSelector::setSource( QTreeWidgetItem *item )
78 int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
82 assert( ( i_type == PL_TYPE || i_type == ML_TYPE || i_type == SD_TYPE ) );
83 if( i_type == SD_TYPE )
85 QString qs = item->data( 0, NAME_ROLE ).toString();
86 if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) ) )
88 playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
92 if( i_type == SD_TYPE )
93 msg_Dbg( p_intf, "SD already loaded, reloading" );
95 playlist_Lock( THEPL );
96 playlist_item_t *pl_item = NULL;
97 if( i_type == SD_TYPE )
98 pl_item = playlist_ChildSearchName( THEPL->p_root_category, qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
99 else if ( i_type == PL_TYPE )
100 pl_item = THEPL->p_local_category;
101 else if ( i_type == ML_TYPE )
102 pl_item = THEPL->p_ml_category;
103 playlist_Unlock( THEPL );
106 emit activated( pl_item );
109 void PLSelector::makeStandardItem( QTreeWidgetItem* item, const QString& str )
111 item->setText( 0,str );
114 void PLSelector::createItems()
116 QTreeWidgetItem *pl = new QTreeWidgetItem( this );
117 makeStandardItem( pl, qtr( "Playlist" ) );
118 pl->setData( 0, TYPE_ROLE, PL_TYPE );
119 pl->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_local_category ) );
121 QTreeWidgetItem *lib = new QTreeWidgetItem( this );
122 makeStandardItem( lib, qtr( "Media Library" ) );
123 lib->setData( 0, TYPE_ROLE, ML_TYPE );
124 lib->setData( 0, PPL_ITEM_ROLE, QVariant::fromValue( THEPL->p_ml_category ) );
126 QTreeWidgetItem *sds = new QTreeWidgetItem( this );
127 makeStandardItem( sds, qtr( "Services" ) );
128 sds->setExpanded( true );
129 sds->setFlags( sds->flags() & ~Qt::ItemIsDropEnabled );
131 QTreeWidgetItem *mfldrs = new QTreeWidgetItem( sds );
132 makeStandardItem( mfldrs, qtr( "Media Folders" ) );
133 mfldrs->setFlags( mfldrs->flags() & ~Qt::ItemIsDropEnabled );
135 QTreeWidgetItem *shouts = new QTreeWidgetItem( sds );
136 makeStandardItem( shouts, qtr( "Shoutcast" ) );
137 shouts->setFlags( shouts->flags() & ~Qt::ItemIsDropEnabled );
139 char **ppsz_longnames;
140 char **ppsz_names = vlc_sd_GetNames( &ppsz_longnames );
144 char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
145 QTreeWidgetItem *sd_item;
146 for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
148 sd_item = new QTreeWidgetItem( );
150 #define SD_IS( name ) ( !strcmp( *ppsz_name, name ) )
152 if( SD_IS("shoutcast") || SD_IS("shoutcasttv") ||
153 SD_IS("frenchtv") || SD_IS("freebox") )
155 shouts->addChild( sd_item );
156 makeStandardItem( sd_item, qfu(*ppsz_longname) );
158 else if( SD_IS("video_dir") || SD_IS("audio_dir") || SD_IS("picture_dir") )
160 mfldrs->addChild( sd_item );
161 makeStandardItem( sd_item, qfu(*ppsz_longname) );
163 else if( SD_IS("podcast") )
165 sds->addChild( sd_item );
167 QLabel *lbl = new QLabel("Podcasts");
169 QPushButton *btn = new QPushButton();
170 btn->setMaximumWidth(30);
171 btn->setIcon( QIcon( ":/menu/preferences" ) );
172 QHBoxLayout *hbox = new QHBoxLayout();
173 hbox->setContentsMargins(0,0,0,0);
174 hbox->addWidget(lbl);
175 hbox->addWidget(btn);
176 QWidget *w = new QWidget();
179 CONNECT( btn, clicked(), THEDP, podcastConfigureDialog() );
180 setItemWidget( sd_item, 0, w );
184 sds->addChild( sd_item );
185 makeStandardItem( sd_item, qfu(*ppsz_longname) );
190 sd_item->setData( 0, TYPE_ROLE, SD_TYPE );
191 sd_item->setData( 0, NAME_ROLE, qfu( *ppsz_name ) );
192 sd_item->setData( 0, LONGNAME_ROLE, qfu( *ppsz_longname ) );
193 sd_item->setFlags( sd_item->flags() & ~Qt::ItemIsDropEnabled );
195 free( *ppsz_longname );
198 free( ppsz_longnames );
201 QStringList PLSelector::mimeTypes() const
204 types << "vlc/qt-playlist-item";
208 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
209 const QMimeData * data, Qt::DropAction action )
211 if( !parent ) return false;
213 QVariant type = parent->data( 0, TYPE_ROLE );
214 if( type == QVariant() ) return false;
215 int i_type = type.toInt();
216 if( i_type != PL_TYPE && i_type != ML_TYPE ) return false;
217 bool to_pl = i_type == PL_TYPE;
219 if( data->hasFormat( "vlc/qt-playlist-item" ) )
221 QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
222 QDataStream stream( &encodedData, QIODevice::ReadOnly );
223 playlist_Lock( THEPL );
224 while( !stream.atEnd() )
227 stream.readRawData( (char*)&item, sizeof(PLItem*) );
228 input_item_t *pl_input =item->inputItem();
229 playlist_AddExt ( THEPL,
230 pl_input->psz_uri, pl_input->psz_name,
231 PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
232 pl_input->i_duration,
233 pl_input->i_options, pl_input->ppsz_options, pl_input->optflagc,
236 playlist_Unlock( THEPL );
241 PLSelector::~PLSelector()