| 1 /* |
|
| 2 * Pidgin - Internet Messenger |
|
| 3 * Copyright (C) Pidgin Developers <devel@pidgin.im> |
|
| 4 * |
|
| 5 * Pidgin is the legal property of its developers, whose names are too numerous |
|
| 6 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
| 7 * source distribution. |
|
| 8 * |
|
| 9 * This program is free software; you can redistribute it and/or modify |
|
| 10 * it under the terms of the GNU General Public License as published by |
|
| 11 * the Free Software Foundation; either version 2 of the License, or |
|
| 12 * (at your option) any later version. |
|
| 13 * |
|
| 14 * This program is distributed in the hope that it will be useful, |
|
| 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 17 * GNU General Public License for more details. |
|
| 18 * |
|
| 19 * You should have received a copy of the GNU General Public License |
|
| 20 * along with this program; if not, see <https://www.gnu.org/licenses/>. |
|
| 21 */ |
|
| 22 |
|
| 23 #include <glib/gi18n-lib.h> |
|
| 24 |
|
| 25 #include <purple.h> |
|
| 26 |
|
| 27 #include <handy.h> |
|
| 28 #include <nice.h> |
|
| 29 |
|
| 30 #include "pidginnetworkpage.h" |
|
| 31 #include "pidginprefsinternal.h" |
|
| 32 |
|
| 33 struct _PidginNetworkPage { |
|
| 34 HdyPreferencesPage parent; |
|
| 35 |
|
| 36 GtkWidget *stun_server; |
|
| 37 GtkWidget *auto_ip; |
|
| 38 GtkWidget *public_ip; |
|
| 39 GtkWidget *public_ip_hbox; |
|
| 40 GtkWidget *map_ports; |
|
| 41 GtkWidget *ports_range_use; |
|
| 42 GtkWidget *ports_range_hbox; |
|
| 43 GtkWidget *ports_range_start; |
|
| 44 GtkWidget *ports_range_end; |
|
| 45 GtkWidget *turn_server; |
|
| 46 GtkWidget *turn_port_udp; |
|
| 47 GtkWidget *turn_port_tcp; |
|
| 48 GtkWidget *turn_username; |
|
| 49 GtkWidget *turn_password; |
|
| 50 }; |
|
| 51 |
|
| 52 G_DEFINE_TYPE(PidginNetworkPage, pidgin_network_page, HDY_TYPE_PREFERENCES_PAGE) |
|
| 53 |
|
| 54 /****************************************************************************** |
|
| 55 * Helpers |
|
| 56 *****************************************************************************/ |
|
| 57 static void |
|
| 58 network_ip_changed(GtkEntry *entry, gpointer data) |
|
| 59 { |
|
| 60 const gchar *text = gtk_entry_get_text(entry); |
|
| 61 GtkStyleContext *context = gtk_widget_get_style_context(GTK_WIDGET(entry)); |
|
| 62 |
|
| 63 if (text && *text) { |
|
| 64 if (g_hostname_is_ip_address(text)) { |
|
| 65 purple_network_set_public_ip(text); |
|
| 66 gtk_style_context_add_class(context, "good-ip"); |
|
| 67 gtk_style_context_remove_class(context, "bad-ip"); |
|
| 68 } else { |
|
| 69 gtk_style_context_add_class(context, "bad-ip"); |
|
| 70 gtk_style_context_remove_class(context, "good-ip"); |
|
| 71 } |
|
| 72 |
|
| 73 } else { |
|
| 74 purple_network_set_public_ip(""); |
|
| 75 gtk_style_context_remove_class(context, "bad-ip"); |
|
| 76 gtk_style_context_remove_class(context, "good-ip"); |
|
| 77 } |
|
| 78 } |
|
| 79 |
|
| 80 static gboolean |
|
| 81 network_stun_server_changed_cb(GtkWidget *widget, |
|
| 82 GdkEventFocus *event, gpointer data) |
|
| 83 { |
|
| 84 GtkEntry *entry = GTK_ENTRY(widget); |
|
| 85 purple_prefs_set_string("/purple/network/stun_server", |
|
| 86 gtk_entry_get_text(entry)); |
|
| 87 purple_network_set_stun_server(gtk_entry_get_text(entry)); |
|
| 88 |
|
| 89 return FALSE; |
|
| 90 } |
|
| 91 |
|
| 92 static gboolean |
|
| 93 network_turn_server_changed_cb(GtkWidget *widget, |
|
| 94 GdkEventFocus *event, gpointer data) |
|
| 95 { |
|
| 96 GtkEntry *entry = GTK_ENTRY(widget); |
|
| 97 purple_prefs_set_string("/purple/network/turn_server", |
|
| 98 gtk_entry_get_text(entry)); |
|
| 99 purple_network_set_turn_server(gtk_entry_get_text(entry)); |
|
| 100 |
|
| 101 return FALSE; |
|
| 102 } |
|
| 103 |
|
| 104 static void |
|
| 105 auto_ip_button_clicked_cb(GtkWidget *button, gpointer null) |
|
| 106 { |
|
| 107 const char *ip; |
|
| 108 PurpleStunNatDiscovery *stun; |
|
| 109 char *auto_ip_text; |
|
| 110 GList *list = NULL; |
|
| 111 |
|
| 112 /* Make a lookup for the auto-detected IP ourselves. */ |
|
| 113 if (purple_prefs_get_bool("/purple/network/auto_ip")) { |
|
| 114 /* Check if STUN discovery was already done */ |
|
| 115 stun = purple_stun_discover(NULL); |
|
| 116 if ((stun != NULL) && (stun->status == PURPLE_STUN_STATUS_DISCOVERED)) { |
|
| 117 ip = stun->publicip; |
|
| 118 } else { |
|
| 119 /* Attempt to get the IP from a NAT device using UPnP */ |
|
| 120 ip = purple_upnp_get_public_ip(); |
|
| 121 if (ip == NULL) { |
|
| 122 /* Attempt to get the IP from a NAT device using NAT-PMP */ |
|
| 123 ip = purple_pmp_get_public_ip(); |
|
| 124 if (ip == NULL) { |
|
| 125 /* Just fetch the first IP of the local system */ |
|
| 126 list = nice_interfaces_get_local_ips(FALSE); |
|
| 127 if (list) { |
|
| 128 ip = list->data; |
|
| 129 } else { |
|
| 130 ip = "0.0.0.0"; |
|
| 131 } |
|
| 132 } |
|
| 133 } |
|
| 134 } |
|
| 135 } else { |
|
| 136 ip = _("Disabled"); |
|
| 137 } |
|
| 138 |
|
| 139 auto_ip_text = g_strdup_printf(_("Use _automatically detected IP address: %s"), ip); |
|
| 140 gtk_button_set_label(GTK_BUTTON(button), auto_ip_text); |
|
| 141 g_free(auto_ip_text); |
|
| 142 g_list_free_full(list, g_free); |
|
| 143 } |
|
| 144 |
|
| 145 /****************************************************************************** |
|
| 146 * GObject Implementation |
|
| 147 *****************************************************************************/ |
|
| 148 static void |
|
| 149 pidgin_network_page_class_init(PidginNetworkPageClass *klass) |
|
| 150 { |
|
| 151 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); |
|
| 152 |
|
| 153 gtk_widget_class_set_template_from_resource( |
|
| 154 widget_class, |
|
| 155 "/im/pidgin/Pidgin3/Prefs/network.ui" |
|
| 156 ); |
|
| 157 |
|
| 158 gtk_widget_class_bind_template_child( |
|
| 159 widget_class, PidginNetworkPage, stun_server); |
|
| 160 gtk_widget_class_bind_template_child( |
|
| 161 widget_class, PidginNetworkPage, auto_ip); |
|
| 162 gtk_widget_class_bind_template_child( |
|
| 163 widget_class, PidginNetworkPage, public_ip); |
|
| 164 gtk_widget_class_bind_template_child( |
|
| 165 widget_class, PidginNetworkPage, public_ip_hbox); |
|
| 166 gtk_widget_class_bind_template_child( |
|
| 167 widget_class, PidginNetworkPage, map_ports); |
|
| 168 gtk_widget_class_bind_template_child( |
|
| 169 widget_class, PidginNetworkPage, ports_range_use); |
|
| 170 gtk_widget_class_bind_template_child( |
|
| 171 widget_class, PidginNetworkPage, ports_range_hbox); |
|
| 172 gtk_widget_class_bind_template_child( |
|
| 173 widget_class, PidginNetworkPage, ports_range_start); |
|
| 174 gtk_widget_class_bind_template_child( |
|
| 175 widget_class, PidginNetworkPage, ports_range_end); |
|
| 176 gtk_widget_class_bind_template_child( |
|
| 177 widget_class, PidginNetworkPage, turn_server); |
|
| 178 gtk_widget_class_bind_template_child( |
|
| 179 widget_class, PidginNetworkPage, turn_port_udp); |
|
| 180 gtk_widget_class_bind_template_child( |
|
| 181 widget_class, PidginNetworkPage, turn_port_tcp); |
|
| 182 gtk_widget_class_bind_template_child( |
|
| 183 widget_class, PidginNetworkPage, turn_username); |
|
| 184 gtk_widget_class_bind_template_child( |
|
| 185 widget_class, PidginNetworkPage, turn_password); |
|
| 186 gtk_widget_class_bind_template_callback(widget_class, |
|
| 187 network_stun_server_changed_cb); |
|
| 188 gtk_widget_class_bind_template_callback(widget_class, |
|
| 189 auto_ip_button_clicked_cb); |
|
| 190 gtk_widget_class_bind_template_callback(widget_class, |
|
| 191 network_ip_changed); |
|
| 192 gtk_widget_class_bind_template_callback(widget_class, |
|
| 193 network_turn_server_changed_cb); |
|
| 194 } |
|
| 195 |
|
| 196 static void |
|
| 197 pidgin_network_page_init(PidginNetworkPage *page) |
|
| 198 { |
|
| 199 GtkStyleContext *context; |
|
| 200 GtkCssProvider *ip_css; |
|
| 201 |
|
| 202 gtk_widget_init_template(GTK_WIDGET(page)); |
|
| 203 |
|
| 204 gtk_entry_set_text(GTK_ENTRY(page->stun_server), |
|
| 205 purple_prefs_get_string("/purple/network/stun_server")); |
|
| 206 |
|
| 207 pidgin_prefs_bind_checkbox("/purple/network/auto_ip", page->auto_ip); |
|
| 208 auto_ip_button_clicked_cb(page->auto_ip, NULL); /* Update label */ |
|
| 209 |
|
| 210 gtk_entry_set_text(GTK_ENTRY(page->public_ip), |
|
| 211 purple_network_get_public_ip()); |
|
| 212 |
|
| 213 ip_css = gtk_css_provider_new(); |
|
| 214 gtk_css_provider_load_from_resource(ip_css, |
|
| 215 "/im/pidgin/Pidgin3/Prefs/ip.css"); |
|
| 216 |
|
| 217 context = gtk_widget_get_style_context(page->public_ip); |
|
| 218 gtk_style_context_add_provider(context, |
|
| 219 GTK_STYLE_PROVIDER(ip_css), |
|
| 220 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); |
|
| 221 |
|
| 222 g_object_bind_property(page->auto_ip, "active", |
|
| 223 page->public_ip_hbox, "sensitive", |
|
| 224 G_BINDING_SYNC_CREATE|G_BINDING_INVERT_BOOLEAN); |
|
| 225 |
|
| 226 pidgin_prefs_bind_checkbox("/purple/network/map_ports", |
|
| 227 page->map_ports); |
|
| 228 |
|
| 229 pidgin_prefs_bind_checkbox("/purple/network/ports_range_use", |
|
| 230 page->ports_range_use); |
|
| 231 g_object_bind_property(page->ports_range_use, "active", |
|
| 232 page->ports_range_hbox, "sensitive", |
|
| 233 G_BINDING_SYNC_CREATE); |
|
| 234 |
|
| 235 pidgin_prefs_bind_spin_button("/purple/network/ports_range_start", |
|
| 236 page->ports_range_start); |
|
| 237 pidgin_prefs_bind_spin_button("/purple/network/ports_range_end", |
|
| 238 page->ports_range_end); |
|
| 239 |
|
| 240 /* TURN server */ |
|
| 241 gtk_entry_set_text(GTK_ENTRY(page->turn_server), |
|
| 242 purple_prefs_get_string("/purple/network/turn_server")); |
|
| 243 |
|
| 244 pidgin_prefs_bind_spin_button("/purple/network/turn_port", |
|
| 245 page->turn_port_udp); |
|
| 246 |
|
| 247 pidgin_prefs_bind_spin_button("/purple/network/turn_port_tcp", |
|
| 248 page->turn_port_tcp); |
|
| 249 |
|
| 250 pidgin_prefs_bind_entry("/purple/network/turn_username", |
|
| 251 page->turn_username); |
|
| 252 pidgin_prefs_bind_entry("/purple/network/turn_password", |
|
| 253 page->turn_password); |
|
| 254 } |
|
| 255 |
|
| 256 /****************************************************************************** |
|
| 257 * API |
|
| 258 *****************************************************************************/ |
|
| 259 GtkWidget * |
|
| 260 pidgin_network_page_new(void) { |
|
| 261 return GTK_WIDGET(g_object_new(PIDGIN_TYPE_NETWORK_PAGE, NULL)); |
|
| 262 } |
|