]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist_item.cpp
Qt4 - Include fix
[vlc] / modules / gui / qt4 / components / playlist / playlist_item.cpp
1 /*****************************************************************************
2  * playlist_item.cpp : Manage playlist item
3  ****************************************************************************
4  * Copyright © 2006-2008 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 "components/playlist/playlist_model.hpp"
33 #include <vlc_intf_strings.h>
34
35 #include "pixmaps/type_unknown.xpm"
36
37 #include <QSettings>
38
39 /*************************************************************************
40  * Playlist item implementation
41  *************************************************************************/
42
43 /*
44    Playlist item is just a wrapper, an abstraction of the playlist_item
45    in order to be managed by PLModel
46
47    PLItem have a parent, and id and a input Id
48 */
49
50
51 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
52 {
53     parentItem = parent;          /* Can be NULL, but only for the rootItem */
54     i_id       = _i_id;           /* Playlist item specific id */
55     i_input_id = _i_input_id;     /* Identifier of the input */
56     model      = m;               /* PLModel (QAbsmodel) */
57     i_type     = -1;              /* Item type - Avoid segfault */
58     b_current  = false;           /* Is the item the current Item or not */
59
60     assert( model );              /* We need a model */
61
62     /* No parent, should be the 2 main ones */
63     if( parentItem == NULL )
64     {
65         if( model->i_depth == DEPTH_SEL )  /* Selector Panel */
66         {
67             item_col_strings.append( "" );
68         }
69         else
70         {
71             QSettings settings( "vlc", "vlc-qt-interface" );
72             i_showflags = settings.value( "qt-pl-showflags" ).toInt();
73             updateColumnHeaders();
74         }
75     }
76     else
77     {
78         i_showflags = parentItem->i_showflags;
79         //Add empty string and update() handles data appending
80         item_col_strings.append( "" );
81     }
82 }
83
84 /*
85    Constructors
86    Call the above function init
87    So far the first constructor isn't used...
88    */
89 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m )
90 {
91     init( _i_id, _i_input_id, parent, m );
92 }
93
94 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
95 {
96     init( p_item->i_id, p_item->p_input->i_id, parent, m );
97 }
98
99 PLItem::~PLItem()
100 {
101     qDeleteAll( children );
102     children.clear();
103 }
104
105 /* Column manager */
106 void PLItem::updateColumnHeaders()
107 {
108     item_col_strings.clear();
109
110     for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
111     {
112         if( i_showflags & i_index )
113         {
114             switch( i_index )
115             {
116             case VLC_META_ENGINE_ARTIST:
117                 item_col_strings.append( qtr( VLC_META_ARTIST ) );
118                 break;
119             case VLC_META_ENGINE_TITLE:
120                 item_col_strings.append( qtr( VLC_META_TITLE ) );
121                 break;
122             case VLC_META_ENGINE_DESCRIPTION:
123                 item_col_strings.append( qtr( VLC_META_DESCRIPTION ) );
124                 break;
125             case VLC_META_ENGINE_DURATION:
126                 item_col_strings.append( qtr( "Duration" ) );
127                 break;
128             case VLC_META_ENGINE_GENRE:
129                 item_col_strings.append( qtr( VLC_META_GENRE ) );
130                 break;
131             case VLC_META_ENGINE_COLLECTION:
132                 item_col_strings.append( qtr( VLC_META_COLLECTION ) );
133                 break;
134             case VLC_META_ENGINE_SEQ_NUM:
135                 item_col_strings.append( qtr( VLC_META_SEQ_NUM ) );
136                 break;
137             case VLC_META_ENGINE_RATING:
138                 item_col_strings.append( qtr( VLC_META_RATING ) );
139                 break;
140             default:
141                 break;
142             }
143         }
144     }
145 }
146
147 /* So far signal is always true.
148    Using signal false would not call PLModel... Why ?
149  */
150 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
151 {
152     if( signal )
153         model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
154     children.insert( i_pos, item );
155     if( signal )
156         model->endInsertRows();
157 }
158
159 void PLItem::remove( PLItem *removed )
160 {
161     if( model->i_depth == DEPTH_SEL || parentItem )
162     {
163         int i_index = parentItem->children.indexOf( removed );
164         model->beginRemoveRows( model->index( parentItem, 0 ),
165                                 i_index, i_index );
166         parentItem->children.removeAt( i_index );
167         model->endRemoveRows();
168     }
169 }
170
171 /* This function is used to get one's parent's row number in the model */
172 int PLItem::row() const
173 {
174     if( parentItem )
175         return parentItem->children.indexOf( const_cast<PLItem*>(this) );
176        // We don't ever inherit PLItem, yet, but it might come :D
177     return 0;
178 }
179
180 /* update the PL Item, get the good names and so on */
181 /* This function may not be the best way to do it
182    It destroys everything and gets everything again instead of just
183    building the necessary columns.
184    This does extra work if you re-display the same column. Slower...
185    On the other hand, this way saves memory.
186    There must be a more clever way.
187    */
188 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
189 {
190     char psz_duration[MSTRTIME_MAX_SIZE];
191     char *psz_meta;
192
193     assert( p_item->p_input->i_id == i_input_id );
194
195     /* Useful for the model */
196     i_type = p_item->p_input->i_type;
197     b_current = iscurrent;
198
199     item_col_strings.clear();
200
201     if( model->i_depth == 1 )  /* Selector Panel */
202     {
203         item_col_strings.append( qfu( p_item->p_input->psz_name ) );
204         return;
205     }
206
207 #define ADD_META( item, meta ) \
208     psz_meta = input_item_Get ## meta ( item->p_input ); \
209     item_col_strings.append( qfu( psz_meta ) ); \
210     free( psz_meta );
211
212     for( int i_index=1; i_index <= VLC_META_ENGINE_ART_URL; i_index *= 2 )
213     {
214         if( parentItem->i_showflags & i_index )
215         {
216             switch( i_index )
217             {
218             case VLC_META_ENGINE_ARTIST:
219                 ADD_META( p_item, Artist );
220                 break;
221             case VLC_META_ENGINE_TITLE:
222                 char *psz_title;
223                 psz_title = input_item_GetTitle( p_item->p_input );
224                 if( psz_title )
225                 {
226                     ADD_META( p_item, Title );
227                     free( psz_title );
228                 }
229                 else
230                 {
231                     psz_title = input_item_GetName( p_item->p_input );
232                     if( psz_title )
233                     {
234                         item_col_strings.append( qfu( psz_title ) );
235                     }
236                     free( psz_title );
237                 }
238                 break;
239             case VLC_META_ENGINE_DESCRIPTION:
240                 ADD_META( p_item, Description );
241                 break;
242             case VLC_META_ENGINE_DURATION:
243                 secstotimestr( psz_duration,
244                     input_item_GetDuration( p_item->p_input ) / 1000000 );
245                 item_col_strings.append( QString( psz_duration ) );
246                 break;
247             case VLC_META_ENGINE_GENRE:
248                 ADD_META( p_item, Genre );
249                 break;
250             case VLC_META_ENGINE_COLLECTION:
251                 ADD_META( p_item, Album );
252                 break;
253             case VLC_META_ENGINE_SEQ_NUM:
254                 ADD_META( p_item, TrackNum );
255                 break;
256             case VLC_META_ENGINE_RATING:
257                 ADD_META( p_item, Rating );
258             default:
259                 break;
260             }
261         }
262     }
263 #undef ADD_META
264 }
265