add user specified stylesheet option
[claws.git] / src / common / w32_reg.c
blob031b8d49ce85f45e339a6026ddcfd810f1bf7b0c
1 /* w32_reg.c - Posix emulation layer for Sylpheed (Claws)
3 * This file is part of w32lib.
5 * w32lib is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * w32lib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * For more information and a list of changes, see w32lib.h
21 #include <windows.h>
22 #include "utils.h"
23 #include "w32lib.h"
25 static HKEY get_root_key_from_str(char *parent)
27 HKEY rootKey = NULL;
28 if (!parent || !strlen(parent))
29 rootKey = HKEY_CURRENT_USER ;
30 else if (!strcmp(parent, "HKCR") || !strcmp(parent,"HKEY_CLASSES_ROOT"))
31 rootKey = HKEY_CLASSES_ROOT ;
32 else if (!strcmp(parent, "HKCU") || !strcmp(parent,"HKEY_CURRENT_USER"))
33 rootKey = HKEY_CURRENT_USER ;
34 else if (!strcmp(parent, "HKLM") || !strcmp(parent,"HKEY_LOCAL_MACHINE"))
35 rootKey = HKEY_LOCAL_MACHINE ;
36 else if (!strcmp(parent, "HKU") || !strcmp(parent,"HKEY_USERS"))
37 rootKey = HKEY_USERS ;
38 else if (!strcmp(parent, "HKCC") || !strcmp(parent,"HKEY_CURRENT_CONFIG"))
39 rootKey = HKEY_CURRENT_CONFIG ;
40 return rootKey;
44 int write_w32_registry_string( char *parent, char *section, char *value, char *data )
46 HKEY hKey, rootKey;
47 int ret;
49 rootKey = get_root_key_from_str(parent);
50 ret = RegCreateKeyEx(rootKey, section, 0, NULL,
51 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
52 if (ret != ERROR_SUCCESS) {
53 debug_print("can't write key %s\\%s: %d\n", parent, section, ret);
54 return -1;
56 ret = RegSetValueEx(hKey, value, 0, REG_SZ, (LPVOID)data, strlen(data)+1);
57 if (ret != ERROR_SUCCESS) {
58 RegCloseKey(hKey);
59 debug_print("can't write key %s\\%s: %d\n", parent, section, ret);
60 return -1;
62 RegCloseKey(hKey);
63 return 0;
66 int write_w32_registry_dword( char *parent, char *section, char *value, int data )
68 HKEY hKey, rootKey;
69 int ret;
71 rootKey = get_root_key_from_str(parent);
72 ret = RegCreateKeyEx(rootKey, section, 0, NULL,
73 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
74 if (ret != ERROR_SUCCESS) {
75 debug_print("can't write key %s\\%s: %d\n", parent, section, ret);
76 return -1;
78 ret = RegSetValueEx(hKey, value, 0, REG_DWORD, (LPBYTE)&data, sizeof(data));
79 if (ret != ERROR_SUCCESS) {
80 RegCloseKey(hKey);
81 debug_print("can't write key %s\\%s: %d\n", parent, section, ret);
82 return -1;
84 RegCloseKey(hKey);
85 return 0;
88 char *read_w32_registry_string( char *parent, char *section, char *key )
90 HKEY hKey, rootKey;
91 char *str;
92 int ret;
94 char buf[ MAX_PATH ];
95 DWORD bufsiz = sizeof( buf );
97 rootKey = get_root_key_from_str(parent);
99 if (!rootKey)
100 return NULL;
102 str = NULL;
103 ret = RegOpenKeyEx( rootKey, section, 0, KEY_READ, &hKey );
104 if ( ERROR_SUCCESS == ret ){
105 ret = RegQueryValueEx( hKey, key, 0, NULL,
106 (LPBYTE)buf, &bufsiz );
107 if ( ERROR_SUCCESS == ret ){
108 str = strdup( buf );
110 RegCloseKey( hKey );
112 return str;
115 char *get_content_type_from_registry_with_ext( char *ext )
117 HKEY hKey, parent;
118 int ret;
119 char buf[ MAX_PATH ];
120 DWORD bufsiz;
121 char *section, *key, *value;
123 if (ext == NULL)
124 return NULL;
126 // parent : HKEY_CLASSES_ROOT
127 // section : ".txt"
128 parent = HKEY_CLASSES_ROOT;
129 section = malloc ( 1 + strlen (ext) + 1);
130 if (!section)
131 return NULL;
132 *section = '.';
133 strcpy (section+1, ext);
135 value = NULL;
136 while ( 1 ) {
137 ret = RegOpenKeyEx( parent, section, 0, KEY_READ, &hKey );
138 if ( ERROR_SUCCESS != ret ) {
139 // If section is not found...
140 value = NULL;
141 break;
144 // key : "Content Type"
145 key = "Content Type";
146 bufsiz = sizeof( buf );
147 ret = RegQueryValueEx( hKey, key, 0, NULL, (LPBYTE)buf, &bufsiz );
148 if ( ERROR_SUCCESS == ret ) {
149 // If value is found!
150 RegCloseKey( hKey );
151 value = strdup( buf );
152 break;
155 key = "";
156 bufsiz = sizeof( buf );
157 ret = RegQueryValueEx( hKey, key, 0, NULL, (LPBYTE)buf, &bufsiz );
158 if ( ERROR_SUCCESS != ret ) {
159 RegCloseKey( hKey );
160 value = NULL;
161 break;
164 RegCloseKey( hKey );
165 free( section );
166 section = strdup( buf );
167 break; //XXX:tm-gtk2
170 free( section );
171 return value;