Remove outdated information from the introduction page and the FAQ.
[wine] / programs / regedit / edit.c
1 /*
2  * Registry editing UI functions.
3  *
4  * Copyright (C) 2003 Dimitrie O. Paun
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define WIN32_LEAN_AND_MEAN     /* Exclude rarely-used stuff from Windows headers */
22
23 #include <windows.h>
24 #include <tchar.h>
25 #include <commctrl.h>
26 #include <commdlg.h>
27 #include <cderr.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <shellapi.h>
31 #include <shlwapi.h>
32
33 #include "main.h"
34 #include "regproc.h"
35 #include "resource.h"
36
37 static const TCHAR* editValueName;
38 static TCHAR* stringValueData;
39 static BOOL isDecimal;
40
41 INT vmessagebox(HWND hwnd, INT buttons, INT titleId, INT resId, va_list ap)
42 {
43     TCHAR title[256];
44     TCHAR errfmt[1024];
45     TCHAR errstr[1024];
46
47     if (!LoadString(hInst, titleId, title, COUNT_OF(title)))
48         lstrcpy(title, "Error");
49
50     if (!LoadString(hInst, resId, errfmt, COUNT_OF(errfmt)))
51         lstrcpy(errfmt, "Unknown error string!");
52
53     _vsntprintf(errstr, COUNT_OF(errstr), errfmt, ap);
54
55     return MessageBox(hwnd, errstr, title, buttons);
56 }
57
58 INT messagebox(HWND hwnd, INT buttons, INT titleId, INT resId, ...)
59 {
60     va_list ap;
61     INT result;
62
63     va_start(ap, resId);
64     result = vmessagebox(hwnd, buttons, titleId, resId, ap);
65     va_end(ap);
66
67     return result;
68 }
69
70 void error(HWND hwnd, INT resId, ...)
71 {
72     va_list ap;
73
74     va_start(ap, resId);
75     vmessagebox(hwnd, MB_OK | MB_ICONERROR, IDS_ERROR, resId, ap);
76     va_end(ap);
77 }
78
79 BOOL change_dword_base(HWND hwndDlg, BOOL toHex)
80 {
81     TCHAR buf[128];
82     DWORD val;
83
84     if (!GetDlgItemText(hwndDlg, IDC_VALUE_DATA, buf, COUNT_OF(buf))) return FALSE;
85     if (!_stscanf(buf, toHex ? "%ld" : "%lx", &val)) return FALSE;
86     wsprintf(buf, toHex ? "%lx" : "%ld", val);
87     return SetDlgItemText(hwndDlg, IDC_VALUE_DATA, buf);    
88 }
89
90 INT_PTR CALLBACK modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
91 {
92     TCHAR* valueData;
93     HWND hwndValue;
94     int len;
95
96     switch(uMsg) {
97     case WM_INITDIALOG:
98         SetDlgItemText(hwndDlg, IDC_VALUE_NAME, editValueName);
99         SetDlgItemText(hwndDlg, IDC_VALUE_DATA, stringValueData);
100         CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, isDecimal ? IDC_DWORD_DEC : IDC_DWORD_HEX);
101         return TRUE;
102     case WM_COMMAND:
103         switch (LOWORD(wParam)) {
104         case IDC_DWORD_HEX:
105             if (isDecimal && change_dword_base(hwndDlg, TRUE)) isDecimal = FALSE;
106         break;
107         case IDC_DWORD_DEC:
108             if (!isDecimal && change_dword_base(hwndDlg, FALSE)) isDecimal = TRUE;
109         break;
110         case IDOK:
111             if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
112                 len = GetWindowTextLength(hwndValue);
113                 if ((valueData = HeapReAlloc(GetProcessHeap(), 0, stringValueData, (len + 1) * sizeof(TCHAR)))) {
114                     stringValueData = valueData;
115                     if (!GetWindowText(hwndValue, stringValueData, len + 1))
116                         *stringValueData = 0;
117                 }
118             }
119             /* Fall through */
120         case IDCANCEL:
121             EndDialog(hwndDlg, wParam);
122             return TRUE;
123         }
124     }
125     return FALSE;
126 }
127
128 static BOOL check_value(HWND hwnd, HKEY hKey, LPCTSTR valueName)
129 {
130     LONG lRet = RegQueryValueEx(hKey, valueName ? valueName : _T(""), 0, NULL, 0, NULL);
131     if(lRet != ERROR_SUCCESS) return FALSE;
132     return TRUE;
133 }
134
135 static LPTSTR read_value(HWND hwnd, HKEY hKey, LPCTSTR valueName, DWORD *lpType, LONG *len)
136 {
137     DWORD valueDataLen;
138     LPTSTR buffer = NULL;
139     LONG lRet;
140
141     lRet = RegQueryValueEx(hKey, valueName ? valueName : _T(""), 0, lpType, 0, &valueDataLen);
142     if (lRet != ERROR_SUCCESS) {
143         if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
144             if (len) *len = 1;
145             if (lpType) *lpType = REG_SZ;
146             buffer = HeapAlloc(GetProcessHeap(), 0, 1);
147             *buffer = '\0';
148             return buffer;
149         }
150         error(hwnd, IDS_BAD_VALUE, valueName);
151         goto done;
152     }
153     if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
154     if (!(buffer = HeapAlloc(GetProcessHeap(), 0, valueDataLen))) {
155         error(hwnd, IDS_TOO_BIG_VALUE, valueDataLen);
156         goto done;
157     }
158     lRet = RegQueryValueEx(hKey, valueName, 0, 0, buffer, &valueDataLen);
159     if (lRet != ERROR_SUCCESS) {
160         error(hwnd, IDS_BAD_VALUE, valueName);
161         goto done;
162     }
163
164     if(len) *len = valueDataLen;
165     return buffer;
166
167 done:
168     HeapFree(GetProcessHeap(), 0, buffer);
169     return NULL;
170 }
171
172 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPTSTR keyName)
173 {
174     BOOL result = FALSE;
175     LONG lRet = ERROR_SUCCESS;
176     HKEY retKey;
177     TCHAR newKey[MAX_NEW_KEY_LEN - 4];
178     int keyNum;
179     HKEY hKey;
180          
181     lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
182     if (lRet != ERROR_SUCCESS) return FALSE;
183
184     if (!LoadString(GetModuleHandle(0), IDS_NEWKEY, newKey, COUNT_OF(newKey))) goto done;
185
186     /* try to find out a name for the newly create key (max 100 times) */
187     for (keyNum = 1; keyNum < 100; keyNum++) {
188         wsprintf(keyName, newKey, keyNum);
189         lRet = RegOpenKey(hKey, keyName, &retKey);
190         if (lRet != ERROR_SUCCESS) break;
191         RegCloseKey(retKey);
192     }
193     if (lRet == ERROR_SUCCESS) goto done;
194     
195     lRet = RegCreateKey(hKey, keyName, &retKey);
196     if (lRet != ERROR_SUCCESS) goto done;
197     result = TRUE;
198
199 done:
200     RegCloseKey(retKey);
201     return result;
202 }
203
204 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR valueName)
205 {
206     BOOL result = FALSE;
207     DWORD type;
208     LONG lRet;
209     HKEY hKey;
210
211     lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
212     if (lRet != ERROR_SUCCESS) return FALSE;
213
214     editValueName = valueName ? valueName : g_pszDefaultValueName;
215     if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, 0))) goto done;
216
217     if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
218         if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
219             lRet = RegSetValueEx(hKey, valueName, 0, type, stringValueData, lstrlen(stringValueData) + 1);
220             if (lRet == ERROR_SUCCESS) result = TRUE;
221         }
222     } else if ( type == REG_DWORD ) {
223         wsprintf(stringValueData, isDecimal ? "%ld" : "%lx", *((DWORD*)stringValueData));
224         if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
225              DWORD val;
226              if (_stscanf(stringValueData, isDecimal ? "%ld" : "%lx", &val)) {
227                 lRet = RegSetValueEx(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
228                 if (lRet == ERROR_SUCCESS) result = TRUE;
229              }
230         }
231     } else {
232         error(hwnd, IDS_UNSUPPORTED_TYPE, type);
233     }
234
235 done:
236     HeapFree(GetProcessHeap(), 0, stringValueData);
237     stringValueData = NULL;
238     RegCloseKey(hKey);
239     return result;
240 }
241
242 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath)
243 {
244     BOOL result = FALSE;
245     LONG lRet;
246     HKEY hKey;
247     
248     lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
249     if (lRet != ERROR_SUCCESS) return FALSE;
250     
251     if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, keyPath) != IDYES)
252         goto done;
253         
254     lRet = SHDeleteKey(hKeyRoot, keyPath);
255     if (lRet != ERROR_SUCCESS) {
256         error(hwnd, IDS_BAD_KEY, keyPath);
257         goto done;
258     }
259     result = TRUE;
260     
261 done:
262     RegCloseKey(hKey);
263     return result;
264 }
265
266 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR valueName)
267 {
268     BOOL result = FALSE;
269     LONG lRet;
270     HKEY hKey;
271     LPCSTR visibleValueName = valueName ? valueName : g_pszDefaultValueName;
272
273     lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
274     if (lRet != ERROR_SUCCESS) return FALSE;
275
276     if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, visibleValueName) != IDYES)
277         goto done;
278
279     lRet = RegDeleteValue(hKey, valueName ? valueName : "");
280     if (lRet != ERROR_SUCCESS && valueName) {
281         error(hwnd, IDS_BAD_VALUE, valueName);
282     }
283     if (lRet != ERROR_SUCCESS) goto done;
284     result = TRUE;
285
286 done:
287     RegCloseKey(hKey);
288     return result;
289 }
290
291 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, DWORD valueType, LPTSTR valueName)
292 {
293     LONG lRet = ERROR_SUCCESS;
294     TCHAR newValue[256];
295     DWORD valueDword = 0;
296     BOOL result = FALSE;
297     int valueNum;
298     HKEY hKey;
299          
300     lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
301     if (lRet != ERROR_SUCCESS) return FALSE;
302
303     if (!LoadString(GetModuleHandle(0), IDS_NEWVALUE, newValue, COUNT_OF(newValue))) goto done;
304
305     /* try to find out a name for the newly create key (max 100 times) */
306     for (valueNum = 1; valueNum < 100; valueNum++) {
307         wsprintf(valueName, newValue, valueNum);
308         lRet = RegQueryValueEx(hKey, valueName, 0, 0, 0, 0);
309         if (lRet != ERROR_SUCCESS) break;
310     }
311     if (lRet == ERROR_SUCCESS) goto done;
312    
313     lRet = RegSetValueEx(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
314     if (lRet != ERROR_SUCCESS) goto done;
315     result = TRUE;
316
317 done:
318     RegCloseKey(hKey);
319     return result;
320 }
321
322 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR oldName, LPCTSTR newName)
323 {
324     LPTSTR value = NULL;
325     DWORD type;
326     LONG len, lRet;
327     BOOL result = FALSE;
328     HKEY hKey;
329
330     if (!oldName) return FALSE;
331     if (!newName) return FALSE;
332
333     lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
334     if (lRet != ERROR_SUCCESS) return FALSE;
335     /* check if value already exists */
336     if (check_value(hwnd, hKey, newName)) goto done;
337     value = read_value(hwnd, hKey, oldName, &type, &len);
338     if(!value) goto done;
339     lRet = RegSetValueEx(hKey, newName, 0, type, (BYTE*)value, len);
340     if (lRet != ERROR_SUCCESS) goto done;
341     lRet = RegDeleteValue(hKey, oldName);
342     if (lRet != ERROR_SUCCESS) {
343         RegDeleteValue(hKey, newName);
344         goto done;
345     }
346     result = TRUE;
347
348 done:
349     HeapFree(GetProcessHeap(), 0, value);
350     RegCloseKey(hKey);
351     return result;
352 }
353
354
355 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCTSTR keyPath, LPCTSTR newName)
356 {
357     LPTSTR parentPath = 0;
358     LPCTSTR srcSubKey = 0;
359     HKEY parentKey = 0;
360     HKEY destKey = 0;
361     BOOL result = FALSE;
362     LONG lRet;
363
364     if (!keyPath || !newName) return FALSE;
365
366     if (!strrchr(keyPath, '\\')) {
367         parentKey = hRootKey;
368         srcSubKey = keyPath;
369     } else {
370         parentPath = strdup(keyPath);
371         srcSubKey = strrchr(parentPath, '\\') + 1;
372         *((LPTSTR)srcSubKey - 1) = 0;
373         lRet = RegOpenKeyEx(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
374         if (lRet != ERROR_SUCCESS) goto done;
375     }
376
377     /* The following fails if the old name is the same as the new name. */
378     if (!strcmp(srcSubKey, newName)) goto done;
379
380     lRet = RegCreateKey(parentKey, newName, &destKey);
381     if (lRet != ERROR_SUCCESS) goto done;
382
383     /* FIXME: SHCopyKey does not copy the security attributes */
384     lRet = SHCopyKey(parentKey, srcSubKey, destKey, 0);
385     if (lRet != ERROR_SUCCESS) goto done;
386
387     lRet = SHDeleteKey(hRootKey, keyPath);
388     if (lRet != ERROR_SUCCESS) goto done;
389
390     result = TRUE;
391
392 done:
393     RegCloseKey(destKey);
394     if (parentKey) {
395         RegCloseKey(parentKey); 
396         free(parentPath);
397     }
398     return result;
399 }