2007-10-16 Tambet Ingo <tambet@gmail.com>

Implement a generic NMSetting creator from setting name.
        While at it, get rid of all nm_setting_foo_new_from_hash()
functions and
        add a virtual function 'populate_fn'.

        * libnm-util/nm-connection.c (nm_connection_create_setting):
        * Implement.
        (register_default_creators): Register setting creators instead
of functions
        that create and then populate.
        (parse_one_setting): Use the common setting creator and then
setting specific
        poplulation function.

        * libnm-util/nm-setting.c: Get rid of
        * nm_setting_foo_new_from_hash() functions,
        they all looked exactly the same.
        Add a 'populate_fn' virtual function to NMSetting.
        Use default virtual functions in case they are not overriden.
        (nm_setting_populate_from_hash): Implement.

        * src/nm-device.c (real_act_stage3_ip_config_start): Don't hard
        * code the setting
        name, use a defined string.
        (real_act_stage4_get_ip4_config): Ditto.



git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@2978 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
This commit is contained in:
Tambet Ingo
2007-10-16 15:18:01 +00:00
parent b85dd0a01c
commit 850f441ea5
6 changed files with 89 additions and 175 deletions

View File

@@ -1,3 +1,25 @@
2007-10-16 Tambet Ingo <tambet@gmail.com>
Implement a generic NMSetting creator from setting name.
While at it, get rid of all nm_setting_foo_new_from_hash() functions and
add a virtual function 'populate_fn'.
* libnm-util/nm-connection.c (nm_connection_create_setting): Implement.
(register_default_creators): Register setting creators instead of functions
that create and then populate.
(parse_one_setting): Use the common setting creator and then setting specific
poplulation function.
* libnm-util/nm-setting.c: Get rid of nm_setting_foo_new_from_hash() functions,
they all looked exactly the same.
Add a 'populate_fn' virtual function to NMSetting.
Use default virtual functions in case they are not overriden.
(nm_setting_populate_from_hash): Implement.
* src/nm-device.c (real_act_stage3_ip_config_start): Don't hard code the setting
name, use a defined string.
(real_act_stage4_get_ip4_config): Ditto.
2007-10-16 Tambet Ingo <tambet@gmail.com> 2007-10-16 Tambet Ingo <tambet@gmail.com>
* src/nm-hal-manager.c (killswitch_getpower_reply): The type returned from * src/nm-hal-manager.c (killswitch_getpower_reply): The type returned from

View File

@@ -1,3 +1,5 @@
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
#include <glib-object.h> #include <glib-object.h>
#include <dbus/dbus-glib.h> #include <dbus/dbus-glib.h>
#include <string.h> #include <string.h>
@@ -30,15 +32,15 @@ register_default_creators (void)
const char *name; const char *name;
NMSettingCreateFn fn; NMSettingCreateFn fn;
} default_map[] = { } default_map[] = {
{ NM_SETTING_CONNECTION, nm_setting_connection_new_from_hash }, { NM_SETTING_CONNECTION, nm_setting_connection_new },
{ NM_SETTING_WIRED, nm_setting_wired_new_from_hash }, { NM_SETTING_WIRED, nm_setting_wired_new },
{ NM_SETTING_WIRELESS, nm_setting_wireless_new_from_hash }, { NM_SETTING_WIRELESS, nm_setting_wireless_new },
{ NM_SETTING_IP4_CONFIG, nm_setting_ip4_config_new_from_hash }, { NM_SETTING_IP4_CONFIG, nm_setting_ip4_config_new },
{ NM_SETTING_WIRELESS_SECURITY, nm_setting_wireless_security_new_from_hash }, { NM_SETTING_WIRELESS_SECURITY, nm_setting_wireless_security_new },
{ NM_SETTING_PPP, nm_setting_ppp_new_from_hash }, { NM_SETTING_PPP, nm_setting_ppp_new },
{ NM_SETTING_VPN, nm_setting_vpn_new_from_hash }, { NM_SETTING_VPN, nm_setting_vpn_new },
{ NM_SETTING_VPN_PROPERTIES, nm_setting_vpn_properties_new_from_hash }, { NM_SETTING_VPN_PROPERTIES, nm_setting_vpn_properties_new },
{ NULL, NULL} { NULL, NULL }
}; };
for (i = 0; default_map[i].name; i++) for (i = 0; default_map[i].name; i++)
@@ -68,20 +70,38 @@ nm_setting_parser_unregister (const char *name)
g_hash_table_remove (registered_setting_creators, name); g_hash_table_remove (registered_setting_creators, name);
} }
NMSetting *
nm_connection_create_setting (const char *name)
{
NMSettingCreateFn fn;
NMSetting *setting;
g_return_val_if_fail (name != NULL, NULL);
fn = (NMSettingCreateFn) g_hash_table_lookup (registered_setting_creators, name);
if (fn)
setting = fn ();
else {
g_warning ("Unknown setting '%s'", name);
setting = NULL;
}
return setting;
}
static void static void
parse_one_setting (gpointer key, gpointer value, gpointer user_data) parse_one_setting (gpointer key, gpointer value, gpointer user_data)
{ {
NMConnection *connection = (NMConnection *) user_data; NMConnection *connection = (NMConnection *) user_data;
NMSettingCreateFn fn;
NMSetting *setting; NMSetting *setting;
fn = (NMSettingCreateFn) g_hash_table_lookup (registered_setting_creators, key); setting = nm_connection_create_setting ((char *) key);
if (fn) { if (setting) {
setting = fn ((GHashTable *) value); if (nm_setting_populate_from_hash (setting, (GHashTable *) value))
if (setting)
nm_connection_add_setting (connection, setting); nm_connection_add_setting (connection, setting);
} else else
g_warning ("Unknown setting '%s'", (char *) key); nm_setting_destroy (setting);
}
} }
void void

View File

@@ -58,6 +58,7 @@ void nm_connection_for_each_setting_value (NMConnection *connection,
GHashTable *nm_connection_to_hash (NMConnection *connection); GHashTable *nm_connection_to_hash (NMConnection *connection);
void nm_connection_dump (NMConnection *connection); void nm_connection_dump (NMConnection *connection);
NMSetting *nm_connection_create_setting (const char *name);
void nm_setting_parser_register (const char *name, void nm_setting_parser_register (const char *name,
NMSettingCreateFn creator); NMSettingCreateFn creator);

View File

@@ -10,7 +10,19 @@
#include "nm-utils.h" #include "nm-utils.h"
static GHashTable * nm_setting_hash (NMSetting *setting); static GHashTable * nm_setting_hash (NMSetting *setting);
static gboolean nm_setting_populate_from_hash_default (NMSetting *setting, GHashTable *table);
static void default_setting_clear_secrets (NMSetting *setting);
gboolean
nm_setting_populate_from_hash (NMSetting *setting, GHashTable *hash)
{
g_return_val_if_fail (setting != NULL, FALSE);
if (setting->populate_fn)
return setting->populate_fn (setting, hash);
else
return nm_setting_populate_from_hash_default (setting, hash);
}
typedef struct { typedef struct {
gboolean success; gboolean success;
@@ -59,9 +71,11 @@ GHashTable *
nm_setting_to_hash (NMSetting *setting) nm_setting_to_hash (NMSetting *setting)
{ {
g_return_val_if_fail (setting != NULL, NULL); g_return_val_if_fail (setting != NULL, NULL);
g_return_val_if_fail (setting->hash_fn != NULL, NULL);
return setting->hash_fn (setting); if (setting->hash_fn)
return setting->hash_fn (setting);
else
return default_setting_hash (setting);
} }
gboolean gboolean
@@ -82,7 +96,9 @@ nm_setting_clear_secrets (NMSetting *setting)
g_return_if_fail (setting != NULL); g_return_if_fail (setting != NULL);
if (setting->clear_secrets_fn) if (setting->clear_secrets_fn)
return setting->clear_secrets_fn (setting); setting->clear_secrets_fn (setting);
else
default_setting_clear_secrets (setting);
} }
GPtrArray * GPtrArray *
@@ -299,7 +315,7 @@ string_slist_validate (GSList *list, const char **valid_values)
/***********************************************************************/ /***********************************************************************/
static gboolean static gboolean
nm_setting_populate_from_hash (NMSetting *setting, GHashTable *table) nm_setting_populate_from_hash_default (NMSetting *setting, GHashTable *table)
{ {
SettingMember *m; SettingMember *m;
@@ -505,29 +521,11 @@ nm_setting_connection_new (void)
setting->name = g_strdup (NM_SETTING_CONNECTION); setting->name = g_strdup (NM_SETTING_CONNECTION);
setting->_members = con_table; setting->_members = con_table;
setting->verify_fn = setting_connection_verify; setting->verify_fn = setting_connection_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_connection_destroy; setting->destroy_fn = setting_connection_destroy;
return setting; return setting;
} }
NMSetting *
nm_setting_connection_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_connection_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* IP4 config */ /* IP4 config */
static gboolean static gboolean
@@ -574,29 +572,11 @@ nm_setting_ip4_config_new (void)
setting->name = g_strdup (NM_SETTING_IP4_CONFIG); setting->name = g_strdup (NM_SETTING_IP4_CONFIG);
setting->_members = ip4_config_table; setting->_members = ip4_config_table;
setting->verify_fn = setting_ip4_config_verify; setting->verify_fn = setting_ip4_config_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_ip4_config_destroy; setting->destroy_fn = setting_ip4_config_destroy;
return setting; return setting;
} }
NMSetting *
nm_setting_ip4_config_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_ip4_config_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* Wired device */ /* Wired device */
static gboolean static gboolean
@@ -660,8 +640,6 @@ nm_setting_wired_new (void)
setting->name = g_strdup (NM_SETTING_WIRED); setting->name = g_strdup (NM_SETTING_WIRED);
setting->_members = wired_table; setting->_members = wired_table;
setting->verify_fn = setting_wired_verify; setting->verify_fn = setting_wired_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_wired_destroy; setting->destroy_fn = setting_wired_destroy;
s_wired->auto_negotiate = TRUE; s_wired->auto_negotiate = TRUE;
@@ -669,22 +647,6 @@ nm_setting_wired_new (void)
return setting; return setting;
} }
NMSetting *
nm_setting_wired_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_wired_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* Wireless device */ /* Wireless device */
static gboolean static gboolean
@@ -813,29 +775,11 @@ nm_setting_wireless_new (void)
setting->name = g_strdup (NM_SETTING_WIRELESS); setting->name = g_strdup (NM_SETTING_WIRELESS);
setting->_members = wireless_table; setting->_members = wireless_table;
setting->verify_fn = setting_wireless_verify; setting->verify_fn = setting_wireless_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_wireless_destroy; setting->destroy_fn = setting_wireless_destroy;
return setting; return setting;
} }
NMSetting *
nm_setting_wireless_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_wireless_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* Wireless security */ /* Wireless security */
static gboolean static gboolean
@@ -1182,31 +1126,13 @@ nm_setting_wireless_security_new (void)
setting->name = g_strdup (NM_SETTING_WIRELESS_SECURITY); setting->name = g_strdup (NM_SETTING_WIRELESS_SECURITY);
setting->_members = wireless_sec_table; setting->_members = wireless_sec_table;
setting->verify_fn = setting_wireless_security_verify; setting->verify_fn = setting_wireless_security_verify;
setting->hash_fn = default_setting_hash;
setting->update_secrets_fn = setting_wireless_security_update_secrets; setting->update_secrets_fn = setting_wireless_security_update_secrets;
setting->need_secrets_fn = setting_wireless_security_need_secrets; setting->need_secrets_fn = setting_wireless_security_need_secrets;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_wireless_security_destroy; setting->destroy_fn = setting_wireless_security_destroy;
return setting; return setting;
} }
NMSetting *
nm_setting_wireless_security_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_wireless_security_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* PPP */ /* PPP */
static gboolean static gboolean
@@ -1255,29 +1181,11 @@ nm_setting_ppp_new (void)
setting->name = g_strdup (NM_SETTING_PPP); setting->name = g_strdup (NM_SETTING_PPP);
setting->_members = ppp_table; setting->_members = ppp_table;
setting->verify_fn = setting_ppp_verify; setting->verify_fn = setting_ppp_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_ppp_destroy; setting->destroy_fn = setting_ppp_destroy;
return setting; return setting;
} }
NMSetting *
nm_setting_ppp_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_ppp_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* vpn setting */ /* vpn setting */
static gboolean static gboolean
@@ -1328,29 +1236,11 @@ nm_setting_vpn_new (void)
setting->name = g_strdup (NM_SETTING_VPN); setting->name = g_strdup (NM_SETTING_VPN);
setting->_members = vpn_table; setting->_members = vpn_table;
setting->verify_fn = setting_vpn_verify; setting->verify_fn = setting_vpn_verify;
setting->hash_fn = default_setting_hash;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_vpn_destroy; setting->destroy_fn = setting_vpn_destroy;
return setting; return setting;
} }
NMSetting *
nm_setting_vpn_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_vpn_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}
/* vpn-properties setting */ /* vpn-properties setting */
static gboolean static gboolean
@@ -1443,7 +1333,6 @@ nm_setting_vpn_properties_new (void)
setting->verify_fn = setting_vpn_properties_verify; setting->verify_fn = setting_vpn_properties_verify;
setting->hash_fn = setting_vpn_properties_hash; setting->hash_fn = setting_vpn_properties_hash;
setting->update_secrets_fn = setting_vpn_properties_update_secrets; setting->update_secrets_fn = setting_vpn_properties_update_secrets;
setting->clear_secrets_fn = default_setting_clear_secrets;
setting->destroy_fn = setting_vpn_properties_destroy; setting->destroy_fn = setting_vpn_properties_destroy;
s_vpn_props = (NMSettingVPNProperties *) setting; s_vpn_props = (NMSettingVPNProperties *) setting;
@@ -1453,19 +1342,3 @@ nm_setting_vpn_properties_new (void)
return setting; return setting;
} }
NMSetting *
nm_setting_vpn_properties_new_from_hash (GHashTable *hash)
{
NMSetting *setting;
g_return_val_if_fail (hash != NULL, NULL);
setting = nm_setting_vpn_properties_new ();
if (!nm_setting_populate_from_hash (setting, hash)) {
nm_setting_destroy (setting);
return NULL;
}
return setting;
}

