Skip to content

Commit

Permalink
[config] Clean-up and add function to check for value changes
Browse files Browse the repository at this point in the history
Add a function to check if a new value is changed or not.

Signed-off-by: Philippe De Swert <philippe.deswert@jollamobile.com>
  • Loading branch information
philippedeswert committed May 27, 2015
1 parent de9faa9 commit 7033527
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
43 changes: 27 additions & 16 deletions src/usb_moded-config.c
Expand Up @@ -330,6 +330,29 @@ char * get_mode_setting(void)

return(get_conf_string(MODE_SETTING_ENTRY, MODE_SETTING_KEY));
}
/*
* @param settingsfile: already opened settingsfile we want to read an entry from
* @param entry: entry we want to read
* @param key: key value of the entry we want to read
* @new_value: potentially new value we want to compare against
*
* @return: 1 when the old value is the same as the new one, 0 otherwise
*/
int config_value_changed(GKeyFile *settingsfile, const char *entry, const char *key, const char *new_value)
{
char *old = g_key_file_get_string(settingsfile, entry, key, NULL);
if (old)
{
gboolean unchanged = (g_strcmp0(old, entry) == 0);
g_free(old);
if (unchanged)
{
return 1;
}
}

return 0;
}

set_config_result_t set_config_setting(const char *entry, const char *key, const char *value)
{
Expand All @@ -342,16 +365,10 @@ set_config_result_t set_config_setting(const char *entry, const char *key, const
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(test)
{
char *old = g_key_file_get_string(settingsfile, entry, key, NULL);
if (old)
if(config_value_changed(settingsfile, entry, key, value))
{
gboolean unchanged = (g_strcmp0(old, value) == 0);
g_free(old);
if (unchanged)
{
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
}
}
}
else
Expand Down Expand Up @@ -400,16 +417,10 @@ set_config_result_t set_network_setting(const char *config, const char *setting)
set_config_result_t ret = SET_CONFIG_ERROR;
if (test)
{
char *old = g_key_file_get_string(settingsfile, NETWORK_ENTRY, config, NULL);
if (old)
if(config_value_changed(settingsfile, NETWORK_ENTRY, config, setting))
{
gboolean unchanged = (g_strcmp0(old, setting) == 0);
g_free(old);
if (unchanged)
{
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
}
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
}
}
else
Expand Down
2 changes: 2 additions & 0 deletions src/usb_moded-config.h
Expand Up @@ -91,6 +91,8 @@ typedef enum set_config_result_t {
} set_config_result_t;

int conf_file_merge(void);

int config_value_changed(GKeyFile *settingsfile, const char *entry, const char *key, const char *new_value);
set_config_result_t set_config_setting(const char *entry, const char *key, const char *value);

#define SET_CONFIG_OK(ret) ((ret) >= SET_CONFIG_UPDATED)
2 changes: 1 addition & 1 deletion src/usb_moded-dbus.h
Expand Up @@ -2,7 +2,7 @@
Copyright (C) 2010 Nokia Corporation. All rights reserved.
Copyright (C) 2012-2015 Jolla. All rights reserved.
Author: Philippe De Swert <philippe.de-swert@meego.com>
Author: Philippe De Swert <philippedeswert@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public License
Expand Down

0 comments on commit 7033527

Please sign in to comment.