[7d3ae3e] | 1 | /* |
---|
| 2 | * Estonian ID card plugin for web browsers |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2010-2011 Codeborne <info@codeborne.com> |
---|
| 5 | * |
---|
| 6 | * This 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 software 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 19 | * |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <windows.h> |
---|
| 23 | #include <windowsx.h> |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <ctype.h> |
---|
| 27 | #include <string.h> |
---|
| 28 | |
---|
| 29 | #include "esteid_log.h" |
---|
| 30 | #include "l10n.h" |
---|
| 31 | |
---|
| 32 | #define LOCALE_BUFFER_SIZE 16 |
---|
| 33 | |
---|
| 34 | int LoadValueFromRegistry(char* destination, HKEY key, WCHAR* subKey, WCHAR* value) { |
---|
| 35 | WCHAR locale[2 * LOCALE_BUFFER_SIZE] = {L""}; |
---|
| 36 | DWORD localeBufferSize = 2 * LOCALE_BUFFER_SIZE; |
---|
| 37 | int returnValue = 0; |
---|
| 38 | HKEY hKey; |
---|
| 39 | |
---|
| 40 | LOG_LOCATION; |
---|
| 41 | |
---|
| 42 | destination[0] = '\0'; |
---|
| 43 | |
---|
| 44 | RegOpenKeyEx(key, subKey, 0, KEY_QUERY_VALUE, &hKey); |
---|
| 45 | if(RegQueryValueEx( hKey, value, NULL, NULL, (LPBYTE) locale, &localeBufferSize ) == ERROR_SUCCESS) { |
---|
| 46 | returnValue = WideCharToMultiByte(CP_UTF8, 0, locale, -1, destination, LOCALE_BUFFER_SIZE, NULL, NULL); |
---|
| 47 | if(returnValue > 0){ |
---|
| 48 | EstEID_log("value in registry is %s ", destination); |
---|
| 49 | } |
---|
| 50 | else { |
---|
| 51 | EstEID_log("unable to load value from registry"); |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | RegCloseKey(hKey); |
---|
| 55 | return returnValue; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | int LoadUserLocaleFromIDCardConfiguration(char* destination) { |
---|
| 59 | LOG_LOCATION; |
---|
| 60 | return LoadValueFromRegistry(destination, HKEY_CURRENT_USER, L"Software\\Estonian ID Card\\OrganizationDefaults\\Main", L"Language"); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | int LoadUserLocaleBySystemCharset(char* destination) { |
---|
| 64 | char codePageBuffer[LOCALE_BUFFER_SIZE] = {""}; |
---|
| 65 | |
---|
| 66 | LOG_LOCATION; |
---|
| 67 | |
---|
| 68 | LoadValueFromRegistry(codePageBuffer, HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Nls\\CodePage", L"ACP"); |
---|
| 69 | EstEID_log("ACP = %s", codePageBuffer); |
---|
| 70 | if(!strcmp("1252", codePageBuffer)){ |
---|
| 71 | strcpy(destination, "en"); |
---|
| 72 | } |
---|
| 73 | else if(!strcmp("1251", codePageBuffer)){ |
---|
| 74 | strcpy(destination, "ru"); |
---|
| 75 | } |
---|
| 76 | else{ |
---|
| 77 | strcpy(destination, "et"); |
---|
| 78 | } |
---|
| 79 | return 1; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | char *EstEID_getUserLocale() { |
---|
| 83 | char locale[LOCALE_BUFFER_SIZE]; |
---|
| 84 | if(!LoadUserLocaleFromIDCardConfiguration(locale)) { |
---|
| 85 | LoadUserLocaleBySystemCharset(locale); |
---|
| 86 | } |
---|
| 87 | return strdup(locale); |
---|
| 88 | } |
---|