core: move generic connection complete function to a generic location

Out of NMDevice specific places to the utils code, so it can be used
more easily from everywhere.  There's nothing device-specific about
it anyway.
This commit is contained in:
Dan Williams
2011-01-13 13:28:52 -06:00
parent b5f3aa7120
commit 53766ae291
11 changed files with 159 additions and 156 deletions

View File

@@ -36,6 +36,9 @@
#include "nm-dbus-manager.h"
#include "nm-dispatcher-action.h"
#include "nm-dbus-glib-types.h"
#include "nm-setting-connection.h"
#include "nm-setting-ip4-config.h"
#include "nm-setting-ip6-config.h"
#include <netlink/addr.h>
#include <netinet/in.h>
@@ -708,3 +711,118 @@ nm_utils_get_proc_sys_net_value (const char *path,
return success;
}
static char *
get_new_connection_name (const GSList *existing,
const char *format,
const char *preferred)
{
GSList *names = NULL;
const GSList *iter;
char *cname = NULL;
int i = 0;
gboolean preferred_found = FALSE;
for (iter = existing; iter; iter = g_slist_next (iter)) {
NMConnection *candidate = NM_CONNECTION (iter->data);
NMSettingConnection *s_con;
const char *id;
s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (candidate, NM_TYPE_SETTING_CONNECTION));
id = nm_setting_connection_get_id (s_con);
g_assert (id);
names = g_slist_append (names, (gpointer) id);
if (preferred && !preferred_found && (strcmp (preferred, id) == 0))
preferred_found = TRUE;
}
/* Return the preferred name if it was unique */
if (preferred && !preferred_found) {
g_slist_free (names);
return g_strdup (preferred);
}
/* Otherwise find the next available unique connection name using the given
* connection name template.
*/
while (!cname && (i++ < 10000)) {
char *temp;
gboolean found = FALSE;
temp = g_strdup_printf (format, i);
for (iter = names; iter; iter = g_slist_next (iter)) {
if (!strcmp (iter->data, temp)) {
found = TRUE;
break;
}
}
if (!found)
cname = temp;
else
g_free (temp);
}
g_slist_free (names);
return cname;
}
void
nm_utils_complete_generic (NMConnection *connection,
const char *ctype,
const GSList *existing,
const char *format,
const char *preferred)
{
NMSettingConnection *s_con;
NMSettingIP4Config *s_ip4;
NMSettingIP6Config *s_ip6;
const char *method;
char *id, *uuid;
s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
if (!s_con) {
s_con = (NMSettingConnection *) nm_setting_connection_new ();
nm_connection_add_setting (connection, NM_SETTING (s_con));
}
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_TYPE, ctype, NULL);
if (!nm_setting_connection_get_uuid (s_con)) {
uuid = nm_utils_uuid_generate ();
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_UUID, uuid, NULL);
g_free (uuid);
}
/* Add a connection ID if absent */
if (!nm_setting_connection_get_id (s_con)) {
id = get_new_connection_name (existing, format, preferred);
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_ID, id, NULL);
g_free (id);
}
/* Add an 'auto' IPv4 connection if present */
s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
if (!s_ip4) {
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
nm_connection_add_setting (connection, NM_SETTING (s_ip4));
}
method = nm_setting_ip4_config_get_method (s_ip4);
if (!method) {
g_object_set (G_OBJECT (s_ip4),
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
NULL);
}
s_ip6 = (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
if (!s_ip6) {
s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new ();
nm_connection_add_setting (connection, NM_SETTING (s_ip6));
}
method = nm_setting_ip6_config_get_method (s_ip6);
if (!method) {
g_object_set (G_OBJECT (s_ip6),
NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO,
NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE,
NULL);
}
}

View File

