2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2013, Radegast Development Team
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // * Neither the name of the application "Radegast", nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 using System.Collections.Generic;
33 using System.Collections;
37 using OpenMetaverse.StructuredData;
41 public class Settings : IDictionary<string, OSD>
43 private string SettingsFile;
44 private OSDMap SettingsData;
46 public delegate void SettingChangedCallback(object sender, SettingsEventArgs e);
47 public event SettingChangedCallback OnSettingChanged;
49 public Settings(string fileName)
51 SettingsFile = fileName;
55 string xml = File.ReadAllText(SettingsFile);
56 SettingsData = (OSDMap)OSDParser.DeserializeLLSDXml(xml);
60 Logger.DebugLog("Failed openning setting file: " + fileName);
61 SettingsData = new OSDMap();
70 File.WriteAllText(SettingsFile, SerializeLLSDXmlStringFormated(SettingsData));
74 Logger.Log("Failed saving settings", Helpers.LogLevel.Warning, ex);
78 public static string SerializeLLSDXmlStringFormated(OSD data)
80 using (StringWriter sw = new StringWriter())
82 using (XmlTextWriter writer = new XmlTextWriter(sw))
84 writer.Formatting = Formatting.Indented;
85 writer.Indentation = 4;
86 writer.IndentChar = ' ';
88 writer.WriteStartElement(String.Empty, "llsd", String.Empty);
89 OSDParser.SerializeLLSDXmlElement(writer, data);
90 writer.WriteEndElement();
99 private void FireEvent(string key, OSD val)
101 if (OnSettingChanged != null)
103 try { OnSettingChanged(this, new SettingsEventArgs(key, val)); }
108 #region IDictionary Implementation
110 public int Count { get { return SettingsData.Count; } }
111 public bool IsReadOnly { get { return false; } }
112 public ICollection<string> Keys { get { return SettingsData.Keys; } }
113 public ICollection<OSD> Values { get { return SettingsData.Values; } }
114 public OSD this[string key]
118 return SettingsData[key];
122 if (string.IsNullOrEmpty(key))
124 Logger.DebugLog("Warning: trying to set an emprty setting: " + Environment.StackTrace.ToString());
128 SettingsData[key] = value;
129 FireEvent(key, value);
135 public bool ContainsKey(string key)
137 return SettingsData.ContainsKey(key);
140 public void Add(string key, OSD llsd)
142 SettingsData.Add(key, llsd);
143 FireEvent(key, llsd);
147 public void Add(KeyValuePair<string, OSD> kvp)
149 SettingsData.Add(kvp.Key, kvp.Value);
150 FireEvent(kvp.Key, kvp.Value);
154 public bool Remove(string key)
156 bool ret = SettingsData.Remove(key);
157 FireEvent(key, null);
162 public bool TryGetValue(string key, out OSD llsd)
164 return SettingsData.TryGetValue(key, out llsd);
169 SettingsData.Clear();
173 public bool Contains(KeyValuePair<string, OSD> kvp)
175 // This is a bizarre function... we don't really implement it
176 // properly, hopefully no one wants to use it
177 return SettingsData.ContainsKey(kvp.Key);
180 public void CopyTo(KeyValuePair<string, OSD>[] array, int index)
182 throw new NotImplementedException();
185 public bool Remove(KeyValuePair<string, OSD> kvp)
187 bool ret = SettingsData.Remove(kvp.Key);
188 FireEvent(kvp.Key, null);
193 public System.Collections.IDictionaryEnumerator GetEnumerator()
195 return SettingsData.GetEnumerator();
198 IEnumerator<KeyValuePair<string, OSD>> IEnumerable<KeyValuePair<string, OSD>>.GetEnumerator()
203 IEnumerator IEnumerable.GetEnumerator()
205 return SettingsData.GetEnumerator();
208 #endregion IDictionary Implementation
212 public class SettingsEventArgs : EventArgs
214 public string Key = string.Empty;
215 public OSD Value = new OSD();
217 public SettingsEventArgs(string key, OSD val)