public MitsubaOptionsPage(Rhino.PersistentSettings settings) : base("Mitsuba") { m_settings = settings; m_mitsubaSettings = new MitsubaSettings(); m_mitsubaSettings.Load(m_settings); m_control = new MitsubaOptionsControl(ref m_mitsubaSettings); }
public bool ContainsModifiedValues(PersistentSettings allUserSettings) { if (null != m_Settings && m_Settings.Count > 0) { foreach (var v in m_Settings) { if (v.Value.ValueDifferentThanDefault) return true; if (null != allUserSettings && allUserSettings.m_Settings.ContainsKey(v.Key) && 0 != string.Compare(v.Value.GetValue(false), allUserSettings.m_Settings[v.Key].GetValue(false), StringComparison.Ordinal)) return true; } } return false; }
internal void CopyFrom(PersistentSettings source) { if (null != source) { foreach (var item in source.m_Settings) { if (m_Settings.ContainsKey(item.Key)) m_Settings[item.Key].CopyFrom(item.Value); else m_Settings.Add(item.Key, new SettingValue(item.Value.GetValue(false), item.Value.GetValue(true))); } } }
public PersistentSettings(PersistentSettings allUserSettings) { AllUserSettings = allUserSettings; m_Settings = new Dictionary<string, SettingValue>(); m_SettingsValidators = new Dictionary<string, EventHandler<PersistentSettingsEventArgs>>(); }
/// <summary> /// Reads existing settings for a plug-in and its associated commands. /// Clears the dirty flag for the settings. /// </summary> /// <returns> /// true if settings are successfully read. false if there was no existing /// settings file to read, or if a read lock could not be acquired. /// </returns> public bool ReadSettingsHelper(bool localSettings) { if (m_PluginSettings == null) { m_PluginSettings = new PersistentSettings(AllUserPlugInSettings); // If AllUserSettings is not null then we are reading local settings, when // reading local settings first get values previously read from the All Users // location and add them to the local dictionary so the settings will propagate // to the current user. if (localSettings && null != AllUserSettings) m_PluginSettings.CopyFrom(AllUserPlugInSettings); } if (m_CommandSettingsDict == null) { m_CommandSettingsDict = new Dictionary<string, PersistentSettings>(); // If AllUserSettings is not null then we are reading local settings, when // reading local settings first get values previously read from the All Users // location and add them to the local dictionary so the settings will propagate // to the current user. if (null != AllUserSettings && null != AllUserSettings.m_CommandSettingsDict) { foreach (var item in AllUserSettings.m_CommandSettingsDict) { // Make a new settings dictionary to associate with this command PersistentSettings settings = new PersistentSettings(item.Value); // Copy settings from global command dictionary to local dictionary settings.CopyFrom(item.Value); // Add the settings to the local dictionary m_CommandSettingsDict.Add(item.Key, settings); } } } string settingsFileName = SettingsFileName(localSettings); if (File.Exists(settingsFileName) == false) return false; FileStream fs = null; try { bool bKeepTrying = false; int lockTryCount = 0; // Lame attempt to handle file locking. For performance reasons, only // try once more after failure to acquire lock. do { try { bKeepTrying = false; fs = new FileStream(settingsFileName, FileMode.Open, FileAccess.Read); } catch (IOException ioe) { if (!(ioe is FileNotFoundException) && !(ioe is DirectoryNotFoundException) && !(ioe is PathTooLongException)) { // File is locked. Try once more then give up. if (lockTryCount < 1) { bKeepTrying = true; lockTryCount++; System.Threading.Thread.Sleep(50); } } } } while (bKeepTrying); // Couldn't acquire lock if (fs == null) return false; XmlDocument doc = new XmlDocument(); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("xml", "http://www.w3.org/XML/1998/namespace"); doc.Load(fs); fs.Close(); fs.Dispose(); // The settings attribute will either be "xml:id" or "id", this will check for "id" if "xml:id" is not found. XmlNode rootNode = doc.SelectSingleNode("/settings[@xml:id=\'" + CURRENT_XML_FORMAT_VERSION + "\']", ns) ?? doc.SelectSingleNode("/settings[@id=\'" + CURRENT_XML_FORMAT_VERSION + "\']", ns); if (rootNode == null) return false; // Parse main <plug-in> entry, if it exists, for plug-in settings m_PluginSettings.ParseXmlNodes(rootNode.SelectSingleNode("./plugin")); // Look for <command> nodes which will have a command name property that identifies the plug-in command settings XmlNodeList commandNodes = rootNode.SelectNodes("./command"); if (commandNodes != null) { foreach (XmlNode commandNode in commandNodes) { XmlAttributeCollection attr_collection = commandNode.Attributes; if( attr_collection==null ) continue; XmlNode attr = attr_collection.GetNamedItem("name"); if (null != attr && !string.IsNullOrEmpty(attr.Value)) { PersistentSettings entries = new PersistentSettings(AllUserCommandSettings(attr.Value)); entries.ParseXmlNodes(commandNode); if (null != AllUserSettings && m_CommandSettingsDict.ContainsKey(attr.Value)) m_CommandSettingsDict[attr.Value].CopyFrom(entries); else m_CommandSettingsDict[attr.Value] = entries; } } } } catch (Exception ex) { Rhino.Runtime.HostUtils.ExceptionReport(ex); return false; } finally { if (fs != null) { fs.Close(); fs.Dispose(); } } return true; }
/// <summary> /// Gets the PersistentSettings associated with the specified command. If the settings file /// has not been previously loaded and exists then it will get read. If the command name is /// not in the command settings dictionary then a new entry will get created and its settings /// will be returned. /// </summary> /// <param name="name">Command name key to search for and/or add.</param> /// <returns>Returns PersistentSettings object associated with command name on success or null on error.</returns> public PersistentSettings CommandSettings(string name) { if (m_CommandSettingsDict == null) { ReadSettings(); if (m_CommandSettingsDict == null) return null; } if (m_CommandSettingsDict.ContainsKey(name)) return m_CommandSettingsDict[name]; // There were no settings available for the command, so create one // for writing m_CommandSettingsDict[name] = new PersistentSettings(AllUserCommandSettings(name)); return m_CommandSettingsDict[name]; }
/// <summary> /// If the settings dictionary contains one or more values, which are not equal to the default value, then Write the contents /// of this settings dictionary to the specified XmlWriter contained within elementName. /// </summary> /// <param name="xmlWriter">XmlWriter object to write to.</param> /// <param name="elementName">Element which will contain key value pairs.</param> /// <param name="attributeName">Optional element attribute.</param> /// <param name="attributeValue">Optional element attribute value.</param> /// <param name="allUserSettings">All users settings to compare with.</param> internal void WriteXmlElement(XmlWriter xmlWriter, string elementName, string attributeName, string attributeValue, PersistentSettings allUserSettings) { if (null != m_Settings && ContainsModifiedValues(allUserSettings)) { xmlWriter.WriteStartElement(elementName); if (!string.IsNullOrEmpty(attributeName) && !string.IsNullOrEmpty(attributeValue)) xmlWriter.WriteAttributeString(attributeName, attributeValue); foreach (var item in m_Settings) { string allUserValue = null; if (null != allUserSettings && allUserSettings.m_Settings.ContainsKey(item.Key)) allUserValue = allUserSettings.m_Settings[item.Key].GetValue(false); string value = item.Value.GetValue(false); bool valueDifferentThanAllUser = (null != allUserValue && 0 != string.Compare(value, allUserValue, StringComparison.Ordinal)); if (valueDifferentThanAllUser || item.Value.ValueDifferentThanDefault) { // Write current value xmlWriter.WriteStartElement("entry"); xmlWriter.WriteAttributeString("key", item.Key); // The following is used when you want to write the default and all user values as item attributes // to the settings output file, useful when trying to determine why a value was written //const bool bWriteDefaultValue = false; //if (bWriteDefaullValue) //{ // string defaultValue = item.Value.GetValue(true); // xmlWriter.WriteAttributeString("DefaultValue", null == defaultValue ? "" : defaultValue); // if (null != allUserValue) // xmlWriter.WriteAttributeString("AllUsersValue", allUserValue); //} if (!string.IsNullOrEmpty(value)) xmlWriter.WriteString(value); xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } }