nmt-page-ip4.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
  2. /*
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License as
  5. * published by the Free Software Foundation; either version 2 of the
  6. * License, or (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Copyright 2013 Red Hat, Inc.
  17. */
  18. /**
  19. * SECTION:nmt-page-ip4
  20. * @short_description: The editor page for IP4 configuration
  21. */
  22. #include "config.h"
  23. #include <stdlib.h>
  24. #include <glib.h>
  25. #include <glib/gi18n-lib.h>
  26. #include "nmt-page-ip4.h"
  27. #include "nmt-ip-entry.h"
  28. #include "nmt-address-list.h"
  29. #include "nmt-route-editor.h"
  30. #include "nm-editor-bindings.h"
  31. G_DEFINE_TYPE (NmtPageIP4, nmt_page_ip4, NMT_TYPE_EDITOR_PAGE)
  32. static NmtNewtPopupEntry ip4methods[] = {
  33. { N_("Disabled"), NM_SETTING_IP4_CONFIG_METHOD_DISABLED },
  34. { N_("Automatic"), NM_SETTING_IP4_CONFIG_METHOD_AUTO },
  35. { N_("Link-Local"), NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL },
  36. { N_("Manual"), NM_SETTING_IP4_CONFIG_METHOD_MANUAL },
  37. { N_("Shared"), NM_SETTING_IP4_CONFIG_METHOD_SHARED },
  38. { NULL, NULL }
  39. };
  40. NmtNewtWidget *
  41. nmt_page_ip4_new (NMConnection *conn)
  42. {
  43. return g_object_new (NMT_TYPE_PAGE_IP4,
  44. "connection", conn,
  45. "title", _("IPv4 CONFIGURATION"),
  46. NULL);
  47. }
  48. gboolean
  49. nmt_page_ip4_is_non_empty (NmtPageIP4 *ip4)
  50. {
  51. NMConnection *conn;
  52. NMSettingIP4Config *s_ip4;
  53. conn = nmt_editor_page_get_connection (NMT_EDITOR_PAGE (ip4));
  54. s_ip4 = nm_connection_get_setting_ip4_config (conn);
  55. if ( !g_strcmp0 (nm_setting_ip4_config_get_method (s_ip4), NM_SETTING_IP4_CONFIG_METHOD_MANUAL)
  56. || nm_setting_ip4_config_get_num_addresses (s_ip4))
  57. return TRUE;
  58. return FALSE;
  59. }
  60. static void
  61. nmt_page_ip4_init (NmtPageIP4 *ip4)
  62. {
  63. }
  64. static void
  65. edit_routes (NmtNewtButton *button,
  66. gpointer user_data)
  67. {
  68. NMSetting *s_ip4 = user_data;
  69. NmtNewtForm *form;
  70. form = nmt_route_editor_new (s_ip4);
  71. nmt_newt_form_run_sync (form);
  72. g_object_unref (form);
  73. }
  74. static gboolean
  75. ip4_routes_transform_to_description (GBinding *binding,
  76. const GValue *source_value,
  77. GValue *target_value,
  78. gpointer user_data)
  79. {
  80. GPtrArray *routes;
  81. char *text;
  82. routes = g_value_get_boxed (source_value);
  83. if (!routes || !routes->len)
  84. text = g_strdup (_("(No custom routes)"));
  85. else {
  86. text = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
  87. "One custom route",
  88. "%d custom routes",
  89. routes->len),
  90. routes->len);
  91. }
  92. g_value_take_string (target_value, text);
  93. return TRUE;
  94. }
  95. static void
  96. nmt_page_ip4_constructed (GObject *object)
  97. {
  98. NmtPageIP4 *ip4 = NMT_PAGE_IP4 (object);
  99. NmtPageGrid *grid;
  100. NMSettingIP4Config *s_ip4;
  101. NmtNewtWidget *widget, *button;
  102. NMConnection *conn;
  103. conn = nmt_editor_page_get_connection (NMT_EDITOR_PAGE (ip4));
  104. s_ip4 = nm_connection_get_setting_ip4_config (conn);
  105. if (!s_ip4) {
  106. s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
  107. g_object_set (G_OBJECT (s_ip4),
  108. NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
  109. NULL);
  110. nm_connection_add_setting (conn, (NMSetting *) s_ip4);
  111. }
  112. widget = nmt_newt_popup_new (ip4methods);
  113. g_object_bind_property (s_ip4, NM_SETTING_IP4_CONFIG_METHOD,
  114. widget, "active-id",
  115. G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
  116. nmt_editor_page_set_header_widget (NMT_EDITOR_PAGE (ip4), widget);
  117. grid = NMT_PAGE_GRID (ip4);
  118. widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4_WITH_PREFIX);
  119. nm_editor_bind_ip4_addresses_with_prefix_to_strv (s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES,
  120. widget, "strings",
  121. G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
  122. nmt_page_grid_append (grid, _("Addresses"), widget, NULL);
  123. widget = nmt_ip_entry_new (25, AF_INET, FALSE, TRUE);
  124. nm_editor_bind_ip4_gateway_to_string (s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES,
  125. widget, "text",
  126. G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
  127. nmt_page_grid_append (grid, _("Gateway"), widget, NULL);
  128. widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4);
  129. nm_editor_bind_ip4_addresses_to_strv (s_ip4, NM_SETTING_IP4_CONFIG_DNS,
  130. widget, "strings",
  131. G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
  132. nmt_page_grid_append (grid, _("DNS servers"), widget, NULL);
  133. widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME);
  134. g_object_bind_property (s_ip4, NM_SETTING_IP4_CONFIG_DNS_SEARCH,
  135. widget, "strings",
  136. G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
  137. nmt_page_grid_append (grid, _("Search domains"), widget, NULL);
  138. nmt_page_grid_append (grid, NULL, nmt_newt_separator_new (), NULL);
  139. widget = g_object_new (NMT_TYPE_NEWT_LABEL,
  140. "text", "",
  141. "style", NMT_NEWT_LABEL_PLAIN,
  142. NULL);
  143. g_object_bind_property_full (s_ip4, NM_SETTING_IP4_CONFIG_ROUTES,
  144. widget, "text",
  145. G_BINDING_SYNC_CREATE,
  146. ip4_routes_transform_to_description,
  147. NULL, NULL, NULL);
  148. button = nmt_newt_button_new (_("Edit..."));
  149. g_signal_connect (button, "clicked", G_CALLBACK (edit_routes), s_ip4);
  150. nmt_page_grid_append (grid, _("Routing"), widget, button);
  151. widget = nmt_newt_checkbox_new (_("Never use this network for default route"));
  152. g_object_bind_property (s_ip4, NM_SETTING_IP4_CONFIG_NEVER_DEFAULT,
  153. widget, "active",
  154. G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
  155. nmt_page_grid_append (grid, NULL, widget, NULL);
  156. nmt_page_grid_append (grid, NULL, nmt_newt_separator_new (), NULL);
  157. widget = nmt_newt_checkbox_new (_("Require IPv4 addressing for this connection"));
  158. g_object_bind_property (s_ip4, NM_SETTING_IP4_CONFIG_MAY_FAIL,
  159. widget, "active",
  160. G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
  161. nmt_page_grid_append (grid, NULL, widget, NULL);
  162. G_OBJECT_CLASS (nmt_page_ip4_parent_class)->constructed (object);
  163. }
  164. static void
  165. nmt_page_ip4_class_init (NmtPageIP4Class *ip4_class)
  166. {
  167. GObjectClass *object_class = G_OBJECT_CLASS (ip4_class);
  168. object_class->constructed = nmt_page_ip4_constructed;
  169. }