diff --git a/panels/network/connection-editor/ce-netmask-entry.c b/panels/network/connection-editor/ce-netmask-entry.c new file mode 100644 index 000000000..9df5547d2 --- /dev/null +++ b/panels/network/connection-editor/ce-netmask-entry.c @@ -0,0 +1,137 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2020 Canonical Ltd. + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include + +#include "ce-netmask-entry.h" + +struct _CENetmaskEntry +{ + GtkEntry parent_instance; +}; + +static void ce_netmask_entry_editable_init (GtkEditableInterface *iface); + +G_DEFINE_TYPE_WITH_CODE (CENetmaskEntry, ce_netmask_entry, GTK_TYPE_ENTRY, + G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE, + ce_netmask_entry_editable_init)) + +static gboolean +parse_netmask (const char *str, guint32 *prefix) +{ + struct in_addr tmp_addr; + glong tmp_prefix; + + /* Is it a prefix? */ + errno = 0; + if (!strchr (str, '.')) + { + tmp_prefix = strtol (str, NULL, 10); + if (!errno && tmp_prefix >= 0 && tmp_prefix <= 32) + { + if (prefix != NULL) + *prefix = tmp_prefix; + return TRUE; + } + } + + /* Is it a netmask? */ + if (inet_pton (AF_INET, str, &tmp_addr) > 0) + { + if (prefix != NULL) + *prefix = nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr); + return TRUE; + } + + return FALSE; +} + +static void +ce_netmask_entry_changed (GtkEditable *editable) +{ + CENetmaskEntry *self = CE_NETMASK_ENTRY (editable); + GtkStyleContext *context; + + context = gtk_widget_get_style_context (GTK_WIDGET (self)); + if (ce_netmask_entry_is_valid (self)) + gtk_style_context_remove_class (context, "error"); + else + gtk_style_context_add_class (context, "error"); +} + +static void +ce_netmask_entry_init (CENetmaskEntry *self) +{ +} + +static void +ce_netmask_entry_editable_init (GtkEditableInterface *iface) +{ + iface->changed = ce_netmask_entry_changed; +} + +static void +ce_netmask_entry_class_init (CENetmaskEntryClass *klass) +{ +} + +CENetmaskEntry * +ce_netmask_entry_new (void) +{ + return CE_NETMASK_ENTRY (g_object_new (ce_netmask_entry_get_type (), NULL)); +} + +gboolean +ce_netmask_entry_is_empty (CENetmaskEntry *self) +{ + const gchar *text; + + g_return_val_if_fail (CE_IS_NETMASK_ENTRY (self), FALSE); + + text = gtk_entry_get_text (GTK_ENTRY (self)); + return text[0] == '\0'; +} + +gboolean +ce_netmask_entry_is_valid (CENetmaskEntry *self) +{ + const gchar *text; + + g_return_val_if_fail (CE_IS_NETMASK_ENTRY (self), FALSE); + + text = gtk_entry_get_text (GTK_ENTRY (self)); + return text[0] == '\0' || parse_netmask (text, NULL); +} + +guint32 +ce_netmask_entry_get_prefix (CENetmaskEntry *self) +{ + const gchar *text; + guint32 prefix = 0; + + g_return_val_if_fail (CE_IS_NETMASK_ENTRY (self), 0); + + text = gtk_entry_get_text (GTK_ENTRY (self)); + parse_netmask (text, &prefix); + + return prefix; +} diff --git a/panels/network/connection-editor/ce-netmask-entry.h b/panels/network/connection-editor/ce-netmask-entry.h new file mode 100644 index 000000000..ff6337e76 --- /dev/null +++ b/panels/network/connection-editor/ce-netmask-entry.h @@ -0,0 +1,38 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2020 Canonical Ltd. + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include + +G_BEGIN_DECLS + +G_DECLARE_FINAL_TYPE (CENetmaskEntry, ce_netmask_entry, CE, NETMASK_ENTRY, GtkEntry) + +CENetmaskEntry *ce_netmask_entry_new (void); + +gboolean ce_netmask_entry_is_empty (CENetmaskEntry *entry); + +gboolean ce_netmask_entry_is_valid (CENetmaskEntry *entry); + +guint32 ce_netmask_entry_get_prefix (CENetmaskEntry *entry); + +G_END_DECLS diff --git a/panels/network/connection-editor/ce-page-ip4.c b/panels/network/connection-editor/ce-page-ip4.c index f6dc38379..c1500f99f 100644 --- a/panels/network/connection-editor/ce-page-ip4.c +++ b/panels/network/connection-editor/ce-page-ip4.c @@ -29,6 +29,7 @@ #include "list-box-helper.h" #include "ce-ip-address-entry.h" +#include "ce-netmask-entry.h" #include "ce-page.h" #include "ce-page-ip4.h" #include "ui-helpers.h" @@ -217,10 +218,10 @@ 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_netmask_entry_new ()); 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), "network", widget); + g_object_set_data (G_OBJECT (row), "netmask", widget); gtk_entry_set_text (GTK_ENTRY (widget), network); gtk_entry_set_width_chars (GTK_ENTRY (widget), 16); gtk_widget_set_hexpand (widget, TRUE); @@ -526,32 +527,6 @@ connect_ip4_page (CEPageIP4 *self) method_changed (self); } -static gboolean -parse_netmask (const char *str, guint32 *prefix) -{ - struct in_addr tmp_addr; - glong tmp_prefix; - - errno = 0; - - /* Is it a prefix? */ - if (!strchr (str, '.')) { - tmp_prefix = strtol (str, NULL, 10); - if (!errno && tmp_prefix >= 0 && tmp_prefix <= 32) { - *prefix = tmp_prefix; - return TRUE; - } - } - - /* Is it a netmask? */ - if (inet_pton (AF_INET, str, &tmp_addr) > 0) { - *prefix = nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr); - return TRUE; - } - - return FALSE; -} - static gboolean ui_to_setting (CEPageIP4 *self) { @@ -588,33 +563,27 @@ ui_to_setting (CEPageIP4 *self) for (GList *l = address_children; l; l = l->next) { GtkWidget *row = l->data; CEIPAddressEntry *address_entry; + CENetmaskEntry *netmask_entry; CEIPAddressEntry *gateway_entry; - const gchar *text_netmask; NMIPAddress *addr; - guint32 prefix; address_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "address")); if (!address_entry) continue; - text_netmask = gtk_entry_get_text (GTK_ENTRY (g_object_get_data (G_OBJECT (row), "network"))); + netmask_entry = CE_NETMASK_ENTRY (g_object_get_data (G_OBJECT (row), "netmask")); gateway_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "gateway")); - if (ce_ip_address_entry_is_empty (address_entry) && !*text_netmask && ce_ip_address_entry_is_empty (gateway_entry)) { + if (ce_ip_address_entry_is_empty (address_entry) && ce_netmask_entry_is_empty (netmask_entry) && ce_ip_address_entry_is_empty (gateway_entry)) { /* ignore empty rows */ - widget_unset_error (g_object_get_data (G_OBJECT (row), "network")); continue; } if (!ce_ip_address_entry_is_valid (address_entry)) ret = FALSE; - if (!parse_netmask (text_netmask, &prefix)) { - widget_set_error (g_object_get_data (G_OBJECT (row), "network")); + if (!ce_netmask_entry_is_valid (netmask_entry)) ret = FALSE; - } else { - widget_unset_error (g_object_get_data (G_OBJECT (row), "network")); - } if (!ce_ip_address_entry_is_valid (gateway_entry)) { ret = FALSE; @@ -628,7 +597,7 @@ ui_to_setting (CEPageIP4 *self) if (!ret) continue; - addr = nm_ip_address_new (AF_INET, gtk_entry_get_text (GTK_ENTRY (address_entry)), prefix, NULL); + addr = nm_ip_address_new (AF_INET, gtk_entry_get_text (GTK_ENTRY (address_entry)), ce_netmask_entry_get_prefix (netmask_entry), NULL); if (addr) g_ptr_array_add (addresses, addr); @@ -684,22 +653,21 @@ ui_to_setting (CEPageIP4 *self) for (GList *l = routes_children; l; l = l->next) { GtkWidget *row = l->data; CEIPAddressEntry *address_entry; + CENetmaskEntry *netmask_entry; CEIPAddressEntry *gateway_entry; - const gchar *text_netmask; const gchar *text_metric; gint64 metric; - guint32 netmask; NMIPRoute *route; address_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "address")); if (!address_entry) continue; - text_netmask = gtk_entry_get_text (GTK_ENTRY (g_object_get_data (G_OBJECT (row), "netmask"))); + netmask_entry = CE_NETMASK_ENTRY (g_object_get_data (G_OBJECT (row), "netmask")); 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 (ce_ip_address_entry_is_empty (address_entry) && !*text_netmask && ce_ip_address_entry_is_empty (gateway_entry) && !*text_metric) { + if (ce_ip_address_entry_is_empty (address_entry) && ce_netmask_entry_is_empty (netmask_entry) && ce_ip_address_entry_is_empty (gateway_entry) && !*text_metric) { /* ignore empty rows */ continue; } @@ -707,12 +675,8 @@ ui_to_setting (CEPageIP4 *self) if (!ce_ip_address_entry_is_valid (address_entry)) ret = FALSE; - if (!parse_netmask (text_netmask, &netmask)) { - widget_set_error (GTK_WIDGET (g_object_get_data (G_OBJECT (row), "netmask"))); + if (!ce_netmask_entry_is_valid (netmask_entry)) ret = FALSE; - } else { - widget_unset_error (GTK_WIDGET (g_object_get_data (G_OBJECT (row), "netmask"))); - } if (!ce_ip_address_entry_is_valid (gateway_entry)) ret = FALSE; @@ -734,7 +698,7 @@ ui_to_setting (CEPageIP4 *self) if (!ret) continue; - 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); + route = nm_ip_route_new (AF_INET, gtk_entry_get_text (GTK_ENTRY (address_entry)), ce_netmask_entry_get_prefix (netmask_entry), gtk_entry_get_text (GTK_ENTRY (gateway_entry)), metric, NULL); if (route) g_ptr_array_add (routes, route); diff --git a/panels/network/connection-editor/meson.build b/panels/network/connection-editor/meson.build index 934392cbd..fd4ddf957 100644 --- a/panels/network/connection-editor/meson.build +++ b/panels/network/connection-editor/meson.build @@ -2,6 +2,7 @@ name = 'connection-editor' sources = files( 'ce-ip-address-entry.c', + 'ce-netmask-entry.c', 'ce-page-8021x-security.c', 'ce-page-details.c', 'ce-page-ethernet.c',