From 8b1411712a68caed0869c888caba1b0f736ae26b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Oct 2016 18:35:23 +0200 Subject: [PATCH] keyfile: refactor GObject type creation --- src/settings/nm-settings.c | 3 +- .../plugins/keyfile/nms-keyfile-connection.c | 113 ++++++++++-------- .../plugins/keyfile/nms-keyfile-connection.h | 9 +- .../plugins/keyfile/nms-keyfile-plugin.c | 4 +- .../plugins/keyfile/nms-keyfile-plugin.h | 2 +- .../plugins/keyfile/nms-keyfile-reader.c | 2 + .../plugins/keyfile/nms-keyfile-utils.c | 4 +- .../plugins/keyfile/nms-keyfile-writer.c | 3 + 8 files changed, 75 insertions(+), 65 deletions(-) diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index 6e52b2db3..514c7edb4 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -798,10 +798,9 @@ find_plugin (GSList *list, const char *pname) static void add_keyfile_plugin (NMSettings *self) { - gs_unref_object GObject *keyfile_plugin = NULL; + gs_unref_object NMSKeyfilePlugin *keyfile_plugin = NULL; keyfile_plugin = nms_keyfile_plugin_new (); - g_assert (keyfile_plugin); if (!add_plugin (self, NM_SETTINGS_PLUGIN (keyfile_plugin))) g_return_if_reached (); } diff --git a/src/settings/plugins/keyfile/nms-keyfile-connection.c b/src/settings/plugins/keyfile/nms-keyfile-connection.c index 0a80073a8..948290fa6 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-connection.c +++ b/src/settings/plugins/keyfile/nms-keyfile-connection.c @@ -36,57 +36,19 @@ #include "nms-keyfile-writer.h" #include "nms-keyfile-utils.h" +/*****************************************************************************/ + +struct _NMSKeyfileConnection { + NMSettingsConnection parent; +}; + +struct _NMSKeyfileConnectionClass { + NMSettingsConnectionClass parent; +}; + G_DEFINE_TYPE (NMSKeyfileConnection, nms_keyfile_connection, NM_TYPE_SETTINGS_CONNECTION) -NMSKeyfileConnection * -nms_keyfile_connection_new (NMConnection *source, - const char *full_path, - GError **error) -{ - GObject *object; - NMConnection *tmp; - const char *uuid; - gboolean update_unsaved = TRUE; - - g_assert (source || full_path); - - /* If we're given a connection already, prefer that instead of re-reading */ - if (source) - tmp = g_object_ref (source); - else { - tmp = nms_keyfile_reader_from_file (full_path, error); - if (!tmp) - return NULL; - - uuid = nm_connection_get_uuid (NM_CONNECTION (tmp)); - if (!uuid) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Connection in file %s had no UUID", full_path); - g_object_unref (tmp); - return NULL; - } - - /* If we just read the connection from disk, it's clearly not Unsaved */ - update_unsaved = FALSE; - } - - object = (GObject *) g_object_new (NMS_TYPE_KEYFILE_CONNECTION, - NM_SETTINGS_CONNECTION_FILENAME, full_path, - NULL); - - /* Update our settings with what was read from the file */ - if (!nm_settings_connection_replace_settings (NM_SETTINGS_CONNECTION (object), - tmp, - update_unsaved, - NULL, - error)) { - g_object_unref (object); - object = NULL; - } - - g_object_unref (tmp); - return (NMSKeyfileConnection *) object; -} +/*****************************************************************************/ static void commit_changes (NMSettingsConnection *connection, @@ -151,19 +113,68 @@ do_delete (NMSettingsConnection *connection, user_data); } -/* GObject */ +/*****************************************************************************/ static void nms_keyfile_connection_init (NMSKeyfileConnection *connection) { } +NMSKeyfileConnection * +nms_keyfile_connection_new (NMConnection *source, + const char *full_path, + GError **error) +{ + GObject *object; + NMConnection *tmp; + const char *uuid; + gboolean update_unsaved = TRUE; + + g_assert (source || full_path); + + /* If we're given a connection already, prefer that instead of re-reading */ + if (source) + tmp = g_object_ref (source); + else { + tmp = nms_keyfile_reader_from_file (full_path, error); + if (!tmp) + return NULL; + + uuid = nm_connection_get_uuid (NM_CONNECTION (tmp)); + if (!uuid) { + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "Connection in file %s had no UUID", full_path); + g_object_unref (tmp); + return NULL; + } + + /* If we just read the connection from disk, it's clearly not Unsaved */ + update_unsaved = FALSE; + } + + object = (GObject *) g_object_new (NMS_TYPE_KEYFILE_CONNECTION, + NM_SETTINGS_CONNECTION_FILENAME, full_path, + NULL); + + /* Update our settings with what was read from the file */ + if (!nm_settings_connection_replace_settings (NM_SETTINGS_CONNECTION (object), + tmp, + update_unsaved, + NULL, + error)) { + g_object_unref (object); + object = NULL; + } + + g_object_unref (tmp); + return (NMSKeyfileConnection *) object; +} + static void nms_keyfile_connection_class_init (NMSKeyfileConnectionClass *keyfile_connection_class) { NMSettingsConnectionClass *settings_class = NM_SETTINGS_CONNECTION_CLASS (keyfile_connection_class); - /* Virtual methods */ settings_class->commit_changes = commit_changes; settings_class->delete = do_delete; } diff --git a/src/settings/plugins/keyfile/nms-keyfile-connection.h b/src/settings/plugins/keyfile/nms-keyfile-connection.h index 95f503dc1..3520f4212 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-connection.h +++ b/src/settings/plugins/keyfile/nms-keyfile-connection.h @@ -31,13 +31,8 @@ #define NMS_IS_KEYFILE_CONNECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NMS_TYPE_KEYFILE_CONNECTION)) #define NMS_KEYFILE_CONNECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NMS_TYPE_KEYFILE_CONNECTION, NMSKeyfileConnectionClass)) -typedef struct { - NMSettingsConnection parent; -} NMSKeyfileConnection; - -typedef struct { - NMSettingsConnectionClass parent; -} NMSKeyfileConnectionClass; +typedef struct _NMSKeyfileConnection NMSKeyfileConnection; +typedef struct _NMSKeyfileConnectionClass NMSKeyfileConnectionClass; GType nms_keyfile_connection_get_type (void); diff --git a/src/settings/plugins/keyfile/nms-keyfile-plugin.c b/src/settings/plugins/keyfile/nms-keyfile-plugin.c index e3414bb5a..d298b1908 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-plugin.c +++ b/src/settings/plugins/keyfile/nms-keyfile-plugin.c @@ -346,7 +346,7 @@ setup_monitoring (NMSettingsPlugin *config) GFile *file; GFileMonitor *monitor; - if (nm_config_get_monitor_connection_files (nm_config_get ())) { + if (nm_config_get_monitor_connection_files (priv->config)) { file = g_file_new_for_path (nms_keyfile_utils_get_path ()); monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, NULL, NULL); g_object_unref (file); @@ -586,7 +586,7 @@ constructed (GObject *object) nm_log_warn (LOGD_SETTINGS, "keyfile: 'hostname' option is deprecated and has no effect"); } -GObject * +NMSKeyfilePlugin * nms_keyfile_plugin_new (void) { return g_object_new (NMS_TYPE_KEYFILE_PLUGIN, NULL); diff --git a/src/settings/plugins/keyfile/nms-keyfile-plugin.h b/src/settings/plugins/keyfile/nms-keyfile-plugin.h index ba62a88b9..41f47aacd 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-plugin.h +++ b/src/settings/plugins/keyfile/nms-keyfile-plugin.h @@ -34,6 +34,6 @@ typedef struct _NMSKeyfilePluginClass NMSKeyfilePluginClass; GType nms_keyfile_plugin_get_type (void); -GObject *nms_keyfile_plugin_new (void); +NMSKeyfilePlugin *nms_keyfile_plugin_new (void); #endif /* __NMS_KEYFILE_PLUGIN_H__ */ diff --git a/src/settings/plugins/keyfile/nms-keyfile-reader.c b/src/settings/plugins/keyfile/nms-keyfile-reader.c index 84bd3fece..c09e68c03 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-reader.c +++ b/src/settings/plugins/keyfile/nms-keyfile-reader.c @@ -29,6 +29,8 @@ #include "NetworkManagerUtils.h" +/*****************************************************************************/ + static const char * _fmt_warn (const char *group, NMSetting *setting, const char *property_name, const char *message, char **out_message) { diff --git a/src/settings/plugins/keyfile/nms-keyfile-utils.c b/src/settings/plugins/keyfile/nms-keyfile-utils.c index 5b9fd69e6..03f06670f 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-utils.c +++ b/src/settings/plugins/keyfile/nms-keyfile-utils.c @@ -32,6 +32,8 @@ #define NM_CONFIG_KEYFILE_PATH_DEFAULT NMCONFDIR "/system-connections" +/*****************************************************************************/ + static const char temp_letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; @@ -166,5 +168,3 @@ nms_keyfile_utils_get_path (void) return path; } -/*****************************************************************************/ - diff --git a/src/settings/plugins/keyfile/nms-keyfile-writer.c b/src/settings/plugins/keyfile/nms-keyfile-writer.c index a9a8122fe..a36a53b90 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-writer.c +++ b/src/settings/plugins/keyfile/nms-keyfile-writer.c @@ -33,10 +33,13 @@ #include "nms-keyfile-utils.h" +/*****************************************************************************/ + typedef struct { const char *keyfile_dir; } WriteInfo; +/*****************************************************************************/ static gboolean write_cert_key_file (const char *path,