View File

@@ -9,9 +9,11 @@ G_BEGIN_DECLS
typedef struct _NMSetting NMSetting; typedef struct _NMSetting NMSetting;
typedef NMSetting *(*NMSettingCreateFn) (GHashTable *settings); typedef NMSetting *(*NMSettingCreateFn) (void);
typedef gboolean (*NMSettingPopulateFn) (NMSetting *setting,
GHashTable *hash);
typedef gboolean (*NMSettingVerifyFn) (NMSetting *setting, typedef gboolean (*NMSettingVerifyFn) (NMSetting *setting,
GHashTable *all_settings); GHashTable *all_settings);
typedef GHashTable *(*NMSettingToHashFn) (NMSetting *setting); typedef GHashTable *(*NMSettingToHashFn) (NMSetting *setting);
@@ -51,6 +53,7 @@ struct _NMSetting {
char *name; char *name;
SettingMember *_members; /* Private */ SettingMember *_members; /* Private */
NMSettingPopulateFn populate_fn;
NMSettingVerifyFn verify_fn; NMSettingVerifyFn verify_fn;
NMSettingToHashFn hash_fn; NMSettingToHashFn hash_fn;
NMSettingUpdateSecretsFn update_secrets_fn; NMSettingUpdateSecretsFn update_secrets_fn;
@@ -59,6 +62,7 @@ struct _NMSetting {
NMSettingDestroyFn destroy_fn; NMSettingDestroyFn destroy_fn;
}; };
gboolean nm_setting_populate_from_hash (NMSetting *setting, GHashTable *hash);
gboolean nm_settings_verify (GHashTable *all_settings); gboolean nm_settings_verify (GHashTable *all_settings);
GHashTable *nm_setting_to_hash (NMSetting *setting); GHashTable *nm_setting_to_hash (NMSetting *setting);
gboolean nm_setting_update_secrets (NMSetting *setting, GHashTable *secrets); gboolean nm_setting_update_secrets (NMSetting *setting, GHashTable *secrets);
@@ -85,7 +89,6 @@ typedef struct {
} NMSettingConnection; } NMSettingConnection;
NMSetting *nm_setting_connection_new (void); NMSetting *nm_setting_connection_new (void);
NMSetting *nm_setting_connection_new_from_hash (GHashTable *settings);
/* IP4 config */ /* IP4 config */
@@ -101,7 +104,6 @@ typedef struct {
} NMSettingIP4Config; } NMSettingIP4Config;
NMSetting *nm_setting_ip4_config_new (void); NMSetting *nm_setting_ip4_config_new (void);
NMSetting *nm_setting_ip4_config_new_from_hash (GHashTable *settings);
/* Wired device */ /* Wired device */
@@ -119,7 +121,6 @@ typedef struct {
} NMSettingWired; } NMSettingWired;
NMSetting *nm_setting_wired_new (void); NMSetting *nm_setting_wired_new (void);
NMSetting *nm_setting_wired_new_from_hash (GHashTable *settings);
/* Wireless device */ /* Wireless device */
@@ -142,7 +143,6 @@ typedef struct {
} NMSettingWireless; } NMSettingWireless;
NMSetting *nm_setting_wireless_new (void); NMSetting *nm_setting_wireless_new (void);
NMSetting *nm_setting_wireless_new_from_hash (GHashTable *settings);
/* Wireless security */ /* Wireless security */
@@ -187,7 +187,6 @@ typedef struct {
} NMSettingWirelessSecurity; } NMSettingWirelessSecurity;
NMSetting *nm_setting_wireless_security_new (void); NMSetting *nm_setting_wireless_security_new (void);
NMSetting *nm_setting_wireless_security_new_from_hash (GHashTable *settings);
/* PPP */ /* PPP */
@@ -217,7 +216,6 @@ typedef struct {
} NMSettingPPP; } NMSettingPPP;
NMSetting *nm_setting_ppp_new (void); NMSetting *nm_setting_ppp_new (void);
NMSetting *nm_setting_ppp_new_from_hash (GHashTable *settings);
/* VPN */ /* VPN */
@@ -232,7 +230,6 @@ typedef struct {
} NMSettingVPN; } NMSettingVPN;
NMSetting *nm_setting_vpn_new (void); NMSetting *nm_setting_vpn_new (void);
NMSetting *nm_setting_vpn_new_from_hash (GHashTable *hash);
/* VPN properties */ /* VPN properties */
@@ -245,7 +242,6 @@ typedef struct {
} NMSettingVPNProperties; } NMSettingVPNProperties;
NMSetting *nm_setting_vpn_properties_new (void); NMSetting *nm_setting_vpn_properties_new (void);
NMSetting *nm_setting_vpn_properties_new_from_hash (GHashTable *hash);
G_END_DECLS G_END_DECLS

