]> sjero.net Git - linphone/blob - linphone/gtk-glade/loginframe.c
d4ed36974b9fdcf6631706a3777f06162f7bdcfb
[linphone] / linphone / gtk-glade / loginframe.c
1 /*
2 linphone, gtk-glade interface.
3 Copyright (C) 2009  Simon MORLAT (simon.morlat@linphone.org)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 #include "linphone.h"
21
22 void linphone_gtk_login_frame_connect_clicked(GtkWidget *button);
23 void linphone_gtk_exit_login_frame(void);
24
25 enum {
26         NetworkKindAdsl,
27         NetworkKindOpticalFiber
28 };
29
30 static gboolean check_login_ok(LinphoneProxyConfig *cfg){
31         if (linphone_proxy_config_is_registered(cfg)){
32                 linphone_gtk_exit_login_frame();
33                 return FALSE;   
34         }
35         return TRUE;
36 }
37
38 static void do_login(SipSetupContext *ssctx, const char *identity, const char * passwd){
39         GtkWidget *mw=linphone_gtk_get_main_window();
40         if (sip_setup_context_login_account(ssctx,identity,passwd)==0){
41                 guint t=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(mw),"login_tout"));
42                 if (t!=0) g_source_remove(t);
43                 t=g_timeout_add(50,(GSourceFunc)check_login_ok,sip_setup_context_get_proxy_config(ssctx));
44                 g_object_set_data(G_OBJECT(mw),"login_tout",GINT_TO_POINTER(t));
45         }
46 }
47
48 static gboolean do_login_noprompt(LinphoneProxyConfig *cfg){
49         SipSetupContext *ssctx=linphone_proxy_config_get_sip_setup_context(cfg);
50         if (ssctx==NULL) return TRUE;/*not ready ?*/
51         do_login(ssctx,linphone_proxy_config_get_identity(cfg),NULL);
52         return FALSE;
53 }
54
55 void linphone_gtk_show_login_frame(LinphoneProxyConfig *cfg){
56         GtkWidget *mw=linphone_gtk_get_main_window();
57         GtkWidget *label=linphone_gtk_get_widget(mw,"login_label");
58         LinphoneAuthInfo *ai;
59         gchar *str;
60         LinphoneAddress *from;
61         LinphoneCore *lc=linphone_gtk_get_core();
62         int nettype;
63
64
65         if (linphone_core_get_download_bandwidth(lc)==512 &&
66                 linphone_core_get_upload_bandwidth(lc)==512)
67                 nettype=NetworkKindOpticalFiber;
68         else nettype=NetworkKindAdsl;
69         gtk_combo_box_set_active(GTK_COMBO_BOX(linphone_gtk_get_widget(mw,"login_internet_kind")),nettype);
70         gtk_combo_box_set_active(GTK_COMBO_BOX(linphone_gtk_get_widget(mw,"internet_kind")),nettype);
71
72         if (linphone_gtk_get_ui_config_int("automatic_login",0) ){
73                 g_timeout_add(250,(GSourceFunc)do_login_noprompt,cfg);
74                 return;
75         }
76
77         gtk_widget_hide(linphone_gtk_get_widget(mw,"logout"));
78         gtk_widget_hide(linphone_gtk_get_widget(mw,"idle_frame"));
79         gtk_widget_show(linphone_gtk_get_widget(mw,"login_frame"));
80         gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"main_menu"),FALSE);
81         gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"modes"),FALSE);
82         str=g_strdup_printf(_("Please enter login information for %s"),linphone_proxy_config_get_domain(cfg));
83         gtk_label_set_text(GTK_LABEL(label),str);
84         g_object_set_data(G_OBJECT(mw),"login_proxy_config",cfg);
85         g_free(str);
86
87         from=linphone_address_new(linphone_proxy_config_get_identity(cfg));
88         
89         ai=linphone_core_find_auth_info(lc,linphone_proxy_config_get_domain(cfg),linphone_address_get_username(from));
90         /*display the last entered username, if not '?????'*/
91         if (linphone_address_get_username(from)[0]!='?')
92                 gtk_entry_set_text(GTK_ENTRY(linphone_gtk_get_widget(mw,"login_username")),
93                         linphone_address_get_username(from));
94         gtk_entry_set_text(GTK_ENTRY(linphone_gtk_get_widget(mw,"login_password")),
95                 ai!=NULL ? ai->passwd : "");
96         
97         linphone_address_destroy(from);
98 }
99
100 void linphone_gtk_exit_login_frame(void){
101         GtkWidget *mw=linphone_gtk_get_main_window();
102         gtk_widget_show(linphone_gtk_get_widget(mw,"idle_frame"));
103         gtk_widget_hide(linphone_gtk_get_widget(mw,"login_frame"));
104         gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"main_menu"),TRUE);
105         gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"modes"),TRUE);
106         gtk_widget_show(linphone_gtk_get_widget(mw,"logout"));
107 }
108
109 void linphone_gtk_logout_clicked(){
110         LinphoneCore *lc=linphone_gtk_get_core();
111         LinphoneProxyConfig *cfg=NULL;
112         linphone_core_get_default_proxy(lc,&cfg);
113         if (cfg){
114                 SipSetupContext *ss=linphone_proxy_config_get_sip_setup_context(cfg);
115                 if (ss){
116                         sip_setup_context_logout(ss);
117                         linphone_gtk_set_ui_config_int("automatic_login",FALSE);
118                         linphone_gtk_show_login_frame(cfg);
119                 }
120         }
121 }
122
123
124
125 void linphone_gtk_login_frame_connect_clicked(GtkWidget *button){
126         GtkWidget *mw=gtk_widget_get_toplevel(button);
127         const char *username;
128         const char *password;
129         char *identity;
130         gboolean autologin;
131         LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)g_object_get_data(G_OBJECT(mw),"login_proxy_config");
132         LinphoneAddress *from;
133         SipSetupContext *ssctx=linphone_proxy_config_get_sip_setup_context(cfg);
134
135         username=gtk_entry_get_text(GTK_ENTRY(linphone_gtk_get_widget(mw,"login_username")));
136         password=gtk_entry_get_text(GTK_ENTRY(linphone_gtk_get_widget(mw,"login_password")));
137
138         if (username==NULL || username[0]=='\0')
139                 return;
140
141         autologin=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(mw,"automatic_login")));
142         linphone_gtk_set_ui_config_int("automatic_login",autologin);
143
144         from=linphone_address_new(linphone_proxy_config_get_identity(cfg));
145         linphone_address_set_username(from,username);
146         identity=linphone_address_as_string(from);
147         do_login(ssctx,identity,password);
148         /*we need to refresh the identities since the proxy config may have changed.*/
149         linphone_gtk_load_identities();
150 }
151
152 void linphone_gtk_internet_kind_changed(GtkWidget *combo){
153         int netkind_id=gtk_combo_box_get_active(GTK_COMBO_BOX(combo));
154         LinphoneCore *lc=linphone_gtk_get_core();
155         if (netkind_id==NetworkKindAdsl){
156                 linphone_core_set_upload_bandwidth(lc,256);
157                 linphone_core_set_download_bandwidth(lc,512);
158         }else if (netkind_id==NetworkKindOpticalFiber){
159                 linphone_core_set_upload_bandwidth(lc,512);
160                 linphone_core_set_download_bandwidth(lc,512);
161         }
162 }