/*****************************************************************************/
/*
 *      rstpwin.c -- REALmagic rtsp streaming client GTK+ graphical elements
 *
 *      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"
#include "vodservertype.h"

#include "../pixmaps/REALmagic.xpm"
 /* #include "../pixmaps/logo.xpm" */
#include "../pixmaps/fileopen.xpm"
#include "vodservertype.h"

gboolean gtk_destroy_stream_window(GtkWidget *widget, gpointer data)
{
  if(GUI.stream_window != NULL)
    {
      gtk_grab_remove(GUI.stream_window);
      gtk_widget_destroy(GUI.stream_window);
      GUI.stream_window = NULL;
    }
  return TRUE;
}

GtkWidget *get_VOD_frame()
{
  GtkWidget *frame;
  GtkWidget *table;
  GtkWidget *port_entry, *url_entry;
  GtkWidget *browse;
  GtkWidget *servertype_label, *port_label, *url_label;
  /*GtkWidget *servertype_combo; */
  GtkWidget *menu_servertype;
  GtkItemFactory *item_factory;
  GtkItemFactoryEntry *menu_items_servertype;
  guint i, nmenu_items_servertype;
  GtkWidget *browse_pixmap;

  
  frame = gtk_frame_new(NULL);
  gtk_frame_set_label( GTK_FRAME(frame), "Server and Resource Info");
  gtk_frame_set_label_align( GTK_FRAME(frame), 0.5, 0.0);
  gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_ETCHED_OUT);
  
  table = gtk_table_new (2, 6, FALSE);
  gtk_widget_show (table);
  gtk_container_add (GTK_CONTAINER (frame), table);
  gtk_container_set_border_width (GTK_CONTAINER (table), 5);
  
  servertype_label = gtk_label_new ("Server type:");
  gtk_label_set_justify (GTK_LABEL (servertype_label), GTK_JUSTIFY_RIGHT);
  gtk_table_attach (GTK_TABLE (table), servertype_label, 0, 1, 0, 1,
		    GTK_FILL|GTK_EXPAND,
		    GTK_FILL|GTK_EXPAND,0, 0);
  gtk_widget_show (servertype_label);
  
  nmenu_items_servertype = sizeof(vodservertype) / sizeof(vodservertype[0]);
  if(!nmenu_items_servertype)
    G_WARNING("We're in trouble with the the servertype menu ...");
  menu_items_servertype = g_new(GtkItemFactoryEntry, nmenu_items_servertype);

  for(i=0;i<nmenu_items_servertype;i++)
    {
      menu_items_servertype[i].path = vodservertype[i].str;
      menu_items_servertype[i].accelerator = NULL;
      menu_items_servertype[i].callback = combo_servertype_cb;
      menu_items_servertype[i].callback_action = vodservertype[i].code;
      menu_items_servertype[i].item_type = NULL;
    }
  
  item_factory = gtk_item_factory_new (GTK_TYPE_OPTION_MENU, "<ServerType>", 
				       NULL);
  gtk_item_factory_create_items (item_factory, nmenu_items_servertype, menu_items_servertype, NULL);  

  menu_servertype = gtk_item_factory_get_widget (item_factory, "<ServerType>");

  gtk_widget_ref(menu_servertype);
  gtk_object_set_data_full (GTK_OBJECT (frame), "menu_servertype", menu_servertype,
			    (GtkDestroyNotify) gtk_widget_unref);
  
  gtk_table_attach (GTK_TABLE (table), menu_servertype, 1, 6, 0, 1,
		    GTK_FILL, 		    
		    GTK_FILL,0, 0);
  gtk_widget_show (menu_servertype);
  gtk_option_menu_set_history(GTK_OPTION_MENU(menu_servertype), 
			      0);
  Info.bserver	= FILE_VOD;
  
  /* ip_label = gtk_label_new ("IP:"); */
/*   gtk_label_set_justify (GTK_LABEL (servertype_label), GTK_JUSTIFY_RIGHT); */
/*   gtk_table_attach (GTK_TABLE (table), ip_label, 2, 3, 0, 1, */
/* 		    GTK_FILL, */
/* 		    GTK_FILL,0, 0); */
/*   gtk_widget_show (ip_label); */

