OSDN Git Service

ab04738ec1d72af6edecd1baf60dd9fe09d15178
[radegast/radegast.git] / Radegast / Core / Settings.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2013, Radegast Development Team
4 // All rights reserved.
5 // 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 
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.
17 // 
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.
28 //
29 // $Id$
30 //
31 using System;
32 using System.Collections.Generic;
33 using System.Collections;
34 using System.IO;
35 using System.Xml;
36 using OpenMetaverse;
37 using OpenMetaverse.StructuredData;
38
39 namespace Radegast
40 {
41     public class Settings : IDictionary<string, OSD>
42     {
43         private string SettingsFile;
44         private OSDMap SettingsData;
45
46         public delegate void SettingChangedCallback(object sender, SettingsEventArgs e);
47         public event SettingChangedCallback OnSettingChanged;
48
49         public Settings(string fileName)
50         {
51             SettingsFile = fileName;
52
53             try
54             {
55                 string xml = File.ReadAllText(SettingsFile);
56                 SettingsData = (OSDMap)OSDParser.DeserializeLLSDXml(xml);
57             }
58             catch
59             {
60                 Logger.DebugLog("Failed openning setting file: " + fileName);
61                 SettingsData = new OSDMap();
62                 Save();
63             }
64         }
65
66         public void Save()
67         {
68             try
69             {
70                 File.WriteAllText(SettingsFile, SerializeLLSDXmlStringFormated(SettingsData));
71             }
72             catch (Exception ex)
73             {
74                 Logger.Log("Failed saving settings", Helpers.LogLevel.Warning, ex);
75             }
76         }
77
78         public static string SerializeLLSDXmlStringFormated(OSD data)
79         {
80             using (StringWriter sw = new StringWriter())
81             {
82                 using (XmlTextWriter writer = new XmlTextWriter(sw))
83                 {
84                     writer.Formatting = Formatting.Indented;
85                     writer.Indentation = 4;
86                     writer.IndentChar = ' ';
87
88                     writer.WriteStartElement(String.Empty, "llsd", String.Empty);
89                     OSDParser.SerializeLLSDXmlElement(writer, data);
90                     writer.WriteEndElement();
91
92                     writer.Close();
93
94                     return sw.ToString();
95                 }
96             }
97         }
98
99         private void FireEvent(string key, OSD val)
100         {
101             if (OnSettingChanged != null)
102             {
103                 try { OnSettingChanged(this, new SettingsEventArgs(key, val)); }
104                 catch (Exception) {}
105             }
106         }
107
108         #region IDictionary Implementation
109
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]
115         {
116             get
117             {
118                 return SettingsData[key];
119             }
120             set 
121             {
122                 if (string.IsNullOrEmpty(key))
123                 {
124                     Logger.DebugLog("Warning: trying to set an emprty setting: " + Environment.StackTrace.ToString());
125                 }
126                 else
127                 {
128                     SettingsData[key] = value;
129                     FireEvent(key, value);
130                     Save();
131                 }
132             }
133         }
134
135         public bool ContainsKey(string key)
136         {
137             return SettingsData.ContainsKey(key);
138         }
139
140         public void Add(string key, OSD llsd)
141         {
142             SettingsData.Add(key, llsd);
143             FireEvent(key, llsd);
144             Save();
145         }
146
147         public void Add(KeyValuePair<string, OSD> kvp)
148         {
149             SettingsData.Add(kvp.Key, kvp.Value);
150             FireEvent(kvp.Key, kvp.Value);
151             Save();
152         }
153
154         public bool Remove(string key)
155         {
156             bool ret = SettingsData.Remove(key);
157             FireEvent(key, null);
158             Save();
159             return ret;
160         }
161
162         public bool TryGetValue(string key, out OSD llsd)
163         {
164             return SettingsData.TryGetValue(key, out llsd);
165         }
166
167         public void Clear()
168         {
169             SettingsData.Clear();
170             Save();
171         }
172
173         public bool Contains(KeyValuePair<string, OSD> kvp)
174         {
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);
178         }
179
180         public void CopyTo(KeyValuePair<string, OSD>[] array, int index)
181         {
182             throw new NotImplementedException();
183         }
184
185         public bool Remove(KeyValuePair<string, OSD> kvp)
186         {
187             bool ret = SettingsData.Remove(kvp.Key);
188             FireEvent(kvp.Key, null);
189             Save();
190             return ret;
191         }
192
193         public System.Collections.IDictionaryEnumerator GetEnumerator()
194         {
195             return SettingsData.GetEnumerator();
196         }
197
198         IEnumerator<KeyValuePair<string, OSD>> IEnumerable<KeyValuePair<string, OSD>>.GetEnumerator()
199         {
200             return null;
201         }
202
203         IEnumerator IEnumerable.GetEnumerator()
204         {
205             return SettingsData.GetEnumerator();
206         }
207
208         #endregion IDictionary Implementation
209
210     }
211
212     public class SettingsEventArgs : EventArgs
213     {
214         public string Key = string.Empty;
215         public OSD Value = new OSD();
216
217         public SettingsEventArgs(string key, OSD val)
218         {
219             Key = key;
220             Value = val;
221         }
222     }
223 }