]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/selector.cpp
Fix Service Discovey plugin names character set
[vlc] / modules / gui / qt4 / components / playlist / selector.cpp
1 /*****************************************************************************
2  * selector.cpp : Playlist source selector
3  ****************************************************************************
4  * Copyright (C) 2006-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <assert.h>
30
31 #include "components/playlist/selector.hpp"
32 #include "playlist_item.hpp"
33 #include "qt4.hpp"
34
35 #include <QVBoxLayout>
36 #include <QHeaderView>
37 #include <QMimeData>
38
39 #include <vlc_playlist.h>
40 #include <vlc_services_discovery.h>
41
42 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
43            : QTreeWidget( p ), p_intf(_p_intf)
44 {
45     setIconSize( QSize( 24,24 ) );
46 //    view->setAlternatingRowColors( true );
47     setIndentation( 10 );
48     header()->hide();
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 );
55
56     createItems();
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 *) ); */
63
64     /* select the first item */
65 //  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
66 }
67
68 void PLSelector::setSource( QTreeWidgetItem *item )
69 {
70     if( !item )
71         return;
72
73     bool b_ok;
74     int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
75     if( !b_ok )
76         return;
77
78     assert( ( i_type == PL_TYPE || i_type == ML_TYPE || i_type == SD_TYPE ) );
79     if( i_type == SD_TYPE )
80     {
81         QString qs = item->data( 0, NAME_ROLE ).toString();
82         if( !playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) ) )
83         {
84             playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
85 #warning FIXME
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 ) );
89
90             emit activated( pl_item );
91             return;
92         }
93     }
94
95     if( i_type == SD_TYPE )
96         msg_Dbg( p_intf, "SD already loaded, reloading" );
97
98     playlist_item_t *pl_item =
99             item->data( 0, PPL_ITEM_ROLE ).value<playlist_item_t *>();
100     if( pl_item )
101             emit activated( pl_item );
102 }
103
104 void PLSelector::createItems()
105 {
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); */
112
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 ) );
117
118 /*  QTreeWidgetItem *empty2 = new QTreeWidgetItem( view );
119     empty2->setFlags(Qt::NoItemFlags);*/
120
121     QTreeWidgetItem *sds = new QTreeWidgetItem( this );
122     sds->setExpanded( true );
123     sds->setText( 0, qtr( "Libraries" ) );
124     sds->setFlags( sds->flags() & ~Qt::ItemIsDropEnabled );
125
126     char **ppsz_longnames;
127     char **ppsz_names = vlc_sd_GetNames( &ppsz_longnames );
128     if( !ppsz_names )
129         return;
130
131     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
132     QTreeWidgetItem *sd_item;
133     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
134     {
135         sd_item = new QTreeWidgetItem( QStringList( qfu(*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 );
140     }
141 }
142
143 QStringList PLSelector::mimeTypes() const
144 {
145     QStringList types;
146     types << "vlc/qt-playlist-item";
147     return types;
148 }
149
150 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
151   const QMimeData * data, Qt::DropAction action )
152 {
153     if( !parent ) return false;
154
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;
160
161     if( data->hasFormat( "vlc/qt-playlist-item" ) )
162     {
163         QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
164         QDataStream stream( &encodedData, QIODevice::ReadOnly );
165         playlist_Lock( THEPL );
166         while( !stream.atEnd() )
167         {
168             PLItem *item;
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,
176                 to_pl, true );
177         }
178         playlist_Unlock( THEPL );
179     }
180     return true;
181 }
182
183 PLSelector::~PLSelector()
184 {
185 }