]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
Rewrite parts of the podcast service discovery module and make it handle additions...
[vlc] / modules / services_discovery / podcast.c
1 /*****************************************************************************
2  * podcast.c:  Podcast services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -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 #include <vlc/vlc.h>
29 #include <vlc_playlist.h>
30 #include <vlc_network.h>
31
32 #include <errno.h>                                                 /* ENOMEM */
33
34 #ifdef HAVE_UNISTD_H
35 #    include <unistd.h>
36 #endif
37 #ifdef HAVE_SYS_TIME_H
38 #    include <sys/time.h>
39 #endif
40
41 /************************************************************************
42  * Macros and definitions
43  ************************************************************************/
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48
49 /* Callbacks */
50 static int  Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
52
53 #define URLS_TEXT N_("Podcast URLs list")
54 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
55                          "separated by '|' (pipe)." )
56
57 vlc_module_begin();
58     set_shortname( "Podcast");
59     set_description( _("Podcasts") );
60     set_category( CAT_PLAYLIST );
61     set_subcategory( SUBCAT_PLAYLIST_SD );
62
63     add_string( "podcast-urls", NULL, NULL,
64                 URLS_TEXT, URLS_LONGTEXT, VLC_FALSE );
65         change_autosave();
66
67     set_capability( "services_discovery", 0 );
68     set_callbacks( Open, Close );
69
70 vlc_module_end();
71
72
73 /*****************************************************************************
74  * Local structures
75  *****************************************************************************/
76
77 struct services_discovery_sys_t
78 {
79     /* playlist node */
80     input_thread_t **pp_input;
81     int i_input;
82
83     char **ppsz_urls;
84     int i_urls;
85
86     vlc_bool_t b_update;
87 };
88
89 /*****************************************************************************
90  * Local prototypes
91  *****************************************************************************/
92 static void Run( services_discovery_t *p_intf );
93 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
94                        vlc_value_t, void * );
95 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
96
97 /*****************************************************************************
98  * Open: initialize and create stuff
99  *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
101 {
102     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
103     services_discovery_sys_t *p_sys  = malloc(
104                                     sizeof( services_discovery_sys_t ) );
105
106     p_sys->i_urls = 0;
107     p_sys->ppsz_urls = NULL;
108     p_sys->i_input = 0;
109     p_sys->pp_input = NULL;
110     p_sys->b_update = VLC_TRUE;
111
112     p_sd->pf_run = Run;
113     p_sd->p_sys  = p_sys;
114
115     /* Give us a name */
116     services_discovery_SetLocalizedName( p_sd, _("Podcasts") );
117
118     return VLC_SUCCESS;
119 }
120
121 /*****************************************************************************
122  * Close:
123  *****************************************************************************/
124 static void Close( vlc_object_t *p_this )
125 {
126     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
127     services_discovery_sys_t *p_sys  = p_sd->p_sys;
128     int i;
129     for( i = 0; i < p_sys->i_input; i++ )
130     {
131         if( p_sd->p_sys->pp_input[i] )
132         {
133             input_StopThread( p_sd->p_sys->pp_input[i] );
134             input_DestroyThread( p_sd->p_sys->pp_input[i] );
135             p_sd->p_sys->pp_input[i] = NULL;
136         }
137     }
138     free( p_sd->p_sys->pp_input );
139     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
140     free( p_sys->ppsz_urls );
141     free( p_sys );
142 }
143
144 /*****************************************************************************
145  * Run: main thread
146  *****************************************************************************/
147 static void Run( services_discovery_t *p_sd )
148 {
149     services_discovery_sys_t *p_sys  = p_sd->p_sys;
150
151     char *psz_urls = var_CreateGetString( p_sd, "podcast-urls" );
152     free( psz_urls ); /* Gruik ? */
153     var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
154
155     while( !p_sd->b_die )
156     {
157         int i;
158         if( p_sys->b_update == VLC_TRUE )
159         {
160             msg_Dbg( p_sd, "Update required" );
161             psz_urls = var_GetString( p_sd, "podcast-urls" );
162             ParseUrls( p_sd, psz_urls );
163             free( psz_urls );
164             p_sys->b_update = VLC_FALSE;
165         }
166
167         for( i = 0; i < p_sd->p_sys->i_input; i++ )
168         {
169             if( p_sd->p_sys->pp_input[i]->b_eof
170                 || p_sd->p_sys->pp_input[i]->b_error )
171             {
172                 input_StopThread( p_sd->p_sys->pp_input[i] );
173                 input_DestroyThread( p_sd->p_sys->pp_input[i] );
174                 p_sd->p_sys->pp_input[i] = NULL;
175                 REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
176                 i--;
177             }
178         }
179         msleep( 500 );
180     }
181 }
182
183 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
184                        vlc_value_t oldval, vlc_value_t newval,
185                        void *p_data )
186 {
187     services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)p_data;
188     p_sys->b_update = VLC_TRUE;
189     return VLC_SUCCESS;
190 }
191
192 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
193 {
194     services_discovery_sys_t *p_sys = p_sd->p_sys;
195     for( ;; )
196     {
197         int i;
198         char *psz_tok = strchr( psz_urls, '|' );
199         if( psz_tok ) *psz_tok = '\0';
200         for( i = 0; i < p_sys->i_urls; i++ )
201             if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
202                 break;
203         if( i == p_sys->i_urls )
204         {
205             /* Only add new urls.
206              * FIXME: We don't delete urls which have been removed from
207              * the config since we don't have a way to know which inputs
208              * they spawned */
209             input_item_t *p_input;
210             INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
211                          strdup( psz_urls ) );
212             p_input = input_ItemNewExt( p_sd, psz_urls,
213                                         psz_urls, 0, NULL, -1 );
214             input_ItemAddOption( p_input, "demux=podcast" );
215             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
216             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
217                          input_CreateThread( p_sd, p_input ) );
218         }
219         if( psz_tok )  psz_urls = psz_tok+1;
220         else           return;
221     }
222 }