// static
bool AIFilePicker::loadFile(std::string const& filename)
{
	LLSD data;
	std::string filepath = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, filename);
	llifstream file(filepath);
	if (file.is_open())
	{
		llinfos << "Loading filepicker context file at \"" << filepath << "\"." << llendl;
		LLSDSerialize::fromXML(data, file);
	}

	if (!data.isMap())
	{
		llinfos << "File missing, ill-formed, or simply undefined; not changing the file (" << filepath << ")." << llendl;
		return false;
	}

	AIAccess<context_map_type> wContextMap(sContextMap);

	for (LLSD::map_const_iterator iter = data.beginMap(); iter != data.endMap(); ++iter)
	{
		wContextMap->insert(context_map_type::value_type(iter->first, iter->second.asString()));
	}

	return true;
}
// static
bool AIFilePicker::saveFile(std::string const& filename)
{
	AIAccess<context_map_type> wContextMap(sContextMap);
	if (wContextMap->empty())
		return false;

	LLSD context;

	for (context_map_type::iterator iter = wContextMap->begin(); iter != wContextMap->end(); ++iter)
	{
		context[iter->first] = iter->second;
	}

	std::string filepath = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, filename);
	llofstream file;
	file.open(filepath.c_str());
	if (!file.is_open()) 
	{
		llwarns << "Unable to open filepicker context file for save: \"" << filepath << "\"." << llendl;
		return false;
	}

	LLSDSerialize::toPrettyXML(context, file);

	file.close();
	llinfos << "Saved default paths to \"" << filepath << "\"." << llendl;

	return true;
}
// static
std::string AIFilePicker::get_folder(std::string const& default_path, std::string const& context)
{
	AIAccess<context_map_type> wContextMap(sContextMap);
	context_map_type::iterator iter = wContextMap->find(context);
	if (iter != wContextMap->end() && !iter->second.empty())
	{
		return iter->second;
	}
	else if (!default_path.empty())
	{
		LL_DEBUGS("Plugin") << "context \"" << context << "\" not found. Returning default_path \"" << default_path << "\"." << LL_ENDL;
		return default_path;
	}
	else if ((iter = wContextMap->find("savefile")) != wContextMap->end() && !iter->second.empty())
	{
		LL_DEBUGS("Plugin") << "context \"" << context << "\" not found and default_path empty. Returning context \"savefile\": \"" << iter->second << "\"." << LL_ENDL;
		return iter->second;
	}
	else
	{
		// This is the last resort when all else failed. Open the file chooser in directory 'home'.
		std::string home;
#if LL_WINDOWS
		WCHAR w_home[MAX_PATH];
		HRESULT hr = ::SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, w_home);
		if(hr == S_OK) {
			home = utf16str_to_utf8str(w_home);
		}
#else
		char const* envname = "HOME";
		char const* t_home = getenv(envname);
		if (t_home)
		{
			home.assign(t_home);
		}
#endif
		if(home.empty())
		{
#if LL_WINDOWS
			// On windows, the filepicker window won't pop up at all if we pass an empty string.
			home = "C:\\";
#else
			home = "/";
#endif
			LL_DEBUGS("Plugin") << "context \"" << context << "\" not found, default_path empty, context \"savefile\" not found "
			  "and $HOME or CSIDL_PERSONAL is empty! Returning \"" << home << "\"." << LL_ENDL;
		}
		else
		{
			LL_DEBUGS("Plugin") << "context \"" << context << "\" not found, default_path empty and context \"savefile\" not found. "
			  "Returning $HOME or CSIDL_PERSONAL (" << home << ")." << LL_ENDL;
		}
		return home;
	}
}
// static
std::string AIFilePicker::get_folder(std::string const& default_path, std::string const& context)
{
	AIAccess<context_map_type> wContextMap(sContextMap);
	context_map_type::iterator iter = wContextMap->find(context);
	if (iter != wContextMap->end() && !iter->second.empty())
	{
		return iter->second;
	}
	else if (!default_path.empty())
	{
		LL_DEBUGS("Plugin") << "context \"" << context << "\" not found. Returning default_path \"" << default_path << "\"." << LL_ENDL;
		return default_path;
	}
	else if ((iter = wContextMap->find("savefile")) != wContextMap->end() && !iter->second.empty())
	{
		LL_DEBUGS("Plugin") << "context \"" << context << "\" not found and default_path empty. Returning context \"savefile\": \"" << iter->second << "\"." << LL_ENDL;
		return iter->second;
	}
	else
	{
		// This is the last resort when all else failed. Open the file chooser in directory 'home'.
#if LL_WINDOWS
		char const* envname = "USERPROFILE";
#else
		char const* envname = "HOME";
#endif
		char const* home = getenv(envname);
		if (!home || home[0] == '\0')
		{
#if LL_WINDOWS
			// On windows, the filepicker window won't pop up at all if we pass an empty string.
			home = "C:\\";
#else
			home = "/";
#endif
			LL_DEBUGS("Plugin") << "context \"" << context << "\" not found, default_path empty, context \"savefile\" not found "
			  "and environment variable \"$" << envname << "\" empty! Returning \"" << home << "\"." << LL_ENDL;
		}
		else
		{
			LL_DEBUGS("Plugin") << "context \"" << context << "\" not found, default_path empty and context \"savefile\" not found. "
			  "Returning environment variable \"$" << envname << "\" (" << home << ")." << LL_ENDL;
		}
		return home;
	}
}