@@ -78,4 +78,10 @@ gboolean nm_utils_get_proc_sys_net_value (const char *path,
const char *iface,
guint32 *out_value);
void nm_utils_complete_generic (NMConnection *connection,
const char *ctype,
const GSList *existing,
const char *format,
const char *preferred);
#endif /* NETWORK_MANAGER_UTILS_H */

View File

@@ -305,11 +305,11 @@ real_complete_connection (NMModem *modem,
nm_connection_add_setting (connection, NM_SETTING (s_ppp));
}
nm_device_complete_generic (connection,
NM_SETTING_CDMA_SETTING_NAME,
existing_connections,
_("CDMA connection %d"),
NULL);
nm_utils_complete_generic (connection,
NM_SETTING_CDMA_SETTING_NAME,
existing_connections,
_("CDMA connection %d"),
NULL);
return TRUE;
}

View File

@@ -508,11 +508,11 @@ real_complete_connection (NMModem *modem,
nm_connection_add_setting (connection, NM_SETTING (s_ppp));
}
nm_device_complete_generic (connection,
NM_SETTING_GSM_SETTING_NAME,
existing_connections,
_("GSM connection %d"),
NULL);
nm_utils_complete_generic (connection,
NM_SETTING_GSM_SETTING_NAME,
existing_connections,
_("GSM connection %d"),
NULL);
return TRUE;
}

View File

