/*****************************************************************************/
/*
 *      rtspwincb.c -- REALmagic rtsp streaming client GTK+ callbacks
 *
 *      Copyright (C) 2001 Sigma Designs
 *                    Pascal Cannenterre <pascal_cannenterre@sdesigns.com>
 *                    Tom Van Hees       <tvh@sdesigns.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*****************************************************************************/

#include "define.h"
#include "gtk_callbacks.h"
#include "interface.h"

static gint global_servertype = -1;

void combo_servertype_cb(GtkWidget *widget, guint action, gpointer data)
{
  global_servertype = (char) action;
  Info.bserver = (char) global_servertype;
}

void VOD_ok_sel( GtkWidget *w,GtkFileSelection *fs)
{
  GtkEntry *url_entry;
  gchar *str_url_set;
  FILE *file;
  
  str_url_set = g_new(gchar,255);
  
  gtk_widget_hide(GTK_WIDGET(fs));
  url_entry = GTK_ENTRY(gtk_object_get_data (GTK_OBJECT (fs),"url_entry"));
  g_free(Info.sFilename);
  Info.sFilename = g_strdup(gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs)));
  g_warning("This is the filename from VOD_ok_sel : %s", Info.sFilename);
  file = fopen(Info.sFilename,"r");
  if(file != NULL)
    {
      if(Info.bserver != FILE_VOD)
	{
	  fscanf(file,"%s",str_url_set);
	  gtk_entry_set_text(GTK_ENTRY(url_entry),str_url_set);
	}
      else
	{
	  gtk_entry_set_text(GTK_ENTRY(url_entry),Info.sFilename);
	}
      fclose(file);
      gtk_destroy_rtsp_file_selector(NULL, NULL);
    }
  else
    {
      gtk_destroy_rtsp_file_selector(NULL, NULL);
      gtk_message_box("Error",
		      "Can't open the specified file",
		      g_strconcat("Can't open ",file," ."),
		      TRUE,
		      NULL,
		      NULL);
    }
  g_free(str_url_set);
}

void c_ok_VOD_playing(GtkWidget *widget, gpointer data)
{
  GtkWidget *port_entry, *url_entry;
  guint text_l;
  gchar *str_port, *str_url;
  RMuint16 uint16_port;
  
  gtk_widget_hide(GUI.stream_window);
  
  if(Info.bDiscType != RTSPURL)
    toggle_player_mode(RTSPURL);
  else
    {
      close_cb(NULL, NULL);
      usleep(WAIT_FOR_FMPCLOSE);
    }
 
  port_entry = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"port_entry"));
  text_l = GTK_ENTRY(port_entry)->text_length;
  if( text_l != 0)
    {
      str_port = gtk_editable_get_chars(GTK_EDITABLE(port_entry),0,text_l);
      g_strstrip(str_port);
      uint16_port = (RMuint16)(g_strtod(str_port, NULL));
      g_free(str_port);
      if(uint16_port < 1024 || uint16_port > 65535)
	Info.iRTPPort = 20000;
      else
	Info.iRTPPort = uint16_port;
    }
  else
    Info.iRTPPort = 20000;
  
  url_entry = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"url_entry"));
  text_l = GTK_ENTRY(url_entry)->text_length;
  
  Info.bserver = global_servertype;
  
  if(text_l != 0 && Info.bserver != -1)
    {
      str_url = gtk_editable_get_chars(GTK_EDITABLE(url_entry),0,255);
      g_strstrip(str_url);
      g_free(Info.sUrl);
      Info.sUrl = g_strdup(str_url);
      g_free(str_url);
      
      play_cb(NULL, NULL);
      
      gtk_window_set_title (GTK_WINDOW (GUI.window), g_strconcat(GTKPLAYERNAME, " : ", 
							     (gchar *) Info.sUrl, NULL) );
    }
  else if(text_l == 0)
    {
      gtk_message_box("Error",
		      "Empty URL field",
		      "You must, at least, provide a valid URL.",
		      TRUE,
		      NULL,
		      NULL);
    }

  gtk_destroy_stream_window(NULL, NULL);
}

