/[RomCheater]/trunk/RomCheater.UserSettingsSupport/SettingSubscriber.cs
ViewVC logotype

Contents of /trunk/RomCheater.UserSettingsSupport/SettingSubscriber.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 724 - (show annotations) (download)
Tue Jun 18 20:22:08 2013 UTC (7 years, 5 months ago) by william
File size: 8782 byte(s)

1 //#define ENABLE_LOAD_SETTINGS // when present will enable calls to LoadSettings()
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Reflection;
7 using System.Configuration;
8 using System.Xml.Serialization;
9 using System.IO;
10 using System.Collections;
11 using System.Security.Policy;
12 using System.Security.Cryptography;
13 using System.Windows.Forms;
14 using System.Diagnostics;
15
16 namespace RomCheater.UserSettingsSupport
17 {
18 public sealed class SettingSubscriber
19 {
20 public SettingSubscriber() { }
21 //public void AddSubscriber(System.Configuration.ApplicationSettingsBase settings) { AddSubscriber(null, settings); }
22 public void AddSubscriber(object consumer, System.Configuration.ApplicationSettingsBase ownersettings)
23 {
24 this.OwnerSettings = ownersettings;
25
26 if (this.OwnerSettings == null)
27 {
28 throw new ArgumentNullException("ownersettings", "ownersettings cannot be null");
29 }
30
31 this.Consumer = consumer;
32 LoadSettings();
33 this.UpgradeSettingsIfNeeded();
34 }
35
36
37
38 private object _Consumer;
39 /// <summary>
40 /// The object that will consume the settings
41 /// </summary>
42 private object Consumer
43 {
44 get { return _Consumer; }
45 set { _Consumer = value; }
46 }
47
48
49
50 /// <summary>
51 /// The Assembly that owns the settings
52 /// </summary>
53 private Assembly OwningAsembly
54 {
55 get { return this.OwnerSettings.GetType().Assembly; }
56 }
57
58 /// <summary>
59 /// The Assembly that will consume the settings
60 /// </summary>
61 private Assembly ConsumerAssembly
62 {
63 get { return this.Consumer == null ? Assembly.GetExecutingAssembly() : this.Consumer.GetType().Assembly; }
64 }
65
66 private System.Configuration.ApplicationSettingsBase _OwnerSettings;
67 public System.Configuration.ApplicationSettingsBase OwnerSettings
68 {
69 get { return _OwnerSettings; }
70 private set { _OwnerSettings = value; }
71 }
72
73 /// <summary>
74 /// Indicates if the consumer is the owning assemlby of the settings
75 /// If the value is false, then the settings are being accessed remotely
76 /// </summary>
77 private bool IsOwningAssembly
78 {
79 get { return this.ConsumerAssembly == this.OwningAsembly; }
80 }
81
82 //private Configuration _config = null;
83
84 #region LoadSettings()
85 [Conditional("ENABLE_LOAD_SETTINGS")]
86 private void LoadSettings()
87 {
88 //if (!this.IsOwningAssembly)
89 //{
90 // //// we need to read the settings in
91 // ////string ConfigFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
92 // ////string ConfigFile = ConfigurationManager.OpenExeConfiguration(SettingsAssmebly.Location).FilePath;
93 // //string path = string.Empty;
94 // //string profile_dir = Environment.GetEnvironmentVariable("LocalAppData");
95 // //string company_name = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(this.OwningAsembly, typeof(AssemblyCompanyAttribute), false)).Company;
96 // //string app_fullname = new FileInfo(this.OwningAsembly.Location).Name;
97 // //string evidence_type = this.OwningAsembly.GetName().GetPublicKey().Length == 0 ? EVIDENCE_URL : EVIDENCE_STRONGNAME;
98 // //string evidence_hash = "<HASH>";
99 // //string version = this.OwningAsembly.GetName().Version.ToString();
100 // //string config = "user.config";
101 // //StringBuilder builder = new StringBuilder();
102 // //builder.AppendFormat(@"{0}", profile_dir);
103 // //if (company_name != string.Empty)
104 // //{
105 // // builder.AppendFormat(@"\{0}", company_name.Replace(" ", "_"));
106 // //}
107 // //builder.AppendFormat(@"\{0}", app_fullname);
108 // //builder.AppendFormat(@"_{0}", evidence_type);
109 // //builder.AppendFormat(@"_{0}", evidence_hash);
110 // //builder.AppendFormat(@"\{0}", version);
111 // //builder.AppendFormat(@"\{0}", config);
112 // //path = builder.ToString();
113 // //#region Hash testing
114 // //Hash owner = new Hash(this.OwningAsembly);
115 // //Hash consumer = new Hash(this.ConsumerAssembly);
116 // //#endregion
117 //IsolatedStoragePath isp1 = new IsolatedStoragePath(this.OwningAsembly);
118 // IsolatedStoragePath isp2 = new IsolatedStoragePath(this.ConsumerAssembly);
119 //}
120 //else
121 //{
122 // string ConfigFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
123 //}
124 }
125 #endregion
126
127
128
129 public void UpgradeSettings()
130 {
131 if (this.OwnerSettings != null)
132 {
133 this.OwnerSettings.Upgrade();
134 return;
135 }
136 throw new ArgumentNullException("Settings", "Settings cannot be null");
137 }
138
139 public void SaveSettings()
140 {
141 if (this.OwnerSettings != null)
142 {
143 this.OwnerSettings.Save();
144 return;
145 }
146 throw new ArgumentNullException("Settings", "Settings cannot be null");
147
148 }
149 public void ReloadSettings()
150 {
151 if (this.OwnerSettings != null)
152 {
153 this.OwnerSettings.Reload();
154 return;
155 }
156 throw new ArgumentNullException("Settings", "Settings cannot be null");
157 }
158 public void ResetSettings()
159 {
160 if (this.OwnerSettings != null)
161 {
162 this.OwnerSettings.Reset();
163 return;
164 }
165 throw new ArgumentNullException("Settings", "Settings cannot be null");
166 }
167 public void UpgradeSettingsIfNeeded()
168 {
169 if (this.OwnerSettings != null)
170 {
171 //SettingCollections.Add(e.Settings);
172 foreach (SettingsProperty prop in this.OwnerSettings.Properties)
173 {
174 if (prop.Name == "UpgradeRequired")
175 {
176 bool UpgradeRequired = Convert.ToBoolean(this.OwnerSettings[prop.Name]);
177 try
178 {
179 if (UpgradeRequired)
180 {
181 UpgradeSettings();
182 //Settings.Reload();
183 SetValue(prop.Name, false);
184 SaveSettings();
185 }
186 }
187 catch { ResetSettings(); }
188 break;
189 }
190 }
191 }
192 else
193 {
194 throw new ArgumentNullException("Settings", "Settings cannot be null");
195 }
196 }
197 public void SetValue(string propertyName, object value)
198 {
199 if (this.OwnerSettings != null)
200 {
201 try
202 {
203 this.OwnerSettings[propertyName] = value;
204 }
205 catch (Exception ex)
206 {
207 throw new KeyNotFoundException(string.Format("Could not find property: '{0}'"), ex);
208 }
209 }
210 else
211 {
212 throw new ArgumentNullException("Settings", "Settings cannot be null");
213 }
214 }
215 public object GetValue(string propertyName)
216 {
217 if (this.OwnerSettings != null)
218 {
219 try
220 {
221 return this.OwnerSettings[propertyName];
222 }
223 catch (Exception ex)
224 {
225 throw new KeyNotFoundException(string.Format("Could not find property: '{0}'"), ex);
226 }
227 }
228 else
229 {
230 throw new ArgumentNullException("Settings", "Settings cannot be null");
231 }
232 }
233 }
234 }

  ViewVC Help
Powered by ViewVC 1.1.22