View File

@@ -555,7 +555,8 @@ real_act_stage3_ip_config_start (NMDevice *self)
NMActStageReturn ret = NM_ACT_STAGE_RETURN_SUCCESS; NMActStageReturn ret = NM_ACT_STAGE_RETURN_SUCCESS;
req = nm_device_get_act_request (self); req = nm_device_get_act_request (self);
setting = (NMSettingIP4Config *) nm_connection_get_setting (nm_act_request_get_connection (req), "ipv4"); setting = (NMSettingIP4Config *) nm_connection_get_setting (nm_act_request_get_connection (req),
NM_SETTING_IP4_CONFIG);
/* If we did not receive IP4 configuration information, default to DHCP */ /* If we did not receive IP4 configuration information, default to DHCP */
if (!setting || setting->manual == FALSE) { if (!setting || setting->manual == FALSE) {
@@ -702,7 +703,8 @@ real_act_stage4_get_ip4_config (NMDevice *self,
} }
req = nm_device_get_act_request (self); req = nm_device_get_act_request (self);
setting = (NMSettingIP4Config *) nm_connection_get_setting (nm_act_request_get_connection (req), "ipv4"); setting = (NMSettingIP4Config *) nm_connection_get_setting (nm_act_request_get_connection (req),
NM_SETTING_IP4_CONFIG);
if (real_config && setting) { if (real_config && setting) {
/* If settings are provided, use them, even if it means overriding the values we got from DHCP */ /* If settings are provided, use them, even if it means overriding the values we got from DHCP */