Merge remote-tracking branch 'origin/master' into YaSp
[scilab.git] / scilab / modules / localization / src / c / LanguagePreferences_Windows.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008-2010 - DIGITEO - Allan CORNET
4 *
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution.  The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 *
11 */
12
13 /*--------------------------------------------------------------------------*/
14 #include <stdio.h>
15 #include <Windows.h>
16 #include <wchar.h>
17 #include "LanguagePreferences_Windows.h"
18 #include "os_strdup.h"
19 #include "os_wcsdup.h"
20 #include "charEncoding.h"
21 #include "setgetlanguage.h"
22 #include "version.h"
23 #include "MALLOC.h"
24 #include "GetWindowsVersion.h"
25 /*--------------------------------------------------------------------------*/
26 #define HKCU_LANGUAGE_FORMAT L"SOFTWARE\\Scilab\\%s\\Settings" /* r/w registry */
27 #define HKCM_LANGUAGE_FORMAT L"SOFTWARE\\Scilab\\%s" /* only read registry */
28 #define LANGUAGE_ENTRY L"LANGUAGE"
29 #define DEFAULT_LANGUAGE_VALUE L"en_US"
30 /*--------------------------------------------------------------------------*/
31 static wchar_t *languageFromCommandLine = NULL;
32 /*--------------------------------------------------------------------------*/
33 static wchar_t *getLanguagePreferencesCurrentUser(void);
34 static wchar_t *getLanguagePreferencesAllUsers(void);
35 static wchar_t *readRegistryLanguage(HKEY hKeyRoot,wchar_t *keyString);
36 /*--------------------------------------------------------------------------*/
37 BOOL isValidLanguage(wchar_t *lang)
38 {
39     if (lang)
40     {
41         if ( wcscmp(lang, L"") == 0 || wcscmp(lang, L"C") == 0 )
42         {
43             return TRUE;
44         }
45
46         /* xx_XX */
47         if ( ((int) wcslen(lang) == 5) && (lang[2] == L'_') )
48         {
49             return TRUE;
50         }
51         else if ((wcslen(lang) == 2) && (convertlanguagealias(lang)))
52         {
53             return TRUE;
54         }
55     }
56     return FALSE;
57 }
58 /*--------------------------------------------------------------------------*/
59 BOOL setLanguageFromCommandLine(wchar_t *lang)
60 {
61     if (lang)
62     {
63         if (languageFromCommandLine)
64         {
65             FREE(languageFromCommandLine);
66             languageFromCommandLine = NULL;
67         }
68
69         languageFromCommandLine = os_wcsdup(convertlanguagealias(lang));
70
71         return TRUE;
72     }
73     return FALSE;
74 }
75 /*--------------------------------------------------------------------------*/
76 wchar_t *getLanguagePreferences(void)
77 {
78     wchar_t *LanguageUser = NULL;
79
80     if (languageFromCommandLine)
81     {
82         LanguageUser = languageFromCommandLine;
83     }
84     else
85     {
86         LanguageUser = getLanguagePreferencesCurrentUser();
87     }
88
89     if (LanguageUser == NULL)
90     {
91         wchar_t *LanguageAllUsers = getLanguagePreferencesAllUsers();
92
93         if (LanguageAllUsers == NULL)
94         {
95             return os_wcsdup(L"");
96         }
97         else
98         {
99             if (isValidLanguage(LanguageAllUsers)) return LanguageAllUsers;
100         }
101     }
102     else
103     {
104         if (isValidLanguage(LanguageUser)) return LanguageUser;
105     }
106     return os_wcsdup(L"");
107 }
108 /*--------------------------------------------------------------------------*/
109 static wchar_t *readRegistryLanguage(HKEY hKeyRoot,wchar_t *keyStringFormat)
110 {
111 #define LENGTH_LANGUAGE_REGISTRY 64
112     wchar_t LANGUAGE_REGISTRY[LENGTH_LANGUAGE_REGISTRY] = DEFAULT_LANGUAGE_VALUE;
113     wchar_t *keyString = NULL;
114     int lenkeyString = (int)(wcslen(keyStringFormat)+wcslen(SCI_VERSION_WIDE_STRING)) + 1;
115
116     keyString = (wchar_t*) MALLOC(sizeof(wchar_t) * lenkeyString);
117
118     if (keyString)
119     {
120
121         DWORD OpensKeyOptions = 0;
122         HKEY hKey;
123         int length = LENGTH_LANGUAGE_REGISTRY;
124         wsprintfW(keyString, keyStringFormat, SCI_VERSION_WIDE_STRING);
125 #ifdef _WIN64 /* Scilab x64 on x64 windows */
126         OpensKeyOptions = KEY_READ | KEY_WOW64_64KEY;
127 #else
128         if (IsWow64()) /* Scilab 32 bits on x64 windows */
129         {
130             OpensKeyOptions = KEY_READ | KEY_WOW64_32KEY;
131         }
132         else /* Scilab 32 bits on windows 32 bits */
133         {
134             OpensKeyOptions = KEY_READ;
135         }
136 #endif
137         if ( RegOpenKeyExW(hKeyRoot, keyString, 0, OpensKeyOptions, &hKey) != ERROR_SUCCESS )
138         {
139             RegCloseKey(hKey);
140             if (keyString) { FREE(keyString); keyString = NULL;}
141             return NULL;
142         }
143
144         if ( RegQueryValueExW(hKey, LANGUAGE_ENTRY, 0, NULL ,(LPBYTE)LANGUAGE_REGISTRY,&length)  !=  ERROR_SUCCESS )
145         {
146             RegCloseKey(hKey);
147             if (keyString) { FREE(keyString); keyString = NULL;}
148             return NULL;
149         }
150
151         RegCloseKey(hKey);
152         if (keyString) { FREE(keyString); keyString = NULL;}
153     }
154     return os_wcsdup(LANGUAGE_REGISTRY);
155 }
156 /*--------------------------------------------------------------------------*/
157 static wchar_t *getLanguagePreferencesCurrentUser(void)
158 {
159     return readRegistryLanguage(HKEY_CURRENT_USER, HKCU_LANGUAGE_FORMAT);
160 }
161 /*--------------------------------------------------------------------------*/
162 static wchar_t *getLanguagePreferencesAllUsers(void)
163 {
164     return readRegistryLanguage(HKEY_LOCAL_MACHINE, HKCM_LANGUAGE_FORMAT);
165 }
166 /*--------------------------------------------------------------------------*/
167 BOOL setLanguagePreferences(void)
168 {
169     char *LANGUAGE = wide_string_to_UTF8(getlanguage());
170
171     if (LANGUAGE)
172     {
173         wchar_t *keyString = NULL;
174         int lenkeyString = (int)(wcslen(HKCU_LANGUAGE_FORMAT)+wcslen(SCI_VERSION_WIDE_STRING)) + 1;
175         keyString = (wchar_t*) MALLOC(sizeof(wchar_t) * lenkeyString);
176         if (keyString)
177         {
178             DWORD OpensKeyOptions = 0;
179             HKEY hKey;
180             DWORD result = 0;
181             wsprintfW(keyString, HKCU_LANGUAGE_FORMAT, SCI_VERSION_WIDE_STRING);
182
183 #ifdef _WIN64 /* Scilab x64 on x64 windows */
184             OpensKeyOptions = KEY_ALL_ACCESS | KEY_WOW64_64KEY;
185 #else
186             if ( IsWow64() ) /* Scilab 32 bits on x64 windows */
187             {
188                 OpensKeyOptions = KEY_ALL_ACCESS | KEY_WOW64_32KEY;
189             }
190             else /* Scilab 32 bits on windows 32 bits */
191             {
192                 OpensKeyOptions = KEY_ALL_ACCESS;
193             }
194 #endif
195             if ( RegCreateKeyExW(HKEY_CURRENT_USER, keyString, 0, NULL, REG_OPTION_NON_VOLATILE, OpensKeyOptions, NULL, &hKey, &result) != ERROR_SUCCESS)
196             {
197                 RegCloseKey(hKey);
198                 if (keyString) { FREE(keyString); keyString = NULL;}
199                 return FALSE;
200             }
201
202             if ( RegSetValueExW(hKey, LANGUAGE_ENTRY, 0, REG_SZ, (LPBYTE)LANGUAGE, (DWORD)(strlen(LANGUAGE)+1)) != ERROR_SUCCESS)
203             {
204                 RegCloseKey(hKey);
205                 if (keyString) { FREE(keyString); keyString = NULL;}
206                 return FALSE;
207             }
208
209             RegCloseKey(hKey);
210             if (keyString) { FREE(keyString); keyString = NULL;}
211             return TRUE;
212         }
213     }
214     return FALSE;
215 }
216 /*--------------------------------------------------------------------------*/