/*   ip_entry = gtk_entry_new(); */
/*   gtk_entry_set_max_length(GTK_ENTRY(servertype_entry),15); */
/*   gtk_widget_ref (ip_entry); */
/*   gtk_object_set_data_full (GTK_OBJECT (frame), "ip_entry", ip_entry, */
/* 			    (GtkDestroyNotify) gtk_widget_unref); */
/*   gtk_entry_set_editable(GTK_ENTRY(ip_entry),TRUE); */
/*   gtk_table_attach (GTK_TABLE (table), ip_entry, 3, 4, 0, 1, */
/* 		    GTK_FILL, */
/* 		    GTK_FILL,0, 0); */
/*   gtk_widget_show (ip_entry); */
  

  port_label = gtk_label_new ("Client Port:");
  gtk_label_set_justify (GTK_LABEL (port_label), GTK_JUSTIFY_RIGHT);
  gtk_table_attach (GTK_TABLE (table), port_label, 2, 3, 0, 1,
		    GTK_FILL,
		    GTK_FILL,0, 0);
  //gtk_widget_show (port_label);

  port_entry = gtk_entry_new();
  gtk_entry_set_max_length(GTK_ENTRY(port_entry),5);
  gtk_widget_ref (port_entry);
  gtk_object_set_data_full (GTK_OBJECT (frame), "port_entry", port_entry,
			    (GtkDestroyNotify) gtk_widget_unref);
  gtk_entry_set_editable(GTK_ENTRY(port_entry),TRUE);
  gtk_table_attach (GTK_TABLE (table), port_entry, 3, 4, 0, 1,
		    GTK_FILL,
		    GTK_FILL,0, 0);
  //gtk_widget_show (port_entry);

  url_label = gtk_label_new ("URL:");
  gtk_label_set_justify (GTK_LABEL (url_label), GTK_JUSTIFY_RIGHT);
  gtk_table_attach (GTK_TABLE (table), url_label, 0, 1, 1, 2,
		    GTK_FILL,
		    GTK_FILL,0, 0);
  gtk_widget_show (url_label);

  url_entry = gtk_entry_new();
  gtk_entry_set_max_length(GTK_ENTRY(url_entry),320);
  gtk_widget_ref (url_entry);
  gtk_object_set_data_full (GTK_OBJECT (frame), "url_entry", url_entry,
			    (GtkDestroyNotify) gtk_widget_unref);
  gtk_entry_set_editable(GTK_ENTRY(url_entry),TRUE);
  gtk_table_attach (GTK_TABLE (table), url_entry, 1, 5, 1, 2,
		    GTK_FILL,
		    GTK_FILL,0, 0);
  gtk_widget_show (url_entry);

  browse = gtk_button_new();
  gtk_widget_ref (browse);
  gtk_object_set_data_full (GTK_OBJECT (frame), "browse", browse,
			    (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (browse);
  gtk_table_attach (GTK_TABLE (table), browse, 5, 6, 1, 2,
		    GTK_FILL,
		    GTK_FILL,0, 0);
  browse_pixmap = create_pixmap (frame, fileopen_xpm);
  gtk_widget_show (browse_pixmap);
  gtk_container_add (GTK_CONTAINER (browse), browse_pixmap);

  /* packetsize_label = gtk_label_new ("Packets size:"); */
/*   gtk_label_set_justify (GTK_LABEL (packetsize_label), GTK_JUSTIFY_RIGHT); */
/*   gtk_table_attach (GTK_TABLE (table), packetsize_label, 0, 1, 2, 3, */
/* 		    GTK_FILL, */
/* 		    GTK_FILL,0, 0); */
/*   gtk_widget_show (packetsize_label); */

/*   packetsize_entry = gtk_entry_new(); */
/*   gtk_entry_set_max_length(GTK_ENTRY(packetsize_entry),5); */
/*   gtk_widget_ref (packetsize_entry); */
/*   gtk_object_set_data_full (GTK_OBJECT (frame), "packetsize_entry", packetsize_entry, */
/* 			    (GtkDestroyNotify) gtk_widget_unref); */
/*   gtk_entry_set_editable(GTK_ENTRY(packetsize_entry),TRUE); */
/*   gtk_table_attach (GTK_TABLE (table), packetsize_entry, 1, 2, 2, 3, */
/* 		    GTK_FILL, */
/* 		    GTK_FILL,0, 0); */
/*   gtk_widget_show (packetsize_entry); */

/*   nbuffer_label = gtk_label_new ("N buffers:"); */
/*   gtk_label_set_justify (GTK_LABEL (nbuffer_label), GTK_JUSTIFY_RIGHT); */
/*   gtk_table_attach (GTK_TABLE (table), nbuffer_label, 2, 3, 2, 3, */
/* 		    GTK_FILL, */
/* 		    GTK_FILL,0, 0); */
/*   gtk_widget_show (nbuffer_label); */

/*   nbuffer_entry = gtk_entry_new(); */
/*   gtk_entry_set_max_length(GTK_ENTRY(nbuffer_entry),2); */
/*   gtk_widget_ref (nbuffer_entry); */
/*   gtk_object_set_data_full (GTK_OBJECT (frame), "nbuffer_entry", nbuffer_entry, */
/* 			    (GtkDestroyNotify) gtk_widget_unref); */
/*   gtk_entry_set_editable(GTK_ENTRY(nbuffer_entry),TRUE); */
/*   gtk_table_attach (GTK_TABLE (table), nbuffer_entry, 3, 4, 2, 3, */
/* 		    GTK_FILL, */
/* 		    GTK_FILL,0, 0); */
/*   gtk_widget_show (nbuffer_entry); */

/*   ncheck_label = gtk_label_new ("N checks:"); */
/*   gtk_label_set_justify (GTK_LABEL (ncheck_label), GTK_JUSTIFY_RIGHT); */
/*   gtk_table_attach (GTK_TABLE (table), ncheck_label, 4, 5, 2, 3, */
/* 		    GTK_FILL, */
/* 		    GTK_FILL,0, 0); */
/*   gtk_widget_show (ncheck_label); */

/*   ncheck_entry = gtk_entry_new(); */
/*   gtk_entry_set_max_length(GTK_ENTRY(ncheck_entry),2); */
/*   gtk_widget_ref (ncheck_entry); */
/*   gtk_object_set_data_full (GTK_OBJECT (frame), "ncheck_entry", ncheck_entry, */
/* 			    (GtkDestroyNotify) gtk_widget_unref); */
/*   gtk_entry_set_editable(GTK_ENTRY(ncheck_entry),TRUE); */
/*   gtk_table_attach (GTK_TABLE (table), ncheck_entry, 5, 6, 2, 3, */
/* 		    GTK_FILL, */
/* 		    GTK_FILL,0, 0); */
/*   gtk_widget_show (ncheck_entry); */
  
  // gtk_widget_set_sensitive(GTK_WIDGET(ip_entry),FALSE);
  // gtk_widget_set_sensitive(GTK_WIDGET(port_entry),FALSE);
  // gtk_widget_set_sensitive(GTK_WIDGET(packetsize_entry),FALSE);
  // gtk_widget_set_sensitive(GTK_WIDGET(nbuffer_entry),FALSE);
  // gtk_widget_set_sensitive(GTK_WIDGET(ncheck_entry),FALSE);
 
  gtk_signal_connect (GTK_OBJECT (browse), "clicked",
		      GTK_SIGNAL_FUNC (get_rtsp_file_selector),
		      url_entry);
  
  return frame;
}

void get_VOD_playing()
{
  GtkAccelGroup *accel_group;
  GtkWidget *vbox1, *frame, *hbuttonbox1;
  guint Start_key, Cancel_key;
  GtkWidget *Start, *Cancel;

  if (GUI.stream_window!= NULL && GUI.stream_window->window != NULL)
    {
      gdk_window_show(GUI.stream_window->window);
      gdk_window_raise(GUI.stream_window->window);
    }
  else
    {
      accel_group = gtk_accel_group_new ();
      
      GUI.stream_window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_widget_realize(GUI.stream_window);

      gtk_window_set_title (GTK_WINDOW (GUI.stream_window), _("Options ..."));
      gtk_window_set_position (GTK_WINDOW (GUI.stream_window), GTK_WIN_POS_CENTER);
      gtk_window_set_policy (GTK_WINDOW (GUI.stream_window), FALSE, FALSE, TRUE);

      vbox1 = gtk_vbox_new (FALSE, 0);
      gtk_widget_show (vbox1);
      gtk_container_add (GTK_CONTAINER (GUI.stream_window), vbox1);
      gtk_container_set_border_width (GTK_CONTAINER (GUI.stream_window), 5);

      frame = get_VOD_frame();
      gtk_widget_ref (frame);
      gtk_object_set_data_full (GTK_OBJECT (GUI.stream_window), "VOD_frame", frame,
			    (GtkDestroyNotify) gtk_widget_unref);
      gtk_box_pack_start (GTK_BOX (vbox1), frame, TRUE, TRUE, 5);
      gtk_widget_show (frame);
      
      hbuttonbox1 = gtk_hbutton_box_new ();
      gtk_widget_show (hbuttonbox1);
      gtk_box_pack_start (GTK_BOX (vbox1), hbuttonbox1, TRUE, FALSE,5);
      gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox1), GTK_BUTTONBOX_SPREAD);
      
      Start = gtk_button_new_with_label ("");
      Start_key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (Start)->child),
					_("_Start"));
      gtk_widget_add_accelerator (Start, "clicked", accel_group,
				  Start_key, GDK_MOD1_MASK, 0);
      gtk_widget_ref (Start);
      gtk_object_set_data_full (GTK_OBJECT (GUI.stream_window), "Start", Start,
				(GtkDestroyNotify) gtk_widget_unref);
      gtk_widget_show (Start);
      
      gtk_container_add (GTK_CONTAINER (hbuttonbox1), Start);
      gtk_widget_add_accelerator (Start, "clicked", accel_group,
				  GDK_S, GDK_MOD1_MASK,
				  GTK_ACCEL_VISIBLE);
      
      Cancel = gtk_button_new_with_label ("");
      Cancel_key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (Cancel)->child),
					_("_Cancel"));
      gtk_widget_add_accelerator (Cancel, "clicked", accel_group,
				  Cancel_key, GDK_MOD1_MASK, 0);
      gtk_widget_ref (Cancel);
      gtk_object_set_data_full (GTK_OBJECT (GUI.stream_window), "Cancel", Cancel,
				(GtkDestroyNotify) gtk_widget_unref);
      gtk_widget_show (Cancel);
      
      gtk_container_add (GTK_CONTAINER (hbuttonbox1), Cancel);
      gtk_widget_add_accelerator (Cancel, "clicked", accel_group,
				  GDK_C, GDK_MOD1_MASK,
				  GTK_ACCEL_VISIBLE);

      gtk_signal_connect(GTK_OBJECT (GUI.stream_window), "delete_event",
			 GTK_SIGNAL_FUNC (gtk_destroy_stream_window),
			 NULL);
      
      gtk_signal_connect(GTK_OBJECT (GUI.stream_window), "destroy",
			 GTK_SIGNAL_FUNC (gtk_destroy_stream_window),
			 NULL);
      
      gtk_signal_connect(GTK_OBJECT (Cancel), "clicked",
			 GTK_SIGNAL_FUNC (gtk_destroy_stream_window),
			 NULL);

      gtk_signal_connect (GTK_OBJECT (Start), "clicked",
			  GTK_SIGNAL_FUNC (c_ok_VOD_playing),
			  frame);
      
      gtk_window_add_accel_group (GTK_WINDOW (GUI.stream_window), accel_group);
      set_window_icon(GUI.stream_window,REALmagic_xpm);
      
      gtk_window_set_transient_for( GTK_WINDOW(GUI.stream_window), GTK_WINDOW(GUI.window));
      gtk_grab_add(GUI.stream_window);
      
      gtk_widget_show(GUI.stream_window);  
    }

}

