]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/selector.cpp
Qt: make the search box take less space.
[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 #include "../../dialogs_provider.hpp"
35 #include "playlist.hpp"
36 #include "util/customwidgets.hpp"
37
38 #include <QVBoxLayout>
39 #include <QHeaderView>
40 #include <QMimeData>
41 #include <QInputDialog>
42 #include <QMessageBox>
43
44 #include <vlc_playlist.h>
45 #include <vlc_services_discovery.h>
46
47 PLSelItem::PLSelItem ( QTreeWidgetItem *i, const QString& text )
48     : qitem(i), lblAction( NULL)
49 {
50     layout = new QHBoxLayout();
51     layout->setContentsMargins(0,0,0,0);
52     layout->addSpacing( 3 );
53
54     lbl = new QLabel( text );
55
56     layout->addWidget(lbl, 1);
57
58     setLayout( layout );
59
60     setMinimumHeight( 22 ); //Action icon height plus 6
61 }
62
63 void PLSelItem::addAction( ItemAction act, const QString& tooltip )
64 {
65     if( lblAction ) return; //might change later
66
67     QIcon icon;
68
69     switch( act )
70     {
71     case ADD_ACTION:
72         icon = QIcon( ":/buttons/playlist/playlist_add" ); break;
73     case RM_ACTION:
74         icon = QIcon( ":/buttons/playlist/playlist_remove" ); break;
75     }
76
77     lblAction = new QVLCIconLabel( icon );
78
79     if( !tooltip.isEmpty() ) lblAction->setToolTip( tooltip );
80
81     layout->addWidget( lblAction, 0 );
82     lblAction->hide();
83     layout->addSpacing( 3 );
84
85     CONNECT( lblAction, clicked(), this, triggerAction() );
86 }
87
88 void PLSelItem::showAction() { if( lblAction ) lblAction->show(); }
89
90 void PLSelItem::hideAction() { if( lblAction ) lblAction->hide(); }
91
92 void PLSelItem::setText( const QString& text ) { lbl->setText( text ); }
93
94 void PLSelItem::enterEvent( QEvent *ev ){ showAction(); }
95
96 void PLSelItem::leaveEvent( QEvent *ev ){ hideAction(); }
97
98 PLSelector::PLSelector( QWidget *p, intf_thread_t *_p_intf )
99            : QTreeWidget( p ), p_intf(_p_intf)
100 {
101     setFrameStyle( QFrame::NoFrame );
102     viewport()->setAutoFillBackground( false );
103     setIconSize( QSize( 24,24 ) );
104     setIndentation( 10 );
105     header()->hide();
106     setRootIsDecorated( false );
107     setAlternatingRowColors( false );
108     podcastsParent = NULL;
109     podcastsParentId = -1;
110
111     viewport()->setAcceptDrops(true);
112     setDropIndicatorShown(true);
113     invisibleRootItem()->setFlags( invisibleRootItem()->flags() & ~Qt::ItemIsDropEnabled );
114
115     CONNECT( THEMIM, playlistItemAppended( int, int ),
116              this, plItemAdded( int, int ) );
117     CONNECT( THEMIM, playlistItemRemoved( int ),
118              this, plItemRemoved( int ) );
119     CONNECT( THEMIM->getIM(), metaChanged( input_item_t *),
120             this, inputItemUpdate( input_item_t * ) );
121
122     createItems();
123     CONNECT( this, itemActivated( QTreeWidgetItem *, int ),
124              this, setSource( QTreeWidgetItem *) );
125     /* I believe this is unnecessary, seeing
126        QStyle::SH_ItemView_ActivateItemOnSingleClick
127         CONNECT( view, itemClicked( QTreeWidgetItem *, int ),
128              this, setSource( QTreeWidgetItem *) ); */
129
130     /* select the first item */
131 //  view->setCurrentIndex( model->index( 0, 0, QModelIndex() ) );
132 }
133
134 PLSelector::~PLSelector()
135 {
136     if( podcastsParent )
137     {
138         int c = podcastsParent->childCount();
139         for( int i = 0; i < c; i++ )
140         {
141             QTreeWidgetItem *item = podcastsParent->child(i);
142             input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
143             vlc_gc_decref( p_input );
144         }
145     }
146 }
147
148 void PLSelector::setSource( QTreeWidgetItem *item )
149 {
150     if( !item )
151         return;
152
153     bool b_ok;
154     int i_type = item->data( 0, TYPE_ROLE ).toInt( &b_ok );
155     if( !b_ok || i_type == CATEGORY_TYPE )
156         return;
157
158     bool sd_loaded;
159     if( i_type == SD_TYPE )
160     {
161         QString qs = item->data( 0, NAME_ROLE ).toString();
162         sd_loaded = playlist_IsServicesDiscoveryLoaded( THEPL, qtu( qs ) );
163         if( !sd_loaded )
164             playlist_ServicesDiscoveryAdd( THEPL, qtu( qs ) );
165     }
166
167     playlist_Lock( THEPL );
168
169     playlist_item_t *pl_item = NULL;
170
171     if( i_type == SD_TYPE )
172     {
173         pl_item = playlist_ChildSearchName( THEPL->p_root_category, qtu( item->data(0, LONGNAME_ROLE ).toString() ) );
174         if( item->data( 0, SPECIAL_ROLE ).toInt() == IS_PODCAST )
175         {
176             if( pl_item && !sd_loaded )
177             {
178                 podcastsParentId = pl_item->i_id;
179                 for( int i=0; i < pl_item->i_children; i++ )
180                     addPodcastItem( pl_item->pp_children[i] );
181             }
182             pl_item = NULL; //to prevent activating it
183         }
184     }
185     else
186         pl_item = item->data( 0, PL_ITEM_ROLE ).value<playlist_item_t*>();
187
188     playlist_Unlock( THEPL );
189
190     if( pl_item )
191        emit activated( pl_item );
192 }
193
194 PLSelItem * PLSelector::addItem (
195   SelectorItemType type, const QString& str, bool drop,
196   QTreeWidgetItem* parentItem )
197 {
198   QTreeWidgetItem *item = parentItem ?
199       new QTreeWidgetItem( parentItem ) : new QTreeWidgetItem( this );
200   PLSelItem *selItem = new PLSelItem( item, str );
201   setItemWidget( item, 0, selItem );
202   item->setData( 0, TYPE_ROLE, (int)type );
203   if( !drop ) item->setFlags( item->flags() & ~Qt::ItemIsDropEnabled );
204
205   return selItem;
206 }
207
208 PLSelItem * putSDData( PLSelItem* item, const char* name, const char* longname )
209 {
210     item->treeItem()->setData( 0, NAME_ROLE, qfu( name ) );
211     item->treeItem()->setData( 0, LONGNAME_ROLE, qfu( longname ) );
212     return item;
213 }
214
215 PLSelItem * putPLData( PLSelItem* item, playlist_item_t* plItem )
216 {
217     item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( plItem ) );
218 /*    item->setData( 0, PL_ITEM_ID_ROLE, plItem->i_id );
219     item->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( (void*) plItem->p_input ) ); );*/
220     return item;
221 }
222
223 PLSelItem *PLSelector::addPodcastItem( playlist_item_t *p_item )
224 {
225   vlc_gc_incref( p_item->p_input );
226   char *psz_name = input_item_GetName( p_item->p_input );
227   PLSelItem *item = addItem(
228       PL_ITEM_TYPE, qfu( psz_name ), false, podcastsParent );
229   item->addAction( RM_ACTION, qtr( "Remove this podcast subscription" ) );
230   item->treeItem()->setData( 0, PL_ITEM_ROLE, QVariant::fromValue( p_item ) );
231   item->treeItem()->setData( 0, PL_ITEM_ID_ROLE, QVariant(p_item->i_id) );
232   item->treeItem()->setData( 0, IN_ITEM_ROLE, QVariant::fromValue( p_item->p_input ) );
233   CONNECT( item, action( PLSelItem* ), this, podcastRemove( PLSelItem* ) );
234   free( psz_name );
235   return item;
236 }
237
238 void PLSelector::createItems()
239 {
240     PLSelItem *pl = putPLData( addItem( PL_ITEM_TYPE, qtr( "Playlist" ), true ),
241                               THEPL->p_local_category );
242     pl->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PL ) );
243
244     PLSelItem *ml = putPLData( addItem( PL_ITEM_TYPE, qtr( "Media Library" ), true ),
245                               THEPL->p_ml_category );
246     ml->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_ML ) );
247
248     QTreeWidgetItem *msrc = addItem( CATEGORY_TYPE, qtr( "Media Sources" ),
249                                       false )->treeItem();
250
251     QTreeWidgetItem *mfldrs = NULL;
252
253     QTreeWidgetItem *shouts = NULL;
254
255     msrc->setExpanded( true );
256
257     char **ppsz_longnames;
258     char **ppsz_names = vlc_sd_GetNames( &ppsz_longnames );
259     if( !ppsz_names )
260         return;
261
262     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
263     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
264     {
265         //msg_Dbg( p_intf, "Adding a SD item: %s", *ppsz_longname );
266 #define SD_IS( name ) ( !strcmp( *ppsz_name, name ) )
267
268         if( SD_IS("shoutcast") || SD_IS("shoutcasttv") ||
269             SD_IS("frenchtv") || SD_IS("freebox") )
270         {
271             if( !shouts ) shouts = addItem( CATEGORY_TYPE, qtr( "Shoutcast" ),
272                                             false, msrc )->treeItem();
273             putSDData( addItem( SD_TYPE, *ppsz_longname, false, shouts ),
274                        *ppsz_name, *ppsz_longname );
275         }
276         else if( SD_IS("video_dir") || SD_IS("audio_dir") || SD_IS("picture_dir") )
277         {
278             if( !mfldrs ) mfldrs = addItem( CATEGORY_TYPE, qtr( "Media Folders" ),
279                                             false, msrc )->treeItem();
280             putSDData( addItem( SD_TYPE, *ppsz_longname, false, mfldrs ),
281                        *ppsz_name, *ppsz_longname );
282         }
283         else if( SD_IS("podcast") )
284         {
285
286             PLSelItem *podItem = addItem( SD_TYPE, qtr( "Podcasts" ), false, msrc );
287             putSDData( podItem, *ppsz_name, *ppsz_longname );
288             podItem->treeItem()->setData( 0, SPECIAL_ROLE, QVariant( IS_PODCAST ) );
289             podItem->addAction( ADD_ACTION, qtr( "Subscribe to a podcast" ) );
290             CONNECT( podItem, action( PLSelItem* ), this, podcastAdd( PLSelItem* ) );
291
292             podcastsParent = podItem->treeItem();
293         }
294         else
295         {
296             putSDData( addItem( SD_TYPE, qtr( *ppsz_longname ), false, msrc ),
297                        *ppsz_name, *ppsz_longname );
298         }
299
300 #undef SD_IS
301
302         free( *ppsz_name );
303         free( *ppsz_longname );
304     }
305     free( ppsz_names );
306     free( ppsz_longnames );
307 }
308
309 QStringList PLSelector::mimeTypes() const
310 {
311     QStringList types;
312     types << "vlc/qt-playlist-item";
313     return types;
314 }
315
316 bool PLSelector::dropMimeData ( QTreeWidgetItem * parent, int index,
317   const QMimeData * data, Qt::DropAction action )
318 {
319     if( !parent ) return false;
320
321     QVariant type = parent->data( 0, TYPE_ROLE );
322     if( type == QVariant() ) return false;
323     int i_truth = parent->data( 0, SPECIAL_ROLE ).toInt();
324
325     if( i_truth != IS_PL && i_truth != IS_ML ) return false;
326     bool to_pl = ( i_truth == IS_PL );
327
328     if( data->hasFormat( "vlc/qt-playlist-item" ) )
329     {
330         QByteArray encodedData = data->data( "vlc/qt-playlist-item" );
331         QDataStream stream( &encodedData, QIODevice::ReadOnly );
332         playlist_Lock( THEPL );
333         while( !stream.atEnd() )
334         {
335             PLItem *item;
336             stream.readRawData( (char*)&item, sizeof(PLItem*) );
337             input_item_t *pl_input =item->inputItem();
338             playlist_AddExt ( THEPL,
339                 pl_input->psz_uri, pl_input->psz_name,
340                 PLAYLIST_APPEND | PLAYLIST_SPREPARSE, PLAYLIST_END,
341                 pl_input->i_duration,
342                 pl_input->i_options, pl_input->ppsz_options, pl_input->optflagc,
343                 to_pl, true );
344         }
345         playlist_Unlock( THEPL );
346     }
347     return true;
348 }
349
350 void PLSelector::plItemAdded( int item, int parent )
351 {
352     if( parent != podcastsParentId ) return;
353
354     playlist_Lock( THEPL );
355
356     playlist_item_t *p_item = playlist_ItemGetById( THEPL, item );
357     if( !p_item ) {
358         playlist_Unlock( THEPL );
359         return;
360     }
361
362     int c = podcastsParent->childCount();
363     for( int i = 0; i < c; i++ )
364     {
365         QTreeWidgetItem *podItem = podcastsParent->child(i);
366         if( podItem->data( 0, PL_ITEM_ID_ROLE ).toInt() == item )
367         {
368           //msg_Dbg( p_intf, "Podcast already in: (%d) %s", item, p_item->p_input->psz_uri);
369           playlist_Unlock( THEPL );
370           return;
371         }
372     }
373
374     //msg_Dbg( p_intf, "Adding podcast: (%d) %s", item, p_item->p_input->psz_uri );
375     addPodcastItem( p_item );
376
377     playlist_Unlock( THEPL );
378
379     podcastsParent->setExpanded( true );
380 }
381
382 void PLSelector::plItemRemoved( int id )
383 {
384     int c = podcastsParent->childCount();
385     for( int i = 0; i < c; i++ )
386     {
387         QTreeWidgetItem *item = podcastsParent->child(i);
388         if( item->data( 0, PL_ITEM_ID_ROLE ).toInt() == id )
389         {
390             input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
391             //msg_Dbg( p_intf, "Removing podcast: (%d) %s", id, p_input->psz_uri );
392             vlc_gc_decref( p_input );
393             delete item;
394             return;
395         }
396     }
397 }
398
399 void PLSelector::inputItemUpdate( input_item_t *arg )
400 {
401     int c = podcastsParent->childCount();
402     for( int i = 0; i < c; i++ )
403     {
404         QTreeWidgetItem *item = podcastsParent->child(i);
405         input_item_t *p_input = item->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
406         if( p_input == arg )
407         {
408             PLSelItem *si = itemWidget( item );
409             char *psz_name = input_item_GetName( p_input );
410             si->setText( qfu( psz_name ) );
411             free( psz_name );
412             return;
413         }
414     }
415 }
416
417 void PLSelector::podcastAdd( PLSelItem* item )
418 {
419     bool ok;
420     QString url = QInputDialog::getText( this, qtr( "Subscribe" ),
421                                          qtr( "Enter URL of the podcast to subscribe to:" ),
422                                          QLineEdit::Normal, QString(), &ok );
423     if( !ok || url.isEmpty() ) return;
424
425     setSource( podcastsParent ); //to load the SD in case it's not loaded
426
427     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
428         p_intf->p_libvlc, "podcast", FIND_CHILD );
429     if( !p_obj ) return;
430
431     QString request("ADD:");
432     request += url;
433     var_SetString( p_obj, "podcast-request", qtu( request ) );
434     vlc_object_release( p_obj );
435 }
436
437 void PLSelector::podcastRemove( PLSelItem* item )
438 {
439     //FIXME will translators know to leave that %1 somewhere inside?
440     QString question ( qtr( "Do you really want to unsubscribe from %1?" ) );
441     question = question.arg( item->text() );
442     QMessageBox::StandardButton res =
443         QMessageBox::question( this, qtr( "Unsubscribe" ), question,
444                                QMessageBox::Ok | QMessageBox::Cancel,
445                                QMessageBox::Cancel );
446     if( res == QMessageBox::Cancel ) return;
447
448     input_item_t *input = item->treeItem()->data( 0, IN_ITEM_ROLE ).value<input_item_t*>();
449     if( !input ) return;
450
451     vlc_object_t *p_obj = (vlc_object_t*) vlc_object_find_name(
452         p_intf->p_libvlc, "podcast", FIND_CHILD );
453     if( !p_obj ) return;
454
455     QString request("RM:");
456     char *psz_uri = input_item_GetURI( input );
457     request += qfu( psz_uri );
458     var_SetString( p_obj, "podcast-request", qtu( request ) );
459     vlc_object_release( p_obj );
460     free( psz_uri );
461 }
462
463 PLSelItem * PLSelector::itemWidget( QTreeWidgetItem *item )
464 {
465     return ( static_cast<PLSelItem*>( QTreeWidget::itemWidget( item, 0 ) ) );
466 }
467