/*  Copyright (C) 2001-2002  Kenichi Suto
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/


#include "defs.h"
#include "global.h"

GtkWidget *external_dlg;
GtkWidget *entry_wave;
GtkWidget *entry_mpeg;
GtkWidget *entry_browser;

void play_external(gchar *filename, gint type)
{
	gchar cmd[512];
	pid_t pid;
//	gint status;
	gchar *p;
	gchar *template;


	switch(type){
	case LINK_TYPE_MPEG:
		template = strdup(mpeg_template);
		break;
	case LINK_TYPE_WAVE:
		template = strdup(wave_template);
		break;
	default:
		return;
		break;
	}

	p = strstr(template, "%f");
	if(p != NULL){
		*p = '%';
		p++;
		*p = 's';
	}

	switch(pid = fork()){
	case -1:
		perror("fork");
		exit(1);
	case  0:
		sprintf(cmd, template, filename);
		g_print("Lanuching external command : %s\n", cmd);
		system(cmd);

#if 0
		split_word(cmd, words);

		for (i = 0; words[i] != NULL; i++);
		if(execvp(words[0], words) == -1){
			perror("exec");
			_exit(100);
		}
		free_words(words);
#endif
		_exit(0);
		
		break;
	default:
		g_print("child is %d\n", pid);
//		waitpid(pid, &status, 0);
	}
	free(template);
}


static void ok_external(GtkWidget *widget, gpointer *data){

	gchar *text;

	gtk_grab_remove(external_dlg);

	text = gtk_entry_get_text(GTK_ENTRY(entry_mpeg));
	if(mpeg_template)
		free(mpeg_template);
	mpeg_template = strdup(text);

	text = gtk_entry_get_text(GTK_ENTRY(entry_wave));
	if(wave_template)
		free(wave_template);
	wave_template = strdup(text);

#ifdef ENABLE_WEBSEARCH

	text = gtk_entry_get_text(GTK_ENTRY(entry_browser));
	if(browser_template)
		free(browser_template);
	browser_template = strdup(text);
#endif

	save_preference();

	gtk_widget_destroy(GTK_WIDGET(data));
	
	auto_lookup_resume();
}

static void delete_event( GtkWidget *widget,
		   GdkEvent  *event,
		   gpointer   data )
{
	gtk_grab_remove(external_dlg);

	gtk_widget_destroy(GTK_WIDGET(widget));
	
	auto_lookup_resume();
}


void preference_external()
{
	GtkWidget *button;
	GtkWidget *hbox;
	GtkWidget *vbox;
	GtkWidget *label;

	auto_lookup_suspend();

	external_dlg = gtk_dialog_new();
	gtk_window_set_position(GTK_WINDOW(external_dlg), GTK_WIN_POS_CENTER_ALWAYS);


//	gtk_widget_set_usize(external_dlg,400,280);
	
	gtk_grab_add(external_dlg);
	gtk_widget_realize(external_dlg);
	gtk_signal_connect (GTK_OBJECT (external_dlg), "delete_event",
			    GTK_SIGNAL_FUNC (delete_event), NULL);

	button = gtk_button_new_with_label(_("Ok"));
	GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
	gtk_box_pack_start (GTK_BOX (GTK_DIALOG (external_dlg)->action_area), button,
			    TRUE, TRUE, 0);

	gtk_signal_connect (GTK_OBJECT (button), "clicked",
			    GTK_SIGNAL_FUNC (ok_external), (gpointer)external_dlg);
	gtk_widget_grab_default (button);


	vbox = (GtkWidget *)(GTK_DIALOG(external_dlg)->vbox);
	gtk_container_border_width(GTK_CONTAINER(vbox), 5);
//	gtk_container_add(GTK_CONTAINER(frame), vbox);


	hbox = gtk_hbox_new(FALSE,10);
	gtk_container_border_width(GTK_CONTAINER(hbox), 2);
	gtk_box_pack_start (GTK_BOX(vbox)
			    , hbox,FALSE, FALSE, 0);

	label = gtk_label_new(_("Command to play sound "));
	gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
//	gtk_widget_set_usize( label, 120, 20 );
	gtk_box_pack_start (GTK_BOX(hbox), label,FALSE, FALSE, 0);


	entry_wave = gtk_entry_new();
	gtk_widget_set_usize(entry_wave,250,20);
	gtk_box_pack_start (GTK_BOX(hbox)
			    , entry_wave,FALSE, FALSE, 0);
	gtk_tooltips_set_tip(tooltip, entry_wave, 
			     _("External command to play WAVE sound. %f will be replaced by data file name."),"Private");

	gtk_entry_set_text(GTK_ENTRY(entry_wave), wave_template);


	hbox = gtk_hbox_new(FALSE,10);
	gtk_container_border_width(GTK_CONTAINER(hbox), 2);
	gtk_box_pack_start (GTK_BOX(vbox)
			    , hbox,FALSE, FALSE, 0);

	label = gtk_label_new(_("Command to play movie "));
	gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
//	gtk_widget_set_usize( label, 120, 20 );
	gtk_box_pack_start (GTK_BOX(hbox), label,FALSE, FALSE, 0);


	entry_mpeg = gtk_entry_new();
	gtk_widget_set_usize(entry_mpeg,250,20);
	gtk_box_pack_start (GTK_BOX(hbox)
			    , entry_mpeg,FALSE, FALSE, 0);
	gtk_tooltips_set_tip(tooltip, entry_mpeg, 
			     _("External command to play MPEG movie. %f will be replaced by data file name."),"Private");
	gtk_entry_set_text(GTK_ENTRY(entry_mpeg), mpeg_template);



#ifdef ENABLE_WEBSEARCH
	hbox = gtk_hbox_new(FALSE,10);
	gtk_container_border_width(GTK_CONTAINER(hbox), 2);
	gtk_box_pack_start (GTK_BOX(vbox)
			    , hbox,FALSE, FALSE, 0);


	label = gtk_label_new(_("Command to launch web browser "));
	gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
//	gtk_widget_set_usize( label, 120, 20 );
	gtk_box_pack_start (GTK_BOX(hbox), label,FALSE, FALSE, 0);


	entry_browser = gtk_entry_new();
	gtk_widget_set_usize(entry_browser,250,20);
	gtk_box_pack_start (GTK_BOX(hbox)
			    , entry_browser,FALSE, FALSE, 0);
	gtk_tooltips_set_tip(tooltip, entry_browser, 
			     _("External command to launch Web browser. %f will be replaced by URL."),"Private");
	gtk_entry_set_text(GTK_ENTRY(entry_browser), browser_template);
#endif

	gtk_widget_show_all(external_dlg);
}



syntax highlighted by Code2HTML, v. 0.9.1