network: Make CEIPAddressEntry

This commit is contained in:
Robert Ancell 2020-12-02 11:11:02 +13:00 committed by Benjamin Berg
parent c9ea93f195
commit c1a13ccaba
5 changed files with 186 additions and 98 deletions

View file

@ -28,6 +28,7 @@
#include <NetworkManager.h>
#include "list-box-helper.h"
#include "ce-ip-address-entry.h"
#include "ce-page.h"
#include "ce-page-ip4.h"
#include "ui-helpers.h"
@ -207,7 +208,7 @@ add_address_row (CEPageIP4 *self,
row_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_style_context_add_class (gtk_widget_get_style_context (row_box), "linked");
widget = gtk_entry_new ();
widget = GTK_WIDGET (ce_ip_address_entry_new (AF_INET));
g_signal_connect_object (widget, "changed", G_CALLBACK (ce_page_changed), self, G_CONNECT_SWAPPED);
g_signal_connect_object (widget, "activate", G_CALLBACK (ensure_empty_address_row), self, G_CONNECT_SWAPPED);
g_object_set_data (G_OBJECT (row), "address", widget);
@ -225,7 +226,7 @@ add_address_row (CEPageIP4 *self,
gtk_widget_set_hexpand (widget, TRUE);
gtk_container_add (GTK_CONTAINER (row_box), widget);
widget = gtk_entry_new ();
widget = GTK_WIDGET (ce_ip_address_entry_new (AF_INET));
g_signal_connect_object (widget, "changed", G_CALLBACK (ce_page_changed), self, G_CONNECT_SWAPPED);
g_signal_connect_object (widget, "activate", G_CALLBACK (ensure_empty_address_row), self, G_CONNECT_SWAPPED);
g_object_set_data (G_OBJECT (row), "gateway", widget);
@ -349,7 +350,7 @@ add_route_row (CEPageIP4 *self,
row_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_style_context_add_class (gtk_widget_get_style_context (row_box), "linked");
widget = gtk_entry_new ();
widget = GTK_WIDGET (ce_ip_address_entry_new (AF_INET));
g_signal_connect_object (widget, "changed", G_CALLBACK (ce_page_changed), self, G_CONNECT_SWAPPED);
g_signal_connect_object (widget, "activate", G_CALLBACK (ensure_empty_routes_row), self, G_CONNECT_SWAPPED);
g_object_set_data (G_OBJECT (row), "address", widget);
@ -367,7 +368,7 @@ add_route_row (CEPageIP4 *self,
gtk_widget_set_hexpand (widget, TRUE);
gtk_container_add (GTK_CONTAINER (row_box), widget);
widget = gtk_entry_new ();
widget = GTK_WIDGET (ce_ip_address_entry_new (AF_INET));
g_signal_connect_object (widget, "changed", G_CALLBACK (ce_page_changed), self, G_CONNECT_SWAPPED);
g_signal_connect_object (widget, "activate", G_CALLBACK (ensure_empty_routes_row), self, G_CONNECT_SWAPPED);
g_object_set_data (G_OBJECT (row), "gateway", widget);
@ -586,37 +587,27 @@ ui_to_setting (CEPageIP4 *self)
for (GList *l = address_children; l; l = l->next) {
GtkWidget *row = l->data;
GtkEntry *entry;
GtkEntry *gateway_entry;
const gchar *text_address;
CEIPAddressEntry *address_entry;
CEIPAddressEntry *gateway_entry;
const gchar *text_netmask;
const gchar *text_gateway = "";
NMIPAddress *addr;
guint32 prefix;
entry = GTK_ENTRY (g_object_get_data (G_OBJECT (row), "address"));
if (!entry)
address_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "address"));
if (!address_entry)
continue;
text_address = gtk_entry_get_text (entry);
text_netmask = gtk_entry_get_text (GTK_ENTRY (g_object_get_data (G_OBJECT (row), "network")));
gateway_entry = g_object_get_data (G_OBJECT (row), "gateway");
text_gateway = gtk_entry_get_text (gateway_entry);
gateway_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "gateway"));
if (!*text_address && !*text_netmask && !*text_gateway) {
if (ce_ip_address_entry_is_empty (address_entry) && !*text_netmask && ce_ip_address_entry_is_empty (gateway_entry)) {
/* ignore empty rows */
widget_unset_error (GTK_WIDGET (entry));
widget_unset_error (g_object_get_data (G_OBJECT (row), "network"));
widget_unset_error (GTK_WIDGET (gateway_entry));
continue;
}
if (!nm_utils_ipaddr_valid (AF_INET, text_address)) {
widget_set_error (GTK_WIDGET (entry));
if (!ce_ip_address_entry_is_valid (address_entry))
ret = FALSE;
} else {
widget_unset_error (GTK_WIDGET (entry));
}
if (!parse_netmask (text_netmask, &prefix)) {
widget_set_error (g_object_get_data (G_OBJECT (row), "network"));
@ -625,22 +616,19 @@ ui_to_setting (CEPageIP4 *self)
widget_unset_error (g_object_get_data (G_OBJECT (row), "network"));
}
if (*text_gateway &&
!nm_utils_ipaddr_valid (AF_INET, text_gateway)) {
widget_set_error (g_object_get_data (G_OBJECT (row), "gateway"));
if (!ce_ip_address_entry_is_valid (gateway_entry)) {
ret = FALSE;
} else {
widget_unset_error (GTK_WIDGET (gateway_entry));
if (*text_gateway) {
if (!ce_ip_address_entry_is_empty (gateway_entry)) {
g_assert (default_gateway == NULL);
default_gateway = text_gateway;
default_gateway = gtk_entry_get_text (GTK_ENTRY (gateway_entry));
}
}
if (!ret)
continue;
addr = nm_ip_address_new (AF_INET, text_address, prefix, NULL);
addr = nm_ip_address_new (AF_INET, gtk_entry_get_text (GTK_ENTRY (address_entry)), prefix, NULL);
if (addr)
g_ptr_array_add (addresses, addr);
@ -695,35 +683,29 @@ ui_to_setting (CEPageIP4 *self)
for (GList *l = routes_children; l; l = l->next) {
GtkWidget *row = l->data;
GtkEntry *entry;
const gchar *text_address;
CEIPAddressEntry *address_entry;
CEIPAddressEntry *gateway_entry;
const gchar *text_netmask;
const gchar *text_gateway;
const gchar *text_metric;
gint64 metric;
guint32 netmask;
NMIPRoute *route;
entry = GTK_ENTRY (g_object_get_data (G_OBJECT (row), "address"));
if (!entry)
address_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "address"));
if (!address_entry)
continue;
text_address = gtk_entry_get_text (entry);
text_netmask = gtk_entry_get_text (GTK_ENTRY (g_object_get_data (G_OBJECT (row), "netmask")));
text_gateway = gtk_entry_get_text (GTK_ENTRY (g_object_get_data (G_OBJECT (row), "gateway")));
gateway_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "gateway"));
text_metric = gtk_entry_get_text (GTK_ENTRY (g_object_get_data (G_OBJECT (row), "metric")));
if (!*text_address && !*text_netmask && !*text_gateway && !*text_metric) {
if (ce_ip_address_entry_is_empty (address_entry) && !*text_netmask && ce_ip_address_entry_is_empty (gateway_entry) && !*text_metric) {
/* ignore empty rows */
continue;
}
if (text_address && !nm_utils_ipaddr_valid (AF_INET, text_address)) {
widget_set_error (GTK_WIDGET (entry));
if (!ce_ip_address_entry_is_valid (address_entry))
ret = FALSE;
} else {
widget_unset_error (GTK_WIDGET (entry));
}
if (!parse_netmask (text_netmask, &netmask)) {
widget_set_error (GTK_WIDGET (g_object_get_data (G_OBJECT (row), "netmask")));
@ -732,12 +714,8 @@ ui_to_setting (CEPageIP4 *self)
widget_unset_error (GTK_WIDGET (g_object_get_data (G_OBJECT (row), "netmask")));
}
if (text_gateway && !nm_utils_ipaddr_valid (AF_INET, text_gateway)) {
widget_set_error (GTK_WIDGET (g_object_get_data (G_OBJECT (row), "gateway")));
if (!ce_ip_address_entry_is_valid (gateway_entry))
ret = FALSE;
} else {
widget_unset_error (GTK_WIDGET (g_object_get_data (G_OBJECT (row), "gateway")));
}
metric = -1;
if (*text_metric) {
@ -756,7 +734,7 @@ ui_to_setting (CEPageIP4 *self)
if (!ret)
continue;
route = nm_ip_route_new (AF_INET, text_address, netmask, text_gateway, metric, NULL);
route = nm_ip_route_new (AF_INET, gtk_entry_get_text (GTK_ENTRY (address_entry)), netmask, gtk_entry_get_text (GTK_ENTRY (gateway_entry)), metric, NULL);
if (route)
g_ptr_array_add (routes, route);