renatofilho@20: #include<gtk/gtk.h>
renatofilho@20: #include<glib.h>
renatofilho@20: #include <sys/types.h>
renatofilho@20: #include <sys/stat.h>
renatofilho@20: #include <unistd.h>
renatofilho@20: #include <string.h>
renatofilho@20: #include <stdio.h>
renatofilho@20: #include <stdlib.h>
renatofilho@20: 
renatofilho@20: #include "mmyth_uisettings.h"
renatofilho@20: 
rosfran@208: #include <gmyth/gmyth_backendinfo.h>
renatofilho@20: 
renatofilho@20: static GtkWidget *settings_dialog;
renatofilho@20: static GtkWidget *entry_hostname;
renatofilho@20: static GtkWidget *entry_dbname;
renatofilho@20: static GtkWidget *entry_username;
renatofilho@20: static GtkWidget *entry_passwd;
renatofilho@20: static GtkWidget *entry_port;
renatofilho@20: 
renatofilho@20: static void settings_dialog_update_data (void);
rosfran@208: static GtkWidget* add_entry_to_table (GtkWidget *table, const gchar *init_str,
renatofilho@20: 	guint pos_left, guint pos_right, guint pos_top, guint pos_bottom);
renatofilho@20: static GtkWidget* add_label_to_table (GtkWidget *table, const gchar *str, 
renatofilho@20: 	guint pos_left, guint pos_right,  guint pos_top, guint pos_bottom);
renatofilho@20: 	
rosfran@208: static GMythBackendInfo *backend_info = NULL;
rosfran@208: 
renatofilho@20: gboolean
renatofilho@20: mmyth_uisettings_run (GtkWindow *main_window)
renatofilho@20: {
renatofilho@20: 
renatofilho@20: 	GtkWidget *settings_table;
renatofilho@20: 	GtkWidget *label_hostname, *label_dbname;
renatofilho@20: 	GtkWidget *label_username, *label_passwd, *label_port;
renatofilho@20: 
morphbr@268: 	backend_info = gmyth_backend_info_new_full( "localhost", "mythtv", 
rosfran@208: 			"mythtv", "mythconverg", 6543 );
renatofilho@20: 	
renatofilho@20: 	settings_dialog = gtk_dialog_new_with_buttons ("Settings",
renatofilho@20:                                          main_window,
renatofilho@20:                                          GTK_DIALOG_DESTROY_WITH_PARENT,
renatofilho@20:                                          GTK_STOCK_OK,
renatofilho@20:                                          GTK_RESPONSE_ACCEPT,
renatofilho@20:                                          GTK_STOCK_CANCEL,
renatofilho@20:                                          GTK_RESPONSE_REJECT,
renatofilho@20:                                          NULL);
renatofilho@20:                                          
renatofilho@20:   gtk_widget_set_size_request (settings_dialog, 400, 244);
renatofilho@20: 
renatofilho@20: /*  scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
renatofilho@20:   gtk_widget_show (scrolledwindow1);
renatofilho@20:   gtk_container_add (GTK_CONTAINER (window1), scrolledwindow1);
renatofilho@20:   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER);
renatofilho@20: 
renatofilho@20:   viewport1 = gtk_viewport_new (NULL, NULL);
renatofilho@20:   gtk_widget_show (viewport1);
renatofilho@20:   gtk_container_add (GTK_CONTAINER (scrolledwindow1), viewport1);
renatofilho@20:   gtk_viewport_set_shadow_type (GTK_VIEWPORT (viewport1), GTK_SHADOW_NONE);
renatofilho@20: */
renatofilho@20: 
renatofilho@20: 	// Creates the table and attach it to the settings dialog
renatofilho@20: 	settings_table = gtk_table_new (5, 2, FALSE);
renatofilho@20: 	gtk_widget_show (settings_table);
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (GTK_DIALOG(settings_dialog)->vbox), settings_table, FALSE, TRUE, 0);
renatofilho@20: 	gtk_container_set_border_width (GTK_CONTAINER (settings_table), 3);
renatofilho@20: 	gtk_table_set_row_spacings (GTK_TABLE (settings_table), 5);
renatofilho@20: 	gtk_table_set_col_spacings (GTK_TABLE (settings_table), 10);
renatofilho@20: 
renatofilho@20: 	label_hostname = add_label_to_table (settings_table, "Host Name:", 0, 1, 0, 1);
renatofilho@20: 	label_dbname   = add_label_to_table (settings_table, "Database Name:", 0, 1, 1, 2);
renatofilho@20: 	label_username = add_label_to_table (settings_table, "Username:", 0, 1, 2, 3);
renatofilho@20: 	label_passwd = add_label_to_table (settings_table, "Password:", 0, 1, 3, 4);
renatofilho@20: 	label_port = add_label_to_table (settings_table, "Server port:", 0, 1, 4, 5);
renatofilho@20: 	
renatofilho@20: 	entry_hostname = add_entry_to_table (settings_table,
rosfran@208: 		gmyth_backend_info_get_hostname (backend_info),
renatofilho@20: 		1, 2, 0, 1);
renatofilho@20: 	entry_dbname = add_entry_to_table (settings_table,
rosfran@208: 		gmyth_backend_info_get_db_name (backend_info),
renatofilho@20: 		1, 2, 1, 2 );
renatofilho@20: 	entry_username = add_entry_to_table (settings_table,
rosfran@208: 		gmyth_backend_info_get_username (backend_info),
renatofilho@20: 		1, 2, 2, 3 );
renatofilho@20: 	entry_passwd = add_entry_to_table (settings_table,
rosfran@208: 		gmyth_backend_info_get_password (backend_info),
renatofilho@20: 		1, 2, 3, 4 );
renatofilho@20: 		
rosfran@208: 	entry_port = add_entry_to_table (settings_table, g_strdup_printf( "%d", gmyth_backend_info_get_port (backend_info) ),
renatofilho@20: 		1, 2, 4, 5 );
renatofilho@20: 		
renatofilho@20: 	if (gtk_dialog_run (GTK_DIALOG (settings_dialog)) == GTK_RESPONSE_ACCEPT) {
renatofilho@20: 		settings_dialog_update_data ();
renatofilho@20: 		gtk_widget_destroy (settings_dialog);
renatofilho@20: 		return TRUE;
renatofilho@20: 	}
renatofilho@20: 	
renatofilho@20: 	gtk_widget_destroy (settings_dialog);
renatofilho@20: 	
renatofilho@20: 	return FALSE;
renatofilho@20: 	
renatofilho@20: }
renatofilho@20: 
renatofilho@20: static GtkWidget*
renatofilho@20: add_label_to_table (GtkWidget *table, const gchar *str, guint pos_left, guint pos_right, 
renatofilho@20: 		guint pos_top, guint pos_bottom )
renatofilho@20: {
renatofilho@20: 	GtkWidget *tmp_label = gtk_label_new (str);
renatofilho@20: 	
renatofilho@20: 	gtk_widget_show (tmp_label);
renatofilho@20: 	gtk_table_attach (GTK_TABLE (table), tmp_label, 
renatofilho@20: 					pos_left, pos_right, pos_top, pos_bottom,
renatofilho@20:                     (GtkAttachOptions) (GTK_FILL),
renatofilho@20:                     (GtkAttachOptions) (0), 0, 0);
renatofilho@20: 	gtk_misc_set_alignment (GTK_MISC (tmp_label), 0, 0.5);
renatofilho@20: 	
renatofilho@20: 	return tmp_label;
renatofilho@20: }
renatofilho@20: 
renatofilho@20: static GtkWidget*
rosfran@208: add_entry_to_table (GtkWidget *table, const gchar *init_str, guint pos_left, guint pos_right,
renatofilho@20: 		guint pos_top, guint pos_bottom)
renatofilho@20: {
renatofilho@20: 	GtkWidget *tmp_entry = gtk_entry_new ();
renatofilho@20: 	gtk_widget_show (tmp_entry);
renatofilho@20: 	gtk_table_attach (GTK_TABLE (table), tmp_entry, 
renatofilho@20: 					pos_left, pos_right, pos_top, pos_bottom,
renatofilho@20:                     (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
renatofilho@20:                     (GtkAttachOptions) (0), 0, 0);
renatofilho@20:     if (init_str)
rosfran@208: 		gtk_entry_set_text (GTK_ENTRY (tmp_entry), init_str);
renatofilho@20: 		
renatofilho@20: 	//gtk_entry_set_invisible_char (GTK_ENTRY (entry_port), 9679);
renatofilho@20: 	
renatofilho@20: 	return tmp_entry;
renatofilho@20: }
renatofilho@20: 
renatofilho@20: static void
renatofilho@20: settings_dialog_update_data (void) 
renatofilho@20: {
rosfran@208: 	//GMythSettings *backend_info = gmyth_context_get_settings();
renatofilho@20: 	
rosfran@208: 	if (!backend_info) {
renatofilho@20: 		g_warning ("[%s] Could not get GMythSettings instance from context\n", __FUNCTION__);
renatofilho@20: 		return;
renatofilho@20: 	}
renatofilho@20: 		
rosfran@208: 	gmyth_backend_info_set_hostname(backend_info, gtk_entry_get_text( GTK_ENTRY(entry_hostname) ));
renatofilho@20: 	
rosfran@208: 	gmyth_backend_info_set_db_name(backend_info, gtk_entry_get_text( GTK_ENTRY(entry_dbname)));
renatofilho@20: 	
rosfran@208: 	gmyth_backend_info_set_username(backend_info, gtk_entry_get_text( GTK_ENTRY(entry_username)));
rosfran@208: 
rosfran@208: 	gmyth_backend_info_set_password(backend_info, gtk_entry_get_text( GTK_ENTRY(entry_passwd)));
renatofilho@20: 	
rosfran@208: 	gmyth_backend_info_set_port(backend_info, (gint)g_ascii_strtoull( gtk_entry_get_text( GTK_ENTRY(entry_port) ), 
rosfran@208: 				NULL, 10) );
renatofilho@20: 	
rosfran@208: 	//gmyth_backend_info_save (backend_info);
renatofilho@20: 	
renatofilho@20: }