00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00026 #include "config.h"
00027 #include <string.h>
00028 #include <gtk/gtk.h>
00029 #include <liberregxml/erregapi.h>
00030 #include "connectionMgrLog.h"
00031 #include "connectionMgr.h"
00032 #include "displayStatus.h"
00033 #include "erbusy.h"
00034 #include "background.h"
00035 #include "gtkSettingItem.h"
00036 #include "gtkInfoItem.h"
00037 #include "editScreen.h"
00038 #include "editScreenOverview.h"
00039 #include "widgetUtils.h"
00040 #include "editScreenProxy.h"
00041 #include "languages.h"
00042 
00043 typedef enum
00044 {
00045     inputHost_e = 0,
00046     inputPort_e,
00047     undefStatus_e 
00048 }proxyStatus_t;
00049 
00050 
00051 static GtkWidget *g_proxy = NULL;
00052 
00053 static wdtLabelEntry *g_host = NULL;
00054 static wdtLabelEntry *g_port = NULL;
00055 
00056 static GtkWidget *g_info = NULL;
00057 
00058 
00059 static regProxySetting_t *g_old_settings = NULL;
00060 
00061 static void edit_proxy_set_host(const char *proxyhost);
00062 static void edit_proxy_set_port(const char *proxyport);
00063 
00064 static const char *edit_proxy_get_host(void);
00065 static const char *edit_proxy_get_port(void);
00066 
00067 static void edit_proxy_backup_network_settings(void);
00068 
00069 static void on_next(GtkToggleButton *button, gpointer data);
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 GtkWidget *edit_proxy_create(void)
00082 {
00083     CN_LOGPRINTF("entry");
00084 
00085     GtkWidget *item = NULL;
00086     GtkWidget *topVbox = NULL;
00087     GtkWidget *background = NULL;
00088     GtkWidget *alignment = NULL;
00089     GtkWidget *vbox = NULL;
00090     wdtLabelEntry *host = NULL;
00091     wdtLabelEntry *port = NULL;
00092     GtkWidget *info = NULL;
00093     GtkWidget *widget = NULL;
00094 
00095     CN_LOGPRINTF("entry");
00096 
00097     
00098     item = gtk_settingitem_new("");
00099 
00100     
00101     topVbox = gtk_vbox_new(FALSE, VBOX_SPACING);
00102     gtk_settingitem_add_details(GTK_SETTINGITEM(item), topVbox);
00103  
00104     
00105     background = gtk_event_box_new();
00106     gtk_widget_set_name(background, "bk_grey_666_140");
00107     gtk_widget_set_size_request(background, 
00108             BK_666_140_WIDTH, BK_666_140_HEIGHT);
00109     gtk_box_pack_start(GTK_BOX(topVbox), background, FALSE, FALSE, 0);
00110 
00111     
00112     alignment = gtk_alignment_new(0.0, 0.0, 0, 0);
00113     gtk_alignment_set_padding(GTK_ALIGNMENT(alignment),
00114             PADDING_TOP, PADDING_BOTTOM, PADDING_LEFT, PADDING_RIGHT);
00115     gtk_container_add(GTK_CONTAINER(background), alignment);
00116  
00117     
00118     vbox = gtk_vbox_new(FALSE, VBOX_SPACING);
00119     gtk_container_add(GTK_CONTAINER(alignment), vbox);
00120 
00121     
00122     host = wdt_label_entry_new(string_e);
00123     gtk_box_pack_start(GTK_BOX(vbox), host->parent, FALSE, FALSE, 0);
00124     
00125     
00126     port = wdt_label_entry_new(integer_e);
00127     gtk_box_pack_start(GTK_BOX(vbox), port->parent, FALSE, FALSE, 0);
00128 
00129     
00130     info = gtk_infoitem_new(FALSE);
00131     gtk_box_pack_start(GTK_BOX(topVbox), info, FALSE, FALSE, 0);
00132 
00133     
00134     widget = port->button;
00135     g_signal_connect_after(G_OBJECT(widget), "toggled", 
00136             G_CALLBACK(on_next), NULL);
00137  
00138     
00139     gtk_widget_show(item);
00140     gtk_widget_show(topVbox);
00141     gtk_widget_show(background);
00142     gtk_widget_show(alignment);
00143     gtk_widget_show(vbox);
00144     gtk_widget_show(host->parent);
00145     gtk_widget_hide(host->button);
00146     gtk_widget_show(port->parent);
00147     gtk_widget_show(info);
00148  
00149     
00150     g_proxy = item;
00151     g_host = host;
00152     g_port = port;
00153     g_info = info;
00154   
00155     CN_LOGPRINTF("done");
00156 
00157     
00158     return item;
00159 }
00160 
00161 void edit_proxy_set_text(void)
00162 {
00163     GtkWidget *widget = NULL;
00164 
00165     CN_LOGPRINTF("entry");
00166   
00167     if (g_proxy)
00168     {
00169         gtk_settingitem_set_header_text(GTK_SETTINGITEM(g_proxy), 
00170                                         _("Network profile"));
00171     }
00172 
00173     if (g_host)
00174     {
00175         widget = g_host->label;
00176         gtk_label_set_text(GTK_LABEL(widget),
00177                 _("Enter the name or address of the HTTP proxy server:"));
00178 
00179     }
00180     
00181     if (g_port)
00182     {
00183         widget = g_port->label;
00184         gtk_label_set_text(GTK_LABEL(widget),
00185                 _("Enter the port number of the HTTP proxy server:"));
00186         widget = g_port->button;
00187         gtk_button_set_label(GTK_BUTTON(widget), _("Next"));
00188     }
00189     
00190     if (g_info)
00191     {
00192         gtk_infoitem_set_text( GTK_INFOITEM(g_info),
00193             _("Do not include the protocol (e.g. http://) "
00194               "and proxy number (e.g. 8080) "
00195               "in the name or address of the HTTP proxy server.\n"
00196               "Hint: If you do not know the proxy settings "
00197               "for this network, "
00198               "please ask your network administrator."));
00199     }
00200 }
00201 
00202 static proxyStatus_t edit_proxy_detect_status(void)
00203 {
00204     proxyStatus_t status;
00205 
00206     CN_LOGPRINTF("entry");
00207 
00208     if (!ergtk_entry_check_field(ERGTK_ENTRY(g_host->entry)))
00209     {
00210         status = inputHost_e;
00211     }
00212     else if (!ergtk_entry_check_field(ERGTK_ENTRY(g_port->entry)))
00213     {
00214         status = inputPort_e;
00215     }
00216     else
00217     {
00218         status = undefStatus_e;
00219     }
00220 
00221     CN_LOGPRINTF("return %d", status);
00222     return status;
00223 }
00224 
00225 static void edit_proxy_determine_status(void)
00226 {
00227     proxyStatus_t status;
00228     GtkWidget *widget = NULL;
00229 
00230     CN_LOGPRINTF("entry");
00231 
00232     status = edit_proxy_detect_status();
00233     switch (status)
00234     {
00235         case inputHost_e:
00236             widget = g_host->entry;
00237             break;
00238         case inputPort_e:
00239             widget = g_port->entry;
00240             break;
00241         case undefStatus_e:
00242             widget = g_host->entry;
00243             break;
00244         default:
00245             break;
00246     }
00247 
00248     if (widget)
00249     {
00250         gtk_widget_grab_focus(widget);
00251     }
00252 }
00253 
00254 void edit_proxy_set_network_settings(const regNetworkProfile_t *settings)
00255 {
00256     char *profilename = NULL;
00257     char *proxyhost = NULL;
00258     char *proxyport = NULL;
00259     regProxySetting_t *proxySettings;
00260 
00261     CN_LOGPRINTF("entry");
00262 
00263     g_return_if_fail(settings != NULL);
00264     
00265 
00266     profilename = settings->name;
00267 
00268     proxySettings = settings->proxySettings;
00269     if (proxySettings)
00270     {
00271         proxyhost = proxySettings->address;
00272         proxyport = proxySettings->port;
00273     }
00274 
00275     edit_proxy_set_title(profilename);
00276     edit_proxy_set_host(proxyhost);
00277     edit_proxy_set_port(proxyport);
00278 
00279     edit_proxy_determine_status();
00280 
00281     
00282     edit_proxy_backup_network_settings();
00283 }
00284 
00285 void edit_proxy_set_title(const char *profilename)
00286 {
00287     char *title = NULL;
00288 
00289     CN_LOGPRINTF("entry[%s]", profilename);
00290 
00291     if (g_proxy)
00292     {
00293         if (profilename)
00294         {
00295             title = g_strdup_printf(_("Network profile: %s/proxy"), 
00296                                     profilename);
00297         }
00298         else
00299         {
00300             title = g_strdup(_("Network profile: proxy"));
00301         }
00302 
00303         gtk_settingitem_set_header_text(GTK_SETTINGITEM(g_proxy), title);
00304 
00305         g_free(title);
00306     }
00307 }
00308 
00309 static void edit_proxy_set_host(const char *proxyhost)
00310 {
00311     GtkWidget *widget;
00312 
00313     CN_LOGPRINTF("entry [%s]", proxyhost);
00314 
00315     if (g_host)
00316     {
00317         widget = g_host->entry;
00318         gtk_entry_set_text(GTK_ENTRY(widget), proxyhost ? proxyhost : "");
00319     }
00320 }
00321 
00322 static void edit_proxy_set_port(const char *proxyport)
00323 {
00324     GtkWidget *widget;
00325 
00326     CN_LOGPRINTF("entry [%s]", proxyport);
00327     if (g_port)
00328     {
00329         widget = g_port->entry;
00330         gtk_entry_set_text(GTK_ENTRY(widget), proxyport ? proxyport: "");
00331     }
00332 }
00333 
00334 void edit_proxy_get_network_settings(regNetworkProfile_t *settings)
00335 {
00336     const gchar *proxyhost = NULL;
00337     const gchar *proxyport = NULL;
00338     regProxySetting_t *proxySettings = NULL;
00339 
00340     CN_LOGPRINTF("entry [%p]", settings);
00341 
00342     g_return_if_fail(settings != NULL);
00343     g_return_if_fail(settings->proxy == TRUE);
00344 
00345     
00346     proxySettings = settings->proxySettings;
00347     if (proxySettings)
00348     {
00349         g_free(proxySettings->address);
00350         g_free(proxySettings->port);
00351         g_free(proxySettings);
00352         proxySettings = NULL;
00353         settings->proxySettings = NULL;
00354     }
00355 
00356     
00357     proxyhost = edit_proxy_get_host();
00358     proxyport = edit_proxy_get_port();
00359   
00360     
00361     if (settings->proxy == TRUE)
00362     {
00363         proxySettings = g_new0(regProxySetting_t, 1);
00364         if (proxySettings)
00365         {
00366             proxySettings->address = g_strdup(proxyhost);
00367             proxySettings->port    = g_strdup(proxyport);
00368         }
00369         settings->proxySettings = proxySettings;
00370     }
00371 }
00372 
00373 static const char *edit_proxy_get_host(void)
00374 {
00375     GtkWidget *widget;
00376     const char *proxyhost = NULL;
00377 
00378     if (g_host)
00379     {
00380         widget = g_host->entry;
00381         proxyhost = gtk_entry_get_text(GTK_ENTRY(widget));
00382     }
00383 
00384     CN_LOGPRINTF("return proxyhost=%s", proxyhost);
00385     return proxyhost;
00386 }
00387 
00388 static const char *edit_proxy_get_port(void)
00389 {
00390     GtkWidget *widget;
00391     const char *proxyport = NULL;
00392 
00393     if (g_port)
00394     {
00395         widget = g_port->entry;
00396         proxyport = gtk_entry_get_text(GTK_ENTRY(widget));
00397     }       
00398     
00399     CN_LOGPRINTF("return proxyport=%s", proxyport);
00400     return proxyport;
00401 }
00402 
00403 
00404 gboolean edit_proxy_check_network_settings(void)
00405 {
00406     proxyStatus_t status;
00407     gboolean valid = TRUE;
00408 
00409     CN_LOGPRINTF("entry");
00410     
00411     status = edit_proxy_detect_status();
00412     if (status != undefStatus_e)
00413     {
00414         valid = FALSE;
00415     }
00416 
00417     CN_LOGPRINTF("return valid=%d", valid);
00418     return valid;
00419 }
00420 
00421 static void edit_proxy_backup_network_settings(void)
00422 {
00423     const char *proxyhost, *proxyport;
00424 
00425     CN_LOGPRINTF("entry");
00426 
00427     proxyhost = edit_proxy_get_host();
00428     proxyport = edit_proxy_get_port();
00429 
00430     if (g_old_settings)
00431     {
00432         g_free(g_old_settings->address);
00433         g_free(g_old_settings->port);
00434         g_free(g_old_settings);
00435     }
00436 
00437     g_old_settings = g_new0(regProxySetting_t, 1);
00438     g_assert(g_old_settings != NULL);
00439     g_old_settings->address = g_strdup(proxyhost);
00440     g_old_settings->port = g_strdup(proxyport);
00441 }
00442 
00443 void edit_proxy_restore_network_settings(void)
00444 {
00445     char *host = NULL;
00446     char *port = NULL; 
00447     
00448     CN_LOGPRINTF("entry");
00449      
00450     if (g_old_settings)
00451     {
00452        host = g_old_settings->address;
00453        port = g_old_settings->port;
00454     }
00455     
00456     edit_proxy_set_host(host);
00457     edit_proxy_set_port(port);
00458 }
00459 
00460 void edit_proxy_screen_leave(void)
00461 {
00462     gboolean valid = TRUE;
00463 
00464     CN_LOGPRINTF("entry");
00465 
00466     valid = edit_proxy_check_network_settings();
00467     
00468     if (!valid)
00469     {
00470         edit_overview_set_proxy(FALSE);
00471     }
00472 }
00473 
00474 static gboolean on_delayed_next(gpointer data)
00475 {
00476     gboolean valid;
00477     GtkToggleButton *button;
00478 
00479     CN_LOGPRINTF("entry");
00480     
00481     erbusy_blink();
00482 
00483     valid = edit_proxy_check_network_settings();
00484     if (valid)
00485     {
00486         edit_proxy_backup_network_settings();
00487         edit_goto_page(editScreenOverview_e);
00488     }
00489     else
00490     {
00491         edit_proxy_determine_status();
00492     }
00493     
00494     button = GTK_TOGGLE_BUTTON(data);
00495     gtk_toggle_button_set_active(button, FALSE);
00496 
00497     return FALSE;
00498 }
00499 
00500 static void on_next(GtkToggleButton *button, gpointer data)
00501 {
00502     gboolean active;
00503 
00504     CN_LOGPRINTF("entry");
00505 
00506     active = gtk_toggle_button_get_active(button);
00507     if (active)
00508     {
00509         g_timeout_add(500, on_delayed_next, button);
00510     }
00511 }
00512 
00513 gboolean on_edit_proxy_keypress(GdkEventKey *event)
00514 {
00515     GtkToggleButton *button;
00516     gboolean ret = FALSE;
00517 
00518     CN_LOGPRINTF("entry");
00519 
00520     switch (event->keyval)
00521     {
00522         case GDK_Return:
00523             
00524             
00525             if (g_port)
00526             {
00527                 button = GTK_TOGGLE_BUTTON(g_port->button);
00528                 gtk_toggle_button_set_active(button, TRUE);
00529             }
00530             ret = TRUE;
00531             break;
00532 
00533         case GDK_F5:
00534             
00535             
00536             cmgr_up_page();
00537             ret = TRUE;
00538             break;
00539 
00540         case GDK_Down:
00541         case GDK_Up:
00542             
00543             erbusy_off();
00544             ret = TRUE;
00545 
00546         default:
00547             erbusy_off();
00548             break;
00549     }
00550 
00551     return ret;
00552 }
00553