2 using System.Collections.Generic;
6 using System.Diagnostics;
8 namespace AppLimit.NetSparkle
11 /// This class handles all registry values which are used from sparkle to handle
12 /// update intervalls. All values are stored in HKCU\Software\Vendor\AppName which
13 /// will be read ot from the assembly information. All values are of the REG_SZ
14 /// type, no matter what their "logical" type is. The following options are
17 /// CheckForUpdate - Boolean - Whether NetSparkle should check for updates
18 /// LastCheckTime - time_t - Time of last check
19 /// SkipThisVersion - String - If the user skipped an update, then the version to ignore is stored here (e.g. "1.4.3")
20 /// DidRunOnce - Boolean - Check only one time when the app launched
22 public class NetSparkleConfiguration
24 public String ApplicationName { get; private set; }
25 public String InstalledVersion { get; private set; }
26 public Boolean CheckForUpdate { get; private set; }
27 public DateTime LastCheckTime { get; private set; }
28 public String SkipThisVersion { get; private set; }
29 public Boolean DidRunOnce { get; private set; }
30 public Boolean ShowDiagnosticWindow { get; private set; }
31 public DateTime LastProfileUpdate { get; private set; }
34 /// If this property is true a reflection based accessor will be used
35 /// to determine the assmebly name and verison, otherwise a System.Diagnostics
36 /// based access will be used
38 public Boolean UseReflectionBasedAssemblyAccessor { get; private set; }
40 private String _referenceAssembly;
43 /// The constructor reads out all configured values
45 public NetSparkleConfiguration(String ReferenceAssembly)
46 : this(ReferenceAssembly, true)
49 public NetSparkleConfiguration(String ReferenceAssembly, Boolean UseReflectionBasedAssemblyAccessor)
52 this.UseReflectionBasedAssemblyAccessor = UseReflectionBasedAssemblyAccessor;
54 // save the referecne assembly
55 _referenceAssembly = ReferenceAssembly;
60 InitWithDefaultValues();
62 // set some value from the binary
63 NetSparkleAssemblyAccessor accessor = new NetSparkleAssemblyAccessor(ReferenceAssembly, this.UseReflectionBasedAssemblyAccessor);
64 ApplicationName = accessor.AssemblyProduct;
65 InstalledVersion = accessor.AssemblyVersion;
68 String regPath = BuildRegistryPath();
71 LoadValuesFromPath(regPath);
75 // disable update checks when exception was called
76 CheckForUpdate = false;
78 if (e.Message.Contains("STOP:"))
84 /// Touches to profile time
86 public void TouchProfileTime()
88 // set the prodilt update time
89 LastProfileUpdate = DateTime.Now;
92 String path = BuildRegistryPath();
95 SaveValuesToPath(path);
99 /// Touches the check time to now, should be used after a check directly
101 public void TouchCheckTime()
103 // set the check tiem
104 LastCheckTime = DateTime.Now;
107 String path = BuildRegistryPath();
110 SaveValuesToPath(path);
114 /// This method allows to skip a specific version
116 /// <param name="version"></param>
117 public void SetVersionToSkip(String version)
119 // set the check tiem
120 SkipThisVersion = version;
123 String path = BuildRegistryPath();
126 SaveValuesToPath(path);
130 /// This function build a valid registry path in dependecy to the
131 /// assembly information
133 /// <returns></returns>
134 private String BuildRegistryPath()
136 NetSparkleAssemblyAccessor accessor = new NetSparkleAssemblyAccessor(_referenceAssembly, UseReflectionBasedAssemblyAccessor);
138 if (accessor.AssemblyCompany == null || accessor.AssemblyCompany.Length == 0 ||
139 accessor.AssemblyProduct == null || accessor.AssemblyProduct.Length == 0)
140 throw new Exception("STOP: Sparkle is missing the company or productname tag in " + _referenceAssembly);
142 return "Software\\" + accessor.AssemblyCompany + "\\" + accessor.AssemblyProduct + "\\AutoUpdate";
146 /// This method set's default values for the config
148 private void InitWithDefaultValues()
150 CheckForUpdate = true;
151 LastCheckTime = new DateTime(0);
152 SkipThisVersion = String.Empty;
154 UseReflectionBasedAssemblyAccessor = true;
158 /// This method loads the values from registry
160 /// <param name="regPath"></param>
161 /// <returns></returns>
162 private Boolean LoadValuesFromPath(String regPath)
164 RegistryKey key = Registry.CurrentUser.OpenSubKey(regPath);
170 String strCheckForUpdate = key.GetValue("CheckForUpdate", "True") as String;
171 String strLastCheckTime = key.GetValue("LastCheckTime", new DateTime(0).ToString()) as String;
172 String strSkipThisVersion = key.GetValue("SkipThisVersion", "") as String;
173 String strDidRunOnc = key.GetValue("DidRunOnce", "False") as String;
174 String strShowDiagnosticWindow = key.GetValue("ShowDiagnosticWindow", "False") as String;
175 String strProfileTime = key.GetValue("LastProfileUpdate", new DateTime(0).ToString()) as String;
177 // convert th right datatypes
178 CheckForUpdate = Convert.ToBoolean(strCheckForUpdate);
179 LastCheckTime = Convert.ToDateTime(strLastCheckTime);
180 SkipThisVersion = strSkipThisVersion;
181 DidRunOnce = Convert.ToBoolean(strDidRunOnc);
182 ShowDiagnosticWindow = Convert.ToBoolean(strShowDiagnosticWindow);
183 LastProfileUpdate = Convert.ToDateTime(strProfileTime);
189 /// This method store the information into registry
191 /// <param name="regPath"></param>
192 /// <returns></returns>
193 private Boolean SaveValuesToPath(String regPath)
195 RegistryKey key = Registry.CurrentUser.CreateSubKey(regPath);
201 String strCheckForUpdate = CheckForUpdate.ToString();
202 String strLastCheckTime = LastCheckTime.ToString();
203 String strSkipThisVersion = SkipThisVersion.ToString();
204 String strDidRunOnc = DidRunOnce.ToString();
205 String strProfileTime = LastProfileUpdate.ToString();
208 key.SetValue("CheckForUpdate", strCheckForUpdate, RegistryValueKind.String);
209 key.SetValue("LastCheckTime", strLastCheckTime, RegistryValueKind.String);
210 key.SetValue("SkipThisVersion", strSkipThisVersion, RegistryValueKind.String);
211 key.SetValue("DidRunOnce", strDidRunOnc, RegistryValueKind.String);
212 key.SetValue("LastProfileUpdate", strProfileTime, RegistryValueKind.String);