1 /*****************************************************************************
2 * mediadirs.c: Picture/Music/Video user directories as service discoveries
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
7 * Authors: Erwan Tulou <erwan10 aT videolan DoT org>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
38 #include <vlc_services_discovery.h>
40 /*****************************************************************************
42 *****************************************************************************/
44 enum type_e { Video = 0, Audio = 1, Picture = 2, Unknown = 3 };
46 static int Open( vlc_object_t *, enum type_e );
47 static void Close( vlc_object_t * );
50 #define OPEN_MODULE( type ) \
51 static int Open##type( vlc_object_t *p_this ) \
53 msg_Dbg( p_this, "Starting " #type ); \
54 return Open( p_this, type ); \
59 OPEN_MODULE( Picture )
63 static int vlc_sd_probe_Open( vlc_object_t * );
66 set_category( CAT_PLAYLIST )
67 set_subcategory( SUBCAT_PLAYLIST_SD )
69 set_shortname( N_("Video") )
70 set_description( N_("My Videos") )
71 set_capability( "services_discovery", 0 )
72 set_callbacks( OpenVideo, Close )
73 add_shortcut( "video_dir" )
76 set_shortname( N_("Audio") )
77 set_description( N_("My Music") )
78 set_capability( "services_discovery", 0 )
79 set_callbacks( OpenAudio, Close )
80 add_shortcut( "audio_dir" )
83 set_shortname( N_("Picture") )
84 set_description( N_("My Pictures") )
85 set_capability( "services_discovery", 0 )
86 set_callbacks( OpenPicture, Close )
87 add_shortcut( "picture_dir" )
89 VLC_SD_PROBE_SUBMODULE
93 /*****************************************************************************
95 *****************************************************************************/
97 static void* Run( void* );
99 static void input_item_subitem_added( const vlc_event_t*, void* );
100 static int onNewFileAdded( vlc_object_t*, char const *,
101 vlc_value_t, vlc_value_t, void *);
103 static enum type_e fileType( services_discovery_t *p_sd, const char* psz_file );
104 static void formatSnapshotItem( input_item_t* );
106 struct services_discovery_sys_t
115 /*****************************************************************************
116 * Open: initialize module
117 *****************************************************************************/
118 static int Open( vlc_object_t *p_this, enum type_e i_type )
120 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
121 services_discovery_sys_t *p_sys;
123 p_sd->p_sys = p_sys = calloc( 1, sizeof( *p_sys) );
127 p_sys->i_type = i_type;
129 if( p_sys->i_type == Video )
131 p_sys->psz_dir[0] = config_GetUserDir( VLC_VIDEOS_DIR );
132 p_sys->psz_dir[1] = var_CreateGetString( p_sd, "input-record-path" );
134 p_sys->psz_var = "record-file";
136 else if( p_sys->i_type == Audio )
138 p_sys->psz_dir[0] = config_GetUserDir( VLC_MUSIC_DIR );
139 p_sys->psz_dir[1] = var_CreateGetString( p_sd, "input-record-path" );
141 p_sys->psz_var = "record-file";
143 else if( p_sys->i_type == Picture )
145 p_sys->psz_dir[0] = config_GetUserDir( VLC_PICTURES_DIR );
146 p_sys->psz_dir[1] = var_CreateGetString( p_sd, "snapshot-path" );
148 p_sys->psz_var = "snapshot-file";
156 var_AddCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
158 if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
160 var_DelCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
161 free( p_sys->psz_dir[1] );
162 free( p_sys->psz_dir[0] );
170 /*****************************************************************************
172 *****************************************************************************/
173 static void *Run( void *data )
175 services_discovery_t *p_sd = data;
176 services_discovery_sys_t *p_sys = p_sd->p_sys;
178 int canc = vlc_savecancel();
180 int num_dir = sizeof( p_sys->psz_dir ) / sizeof( p_sys->psz_dir[0] );
181 for( int i = 0; i < num_dir; i++ )
183 char* psz_dir = p_sys->psz_dir[i];
185 /* make sure the directory exists */
187 if( psz_dir == NULL ||
188 vlc_stat( psz_dir, &st ) ||
189 !S_ISDIR( st.st_mode ) )
192 char* psz_uri = vlc_path2uri( psz_dir, "file" );
194 input_item_t* p_root = input_item_New( psz_uri, NULL );
195 if( p_sys->i_type == Picture )
196 input_item_AddOption( p_root, "ignore-filetypes=ini,db,lnk,txt",
197 VLC_INPUT_OPTION_TRUSTED );
199 input_item_AddOption( p_root, "recursive=collapse",
200 VLC_INPUT_OPTION_TRUSTED );
203 vlc_event_manager_t *p_em = &p_root->event_manager;
204 vlc_event_attach( p_em, vlc_InputItemSubItemAdded,
205 input_item_subitem_added, p_sd );
207 input_Read( p_sd, p_root );
209 vlc_event_detach( p_em, vlc_InputItemSubItemAdded,
210 input_item_subitem_added, p_sd );
212 vlc_gc_decref( p_root );
216 vlc_restorecancel(canc);
220 /*****************************************************************************
222 *****************************************************************************/
223 static void Close( vlc_object_t *p_this )
225 services_discovery_t *p_sd = (services_discovery_t *)p_this;
226 services_discovery_sys_t *p_sys = p_sd->p_sys;
228 vlc_cancel( p_sys->thread );
229 vlc_join( p_sys->thread, NULL );
231 var_DelCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
233 free( p_sys->psz_dir[1] );
234 free( p_sys->psz_dir[0] );
239 /*****************************************************************************
240 * Callbacks and helper functions
241 *****************************************************************************/
242 static void input_item_subitem_added( const vlc_event_t * p_event,
245 services_discovery_t *p_sd = user_data;
246 services_discovery_sys_t *p_sys = p_sd->p_sys;
248 /* retrieve new item */
249 input_item_t *p_item = p_event->u.input_item_subitem_added.p_new_child;
251 if( p_sys->i_type == Picture )
252 formatSnapshotItem( p_item );
254 services_discovery_AddItem( p_sd, p_item, NULL );
257 static int onNewFileAdded( vlc_object_t *p_this, char const *psz_var,
258 vlc_value_t oldval, vlc_value_t newval, void *p_data )
262 services_discovery_t *p_sd = p_data;
263 services_discovery_sys_t *p_sys = p_sd->p_sys;
265 (void)psz_var; (void)oldval;
266 char* psz_file = newval.psz_string;
267 if( !psz_file || !*psz_file )
270 char* psz_uri = vlc_path2uri( psz_file, "file" );
271 input_item_t* p_item = input_item_New( psz_uri, NULL );
273 if( p_sys->i_type == Picture )
275 if( fileType( p_sd, psz_file ) == Picture )
277 formatSnapshotItem( p_item );
278 services_discovery_AddItem( p_sd, p_item, NULL );
280 msg_Dbg( p_sd, "New snapshot added : %s", psz_file );
283 else if( p_sys->i_type == Audio )
285 if( fileType( p_sd, psz_file ) == Audio )
287 services_discovery_AddItem( p_sd, p_item, NULL );
289 msg_Dbg( p_sd, "New recorded audio added : %s", psz_file );
292 else if( p_sys->i_type == Video )
294 if( fileType( p_sd, psz_file ) == Video ||
295 fileType( p_sd, psz_file ) == Unknown )
297 services_discovery_AddItem( p_sd, p_item, NULL );
299 msg_Dbg( p_sd, "New recorded video added : %s", psz_file );
303 vlc_gc_decref( p_item );
309 void formatSnapshotItem( input_item_t *p_item )
314 char* psz_uri = input_item_GetURI( p_item );
316 /* copy the snapshot mrl as a ArtURL */
318 input_item_SetArtURL( p_item, psz_uri );
324 enum type_e fileType( services_discovery_t *p_sd, const char* psz_file )
326 services_discovery_sys_t *p_sys = p_sd->p_sys;
327 enum type_e i_ret = Unknown;
329 char* psz_dir = strdup( psz_file );
330 char* psz_tmp = strrchr( psz_dir, DIR_SEP_CHAR );
334 int num_dir = sizeof( p_sys->psz_dir ) / sizeof( p_sys->psz_dir[0] );
335 for( int i = 0; i < num_dir; i++ )
337 char* psz_known_dir = p_sys->psz_dir[i];
339 if( psz_known_dir && !strcmp( psz_dir, psz_known_dir ) )
340 i_ret = p_sys->i_type;
347 static int vlc_sd_probe_Open( vlc_object_t *obj )
349 vlc_probe_t *probe = (vlc_probe_t *)obj;
351 vlc_sd_probe_Add( probe, "video_dir{longname=\"My Videos\"}",
352 N_("My Videos"), SD_CAT_MYCOMPUTER );
353 vlc_sd_probe_Add( probe, "audio_dir{longname=\"My Music\"}",
354 N_("My Music"), SD_CAT_MYCOMPUTER );
355 vlc_sd_probe_Add( probe, "picture_dir{longname=\"My Pictures\"}",
356 N_("My Pictures"), SD_CAT_MYCOMPUTER );
357 return VLC_PROBE_CONTINUE;