l10n: sync translations with transifex.com (es, pt_BR)
[siplcs.git] / src / core / sipe-win32dep.c
blob023248b38b194c2a47b165cae584d2604e6b6481
1 /**
2 * @file sipe-win32dep.c
4 * pidgin-sipe
6 * Copyright (C) 2010 pier11 <pier11@operamail.com> (fix for REG_EXPAND_SZ)
7 * Copyright (C) 2002-2003, Herman Bloggs <hermanator12002@yahoo.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* This file is an attempt to fix purple's wpurple_read_reg_string()
25 * which doesn't read REG_EXPAND_SZ types from registry, only REG_SZ.
26 * The code is identical (v2.6.6) apart from one line.
28 * The nead is desperate to read Lotus Notes' notes.ini file location
29 * stored in REG_EXPAND_SZ type.
32 #include "glib.h"
34 #include "sipe-win32dep.h"
35 #include "sipe-backend.h"
37 static HKEY _reg_open_key(HKEY rootkey, const char *subkey, REGSAM access) {
38 HKEY reg_key = NULL;
39 LONG rv;
41 if(G_WIN32_HAVE_WIDECHAR_API()) {
42 wchar_t *wc_subkey = g_utf8_to_utf16(subkey, -1, NULL,
43 NULL, NULL);
44 rv = RegOpenKeyExW(rootkey, wc_subkey, 0, access, &reg_key);
45 g_free(wc_subkey);
46 } else {
47 char *cp_subkey = g_locale_from_utf8(subkey, -1, NULL,
48 NULL, NULL);
49 rv = RegOpenKeyExA(rootkey, cp_subkey, 0, access, &reg_key);
50 g_free(cp_subkey);
53 if (rv != ERROR_SUCCESS) {
54 char *errmsg = g_win32_error_message(rv);
55 SIPE_DEBUG_ERROR("Could not open reg key '%s' subkey '%s'.\nMessage: (%ld) %s\n",
56 ((rootkey == HKEY_LOCAL_MACHINE) ? "HKLM" :
57 (rootkey == HKEY_CURRENT_USER) ? "HKCU" :
58 (rootkey == HKEY_CLASSES_ROOT) ? "HKCR" : "???"),
59 subkey, rv, errmsg);
60 g_free(errmsg);
63 return reg_key;
66 static gboolean _reg_read(HKEY reg_key, const char *valname, LPDWORD type, LPBYTE data, LPDWORD data_len) {
67 LONG rv;
69 if(G_WIN32_HAVE_WIDECHAR_API()) {
70 wchar_t *wc_valname = NULL;
71 if (valname)
72 wc_valname = g_utf8_to_utf16(valname, -1, NULL, NULL, NULL);
73 rv = RegQueryValueExW(reg_key, wc_valname, 0, type, data, data_len);
74 g_free(wc_valname);
75 } else {
76 char *cp_valname = NULL;
77 if(valname)
78 cp_valname = g_locale_from_utf8(valname, -1, NULL, NULL, NULL);
79 rv = RegQueryValueExA(reg_key, cp_valname, 0, type, data, data_len);
80 g_free(cp_valname);
83 if (rv != ERROR_SUCCESS) {
84 char *errmsg = g_win32_error_message(rv);
85 SIPE_DEBUG_ERROR("Could not read from reg key value '%s'.\nMessage: (%ld) %s\n",
86 valname, rv, errmsg);
87 g_free(errmsg);
90 return (rv == ERROR_SUCCESS);
93 char *wpurple_read_reg_expand_string(HKEY rootkey, const char *subkey, const char *valname) {
95 DWORD type;
96 DWORD nbytes;
97 HKEY reg_key = _reg_open_key(rootkey, subkey, KEY_QUERY_VALUE);
98 char *result = NULL;
100 if(reg_key) {
101 if(_reg_read(reg_key, valname, &type, NULL, &nbytes) && (type == REG_SZ || type == REG_EXPAND_SZ)) {
102 LPBYTE data;
103 if(G_WIN32_HAVE_WIDECHAR_API())
104 data = (LPBYTE) g_new(wchar_t, ((nbytes + 1) / sizeof(wchar_t)) + 1);
105 else
106 data = (LPBYTE) g_malloc(nbytes + 1);
108 if(_reg_read(reg_key, valname, &type, data, &nbytes)) {
109 if(G_WIN32_HAVE_WIDECHAR_API()) {
110 wchar_t *wc_temp = (wchar_t*) data;
111 wc_temp[nbytes / sizeof(wchar_t)] = '\0';
112 result = g_utf16_to_utf8(wc_temp, -1,
113 NULL, NULL, NULL);
114 } else {
115 char *cp_temp = (char*) data;
116 cp_temp[nbytes] = '\0';
117 result = g_locale_to_utf8(cp_temp, -1,
118 NULL, NULL, NULL);
121 g_free(data);
123 RegCloseKey(reg_key);
126 return result;
130 Local Variables:
131 mode: c
132 c-file-style: "bsd"
133 indent-tabs-mode: t
134 tab-width: 8
135 End: