Reading the config directory from the Windows API instead of GLib

Before we used g_get_user_config_dir() but GLib changed the returned
location in newer versions, so use the Windows API directly
to get the old location, at least for now.
Also add utils_get_user_config_dir() wrapper.

Code is based almost completely on a patch from Matthew.
This commit is contained in:
Enrico Tröger 2014-10-05 12:55:33 +02:00
parent f262f5986d
commit 3e089e4c2c
5 changed files with 42 additions and 1 deletions

View File

@ -608,7 +608,7 @@ static void parse_command_line_options(gint *argc, gchar ***argv)
}
else
{
app->configdir = g_build_filename(g_get_user_config_dir(), "geany", NULL);
app->configdir = utils_get_user_config_dir();
}
if (generate_tags)

View File

@ -2078,3 +2078,13 @@ gchar *utils_parse_and_format_build_date(const gchar *input)
return g_strdup(input);
}
gchar *utils_get_user_config_dir(void)
{
#ifdef G_OS_WIN32
return win32_get_user_config_dir();
#else
return g_build_filename(g_get_user_data_dir(), "geany", NULL);
#endif
}

View File

@ -289,6 +289,8 @@ GDate *utils_parse_date(const gchar *input);
gchar *utils_parse_and_format_build_date(const gchar *input);
gchar *utils_get_user_config_dir(void);
G_END_DECLS
#endif /* GEANY_UTILS_H */

View File

@ -1497,4 +1497,31 @@ gchar *win32_get_installation_dir(void)
}
gchar *win32_get_user_config_dir(void)
{
HRESULT hr;
wchar_t path[MAX_PATH];
LPSTR w_title[512];
hr = SHGetFolderPathAndSubDirW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, L"geany", path);
if (SUCCEEDED(hr))
{
// GLib always uses UTF-8 for filename encoding on Windows
int u8_size = WideCharToMultiByte(CP_UTF8, 0, path, -1, NULL, 0, NULL, NULL);
if (u8_size > 0)
{
gchar *u8_path = g_malloc0(u8_size + 1);
if (u8_path != NULL &&
WideCharToMultiByte(CP_UTF8, 0, path, -1, u8_path, u8_size, NULL, NULL))
{
return u8_path;
}
}
}
// glib fallback
g_warning("Failed to retrieve Windows config dir, falling back to default");
return g_build_filename(g_get_user_config_dir(), "geany", NULL);
}
#endif

View File

@ -71,6 +71,8 @@ gchar *win32_get_installation_dir(void);
gchar *win32_expand_environment_variables(const gchar *str);
gchar *win32_get_user_config_dir(void);
G_END_DECLS
#endif /* G_OS_WIN32 */