gboolean gtk_destroy_rtsp_file_selector(GtkWidget *widget, gpointer data)
{
  
  if(GUI.filew != NULL)
    {
      gtk_grab_remove(GUI.filew);
      gtk_widget_destroy(GUI.filew);
      GUI.filew = NULL;
    }
  
  return TRUE;
}

void get_rtsp_file_selector(GtkWidget  *widget, gpointer data)
{ 
  if (GUI.filew != NULL && GUI.filew->window != NULL)
    {
      gdk_window_show(GUI.filew->window);
      gdk_window_raise(GUI.filew->window);
    }
  else
    {
      GUI.filew = gtk_file_selection_new ("Open a file ...");
      gtk_widget_ref (data);
      gtk_object_set_data_full (GTK_OBJECT (GUI.filew), "url_entry", data,
				(GtkDestroyNotify) gtk_widget_unref);
      /*gtk_window_set_modal(filew,TRUE);*/
      gtk_signal_connect(GTK_OBJECT (GUI.filew), "destroy",
			 (GtkSignalFunc) gtk_destroy_rtsp_file_selector,
			 NULL);
     
      gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (GUI.filew)->ok_button),
			  "clicked", (GtkSignalFunc) VOD_ok_sel, GUI.filew );
      gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(GUI.filew)->cancel_button),
			  "clicked", (GtkSignalFunc) gtk_destroy_rtsp_file_selector,
			  NULL);
      
      if(Info.sUrl != NULL)
	gtk_file_selection_set_filename (GTK_FILE_SELECTION(GUI.filew),
					 (gchar *)Info.sUrl);
      else if(Info.sFilename != NULL)
	gtk_file_selection_set_filename (GTK_FILE_SELECTION(GUI.filew),
					 (gchar *) Info.sFilename);
      else
	gtk_file_selection_set_filename (GTK_FILE_SELECTION(GUI.filew), 
					 (gchar *) "You have to seek for a file !");
      
      set_window_icon(GUI.filew,fileopen_xpm);
      gtk_window_set_transient_for( GTK_WINDOW(GUI.filew), GTK_WINDOW(GUI.stream_window));
      gtk_grab_add(GUI.filew);
      gtk_widget_show(GUI.filew);
      gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(GUI.filew));
    }
}