@@ -373,11 +373,11 @@ real_complete_connection (NMDevice *device,
return FALSE;
}
nm_device_complete_generic (connection,
NM_SETTING_BLUETOOTH_SETTING_NAME,
existing_connections,
format,
preferred);
nm_utils_complete_generic (connection,
NM_SETTING_BLUETOOTH_SETTING_NAME,
existing_connections,
format,
preferred);
setting_bdaddr = nm_setting_bluetooth_get_bdaddr (s_bt);
if (setting_bdaddr) {

View File

@@ -1647,11 +1647,11 @@ real_complete_connection (NMDevice *device,
/* Default to an ethernet-only connection, but if a PPPoE setting was given
* then PPPoE should be our connection type.
*/
nm_device_complete_generic (connection,
s_pppoe ? NM_SETTING_PPPOE_SETTING_NAME : NM_SETTING_CONNECTION_SETTING_NAME,
existing_connections,
s_pppoe ? _("PPPoE connection %d") : _("Wired connection %d"),
NULL);
nm_utils_complete_generic (connection,
s_pppoe ? NM_SETTING_PPPOE_SETTING_NAME : NM_SETTING_CONNECTION_SETTING_NAME,
existing_connections,
s_pppoe ? _("PPPoE connection %d") : _("Wired connection %d"),
NULL);
s_wired = (NMSettingWired *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRED);
if (!s_wired) {

View File

@@ -416,11 +416,11 @@ real_complete_connection (NMDevice *device,
}
nm_device_complete_generic (connection,
NM_SETTING_OLPC_MESH_SETTING_NAME,
existing_connections,
_("Mesh %d"),
NULL);
nm_utils_complete_generic (connection,
NM_SETTING_OLPC_MESH_SETTING_NAME,
existing_connections,
_("Mesh %d"),
NULL);
return TRUE;
}

View File

@@ -46,10 +46,4 @@ gboolean nm_device_get_firmware_missing (NMDevice *self);
void nm_device_set_firmware_missing (NMDevice *self, gboolean missing);
void nm_device_complete_generic (NMConnection *connection,
const char *ctype,
const GSList *existing,
const char *format,
const char *preferred);
#endif /* NM_DEVICE_PRIVATE_H */

View File

@@ -1462,11 +1462,11 @@ real_complete_connection (NMDevice *device,
str_ssid = nm_utils_ssid_to_utf8 ((const char *) ssid, ssid->len);
format = g_strdup_printf ("%s %%d", str_ssid);
nm_device_complete_generic (connection,
NM_SETTING_WIRELESS_SETTING_NAME,
existing_connections,
format,
str_ssid);
nm_utils_complete_generic (connection,
NM_SETTING_WIRELESS_SETTING_NAME,
existing_connections,
format,
str_ssid);
g_free (str_ssid);
g_free (format);

View File

@@ -604,121 +604,6 @@ nm_device_complete_connection (NMDevice *self,
return success;
}
static char *
nm_device_new_connection_name (const GSList *existing,
const char *format,
const char *preferred)
{
GSList *names = NULL;
const GSList *iter;
char *cname = NULL;
int i = 0;
gboolean preferred_found = FALSE;
for (iter = existing; iter; iter = g_slist_next (iter)) {
NMConnection *candidate = NM_CONNECTION (iter->data);
NMSettingConnection *s_con;
const char *id;
s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (candidate, NM_TYPE_SETTING_CONNECTION));
id = nm_setting_connection_get_id (s_con);
g_assert (id);
names = g_slist_append (names, (gpointer) id);
if (preferred && !preferred_found && (strcmp (preferred, id) == 0))
preferred_found = TRUE;
}
/* Return the preferred name if it was unique */
if (preferred && !preferred_found) {
g_slist_free (names);
return g_strdup (preferred);
}
/* Otherwise find the next available unique connection name using the given
* connection name template.
*/
while (!cname && (i++ < 10000)) {
char *temp;
gboolean found = FALSE;
temp = g_strdup_printf (format, i);
for (iter = names; iter; iter = g_slist_next (iter)) {
if (!strcmp (iter->data, temp)) {
found = TRUE;
break;
}
}
if (!found)
cname = temp;
else
g_free (temp);
}
g_slist_free (names);
return cname;
}
void
nm_device_complete_generic (NMConnection *connection,
const char *ctype,
const GSList *existing,
const char *format,
const char *preferred)
{
NMSettingConnection *s_con;
NMSettingIP4Config *s_ip4;
NMSettingIP6Config *s_ip6;
const char *method;
char *id, *uuid;
s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
if (!s_con) {
s_con = (NMSettingConnection *) nm_setting_connection_new ();
nm_connection_add_setting (connection, NM_SETTING (s_con));
}
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_TYPE, ctype, NULL);
if (!nm_setting_connection_get_uuid (s_con)) {
uuid = nm_utils_uuid_generate ();
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_UUID, uuid, NULL);
g_free (uuid);
}
/* Add a connection ID if absent */
if (!nm_setting_connection_get_id (s_con)) {
id = nm_device_new_connection_name (existing, format, preferred);
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_ID, id, NULL);
g_free (id);
}
/* Add an 'auto' IPv4 connection if present */
s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
if (!s_ip4) {
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
nm_connection_add_setting (connection, NM_SETTING (s_ip4));
}
method = nm_setting_ip4_config_get_method (s_ip4);
if (!method) {
g_object_set (G_OBJECT (s_ip4),
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
NULL);
}
s_ip6 = (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
if (!s_ip6) {
s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new ();
nm_connection_add_setting (connection, NM_SETTING (s_ip6));
}
method = nm_setting_ip6_config_get_method (s_ip6);
if (!method) {
g_object_set (G_OBJECT (s_ip6),
NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO,
NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE,
NULL);
}
}
static void
dnsmasq_state_changed_cb (NMDnsMasqManager *manager, guint32 status, gpointer user_data)
{

View File

@@ -559,11 +559,11 @@ real_complete_connection (NMDevice *device,
g_assert (nsp_name);
format = g_strdup_printf ("%s %%d", nsp_name);
nm_device_complete_generic (connection,
NM_SETTING_WIMAX_SETTING_NAME,
existing_connections,
format,
nsp_name);
nm_utils_complete_generic (connection,
NM_SETTING_WIMAX_SETTING_NAME,
existing_connections,
format,
nsp_name);
g_free (format);
g_object_set (G_OBJECT (s_wimax), NM_SETTING_WIMAX_NETWORK_NAME, nsp_name, NULL);