network: Make CENetmaskEntry

This commit is contained in:
Robert Ancell 2020-12-02 11:22:32 +13:00 committed by Benjamin Berg
parent c1a13ccaba
commit d79fa68fbc
4 changed files with 189 additions and 49 deletions

View file

@ -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 <arpa/inet.h>
#include <NetworkManager.h>
#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;
}

View file

@ -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 <gtk/gtk.h>
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

View file

@ -29,6 +29,7 @@
#include "list-box-helper.h" #include "list-box-helper.h"
#include "ce-ip-address-entry.h" #include "ce-ip-address-entry.h"
#include "ce-netmask-entry.h"
#include "ce-page.h" #include "ce-page.h"
#include "ce-page-ip4.h" #include "ce-page-ip4.h"
#include "ui-helpers.h" #include "ui-helpers.h"
@ -217,10 +218,10 @@ add_address_row (CEPageIP4 *self,
gtk_widget_set_hexpand (widget, TRUE); gtk_widget_set_hexpand (widget, TRUE);
gtk_container_add (GTK_CONTAINER (row_box), widget); 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, "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_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_text (GTK_ENTRY (widget), network);
gtk_entry_set_width_chars (GTK_ENTRY (widget), 16); gtk_entry_set_width_chars (GTK_ENTRY (widget), 16);
gtk_widget_set_hexpand (widget, TRUE); gtk_widget_set_hexpand (widget, TRUE);
@ -526,32 +527,6 @@ connect_ip4_page (CEPageIP4 *self)
method_changed (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 static gboolean
ui_to_setting (CEPageIP4 *self) ui_to_setting (CEPageIP4 *self)
{ {
@ -588,33 +563,27 @@ ui_to_setting (CEPageIP4 *self)
for (GList *l = address_children; l; l = l->next) { for (GList *l = address_children; l; l = l->next) {
GtkWidget *row = l->data; GtkWidget *row = l->data;
CEIPAddressEntry *address_entry; CEIPAddressEntry *address_entry;
CENetmaskEntry *netmask_entry;
CEIPAddressEntry *gateway_entry; CEIPAddressEntry *gateway_entry;
const gchar *text_netmask;
NMIPAddress *addr; NMIPAddress *addr;
guint32 prefix;
address_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "address")); address_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "address"));
if (!address_entry) if (!address_entry)
continue; 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")); 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 */ /* ignore empty rows */
widget_unset_error (g_object_get_data (G_OBJECT (row), "network"));
continue; continue;
} }
if (!ce_ip_address_entry_is_valid (address_entry)) if (!ce_ip_address_entry_is_valid (address_entry))
ret = FALSE; ret = FALSE;
if (!parse_netmask (text_netmask, &prefix)) { if (!ce_netmask_entry_is_valid (netmask_entry))
widget_set_error (g_object_get_data (G_OBJECT (row), "network"));
ret = FALSE; ret = FALSE;
} else {
widget_unset_error (g_object_get_data (G_OBJECT (row), "network"));
}
if (!ce_ip_address_entry_is_valid (gateway_entry)) { if (!ce_ip_address_entry_is_valid (gateway_entry)) {
ret = FALSE; ret = FALSE;
@ -628,7 +597,7 @@ ui_to_setting (CEPageIP4 *self)
if (!ret) if (!ret)
continue; 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) if (addr)
g_ptr_array_add (addresses, 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) { for (GList *l = routes_children; l; l = l->next) {
GtkWidget *row = l->data; GtkWidget *row = l->data;
CEIPAddressEntry *address_entry; CEIPAddressEntry *address_entry;
CENetmaskEntry *netmask_entry;
CEIPAddressEntry *gateway_entry; CEIPAddressEntry *gateway_entry;
const gchar *text_netmask;
const gchar *text_metric; const gchar *text_metric;
gint64 metric; gint64 metric;
guint32 netmask;
NMIPRoute *route; NMIPRoute *route;
address_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "address")); address_entry = CE_IP_ADDRESS_ENTRY (g_object_get_data (G_OBJECT (row), "address"));
if (!address_entry) if (!address_entry)
continue; 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")); 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"))); 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 */ /* ignore empty rows */
continue; continue;
} }
@ -707,12 +675,8 @@ ui_to_setting (CEPageIP4 *self)
if (!ce_ip_address_entry_is_valid (address_entry)) if (!ce_ip_address_entry_is_valid (address_entry))
ret = FALSE; ret = FALSE;
if (!parse_netmask (text_netmask, &netmask)) { if (!ce_netmask_entry_is_valid (netmask_entry))
widget_set_error (GTK_WIDGET (g_object_get_data (G_OBJECT (row), "netmask")));
ret = FALSE; 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)) if (!ce_ip_address_entry_is_valid (gateway_entry))
ret = FALSE; ret = FALSE;
@ -734,7 +698,7 @@ ui_to_setting (CEPageIP4 *self)
if (!ret) if (!ret)
continue; 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) if (route)
g_ptr_array_add (routes, route); g_ptr_array_add (routes, route);

View file

@ -2,6 +2,7 @@ name = 'connection-editor'
sources = files( sources = files(
'ce-ip-address-entry.c', 'ce-ip-address-entry.c',
'ce-netmask-entry.c',
'ce-page-8021x-security.c', 'ce-page-8021x-security.c',
'ce-page-details.c', 'ce-page-details.c',
'ce-page-ethernet.c', 'ce-page-ethernet.c',