* Allow service discoveries to state whether they prefer being displayed as tree
[vlc.git] / modules / services_discovery / shout.c
1 /*****************************************************************************
2  * shout.c:  Shoutcast services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *          Antoine Cellerier <dionoea -@T- videolan -d.t- 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 /*****************************************************************************
26  * Includes
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32 #include <vlc_interaction.h>
33
34 #include <vlc/input.h>
35
36 #include "network.h"
37
38 #include <errno.h>                                                 /* ENOMEM */
39
40 #ifdef HAVE_UNISTD_H
41 #    include <unistd.h>
42 #endif
43 #ifdef HAVE_SYS_TIME_H
44 #    include <sys/time.h>
45 #endif
46
47 /************************************************************************
48  * Macros and definitions
49  ************************************************************************/
50
51 #define MAX_LINE_LENGTH 256
52 #define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
53 #define SHOUTCAST_TV_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newtvlister.phtml?alltv=1"
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58
59 /* Callbacks */
60     static int  Open ( vlc_object_t *, int );
61     static int  OpenRadio ( vlc_object_t * );
62     static int  OpenTV ( vlc_object_t * );
63     static void Close( vlc_object_t * );
64
65 vlc_module_begin();
66     set_shortname( "Shoutcast");
67     set_description( _("Shoutcast radio listings") );
68     add_shortcut( "shoutcast" );
69     set_category( CAT_PLAYLIST );
70     set_subcategory( SUBCAT_PLAYLIST_SD );
71
72     add_suppressed_integer( "shoutcast-limit" );
73
74     set_capability( "services_discovery", 0 );
75     set_callbacks( OpenRadio, Close );
76
77     add_submodule();
78         set_shortname( "ShoutcastTV" );
79         set_description( _("Shoutcast TV listings") );
80         set_capability( "services_discovery", 0 );
81         set_callbacks( OpenTV, Close );
82         add_shortcut( "shoutcasttv" );
83
84 vlc_module_end();
85
86
87 /*****************************************************************************
88  * Local structures
89  *****************************************************************************/
90
91 struct services_discovery_sys_t
92 {
93     playlist_item_t *p_node_cat,*p_node_one;
94     input_item_t *p_input;
95     vlc_bool_t b_dialog;
96 };
97
98 #define RADIO 0
99 #define TV 1
100
101 /*****************************************************************************
102  * Local prototypes
103  *****************************************************************************/
104
105 /* Main functions */
106     static void Run    ( services_discovery_t *p_intf );
107
108 static int OpenRadio( vlc_object_t *p_this )
109 {
110     return Open( p_this, RADIO );
111 }
112
113 static int OpenTV( vlc_object_t *p_this )
114 {
115     return Open( p_this, TV );
116 }
117
118 /*****************************************************************************
119  * Open: initialize and create stuff
120  *****************************************************************************/
121 static int Open( vlc_object_t *p_this, int i_type )
122 {
123     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
124     services_discovery_sys_t *p_sys  = malloc(
125                                     sizeof( services_discovery_sys_t ) );
126
127     vlc_value_t         val;
128     playlist_t          *p_playlist;
129     playlist_view_t     *p_view;
130
131     p_sd->pf_run = Run;
132     p_sd->p_sys  = p_sys;
133
134     /* Create our playlist node */
135     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
136                                                 FIND_ANYWHERE );
137     if( !p_playlist )
138     {
139         msg_Warn( p_sd, "unable to find playlist, cancelling");
140         return VLC_EGENERIC;
141     }
142
143     switch( i_type )
144     {
145         case TV:
146             p_sys->p_input = input_ItemNewExt( p_playlist,
147                                 SHOUTCAST_TV_BASE_URL, _("Shoutcast TV"),
148                                 0, NULL, -1 );
149             break;
150         case RADIO:
151         default:
152             p_sys->p_input = input_ItemNewExt( p_playlist,
153                                 SHOUTCAST_BASE_URL, _("Shoutcast"),
154                                 0, NULL, -1 );
155             break;
156     }
157     p_sys->p_input->b_prefers_tree = VLC_TRUE;
158     p_sys->p_node_cat = playlist_NodeAddInput( p_playlist, p_sys->p_input,
159                            p_playlist->p_root_category,
160                            PLAYLIST_APPEND, PLAYLIST_END );
161     p_sys->p_node_one = playlist_NodeAddInput( p_playlist, p_sys->p_input,
162                            p_playlist->p_root_onelevel,
163                            PLAYLIST_APPEND, PLAYLIST_END );
164     p_sys->p_node_cat->i_flags |= PLAYLIST_RO_FLAG;
165     p_sys->p_node_cat->i_flags |= PLAYLIST_SKIP_FLAG;
166     p_sys->p_node_one->i_flags |= PLAYLIST_RO_FLAG;
167     p_sys->p_node_one->i_flags |= PLAYLIST_SKIP_FLAG;
168     p_sys->p_node_one->p_input->i_id = p_sys->p_node_cat->p_input->i_id;
169
170     val.b_bool = VLC_TRUE;
171     var_Set( p_playlist, "intf-change", val );
172
173     vlc_object_release( p_playlist );
174
175     return VLC_SUCCESS;
176 }
177
178 /*****************************************************************************
179  * Close:
180  *****************************************************************************/
181 static void Close( vlc_object_t *p_this )
182 {
183     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
184     services_discovery_sys_t *p_sys  = p_sd->p_sys;
185     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
186                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
187     if( p_playlist )
188     {
189         playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE, VLC_TRUE );
190         playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE, VLC_TRUE );
191         vlc_object_release( p_playlist );
192     }
193     free( p_sys );
194 }
195
196 /*****************************************************************************
197  * Run: main thread
198  *****************************************************************************/
199 static void Run( services_discovery_t *p_sd )
200 {
201     services_discovery_sys_t *p_sys  = p_sd->p_sys;
202     int i_id = input_Read( p_sd, p_sys->p_input, VLC_FALSE );
203     int i_dialog_id;
204
205     i_dialog_id = intf_UserProgress( p_sd, "Shoutcast" , 
206                                      _("Connecting...") , 0.0, 0 );
207
208     p_sys->b_dialog = VLC_TRUE;
209     while( !p_sd->b_die )
210     {
211         input_thread_t *p_input = (input_thread_t *)vlc_object_get( p_sd,
212                                                                     i_id );
213
214         /* The Shoutcast server does not return a content-length so we
215          * can't know where we are. Use the number of inserted items
216          * as a hint */
217         if( p_input != NULL )
218         {
219             int i_state = var_GetInteger( p_input, "state" );
220             if( i_state == PLAYING_S )
221             {
222                 float f_pos = (float)(p_sys->p_node_cat->i_children)* 2 *100.0 /
223                               260 /* gruiiik FIXME */;
224                 intf_ProgressUpdate( p_sd, i_dialog_id, "Downloading",
225                                      f_pos, 0 );
226             }
227             vlc_object_release( p_input );
228         }
229         else if( p_sys->b_dialog )
230         {
231             p_sys->b_dialog  = VLC_FALSE;
232             intf_UserHide( p_sd, i_dialog_id );
233         }
234         msleep( 10000 );
235     }
236 }