--- trunk/NexusPowerControl/ThemeLoader.cs 2011/10/22 08:58:41 8 +++ trunk/NexusPowerControl/ThemeLoader.cs 2011/10/22 09:46:14 9 @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.IO; +using System.Xml; namespace NexusPowerControl { @@ -36,25 +38,117 @@ namespace NexusPowerControl #endregion #region sub-classes - public class ThemeDefinition + public class ThemeElements { + public const string main = "main"; + public const string shutdown = "shutdown"; + public const string restart = "restart"; + public const string logoff = "logoff"; + public const string lockscreen = "lockscreen"; + public const string sleep = "sleep"; + public const string close = "close"; + } + public interface IThemeDefinition + { + bool ThemeLoaded { get; set; } + string MainImage { get; set; } + string ShutdownImage { get; set; } + string RestartImage { get; set; } + string LogoffImage { get; set; } + string LockScreenImage { get; set; } + string SleepImage { get; set; } + string CloseImage { get; set; } + } + public class ThemeDefinition : IThemeDefinition + { + private const string MissingImage = "missing.png"; private const string ThemeDirectory = "theme"; + private const string ThemeFile = "theme.xml"; public const string DefaultTheme = "AeroLowRes"; public ThemeDefinition() : this(ThemeDefinition.DefaultTheme) { } - public ThemeDefinition(string ThemeXml) { this.LoadTheme(ThemeXml); } + public ThemeDefinition(string ThemeXml) { this.init(); this.LoadTheme(ThemeXml); } + + private void init() + { + this.ThemeLoaded = false; + this.MainImage = ""; + this.ShutdownImage = ""; + this.RestartImage = ""; + this.LogoffImage = ""; + this.LockScreenImage = ""; + this.SleepImage = ""; + this.CloseImage = ""; + } private void LoadTheme(string theme) { if (theme.ToLower().Contains(".xml")) { this.LoadCustomTheme(theme); } else { this.LoadStandardTheme(theme); } } private void LoadStandardTheme(string theme) { // load theme in format: theme/<themname>/theme.xml : will throw file not found excetion (use default) + string xml_file = string.Format(@"{0}\{1}\{2}", ThemeDirectory, theme, ThemeFile); + this.LoadCustomTheme(xml_file); } private void LoadCustomTheme(string theme) { // load theme in format: <theme xml file>: will throw file not found excetion (use default) + try { if (!File.Exists(theme)) { throw new FileNotFoundException(theme); } else { try { this.ReadThemeXml(theme); } catch { this.ThemeLoaded = false; return; } } this.ThemeLoaded = true; } + catch (FileNotFoundException) { this.ThemeLoaded = false; } + catch (Exception) { this.ThemeLoaded = false; } } + + private void ReadThemeXml(string theme_xml) + { + FileStream fs = new FileStream(theme_xml, FileMode.Open, FileAccess.Read); + XmlReader r = XmlReader.Create(fs); + while (r.Read()) + { + if (r.NodeType == XmlNodeType.Element) + { + if (r.Name.ToLower() == "theme") { r.Read(); } + + if (r.HasAttributes) + { + switch (r.Name.ToLower()) + { + case ThemeElements.close: this.CloseImage = BuildThemeImageURI(theme_xml,r.GetAttribute("value")); break; + case ThemeElements.lockscreen: this.LockScreenImage =BuildThemeImageURI(theme_xml,r.GetAttribute("value")); break; + case ThemeElements.logoff: this.LogoffImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break; + case ThemeElements.main: this.MainImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break; + case ThemeElements.restart: this.RestartImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break; + case ThemeElements.shutdown: this.ShutdownImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break; + case ThemeElements.sleep: this.SleepImage = BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break; + default: BuildThemeImageURI(ThemeDirectory,MissingImage); break; + } + } + } + } + } + + private string BuildThemeImageURI(string theme_xml, string image) + { + FileInfo fs = new FileInfo(theme_xml); + string path = fs.Directory.FullName; + string fullpath = string.Format(@"{0}\{1}", path, image); + fs = new FileInfo(fullpath); + if (!fs.Exists) + { + fs = new FileInfo(ThemeDirectory); + fullpath = string.Format(@"{0}\{1}\{2}", fs.DirectoryName, ThemeDirectory, MissingImage); + } + return fullpath; + } + + #region IThemeDefinition Members public bool ThemeLoaded { get; set; } + public string MainImage { get; set; } + public string ShutdownImage { get; set; } + public string RestartImage { get; set; } + public string LogoffImage { get; set; } + public string LockScreenImage { get; set; } + public string SleepImage { get; set; } + public string CloseImage { get; set; } + #endregion } #endregion } |