maemo-ui/src/mmyth_uisettings.c
author rosfran
Wed Nov 22 19:53:52 2006 +0000 (2006-11-22)
branchtrunk
changeset 101 cf4a26332b8c
child 208 c3c073032757
permissions -rw-r--r--
[svn r102] Removed unused code, remade buffer fillment in the file transfer read methods.
     1 #include<gtk/gtk.h>
     2 #include<glib.h>
     3 #include <sys/types.h>
     4 #include <sys/stat.h>
     5 #include <unistd.h>
     6 #include <string.h>
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 
    10 #include "mmyth_uisettings.h"
    11 
    12 #include "gmyth_context.h"
    13 
    14 static GtkWidget *settings_dialog;
    15 static GtkWidget *entry_hostname;
    16 static GtkWidget *entry_dbname;
    17 static GtkWidget *entry_username;
    18 static GtkWidget *entry_passwd;
    19 static GtkWidget *entry_port;
    20 
    21 static void settings_dialog_update_data (void);
    22 static GtkWidget* add_entry_to_table (GtkWidget *table, GString *init_str,
    23 	guint pos_left, guint pos_right, guint pos_top, guint pos_bottom);
    24 static GtkWidget* add_label_to_table (GtkWidget *table, const gchar *str, 
    25 	guint pos_left, guint pos_right,  guint pos_top, guint pos_bottom);
    26 	
    27 	
    28 gboolean
    29 mmyth_uisettings_run (GtkWindow *main_window)
    30 {
    31 
    32 	GtkWidget *settings_table;
    33 	GtkWidget *label_hostname, *label_dbname;
    34 	GtkWidget *label_username, *label_passwd, *label_port;
    35 
    36 	GMythSettings *msettings = gmyth_context_get_settings();
    37 	
    38 	settings_dialog = gtk_dialog_new_with_buttons ("Settings",
    39                                          main_window,
    40                                          GTK_DIALOG_DESTROY_WITH_PARENT,
    41                                          GTK_STOCK_OK,
    42                                          GTK_RESPONSE_ACCEPT,
    43                                          GTK_STOCK_CANCEL,
    44                                          GTK_RESPONSE_REJECT,
    45                                          NULL);
    46                                          
    47   gtk_widget_set_size_request (settings_dialog, 400, 244);
    48 
    49 /*  scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
    50   gtk_widget_show (scrolledwindow1);
    51   gtk_container_add (GTK_CONTAINER (window1), scrolledwindow1);
    52   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER);
    53 
    54   viewport1 = gtk_viewport_new (NULL, NULL);
    55   gtk_widget_show (viewport1);
    56   gtk_container_add (GTK_CONTAINER (scrolledwindow1), viewport1);
    57   gtk_viewport_set_shadow_type (GTK_VIEWPORT (viewport1), GTK_SHADOW_NONE);
    58 */
    59 
    60 	// Creates the table and attach it to the settings dialog
    61 	settings_table = gtk_table_new (5, 2, FALSE);
    62 	gtk_widget_show (settings_table);
    63 	gtk_box_pack_start (GTK_BOX (GTK_DIALOG(settings_dialog)->vbox), settings_table, FALSE, TRUE, 0);
    64 	gtk_container_set_border_width (GTK_CONTAINER (settings_table), 3);
    65 	gtk_table_set_row_spacings (GTK_TABLE (settings_table), 5);
    66 	gtk_table_set_col_spacings (GTK_TABLE (settings_table), 10);
    67 
    68 	label_hostname = add_label_to_table (settings_table, "Host Name:", 0, 1, 0, 1);
    69 	label_dbname   = add_label_to_table (settings_table, "Database Name:", 0, 1, 1, 2);
    70 	label_username = add_label_to_table (settings_table, "Username:", 0, 1, 2, 3);
    71 	label_passwd = add_label_to_table (settings_table, "Password:", 0, 1, 3, 4);
    72 	label_port = add_label_to_table (settings_table, "Server port:", 0, 1, 4, 5);
    73 	
    74 	entry_hostname = add_entry_to_table (settings_table,
    75 		gmyth_settings_get_backend_hostname (msettings),
    76 		1, 2, 0, 1);
    77 	entry_dbname = add_entry_to_table (settings_table,
    78 		gmyth_settings_get_dbname (msettings),
    79 		1, 2, 1, 2 );
    80 	entry_username = add_entry_to_table (settings_table,
    81 		gmyth_settings_get_username (msettings),
    82 		1, 2, 2, 3 );
    83 	entry_passwd = add_entry_to_table (settings_table,
    84 		gmyth_settings_get_password (msettings),
    85 		1, 2, 3, 4 );
    86 		
    87 	GString *str_port = g_string_new ("");
    88 	g_string_printf (str_port, "%d", 
    89 		gmyth_settings_get_backend_port (msettings));
    90 	entry_port = add_entry_to_table (settings_table, str_port,
    91 		1, 2, 4, 5 );
    92 	g_string_free (str_port, TRUE);
    93 		
    94 	if (gtk_dialog_run (GTK_DIALOG (settings_dialog)) == GTK_RESPONSE_ACCEPT) {
    95 		settings_dialog_update_data ();
    96 		gtk_widget_destroy (settings_dialog);
    97 		return TRUE;
    98 	}
    99 	
   100 	gtk_widget_destroy (settings_dialog);
   101 	
   102 	return FALSE;
   103 	
   104 }
   105 
   106 static GtkWidget*
   107 add_label_to_table (GtkWidget *table, const gchar *str, guint pos_left, guint pos_right, 
   108 		guint pos_top, guint pos_bottom )
   109 {
   110 	GtkWidget *tmp_label = gtk_label_new (str);
   111 	
   112 	gtk_widget_show (tmp_label);
   113 	gtk_table_attach (GTK_TABLE (table), tmp_label, 
   114 					pos_left, pos_right, pos_top, pos_bottom,
   115                     (GtkAttachOptions) (GTK_FILL),
   116                     (GtkAttachOptions) (0), 0, 0);
   117 	gtk_misc_set_alignment (GTK_MISC (tmp_label), 0, 0.5);
   118 	
   119 	return tmp_label;
   120 }
   121 
   122 static GtkWidget*
   123 add_entry_to_table (GtkWidget *table, GString *init_str, guint pos_left, guint pos_right,
   124 		guint pos_top, guint pos_bottom)
   125 {
   126 	GtkWidget *tmp_entry = gtk_entry_new ();
   127 	gtk_widget_show (tmp_entry);
   128 	gtk_table_attach (GTK_TABLE (table), tmp_entry, 
   129 					pos_left, pos_right, pos_top, pos_bottom,
   130                     (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
   131                     (GtkAttachOptions) (0), 0, 0);
   132     if (init_str)
   133 		gtk_entry_set_text (GTK_ENTRY (tmp_entry), (init_str->str));
   134 		
   135 	//gtk_entry_set_invisible_char (GTK_ENTRY (entry_port), 9679);
   136 	
   137 	return tmp_entry;
   138 }
   139 
   140 static void
   141 settings_dialog_update_data (void) 
   142 {
   143 	GString *tmp_entry_text;
   144 	GMythSettings *msettings = gmyth_context_get_settings();
   145 	
   146 	if (!msettings) {
   147 		g_warning ("[%s] Could not get GMythSettings instance from context\n", __FUNCTION__);
   148 		return;
   149 	}
   150 		
   151 	tmp_entry_text = g_string_new("");
   152 	g_string_printf(tmp_entry_text, "%s", gtk_entry_get_text( GTK_ENTRY(entry_hostname)));
   153 	gmyth_settings_set_backend_hostname(msettings, tmp_entry_text);
   154 	
   155 	g_string_printf(tmp_entry_text, "%s", gtk_entry_get_text( GTK_ENTRY(entry_dbname)));
   156 	gmyth_settings_set_dbname(msettings, tmp_entry_text);
   157 	
   158 	g_string_printf(tmp_entry_text, "%s", gtk_entry_get_text( GTK_ENTRY(entry_username)));
   159 	gmyth_settings_set_username(msettings, tmp_entry_text);
   160 	
   161 	g_string_printf(tmp_entry_text, "%s", gtk_entry_get_text( GTK_ENTRY(entry_passwd)));
   162 	gmyth_settings_set_password(msettings, tmp_entry_text);	
   163 	
   164 	g_string_printf(tmp_entry_text, "%s", gtk_entry_get_text( GTK_ENTRY(entry_port)));
   165 	gmyth_settings_set_backend_port(msettings, atoi(tmp_entry_text->str));
   166 	
   167 	gmyth_settings_save (msettings);
   168 	
   169 }