]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_item.cpp
1a130aa83a9af60a0cec7a19f2b78f79d24d0b56
[vlc] / modules / gui / qt4 / components / playlist / playlist_item.cpp
1 /*****************************************************************************
2  * playlist_item.cpp : Manage playlist item
3  ****************************************************************************
4  * Copyright © 2006-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
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 "qt4.hpp"
32 #include "playlist_item.hpp"
33 #include <vlc_input_item.h>
34
35 /*************************************************************************
36  * Playlist item implementation
37  *************************************************************************/
38
39 void AbstractPLItem::clearChildren()
40 {
41     qDeleteAll( children );
42     children.clear();
43 }
44
45 void AbstractPLItem::removeChild( AbstractPLItem *item )
46 {
47     children.removeOne( item );
48     delete item;
49 }
50
51 /*
52    Playlist item is just a wrapper, an abstraction of the playlist_item
53    in order to be managed by PLModel
54
55    PLItem have a parent, and id and a input Id
56 */
57
58 void PLItem::init( playlist_item_t *_playlist_item, PLItem *parent )
59 {
60     parentItem = parent;          /* Can be NULL, but only for the rootItem */
61     i_playlist_id = _playlist_item->i_id;           /* Playlist item specific id */
62     p_input = _playlist_item->p_input;
63     vlc_gc_incref( p_input );
64 }
65
66 /*
67    Constructors
68    Call the above function init
69    */
70 PLItem::PLItem( playlist_item_t *p_item, PLItem *parent )
71 {
72     init( p_item, parent );
73 }
74
75 PLItem::PLItem( playlist_item_t * p_item )
76 {
77     init( p_item, NULL );
78 }
79
80 PLItem::~PLItem()
81 {
82     vlc_gc_decref( p_input );
83     qDeleteAll( children );
84     children.clear();
85 }
86
87 int PLItem::id( int type )
88 {
89     switch( type )
90     {
91     case INPUTITEM_ID:
92         return inputItem()->i_id;
93     case PLAYLIST_ID:
94         return i_playlist_id;
95     default:
96     case MLMEDIA_ID:
97         assert( 0 );
98         return -1;
99     }
100 }
101
102 void PLItem::takeChildAt( int index )
103 {
104     AbstractPLItem *child = children[index];
105     child->parentItem = NULL;
106     children.removeAt( index );
107 }
108
109 /* This function is used to get one's parent's row number in the model */
110 int PLItem::row()
111 {
112     if( parentItem )
113         return parentItem->indexOf( this );
114     return 0;
115 }
116
117 bool PLItem::operator< ( AbstractPLItem& other )
118 {
119     AbstractPLItem *item1 = this;
120     while( item1->parentItem )
121     {
122         AbstractPLItem *item2 = &other;
123         while( item2->parentItem )
124         {
125             if( item1 == item2->parentItem ) return true;
126             if( item2 == item1->parentItem ) return false;
127             if( item1->parentItem == item2->parentItem )
128                 return item1->parentItem->indexOf( item1 ) <
129                        item1->parentItem->indexOf( item2 );
130             item2 = item2->parentItem;
131         }
132         item1 = item1->parentItem;
133     }
134     return false;
135 }
136
137 QUrl PLItem::getURI() const
138 {
139     QString uri;
140     vlc_mutex_lock( &p_input->lock );
141     uri = QString( p_input->psz_uri );
142     vlc_mutex_unlock( &p_input->lock );
143     return QUrl( uri );
144 }
145
146 QString PLItem::getTitle() const
147 {
148     QString title;
149     char *fb_name = input_item_GetTitle( p_input );
150     if( EMPTY_STR( fb_name ) )
151     {
152         free( fb_name );
153         fb_name = input_item_GetName( p_input );
154     }
155     title = qfu(fb_name);
156     free(fb_name);
157     return title;
158 }