X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=lib-stock-settings.c;fp=lib-stock-settings.c;h=c2206456a7f402ad0538013c3eaf272b5d583119;hb=debf1fe34b36af95996fb9d5a88eeb5f5ddf59da;hp=0000000000000000000000000000000000000000;hpb=0c169a3f430a651f590a4dddc8114288e542e34f;p=stockwidget diff --git a/lib-stock-settings.c b/lib-stock-settings.c new file mode 100644 index 0000000..c220645 --- /dev/null +++ b/lib-stock-settings.c @@ -0,0 +1,126 @@ +/* + * Stock Widget: Resource settings + * Jon Parr + */ +#include "lib-stock-settings.h" + +void +stock_free_settings(StockPluginSettings *psSettings) +{ + if(psSettings->ppszTickers) + { + g_strfreev(psSettings->ppszTickers); + psSettings->ppszTickers = NULL; + } + + psSettings->uiNumTickers = 0; + psSettings->uiUpdateTime = 0; +} + +void +stock_set_default_settings(StockPluginSettings *psSettings) +{ + /* Default to a single ticker ;) */ + psSettings->uiNumTickers = 1; + psSettings->ppszTickers = g_malloc0(sizeof(char*)*2); + psSettings->ppszTickers[0] = g_strdup(STOCK_PLUGIN_DEFAULT_TICKER); + + /* Default to no automatic update */ + psSettings->uiUpdateTime = 0; +} + +void +stock_read_settings(StockPluginSettings *psSettings) +{ + GKeyFile *psKeyFile; + gchar *pszFileName; + gboolean bExists; + gint nItems = 0; + + psKeyFile = g_key_file_new(); + pszFileName = g_strconcat(g_get_home_dir(), STOCK_PLUGIN_SETTINGS_FILE, NULL); + bExists = g_key_file_load_from_file(psKeyFile, pszFileName, G_KEY_FILE_KEEP_COMMENTS, NULL); + + if(bExists) + { + /*TODO: Check case if config file exists but no data, simply use defaults */ + + if(g_key_file_has_key(psKeyFile,psSettings->iD,STOCK_PLUGIN_TICKERS,NULL)) + { + psSettings->ppszTickers = + g_key_file_get_string_list(psKeyFile, + psSettings->iD, + STOCK_PLUGIN_TICKERS, + &psSettings->uiNumTickers, + NULL); + } + else + { + bExists = FALSE; + } + + if(g_key_file_has_key(psKeyFile,psSettings->iD,STOCK_PLUGIN_UPDATE_TIME,NULL)) + { + psSettings->uiUpdateTime = + g_key_file_get_integer(psKeyFile, + psSettings->iD, + STOCK_PLUGIN_UPDATE_TIME, + NULL); + nItems++; + } + else + { + if(psSettings->ppszTickers) + { + g_strfreev(psSettings->ppszTickers); + psSettings->ppszTickers = NULL; + } + + bExists = FALSE; + } + } + + /* If it doesn't exists or there's an error reading data, just set defaults */ + if(!bExists) + stock_set_default_settings(psSettings); + + g_key_file_free(psKeyFile); + g_free(pszFileName); +} + + +void +stock_save_settings(StockPluginSettings *psSettings) +{ + GKeyFile *psKeyFile; + gchar *pszFileName; + gchar *pszFileData; + gsize nSize; + + /* Create key file */ + psKeyFile = g_key_file_new(); + pszFileName = g_strconcat(g_get_home_dir(), STOCK_PLUGIN_SETTINGS_FILE, NULL); + g_key_file_load_from_file(psKeyFile, pszFileName, G_KEY_FILE_KEEP_COMMENTS, NULL); + + /* Set tickers */ + g_key_file_set_string_list(psKeyFile, + psSettings->iD, + STOCK_PLUGIN_TICKERS, + (const gchar **)psSettings->ppszTickers, + psSettings->uiNumTickers); + + /* Set update time */ + g_key_file_set_integer(psKeyFile, + psSettings->iD, + STOCK_PLUGIN_UPDATE_TIME, + psSettings->uiUpdateTime); + + /* Save to disk */ + pszFileData = g_key_file_to_data(psKeyFile,&nSize,NULL); + g_file_set_contents(pszFileName,pszFileData,nSize,NULL); + + /* Cleanup */ + g_key_file_free(psKeyFile); + g_free(pszFileName); + g_free(pszFileData); +}