* removed an useless inclusion
[vlc.git] / modules / gui / gtk / open.c
1 /*****************************************************************************
2  * gtk_open.c : functions to handle file/disc/network open widgets.
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: open.c,v 1.5 2003/01/10 10:01:47 lool Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          St├ęphane Borel <stef@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <sys/types.h>                                              /* off_t */
29 #include <stdlib.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/intf.h>
33
34 #ifdef MODULE_NAME_IS_gnome
35 #   include <gnome.h>
36 #else
37 #   include <gtk/gtk.h>
38 #endif
39
40 #include <string.h>
41
42 #include "gtk_callbacks.h"
43 #include "gtk_interface.h"
44 #include "gtk_support.h"
45
46 #include "common.h"
47
48 #include "netutils.h"
49
50 static void GtkOpenShow( intf_thread_t *, int );
51
52 static void GtkFileOpenChanged    ( GtkWidget *, gpointer );
53 static void GtkDiscOpenChanged    ( GtkWidget *, gpointer );
54 static void GtkNetworkOpenChanged ( GtkWidget *, gpointer );
55 static void GtkSatOpenChanged     ( GtkWidget *, gpointer );
56
57 /*****************************************************************************
58  * File requester callbacks
59  *****************************************************************************
60  * The following callbacks are related to the file requester.
61  *****************************************************************************/
62 void GtkFileShow( GtkButton * button, gpointer user_data )
63 {
64     GtkWidget * p_file = create_intf_file();
65
66     gtk_object_set_data( GTK_OBJECT(p_file), "p_intf",
67                          GtkGetIntf( button ) );
68
69     gtk_widget_show( p_file );
70     gdk_window_raise( p_file->window );
71 }
72
73 void GtkFileOk( GtkButton * button, gpointer user_data )
74 {
75     GtkWidget * p_file = gtk_widget_get_toplevel( GTK_WIDGET (button) );
76
77     char *psz_filename;
78     intf_thread_t * p_intf = GtkGetIntf( button );
79
80     /* add the new file to the dialog box */
81     psz_filename =
82             gtk_file_selection_get_filename( GTK_FILE_SELECTION( p_file ) );
83     gtk_entry_set_text( GTK_ENTRY( lookup_widget( p_intf->p_sys->p_open,
84                                                   "entry_file" ) ),
85                         psz_filename );
86     gtk_widget_destroy( p_file );
87 }
88
89 void GtkFileCancel( GtkButton * button, gpointer user_data )
90 {
91     gtk_widget_destroy( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
92 }
93
94 /*****************************************************************************
95  * Open file callbacks
96  *****************************************************************************
97  * The following callbacks are related to the file tab.
98  *****************************************************************************/
99 gboolean GtkFileOpenShow( GtkWidget       *widget,
100                           gpointer         user_data )
101 {
102     GtkOpenShow( GtkGetIntf( widget ), 0 );
103
104     return TRUE;
105 }
106
107 static void GtkFileOpenChanged( GtkWidget * button, gpointer user_data )
108 {
109     GString *       p_target;
110
111     p_target = g_string_new( "file://" );
112     g_string_append( p_target,
113                      gtk_entry_get_text( GTK_ENTRY( lookup_widget(
114                                      GTK_WIDGET(button), "entry_file" ) ) ) );
115     gtk_entry_set_text( GTK_ENTRY( lookup_widget(
116                                    GTK_WIDGET(button), "entry_open" ) ),
117                         p_target->str );
118     g_string_free( p_target, TRUE );
119 }
120
121 /*****************************************************************************
122  * Open disc callbacks
123  *****************************************************************************
124  * The following callbacks are related to the disc manager.
125  *****************************************************************************/
126 gboolean GtkDiscOpenShow( GtkWidget       *widget,
127                           gpointer         user_data)
128 {
129     GtkOpenShow( GtkGetIntf( widget ), 1 );
130
131     return TRUE;
132 }
133
134 void GtkDiscOpenDvd( GtkToggleButton * togglebutton, gpointer user_data )
135 {
136     intf_thread_t * p_intf = GtkGetIntf( togglebutton );
137     char *psz_device;
138
139     if( togglebutton->active
140          && (psz_device = config_GetPsz( p_intf, "dvd" )) )
141     {
142         gtk_entry_set_text(
143             GTK_ENTRY( lookup_widget( GTK_WIDGET(togglebutton),
144                                       "disc_name" ) ), psz_device );
145         free( psz_device );
146     }
147 }
148
149 void GtkDiscOpenVcd( GtkToggleButton * togglebutton, gpointer user_data )
150 {
151     intf_thread_t * p_intf = GtkGetIntf( togglebutton );
152     char *psz_device;
153
154     if( togglebutton->active
155          && (psz_device = config_GetPsz( p_intf, "vcd" )) )
156     {
157         gtk_entry_set_text(
158             GTK_ENTRY( lookup_widget( GTK_WIDGET(togglebutton),
159                                       "disc_name" ) ), psz_device );
160         free( psz_device );
161     }
162 }
163
164 static void GtkDiscOpenChanged( GtkWidget * button, gpointer user_data )
165 {
166     GString * p_target = g_string_new( "" );
167
168     if( GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button), 
169                                           "disc_dvd" ) )->active )
170     {
171         g_string_append( p_target, "dvd://" );
172     }
173     else if( GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button),
174                                                "disc_vcd" ) )->active )
175     {
176         g_string_append( p_target, "vcd://" );
177     }       
178
179     g_string_append( p_target,
180                      gtk_entry_get_text( GTK_ENTRY( lookup_widget(
181                                      GTK_WIDGET(button), "disc_name" ) ) ) );
182     g_string_sprintfa( p_target, "@%i,%i",
183                        gtk_spin_button_get_value_as_int(
184                               GTK_SPIN_BUTTON( lookup_widget(
185                                   GTK_WIDGET(button), "disc_title" ) ) ),
186                        gtk_spin_button_get_value_as_int(
187                               GTK_SPIN_BUTTON( lookup_widget(
188                                   GTK_WIDGET(button), "disc_chapter" ) ) ) );
189
190     gtk_entry_set_text( GTK_ENTRY( lookup_widget(
191                                    GTK_WIDGET(button), "entry_open" ) ),
192                         p_target->str );
193     g_string_free( p_target, TRUE );
194 }
195
196 /*****************************************************************************
197  * Network stream callbacks
198  *****************************************************************************
199  * The following callbacks are related to the network stream manager.
200  *****************************************************************************/
201 gboolean GtkNetworkOpenShow( GtkWidget       *widget,
202                              gpointer         user_data )
203 {
204     GtkOpenShow( GtkGetIntf( widget ), 2 );
205
206     return TRUE;
207 }
208
209 static void GtkNetworkOpenChanged( GtkWidget *button, gpointer user_data )
210 {
211     intf_thread_t * p_intf = GtkGetIntf( button );
212     GString *       p_target = g_string_new( "" );
213
214     unsigned int    i_port;
215     vlc_bool_t      b_channel;
216
217     /* Manage channel server */
218     b_channel = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(
219             lookup_widget( GTK_WIDGET(button), "network_channel" ) ) );
220     config_PutInt( p_intf, "network-channel", b_channel );
221
222 #define SELECTED( s ) GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button), \
223                        (s) ) )->active
224     /* Check which option was chosen */
225     if( SELECTED( "network_udp" ) )
226     {
227         g_string_append( p_target, "udp://" );
228         i_port = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON(
229                                lookup_widget( GTK_WIDGET(button),
230                                               "network_udp_port" ) ) );
231         if( i_port != 1234 )
232         {
233             g_string_sprintfa( p_target, "@:%i", i_port );
234         }
235     }
236     else if( SELECTED( "network_multicast" ) )
237     {
238         g_string_sprintfa( p_target, "udp://@%s",
239                            gtk_entry_get_text( GTK_ENTRY(
240                             lookup_widget( GTK_WIDGET(button),
241                                            "network_multicast_address" ) ) ) );
242         i_port = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON(
243                                lookup_widget( GTK_WIDGET(button),
244                                               "network_multicast_port" ) ) );
245         if( i_port != 1234 )
246         {
247             g_string_sprintfa( p_target, ":%i", i_port );
248         }
249     }
250     else if( SELECTED( "network_channel" ) )
251     {
252         char *          psz_channel;
253         unsigned int    i_channel_port;
254
255         if( p_intf->p_vlc->p_channel == NULL )
256         {
257             network_ChannelCreate( p_intf );
258         }
259
260         psz_channel = gtk_entry_get_text( GTK_ENTRY( lookup_widget(
261                         GTK_WIDGET(button), "network_channel_address" ) ) );
262         i_channel_port = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON(
263             lookup_widget( GTK_WIDGET(button), "network_channel_port" ) ) );
264
265         config_PutPsz( p_intf, "channel-server", psz_channel );
266         if( i_channel_port < 65536 )
267         {
268             config_PutInt( p_intf, "channel-port", i_channel_port );
269         }
270
271         /* FIXME: we should use a playlist server instead */
272         g_string_append( p_target, "udp://" );
273     }
274     else if( SELECTED( "network_http" ) )
275     {
276         g_string_assign( p_target,
277                          gtk_entry_get_text( GTK_ENTRY( lookup_widget(
278                                GTK_WIDGET(button), "network_http_url" ) ) ) );
279     }
280
281     gtk_entry_set_text( GTK_ENTRY( lookup_widget(
282                                    GTK_WIDGET(button), "entry_open" ) ),
283                         p_target->str );
284     g_string_free( p_target, TRUE );
285 }
286
287 void GtkNetworkOpenUDP( GtkToggleButton *togglebutton,
288                                         gpointer user_data )
289 {
290     GtkWidget *     p_open;
291
292     p_open = gtk_widget_get_toplevel( GTK_WIDGET (togglebutton) );
293
294     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
295                     "network_udp_port_label" ),
296                     gtk_toggle_button_get_active( togglebutton ) );
297     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
298                     "network_udp_port" ),
299                     gtk_toggle_button_get_active( togglebutton ) );
300
301     GtkNetworkOpenChanged( GTK_WIDGET( togglebutton ), user_data );
302 }
303
304 void GtkNetworkOpenMulticast( GtkToggleButton *togglebutton,
305                                               gpointer user_data )
306 {
307     GtkWidget *     p_open;
308
309     p_open = gtk_widget_get_toplevel( GTK_WIDGET (togglebutton) );
310     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
311                     "network_multicast_address_label" ),
312                     gtk_toggle_button_get_active( togglebutton ) );
313     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
314                     "network_multicast_address_combo" ),
315                     gtk_toggle_button_get_active( togglebutton ) );
316
317     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
318                     "network_multicast_port_label" ),
319                     gtk_toggle_button_get_active( togglebutton ) );
320     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
321                     "network_multicast_port" ),
322                     gtk_toggle_button_get_active( togglebutton ) );
323
324     GtkNetworkOpenChanged( GTK_WIDGET( togglebutton ), user_data );
325 }
326
327
328 void GtkNetworkOpenChannel( GtkToggleButton *togglebutton,
329                                        gpointer user_data )
330 {
331     GtkWidget *     p_open;
332
333     p_open = gtk_widget_get_toplevel( GTK_WIDGET (togglebutton) );
334     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
335                     "network_channel_address_label" ),
336                     gtk_toggle_button_get_active( togglebutton ) );
337     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
338                     "network_channel_address_combo" ),
339                     gtk_toggle_button_get_active( togglebutton ) );
340
341     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
342                     "network_channel_port_label" ),
343                     gtk_toggle_button_get_active( togglebutton ) );
344     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
345                     "network_channel_port" ),
346                     gtk_toggle_button_get_active( togglebutton ) );
347
348     GtkNetworkOpenChanged( GTK_WIDGET( togglebutton ), user_data );
349 }
350
351 void GtkNetworkOpenHTTP( GtkToggleButton *togglebutton,
352                                          gpointer user_data )
353 {   
354     GtkWidget *     p_open;
355
356     p_open = gtk_widget_get_toplevel( GTK_WIDGET (togglebutton) );
357     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
358                     "network_http_url_label" ),
359                     gtk_toggle_button_get_active( togglebutton ) );
360     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_open ),
361                     "network_http_url" ),
362                     gtk_toggle_button_get_active( togglebutton ) );
363
364     GtkNetworkOpenChanged( GTK_WIDGET( togglebutton ), user_data );
365 }
366
367 /*****************************************************************************
368  * Open satellite callbacks
369  *****************************************************************************
370  * The following callbacks are related to the satellite card manager.
371  *****************************************************************************/
372 gboolean GtkSatOpenShow( GtkWidget       *widget,
373                          gpointer         user_data)
374 {
375     GtkOpenShow( GtkGetIntf( widget ), 3 );
376
377     return TRUE;
378 }
379
380 static void GtkSatOpenChanged( GtkWidget * button, gpointer user_data )
381 {
382     GString *       p_target = g_string_new( "" );
383
384     g_string_sprintfa( p_target, "%s://%d,%d,%ld,%d", "satellite",
385                        gtk_spin_button_get_value_as_int(
386                               GTK_SPIN_BUTTON( lookup_widget(
387                                   GTK_WIDGET(button), "sat_freq" ) ) ),
388                        !GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET( button ),
389                                                "sat_pol_vert" ) )->active,
390                        strtol( gtk_entry_get_text( GTK_ENTRY( GTK_COMBO( 
391                                lookup_widget( GTK_WIDGET( button ), "sat_fec" )
392                                )->entry ) ), NULL, 10 ),
393                        gtk_spin_button_get_value_as_int(
394                               GTK_SPIN_BUTTON( lookup_widget(
395                                   GTK_WIDGET(button), "sat_srate" ) ) ) );
396
397     gtk_entry_set_text( GTK_ENTRY( lookup_widget(
398                                    GTK_WIDGET(button), "entry_open" ) ),
399                         p_target->str );
400     g_string_free( p_target, TRUE );
401 }
402
403 void
404 GtkSatOpenToggle                       (GtkToggleButton *togglebutton,
405                                         gpointer         user_data)
406 {
407     if( togglebutton->active )
408     {
409         GtkSatOpenChanged( GTK_WIDGET( togglebutton ), user_data );
410     }
411 }
412
413 /******************************
414   ******************************/
415
416 static void GtkOpenShow( intf_thread_t *p_intf, int i_page )
417 {
418     char *psz_var;
419     GtkWidget *p_notebook;
420
421     /* If we have already created this window, do nothing */
422     if( GTK_IS_WIDGET( p_intf->p_sys->p_open ) )
423     {
424         goto setpage;
425     }
426
427     p_intf->p_sys->p_open = create_intf_open();
428     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_open ),
429                          "p_intf", p_intf );
430
431     /* FileOpen stuff */
432     psz_var = config_GetPsz( p_intf, "search-path" );
433     if( psz_var )
434     {
435         gtk_file_selection_set_filename( GTK_FILE_SELECTION(
436             p_intf->p_sys->p_open ), psz_var );
437         free( psz_var );
438     }
439
440     /* Disc stuff */
441     psz_var = config_GetPsz( p_intf, "dvd" );
442     if( psz_var )
443     {
444         gtk_entry_set_text( GTK_ENTRY( gtk_object_get_data(
445             GTK_OBJECT( p_intf->p_sys->p_open ), "disc_name" ) ),
446             psz_var );
447         free( psz_var );
448     }
449
450     /* Network stuff */
451     gtk_spin_button_set_value( GTK_SPIN_BUTTON( gtk_object_get_data(
452         GTK_OBJECT( p_intf->p_sys->p_open ), "network_udp_port" ) ),
453         config_GetInt( p_intf, "server-port" ) );
454
455     psz_var = config_GetPsz( p_intf, "channel-server" );
456     if( psz_var )
457     {
458         gtk_entry_set_text( GTK_ENTRY( gtk_object_get_data(
459             GTK_OBJECT( p_intf->p_sys->p_open ), "network_channel_address" ) ),
460             psz_var );
461         free( psz_var );
462     }
463
464     gtk_entry_set_text( GTK_ENTRY( gtk_object_get_data(
465         GTK_OBJECT( p_intf->p_sys->p_open ), "network_http_url" ) ),
466         "http://" );
467
468     gtk_spin_button_set_value( GTK_SPIN_BUTTON( gtk_object_get_data(
469         GTK_OBJECT( p_intf->p_sys->p_open ), "network_channel_port" ) ),
470         config_GetInt( p_intf, "channel-port" ) );
471
472     gtk_toggle_button_set_active( gtk_object_get_data(
473         GTK_OBJECT( p_intf->p_sys->p_open ), "network_channel" ),
474         config_GetInt( p_intf, "network-channel" ) );
475
476     /* Satellite stuff */
477     psz_var = config_GetPsz( p_intf, "frequency" );
478     if( psz_var )
479     {
480         gtk_entry_set_text( GTK_ENTRY( gtk_object_get_data(
481             GTK_OBJECT( p_intf->p_sys->p_open ), "sat_freq" ) ),
482             psz_var );
483         free( psz_var );
484     }
485
486     psz_var = config_GetPsz( p_intf, "symbol-rate" );
487     if( psz_var )
488     {
489         gtk_entry_set_text( GTK_ENTRY( gtk_object_get_data(
490             GTK_OBJECT( p_intf->p_sys->p_open ), "sat_srate" ) ),
491             psz_var );
492         free( psz_var );
493     }
494
495     /* Set the right page */
496 setpage:
497     p_notebook = lookup_widget( GTK_WIDGET( p_intf->p_sys->p_open ),
498                                 "open_notebook" );
499     gtk_notebook_set_page( GTK_NOTEBOOK( p_notebook ), i_page );
500
501     gtk_widget_show( p_intf->p_sys->p_open );
502     gdk_window_raise( p_intf->p_sys->p_open->window );
503 }
504
505 void GtkOpenOk( GtkButton * button, gpointer user_data )
506 {
507     /* Check what was pressed */
508     intf_thread_t * p_intf = GtkGetIntf( button );
509     playlist_t *    p_playlist;
510     GtkCList *      p_playlist_clist;
511     gchar *         psz_target;
512
513     /* Hide the dialog box */
514     gtk_widget_hide( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
515
516     /* Update the playlist */
517     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
518     if( p_playlist == NULL )
519     {
520         return;
521     }
522
523     psz_target = gtk_entry_get_text( GTK_ENTRY( lookup_widget(
524                                        GTK_WIDGET(button), "entry_open" ) ) );
525     playlist_Add( p_playlist, (char*)psz_target,
526                   PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
527
528     /* catch the GTK CList */
529     p_playlist_clist = GTK_CLIST( gtk_object_get_data(
530         GTK_OBJECT( p_intf->p_sys->p_playwin ), "playlist_clist" ) );
531     /* update the plugin display */
532     GtkRebuildCList( p_playlist_clist, p_playlist );
533
534     vlc_object_release( p_playlist );
535 }
536
537 void GtkOpenCancel( GtkButton * button, gpointer user_data )
538 {
539     gtk_widget_hide( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
540 }
541
542 void GtkOpenChanged( GtkWidget * button, gpointer user_data )
543 {
544     intf_thread_t * p_intf = GtkGetIntf( button );
545     GtkWidget *p_notebook;
546     int i_page;
547
548     p_notebook = lookup_widget( GTK_WIDGET( p_intf->p_sys->p_open ),
549                                 "open_notebook" );
550     i_page = gtk_notebook_get_current_page( GTK_NOTEBOOK( p_notebook ) );
551
552     switch( i_page )
553     {
554         case 0:
555             GtkFileOpenChanged( button, NULL );
556             break;
557         case 1:
558             GtkDiscOpenChanged( button, NULL );
559             break;
560         case 2:
561             GtkNetworkOpenChanged( button, NULL );
562             break;
563         case 3:
564             GtkSatOpenChanged( button, NULL );
565             break;
566     }
567 }
568