- Bugfixes to the transaction system.
[wine] / programs / winecfg / winecfg.c
1 /*
2  * WineCfg configuration management
3  *
4  * Copyright 2002 Jaco Greeff
5  * Copyright 2003 Dimitrie O. Paun
6  * Copyright 2003 Mike Hearn
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <limits.h>
27 #include <windows.h>
28 #include <winreg.h>
29 #include <wine/debug.h>
30
31 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
32
33 #include "winecfg.h"
34
35 HKEY configKey = NULL;
36
37
38 int initialize(void) {
39     DWORD res = RegCreateKey(HKEY_LOCAL_MACHINE, WINE_KEY_ROOT, &configKey);
40     if (res != ERROR_SUCCESS) {
41         WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
42         return 1;
43     }
44     return 0;
45 }
46
47
48 /*****************************************************************************
49  * getConfigValue: Retrieves a configuration value from the registry
50  *
51  * char *subKey : the name of the config section
52  * char *valueName : the name of the config value
53  * char *defaultResult : if the key isn't found, return this value instead
54  *
55  * Returns a buffer holding the value if successful, NULL if not. Caller is responsible for freeing the result.
56  *
57  */
58 char *getConfigValue (char *subkey, char *valueName, char *defaultResult)
59 {
60     char *buffer = NULL;
61     DWORD dataLength;
62     HKEY hSubKey = NULL;
63     DWORD res;
64
65     WINE_TRACE("subkey=%s, valueName=%s, defaultResult=%s\n", subkey, valueName, defaultResult);
66
67     res = RegOpenKeyEx( configKey, subkey, 0, KEY_ALL_ACCESS, &hSubKey );
68     if(res != ERROR_SUCCESS)  {
69         if( res==ERROR_FILE_NOT_FOUND )
70         {
71             WINE_TRACE("Section key not present - using default\n");
72             return strdup(defaultResult);
73         }
74         else
75         {
76             WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
77         }
78         goto end;
79     }
80     
81     res = RegQueryValueExA( hSubKey, valueName, NULL, NULL, NULL, &dataLength);
82     if( res == ERROR_FILE_NOT_FOUND ) {
83         WINE_TRACE("Value not present - using default\n");
84         buffer = strdup(defaultResult);
85         goto end;
86     } else if( res!=ERROR_SUCCESS )  {
87         WINE_ERR("Couldn't query value's length (res=%ld)\n", res );
88         goto end;
89     }
90
91     buffer = malloc(dataLength);
92     if( buffer==NULL )
93     {
94         WINE_ERR("Couldn't allocate %lu bytes for the value\n", dataLength );
95         goto end;
96     }
97     
98     RegQueryValueEx(hSubKey, valueName, NULL, NULL, (LPBYTE)buffer, &dataLength);
99     
100 end:
101     if( hSubKey!=NULL )
102         RegCloseKey( hSubKey );
103
104     return buffer;
105     
106 }
107
108 /*****************************************************************************
109  * setConfigValue : Sets a configuration key in the registry. Section will be created if it doesn't already exist
110  *
111  * HKEY  hCurrent : the registry key that the configuration is rooted at
112  * char *subKey : the name of the config section
113  * char *valueName : the name of the config value
114  * char *value : the value to set the configuration key to
115  *
116  * Returns 0 on success, non-zero otherwise
117  *
118  */
119 int setConfigValue (char *subkey, char *valueName, const char *value) {
120     DWORD res = 1;
121     HKEY key = NULL;
122
123     WINE_TRACE("subkey=%s, valueName=%s, value=%s\n", subkey, valueName, value);
124
125     assert( subkey != NULL );
126     assert( valueName != NULL );
127     assert( value != NULL );
128     
129     res = RegCreateKey(configKey, subkey, &key);
130     if (res != ERROR_SUCCESS) goto end;
131
132     res = RegSetValueEx(key, valueName, 0, REG_SZ, value, strlen(value) + 1);
133     if (res != ERROR_SUCCESS) goto end;
134
135     res = 0;
136 end:
137     if (key) RegCloseKey(key);
138     if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s to %s, res=%ld\n", valueName, subkey, value, res);
139     return res;
140 }
141
142 /* returns 0 on success, an HRESULT from the registry funtions otherwise */
143 HRESULT doesConfigValueExist(char *subkey, char *valueName) {
144     HRESULT hr;
145     HKEY key;
146
147     WINE_TRACE("subkey=%s, valueName=%s - ", subkey, valueName);
148     
149     hr = RegOpenKeyEx(configKey, subkey, 0, KEY_READ, &key);
150     if (hr != S_OK) {
151         WINE_TRACE("no: subkey does not exist\n");
152         return hr;
153     }
154
155     hr = RegQueryValueEx(key, valueName, NULL, NULL, NULL, NULL);
156     if (hr != S_OK) {
157         WINE_TRACE("no: key does not exist\n");
158         return hr;
159     }
160
161     RegCloseKey(key);
162     WINE_TRACE("yes\n");
163     return S_OK;
164 }
165
166 /* removes the requested value from the registry, however, does not remove the section if empty. Returns S_OK (0) on success. */
167 HRESULT removeConfigValue(char *subkey, char *valueName) {
168     HRESULT hr;
169     HKEY key;
170     WINE_TRACE("subkey=%s, valueName=%s\n", subkey, valueName);
171     
172     hr = RegOpenKeyEx(configKey, subkey, 0, KEY_READ, &key);
173     if (hr != S_OK) return hr;
174
175     hr = RegDeleteValue(key, valueName);
176     if (hr != ERROR_SUCCESS) return hr;
177
178     return S_OK;
179 }
180
181
182 /* ========================================================================= */
183 /* Transaction management code */
184
185 struct transaction *tqhead, *tqtail;
186 int instantApply = 1;
187
188 void destroyTransaction(struct transaction *trans) {
189     assert( trans != NULL );
190     
191     WINE_TRACE("destroying %p\n", trans);
192     
193     free(trans->section);
194     free(trans->key);
195     if (trans->newValue) free(trans->newValue);
196     
197     if (trans->next) trans->next->prev = trans->prev;
198     if (trans->prev) trans->prev->next = trans->next;
199     if (trans == tqhead) tqhead = NULL;
200     if (trans == tqtail) tqtail = NULL;
201     
202     free(trans);
203 }
204
205 void addTransaction(char *section, char *key, enum transaction_action action, char *newValue) {
206     struct transaction *trans = malloc(sizeof(struct transaction));
207     
208     assert( section != NULL );
209     assert( key != NULL );
210     if (action == ACTION_SET) assert( newValue != NULL );
211
212     trans->section = strdup(section);
213     trans->key = strdup(key);
214     trans->newValue = strdup(newValue);
215     trans->action = action;
216     trans->next = NULL;
217     trans->prev = NULL;
218     
219     if (tqtail == NULL) {
220         tqtail = trans;
221         tqhead = tqtail;
222     } else {
223         tqhead->next = trans;
224         trans->prev = tqhead;
225         tqhead = trans;
226     }
227
228     if (instantApply) {
229         processTransaction(trans);
230         destroyTransaction(trans);
231     }
232 }
233
234 void processTransaction(struct transaction *trans) {
235     if (trans->action == ACTION_SET) {
236         WINE_TRACE("Setting %s\\%s to '%s'\n", trans->section, trans->key, trans->newValue);
237         setConfigValue(trans->section, trans->key, trans->newValue);
238     } else if (trans->action == ACTION_REMOVE) {
239         WINE_TRACE("Removing %s\\%s", trans->section, trans->key);
240         removeConfigValue(trans->section, trans->key);
241     }
242     /* TODO: implement notifications here */
243 }
244
245 void processTransQueue(void)
246 {
247     WINE_TRACE("\n");
248     while (tqtail != NULL) {
249         struct transaction *next = tqtail->next;
250         processTransaction(tqtail);
251         destroyTransaction(tqtail);
252         tqtail = next;  
253     }
254 }
255
256
257 /* ================================== utility functions ============================ */
258
259 /* returns a string with the window text of the dialog item. user is responsible for freeing the result */
260 char *getDialogItemText(HWND hDlg, WORD controlID) {
261     HWND item = GetDlgItem(hDlg, controlID);
262     int len = GetWindowTextLength(item) + 1;
263     char *result = malloc(len);
264     if (GetWindowText(item, result, len) == 0) return NULL;
265     return result;
266 }