]> git.sesse.net Git - vlc/blob - modules/access/directory.c
directory.c: sort items before adding to playlist
[vlc] / modules / access / directory.c
1 /*****************************************************************************
2  * directory.c: expands a directory (directory: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
31
32 #include <stdlib.h>
33 #include <string.h>
34 #ifdef HAVE_SYS_TYPES_H
35 #   include <sys/types.h>
36 #endif
37 #ifdef HAVE_SYS_STAT_H
38 #   include <sys/stat.h>
39 #endif
40 #ifdef HAVE_ERRNO_H
41 #   include <errno.h>
42 #endif
43 #ifdef HAVE_FCNTL_H
44 #   include <fcntl.h>
45 #endif
46
47 #ifdef HAVE_UNISTD_H
48 #   include <unistd.h>
49 #elif defined( WIN32 ) && !defined( UNDER_CE )
50 #   include <io.h>
51 #endif
52
53 #if (!defined( WIN32 ) || defined(__MINGW32__))
54 /* Mingw has its own version of dirent */
55 #   include <dirent.h>
56 #endif
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 static int  Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
63
64 static int  DemuxOpen ( vlc_object_t * );
65
66 #define RECURSIVE_TEXT N_("Subdirectory behavior")
67 #define RECURSIVE_LONGTEXT N_( \
68         "Select whether subdirectories must be expanded.\n" \
69         "none: subdirectories do not appear in the playlist.\n" \
70         "collapse: subdirectories appear but are expanded on first play.\n" \
71         "expand: all subdirectories are expanded.\n" )
72
73 static char *psz_recursive_list[] = { "none", "collapse", "expand" };
74 static char *psz_recursive_list_text[] = { N_("none"), N_("collapse"),
75                                            N_("expand") };
76
77 vlc_module_begin();
78     set_category( CAT_INPUT );
79     set_name( _("Directory" ) );
80     set_subcategory( SUBCAT_INPUT_ACCESS );
81     set_description( _("Standard filesystem directory input") );
82     set_capability( "access2", 55 );
83     add_shortcut( "directory" );
84     add_shortcut( "dir" );
85     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
86                 RECURSIVE_LONGTEXT, VLC_FALSE );
87       change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
88     set_callbacks( Open, Close );
89
90     add_submodule();
91         set_description( "Directory EOF");
92         set_capability( "demux2", 0 );
93         add_shortcut( "directory" );
94         set_callbacks( DemuxOpen, NULL );
95 vlc_module_end();
96
97
98 /*****************************************************************************
99  * Local prototypes, constants, structures
100  *****************************************************************************/
101
102 #define MODE_EXPAND 0
103 #define MODE_COLLAPSE 1
104 #define MODE_NONE 2
105
106 static int Read( access_t *, uint8_t *, int );
107 static int ReadNull( access_t *, uint8_t *, int );
108 static int Control( access_t *, int, va_list );
109
110 static int Demux( demux_t *p_demux );
111 static int DemuxControl( demux_t *p_demux, int i_query, va_list args );
112
113
114 static int ReadDir( playlist_t *, char *psz_name, int i_mode, int *pi_pos,
115                     playlist_item_t * );
116
117 /*****************************************************************************
118  * Open: open the directory
119  *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
121 {
122     access_t *p_access = (access_t*)p_this;
123
124 #ifdef HAVE_SYS_STAT_H
125     struct stat stat_info;
126
127     if( ( stat( p_access->psz_path, &stat_info ) == -1 ) ||
128         !S_ISDIR( stat_info.st_mode ) )
129 #else
130     if( strcmp( p_access->psz_access, "dir") &&
131         strcmp( p_access->psz_access, "directory") )
132 #endif
133     {
134         return VLC_EGENERIC;
135     }
136
137     p_access->pf_read  = Read;
138     p_access->pf_block = NULL;
139     p_access->pf_seek  = NULL;
140     p_access->pf_control= Control;
141
142     /* Force a demux */
143     p_access->psz_demux = strdup( "directory" );
144
145     return VLC_SUCCESS;
146 }
147
148 /*****************************************************************************
149  * Close: close the target
150  *****************************************************************************/
151 static void Close( vlc_object_t * p_this )
152 {
153
154 }
155
156 /*****************************************************************************
157  * ReadNull: read the directory
158  *****************************************************************************/
159 static int ReadNull( access_t *p_access, uint8_t *p_buffer, int i_len)
160 {
161     /* Return fake data */
162     memset( p_buffer, 0, i_len );
163     return i_len;
164 }
165
166 /*****************************************************************************
167  * Read: read the directory
168  *****************************************************************************/
169 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len)
170 {
171     char *psz_name = NULL;
172     char *psz;
173     int  i_mode, i_pos;
174
175     playlist_item_t *p_item;
176     vlc_bool_t b_play = VLC_FALSE;
177
178     playlist_t *p_playlist =
179         (playlist_t *) vlc_object_find( p_access,
180                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
181
182     if( !p_playlist )
183     {
184         msg_Err( p_access, "can't find playlist" );
185         goto end;
186     }
187
188     /* Remove the ending '/' char */
189     psz_name = strdup( p_access->psz_path );
190     if( psz_name == NULL )
191         goto end;
192
193     if( (psz_name[strlen(psz_name)-1] == '/') ||
194         (psz_name[strlen(psz_name)-1] == '\\') )
195     {
196         psz_name[strlen(psz_name)-1] = '\0';
197     }
198
199     /* Initialize structure */
200     psz = var_CreateGetString( p_access, "recursive" );
201     if( *psz == '\0' || !strncmp( psz, "none" , 4 )  )
202     {
203         i_mode = MODE_NONE;
204     }
205     else if( !strncmp( psz, "collapse", 8 )  )
206     {
207         i_mode = MODE_COLLAPSE;
208     }
209     else
210     {
211         i_mode = MODE_EXPAND;
212     }
213     free( psz );
214
215     /* Make sure we are deleted when we are done */
216     /* The playlist position we will use for the add */
217     i_pos = p_playlist->i_index + 1;
218
219     msg_Dbg( p_access, "opening directory `%s'", psz_name );
220
221     if( &p_playlist->status.p_item->input ==
222         ((input_thread_t *)p_access->p_parent)->input.p_item )
223     {
224         p_item = p_playlist->status.p_item;
225         b_play = VLC_TRUE;
226         msg_Dbg( p_access, "starting directory playback");
227     }
228     else
229     {
230         input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
231                                                         input.p_item;
232         p_item = playlist_ItemGetByInput( p_playlist, p_current );
233         msg_Dbg( p_access, "not starting directory playback");
234         if( !p_item )
235         {
236             msg_Dbg( p_playlist, "unable to find item in playlist");
237             return -1;
238         }
239         b_play = VLC_FALSE;
240     }
241     p_item->input.i_type = ITEM_TYPE_DIRECTORY;
242     if( ReadDir( p_playlist, psz_name , i_mode, &i_pos,
243                  p_item ) != VLC_SUCCESS )
244     {
245     }
246 end:
247
248     /* Begin to read the directory */
249     if( b_play )
250     {
251         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
252                           p_playlist->status.i_view,
253                           p_playlist->status.p_item, NULL );
254     }
255     if( psz_name ) free( psz_name );
256     vlc_object_release( p_playlist );
257
258     /* Return fake data forever */
259     p_access->pf_read = ReadNull;
260     return ReadNull( p_access, p_buffer, i_len );
261 }
262
263 /*****************************************************************************
264  * DemuxOpen:
265  *****************************************************************************/
266 static int Control( access_t *p_access, int i_query, va_list args )
267 {
268     vlc_bool_t   *pb_bool;
269     int          *pi_int;
270     int64_t      *pi_64;
271
272     switch( i_query )
273     {
274         /* */
275         case ACCESS_CAN_SEEK:
276         case ACCESS_CAN_FASTSEEK:
277         case ACCESS_CAN_PAUSE:
278         case ACCESS_CAN_CONTROL_PACE:
279             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
280             *pb_bool = VLC_FALSE;    /* FIXME */
281             break;
282
283         /* */
284         case ACCESS_GET_MTU:
285             pi_int = (int*)va_arg( args, int * );
286             *pi_int = 0;
287             break;
288
289         case ACCESS_GET_PTS_DELAY:
290             pi_64 = (int64_t*)va_arg( args, int64_t * );
291             *pi_64 = DEFAULT_PTS_DELAY * 1000;
292             break;
293
294         /* */
295         case ACCESS_SET_PAUSE_STATE:
296         case ACCESS_GET_TITLE_INFO:
297         case ACCESS_SET_TITLE:
298         case ACCESS_SET_SEEKPOINT:
299         case ACCESS_SET_PRIVATE_ID_STATE:
300             return VLC_EGENERIC;
301
302         default:
303             msg_Warn( p_access, "unimplemented query in control" );
304             return VLC_EGENERIC;
305     }
306     return VLC_SUCCESS;
307 }
308
309 /*****************************************************************************
310  * DemuxOpen:
311  *****************************************************************************/
312 static int DemuxOpen ( vlc_object_t *p_this )
313 {
314     demux_t *p_demux = (demux_t*)p_this;
315
316     if( strcmp( p_demux->psz_demux, "directory" ) )
317         return VLC_EGENERIC;
318
319     p_demux->pf_demux   = Demux;
320     p_demux->pf_control = DemuxControl;
321     return VLC_SUCCESS;
322 }
323
324 /*****************************************************************************
325  * Demux: EOF
326  *****************************************************************************/
327 static int Demux( demux_t *p_demux )
328 {
329     return 0;
330 }
331 /*****************************************************************************
332  * DemuxControl:
333  *****************************************************************************/
334 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
335 {
336     return demux2_vaControlHelper( p_demux->s,
337                                    0, 0, 0, 1,
338                                    i_query, args );
339 }
340
341 static int Filter( struct direct *foo )
342 {
343     return VLC_TRUE;
344 }
345 /*****************************************************************************
346  * ReadDir: read a directory and add its content to the list
347  *****************************************************************************/
348 static int ReadDir( playlist_t *p_playlist,
349                     char *psz_name , int i_mode, int *pi_position,
350                     playlist_item_t *p_parent )
351 {
352     DIR *                       p_current_dir;
353     struct dirent *             p_dir_content;
354     struct dirent **            pp_dir_content;
355     int                         i_dir_content;
356     playlist_item_t *p_node;
357     int i = 0;
358
359    /* Change the item to a node */
360    if( p_parent->i_children == -1)
361    {
362         playlist_ItemToNode( p_playlist,p_parent );
363    }
364
365     /* get the first directory entry */
366     i_dir_content = scandir( psz_name, &pp_dir_content, Filter, alphasort );
367     p_dir_content = pp_dir_content[0];
368
369     /* while we still have entries in the directory */
370     while( i < i_dir_content )
371     {
372         int i_size_entry = strlen( psz_name ) +
373                            strlen( p_dir_content->d_name ) + 2;
374         char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
375
376         sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
377
378         /* if it starts with '.' then forget it */
379         if( p_dir_content->d_name[0] != '.' )
380         {
381 #if defined( S_ISDIR )
382             struct stat stat_data;
383             stat( psz_uri, &stat_data );
384             if( S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
385 #elif defined( DT_DIR )
386             if( ( p_dir_content->d_type & DT_DIR ) && i_mode != MODE_COLLAPSE )
387 #else
388             if( 0 )
389 #endif
390             {
391                 if( i_mode == MODE_NONE )
392                 {
393                     msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
394                     p_dir_content = readdir( p_current_dir );
395                     continue;
396                 }
397                 else if(i_mode == MODE_EXPAND )
398                 {
399                     char *psz_newname;
400                     msg_Dbg(p_playlist, "Reading subdirectory %s", psz_uri );
401
402                     if( !strncmp( psz_uri, psz_name, strlen( psz_name ) ) )
403                     {
404                         char *psz_subdir = psz_uri;
405                         /* Skip the parent path + the separator */
406                         psz_subdir += strlen( psz_name ) + 1;
407                         psz_newname = strdup( psz_subdir );
408                     }
409                     else
410                     {
411                         psz_newname = strdup( psz_uri );
412                     }
413                     p_node = playlist_NodeCreate( p_playlist,
414                                        p_parent->pp_parents[0]->i_view,
415                                        psz_newname, p_parent );
416
417                     playlist_CopyParents(  p_parent, p_node );
418
419                     p_node->input.i_type = ITEM_TYPE_DIRECTORY;
420
421                     if( ReadDir( p_playlist, psz_uri , MODE_EXPAND,
422                                  pi_position, p_node ) != VLC_SUCCESS )
423                     {
424                         return VLC_EGENERIC;
425                     }
426
427                     free( psz_newname );
428                 }
429             }
430             else
431             {
432                 playlist_item_t *p_item = playlist_ItemNew( p_playlist,
433                                 psz_uri, p_dir_content->d_name );
434                 playlist_NodeAddItem( p_playlist,p_item,
435                                       p_parent->pp_parents[0]->i_view,
436                                       p_parent,
437                                       PLAYLIST_APPEND, PLAYLIST_END );
438
439                 playlist_CopyParents( p_parent, p_item );
440             }
441         }
442         free( psz_uri );
443         i++;
444         p_dir_content = pp_dir_content[i];
445     }
446     free( pp_dir_content );
447     return VLC_SUCCESS;
448 }
449
450