U32 LLControlGroup::loadFromFile(const std::string& filename, bool set_default_values, bool save_values)
{
	LLSD settings;
	llifstream infile;
	infile.open(filename.c_str());
	if(!infile.is_open())
	{
		LL_WARNS("Settings") << "Cannot find file " << filename << " to load." << LL_ENDL;
		return 0;
	}

	if (LLSDParser::PARSE_FAILURE == LLSDSerialize::fromXML(settings, infile))
	{
		infile.close();
		LL_WARNS("Settings") << "Unable to parse LLSD control file " << filename << ". Trying Legacy Method." << LL_ENDL;
		return loadFromFileLegacy(filename, TRUE, TYPE_STRING);
	}

	U32	validitems = 0;
	bool hidefromsettingseditor = false;
	
	for(LLSD::map_const_iterator itr = settings.beginMap(); itr != settings.endMap(); ++itr)
	{
		LLControlVariable::ePersist persist = LLControlVariable::PERSIST_NONDFT;
		bool can_backup = true;		// <FS:Zi> Backup Settings
		std::string const & name = itr->first;
		LLSD const & control_map = itr->second;
		
		if(control_map.has("Persist")) 
		{
			persist = control_map["Persist"].asInteger()?
					  LLControlVariable::PERSIST_NONDFT : LLControlVariable::PERSIST_NO;
		}

		// <FS:Zi> Backup Settings
		if(control_map.has("Backup")) 
		{
			can_backup = control_map["Backup"].asInteger();
		}
		// </FS:Zi>

		// Sometimes we want to use the settings system to provide cheap persistence, but we
		// don't want the settings themselves to be easily manipulated in the UI because 
		// doing so can cause support problems. So we have this option:
		if(control_map.has("HideFromEditor"))
		{
			hidefromsettingseditor = control_map["HideFromEditor"].asInteger();
		}
		else
		{
			hidefromsettingseditor = false;
		}
		
		// If the control exists just set the value from the input file.
		LLControlVariable* existing_control = getControl(name);
		if(existing_control)
		{
			// set_default_values is true when we're loading the initial,
			// immutable files from app_settings, e.g. settings.xml.
			if(set_default_values)
			{
				// Override all previously set properties of this control.
				// ... except for type. The types must match.
				eControlType new_type = typeStringToEnum(control_map["Type"].asString());
				if(existing_control->isType(new_type))
				{
					existing_control->setDefaultValue(control_map["Value"]);
					existing_control->setPersist(persist);
					existing_control->setHiddenFromSettingsEditor(hidefromsettingseditor);
					existing_control->setComment(control_map["Comment"].asString());
					existing_control->setBackupable(can_backup);		// <FS:Zi> Backup Settings
				}
				else
				{
					LL_ERRS() << "Mismatched type of control variable '"
						   << name << "' found while loading '"
						   << filename << "'." << LL_ENDL;
				}
			}
			else if(existing_control->isPersisted())
			{
				// save_values is specifically false for (e.g.)
				// SessionSettingsFile and UserSessionSettingsFile -- in other
				// words, for a file that's supposed to be transient.
				existing_control->setValue(control_map["Value"], save_values);
			}
			// *NOTE: If not persisted and not setting defaults, 
			// the value should not get loaded.
		}
		else
		{
			// We've never seen this control before. Either we're loading up
			// the initial set of default settings files (set_default_values)
			// -- or we're loading user settings last saved by a viewer that
			// supports a superset of the variables we know.
			// CHOP-962: if we're loading an unrecognized user setting, make
			// sure we save it later. If you try an experimental viewer, tweak
			// a new setting, briefly revert to an old viewer, then return to
			// the new one, we don't want the old viewer to discard the
			// setting you changed.
			if (! set_default_values)
			{
				// Using PERSIST_ALWAYS insists that saveToFile() (which calls
				// LLControlVariable::shouldSave()) must save this control
				// variable regardless of its value. We can safely set this
				// LLControlVariable persistent because the 'persistent' flag
				// is not itself persisted!
				persist = LLControlVariable::PERSIST_ALWAYS;
				// We want to mention unrecognized user settings variables
				// (e.g. from a newer version of the viewer) in the log. But
				// we also arrive here for Boolean variables generated by
				// the notifications subsystem when the user checks "Don't
				// show me this again." These aren't declared in settings.xml;
				// they're actually named for the notification they suppress.
				// We don't want to mention those. Apologies, this is a bit of
				// a hack: we happen to know that user settings go into an
				// LLControlGroup whose name is "Global".
				if (getKey() == "Global")
				{
					LL_INFOS("LLControlGroup") << "preserving unrecognized " << getKey()
											   << " settings variable " << name << LL_ENDL;
				}
			}

			declareControl(name, 
						   typeStringToEnum(control_map["Type"].asString()), 
						   control_map["Value"], 
						   control_map["Comment"].asString(),
						   sanityTypeStringToEnum(control_map["SanityCheckType"].asString()),
						   control_map["SanityValue"],
						   control_map["SanityComment"].asString(),
						   persist,
						   can_backup,		// <FS:Zi> Backup Settings
						   hidefromsettingseditor
						   );
		}

		++validitems;
	}

	LL_DEBUGS("Settings") << "Loaded " << validitems << " settings from " << filename << LL_ENDL;
	return validitems;
}
Beispiel #2
0
U32 LLControlGroup::loadFromFile(const std::string& filename, bool set_default_values, bool save_values)
{
	LLSD settings;
	llifstream infile;
	infile.open(filename);
	if(!infile.is_open())
	{
		llwarns << "Cannot find file " << filename << " to load." << llendl;
		return 0;
	}

	S32 ret = LLSDSerialize::fromXML(settings, infile);

	if (ret <= 0)
	{
		infile.close();
		llwarns << "Unable to open LLSD control file " << filename << ". Trying Legacy Method." << llendl;		
		return loadFromFileLegacy(filename, TRUE, TYPE_STRING);
	}

	U32	validitems = 0;
	bool hidefromsettingseditor = false;
	
	for(LLSD::map_const_iterator itr = settings.beginMap(); itr != settings.endMap(); ++itr)
	{
		bool persist = true;
		std::string const & name = itr->first;
		LLSD const & control_map = itr->second;
		
		if(control_map.has("Persist")) 
		{
			persist = control_map["Persist"].asInteger();
		}
		
		// Sometimes we want to use the settings system to provide cheap persistence, but we
		// don't want the settings themselves to be easily manipulated in the UI because 
		// doing so can cause support problems. So we have this option:
		if(control_map.has("HideFromEditor"))
		{
			hidefromsettingseditor = control_map["HideFromEditor"].asInteger();
		}
		else
		{
			hidefromsettingseditor = false;
		}
		
		// If the control exists just set the value from the input file.
		LLControlVariable* existing_control = getControl(name);
		if(existing_control)
		{
			if(set_default_values)
			{
				// Override all previously set properties of this control.
				// ... except for type. The types must match.
				eControlType new_type = typeStringToEnum(control_map["Type"].asString());
				if(existing_control->isType(new_type))
				{
					existing_control->setDefaultValue(control_map["Value"]);
					existing_control->setPersist(persist);
					existing_control->setHiddenFromSettingsEditor(hidefromsettingseditor);
					existing_control->setComment(control_map["Comment"].asString());
				}
				else
				{
					llerrs << "Mismatched type of control variable '"
						   << name << "' found while loading '"
						   << filename << "'." << llendl;
				}
			}
			else if(existing_control->isPersisted())
			{
				existing_control->setValue(control_map["Value"], save_values);
			}
			// *NOTE: If not persisted and not setting defaults, 
			// the value should not get loaded.
		}
		else
		{
			declareControl(name, 
						   typeStringToEnum(control_map["Type"].asString()), 
						   control_map["Value"], 
						   control_map["Comment"].asString(),
						   sanityTypeStringToEnum(control_map["SanityCheckType"].asString()),
						   control_map["SanityValue"],
						   control_map["SanityComment"].asString(),
						   persist,
						   hidefromsettingseditor
						   );
		}
		
		++validitems;
	}

	return validitems;
}