]> git.sesse.net Git - vlc/blob - modules/services_discovery/mediadirs.c
0e081e986acdb72891f9623eda266526afe001bd
[vlc] / modules / services_discovery / mediadirs.c
1 /*****************************************************************************
2  * mediadirs.c:  Picture/Music/Video user directories as service discoveries
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Erwan Tulou <erwan10 aT videolan DoT org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Includes
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <sys/stat.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_url.h>
37 #include <vlc_fs.h>
38 #include <vlc_services_discovery.h>
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43
44 enum type_e { Video = 0, Audio = 1, Picture = 2, Unknown = 3 };
45
46 static int  Open( vlc_object_t *, enum type_e );
47 static void Close( vlc_object_t * );
48
49 /* Main functions */
50 #define OPEN_MODULE( type )                        \
51 static int Open##type( vlc_object_t *p_this )      \
52 {                                                  \
53     msg_Dbg( p_this, "Starting " #type );          \
54     return Open( p_this, type );                   \
55 }
56
57 OPEN_MODULE( Video )
58 OPEN_MODULE( Audio )
59 OPEN_MODULE( Picture )
60
61 #undef OPEN_MODULE
62
63 static int vlc_sd_probe_Open( vlc_object_t * );
64
65 vlc_module_begin ()
66     set_category( CAT_PLAYLIST )
67     set_subcategory( SUBCAT_PLAYLIST_SD )
68
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" )
74
75     add_submodule ()
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" )
81
82     add_submodule ()
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" )
88
89     VLC_SD_PROBE_SUBMODULE
90 vlc_module_end ()
91
92
93 /*****************************************************************************
94  * Local prototypes
95  *****************************************************************************/
96
97 static void* Run( void* );
98
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 *);
102
103 static enum type_e fileType( services_discovery_t *p_sd, const char* psz_file );
104 static void formatSnapshotItem( input_item_t* );
105
106 struct services_discovery_sys_t
107 {
108     vlc_thread_t thread;
109     enum type_e i_type;
110
111     char* psz_dir[2];
112     const char* psz_var;
113 };
114
115 /*****************************************************************************
116  * Open: initialize module
117  *****************************************************************************/
118 static int Open( vlc_object_t *p_this, enum type_e i_type )
119 {
120     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
121     services_discovery_sys_t *p_sys;
122
123     p_sd->p_sys = p_sys = calloc( 1, sizeof( *p_sys) );
124     if( !p_sys )
125         return VLC_ENOMEM;
126
127     p_sys->i_type = i_type;
128
129     if( p_sys->i_type == Video )
130     {
131         p_sys->psz_dir[0] = config_GetUserDir( VLC_VIDEOS_DIR );
132         p_sys->psz_dir[1] = var_CreateGetString( p_sd, "input-record-path" );
133
134         p_sys->psz_var = "record-file";
135     }
136     else if( p_sys->i_type == Audio )
137     {
138         p_sys->psz_dir[0] = config_GetUserDir( VLC_MUSIC_DIR );
139         p_sys->psz_dir[1] = var_CreateGetString( p_sd, "input-record-path" );
140
141         p_sys->psz_var = "record-file";
142     }
143     else if( p_sys->i_type == Picture )
144     {
145         p_sys->psz_dir[0] = config_GetUserDir( VLC_PICTURES_DIR );
146         p_sys->psz_dir[1] = var_CreateGetString( p_sd, "snapshot-path" );
147
148         p_sys->psz_var = "snapshot-file";
149     }
150     else
151     {
152         free( p_sys );
153         return VLC_EGENERIC;
154     }
155
156     var_AddCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
157
158     if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
159     {
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] );
163         free( p_sys );
164         return VLC_EGENERIC;
165     }
166
167     return VLC_SUCCESS;
168 }
169
170 /*****************************************************************************
171  * Run:
172  *****************************************************************************/
173 static void *Run( void *data )
174 {
175     services_discovery_t *p_sd = data;
176     services_discovery_sys_t *p_sys = p_sd->p_sys;
177
178     int canc = vlc_savecancel();
179
180     int num_dir = sizeof( p_sys->psz_dir ) / sizeof( p_sys->psz_dir[0] );
181     for( int i = 0; i < num_dir; i++ )
182     {
183         char* psz_dir = p_sys->psz_dir[i];
184
185         /* make sure the directory exists */
186         struct stat st;
187         if( psz_dir == NULL            ||
188             vlc_stat( psz_dir, &st )  ||
189             !S_ISDIR( st.st_mode ) )
190             continue;
191
192         char* psz_uri = vlc_path2uri( psz_dir, "file" );
193
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 );
198
199         input_item_AddOption( p_root, "recursive=collapse",
200                               VLC_INPUT_OPTION_TRUSTED );
201
202
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 );
206
207         input_Read( p_sd, p_root );
208
209         vlc_event_detach( p_em, vlc_InputItemSubItemAdded,
210                           input_item_subitem_added, p_sd );
211
212         vlc_gc_decref( p_root );
213         free( psz_uri );
214     }
215
216     vlc_restorecancel(canc);
217     return NULL;
218 }
219
220 /*****************************************************************************
221  * Close:
222  *****************************************************************************/
223 static void Close( vlc_object_t *p_this )
224 {
225     services_discovery_t *p_sd = (services_discovery_t *)p_this;
226     services_discovery_sys_t *p_sys = p_sd->p_sys;
227
228     vlc_cancel( p_sys->thread );
229     vlc_join( p_sys->thread, NULL );
230
231     var_DelCallback( p_sd->p_libvlc, p_sys->psz_var, onNewFileAdded, p_sd );
232
233     free( p_sys->psz_dir[1] );
234     free( p_sys->psz_dir[0] );
235     free( p_sys );
236 }
237
238
239 /*****************************************************************************
240  * Callbacks and helper functions
241  *****************************************************************************/
242 static void input_item_subitem_added( const vlc_event_t * p_event,
243                                       void * user_data )
244 {
245     services_discovery_t *p_sd = user_data;
246     services_discovery_sys_t *p_sys = p_sd->p_sys;
247
248     /* retrieve new item */
249     input_item_t *p_item = p_event->u.input_item_subitem_added.p_new_child;
250
251     if( p_sys->i_type == Picture )
252         formatSnapshotItem( p_item );
253
254     services_discovery_AddItem( p_sd, p_item, NULL );
255 }
256
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 )
259 {
260     (void)p_this;
261
262     services_discovery_t *p_sd = p_data;
263     services_discovery_sys_t *p_sys = p_sd->p_sys;
264
265     (void)psz_var; (void)oldval;
266     char* psz_file = newval.psz_string;
267     if( !psz_file || !*psz_file )
268         return VLC_EGENERIC;
269
270     char* psz_uri = vlc_path2uri( psz_file, "file" );
271     input_item_t* p_item = input_item_New( psz_uri, NULL );
272
273     if( p_sys->i_type == Picture )
274     {
275         if( fileType( p_sd, psz_file ) == Picture )
276         {
277             formatSnapshotItem( p_item );
278             services_discovery_AddItem( p_sd, p_item, NULL );
279
280             msg_Dbg( p_sd, "New snapshot added : %s", psz_file );
281         }
282     }
283     else if( p_sys->i_type == Audio )
284     {
285         if( fileType( p_sd, psz_file ) == Audio )
286         {
287             services_discovery_AddItem( p_sd, p_item, NULL );
288
289             msg_Dbg( p_sd, "New recorded audio added : %s", psz_file );
290         }
291     }
292     else if( p_sys->i_type == Video )
293     {
294         if( fileType( p_sd, psz_file ) == Video ||
295             fileType( p_sd, psz_file ) == Unknown )
296         {
297             services_discovery_AddItem( p_sd, p_item, NULL );
298
299             msg_Dbg( p_sd, "New recorded video added : %s", psz_file );
300         }
301     }
302
303     vlc_gc_decref( p_item );
304     free( psz_uri );
305
306     return VLC_SUCCESS;
307 }
308
309 void formatSnapshotItem( input_item_t *p_item )
310 {
311     if( !p_item )
312         return;
313
314     char* psz_uri = input_item_GetURI( p_item );
315
316     /* copy the snapshot mrl as a ArtURL */
317     if( psz_uri )
318         input_item_SetArtURL( p_item, psz_uri );
319
320     free( psz_uri );
321 }
322
323
324 enum type_e fileType( services_discovery_t *p_sd, const char* psz_file )
325 {
326     services_discovery_sys_t *p_sys = p_sd->p_sys;
327     enum type_e i_ret = Unknown;
328
329     char* psz_dir = strdup( psz_file );
330     char* psz_tmp = strrchr( psz_dir, DIR_SEP_CHAR );
331     if( psz_tmp )
332         *psz_tmp = '\0';
333
334     int num_dir = sizeof( p_sys->psz_dir ) / sizeof( p_sys->psz_dir[0] );
335     for( int i = 0; i < num_dir; i++ )
336     {
337         char* psz_known_dir = p_sys->psz_dir[i];
338
339         if( psz_known_dir && !strcmp( psz_dir, psz_known_dir ) )
340             i_ret = p_sys->i_type;
341     }
342
343     free( psz_dir );
344     return i_ret;
345 }
346
347 static int vlc_sd_probe_Open( vlc_object_t *obj )
348 {
349     vlc_probe_t *probe = (vlc_probe_t *)obj;
350
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;
358 }