Fixed redrawing causing corruption of background.
[stockwidget] / lib-stock-settings.c
1 /*
2  * Stock Widget: Resource settings
3  * Jon Parr
4  */
5 #include "lib-stock-settings.h"
6
7 void
8 stock_free_settings(StockPluginSettings *psSettings)
9 {
10         if(psSettings->ppszTickers)
11         {
12                 g_strfreev(psSettings->ppszTickers);
13                 psSettings->ppszTickers = NULL;
14         }
15         
16         psSettings->uiNumTickers = 0;
17         psSettings->uiUpdateTime = 0;
18 }
19
20 void
21 stock_set_default_settings(StockPluginSettings *psSettings)
22 {
23         /* Default to a single ticker ;) */
24         psSettings->uiNumTickers   = 1;
25         psSettings->ppszTickers    = g_malloc0(sizeof(char*)*2);
26         psSettings->ppszTickers[0] = g_strdup(STOCK_PLUGIN_DEFAULT_TICKER);
27
28         /* Default to no automatic update */
29         psSettings->uiUpdateTime = 0;
30 }
31
32 void
33 stock_read_settings(StockPluginSettings *psSettings)
34 {
35         GKeyFile        *psKeyFile;
36         gchar           *pszFileName;
37         gboolean         bExists;
38         gint             nItems = 0;
39
40         psKeyFile       = g_key_file_new();
41         pszFileName = g_strconcat(g_get_home_dir(), STOCK_PLUGIN_SETTINGS_FILE, NULL);
42         bExists         = g_key_file_load_from_file(psKeyFile, pszFileName, G_KEY_FILE_KEEP_COMMENTS, NULL);
43
44         if(bExists)
45         {
46                 /*TODO: Check case if config file exists but no data, simply use defaults */
47                 
48                 if(g_key_file_has_key(psKeyFile,psSettings->iD,STOCK_PLUGIN_TICKERS,NULL))
49                 {
50                         psSettings->ppszTickers =
51                                 g_key_file_get_string_list(psKeyFile,
52                                                                                    psSettings->iD,
53                                                                                    STOCK_PLUGIN_TICKERS,
54                                                                                    &psSettings->uiNumTickers,
55                                                                                    NULL);
56                 }
57                 else
58                 {
59                         bExists = FALSE;
60                 }
61
62                 if(g_key_file_has_key(psKeyFile,psSettings->iD,STOCK_PLUGIN_UPDATE_TIME,NULL))
63                 {
64                         psSettings->uiUpdateTime =
65                                 g_key_file_get_integer(psKeyFile,
66                                                                            psSettings->iD,
67                                                                            STOCK_PLUGIN_UPDATE_TIME,
68                                                                            NULL);
69                         nItems++;
70                 }
71                 else
72                 {
73                         if(psSettings->ppszTickers)
74                         {
75                                 g_strfreev(psSettings->ppszTickers);
76                                 psSettings->ppszTickers = NULL;
77                         }
78                                 
79                         bExists = FALSE;
80                 }
81         }
82
83         /* If it doesn't exists or there's an error reading data, just set defaults */
84         if(!bExists)
85                 stock_set_default_settings(psSettings);
86
87         g_key_file_free(psKeyFile);
88         g_free(pszFileName);
89 }
90
91
92 void
93 stock_save_settings(StockPluginSettings *psSettings)
94 {
95         GKeyFile *psKeyFile;
96         gchar *pszFileName;
97         gchar *pszFileData;
98         gsize nSize;
99
100         /* Create key file */
101         psKeyFile       = g_key_file_new();
102         pszFileName = g_strconcat(g_get_home_dir(), STOCK_PLUGIN_SETTINGS_FILE, NULL);
103         g_key_file_load_from_file(psKeyFile, pszFileName, G_KEY_FILE_KEEP_COMMENTS, NULL);
104
105         /* Set tickers */
106         g_key_file_set_string_list(psKeyFile,
107                                                            psSettings->iD,
108                                                            STOCK_PLUGIN_TICKERS,
109                                                            (const gchar **)psSettings->ppszTickers,
110                                                            psSettings->uiNumTickers);
111
112         /* Set update time */
113         g_key_file_set_integer(psKeyFile,
114                                                    psSettings->iD,
115                                                    STOCK_PLUGIN_UPDATE_TIME,
116                                                    psSettings->uiUpdateTime);
117
118         /* Save to disk */
119         pszFileData = g_key_file_to_data(psKeyFile,&nSize,NULL);
120         g_file_set_contents(pszFileName,pszFileData,nSize,NULL);
121         
122         /* Cleanup */
123         g_key_file_free(psKeyFile);
124         g_free(pszFileName);
125         g_free(pszFileData);
126 }