ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/NexusPowerControl/trunk/NexusPowerControl/ThemeLoader.cs
(Generate patch)

Comparing trunk/NexusPowerControl/ThemeLoader.cs (file contents):
Revision 8 by william, Sat Oct 22 08:58:41 2011 UTC vs.
Revision 9 by william, Sat Oct 22 09:46:14 2011 UTC

# Line 2 | Line 2
2   using System.Collections.Generic;
3   using System.Linq;
4   using System.Text;
5 + using System.IO;
6 + using System.Xml;
7  
8   namespace NexusPowerControl
9   {
# Line 36 | Line 38 | namespace NexusPowerControl
38          #endregion
39  
40          #region sub-classes
41 <        public class ThemeDefinition
41 >        public class ThemeElements
42          {
43 +            public const string main = "main";
44 +            public const string shutdown = "shutdown";
45 +            public const string restart = "restart";
46 +            public const string logoff = "logoff";
47 +            public const string lockscreen = "lockscreen";
48 +            public const string sleep = "sleep";
49 +            public const string close = "close";
50 +        }
51 +        public interface IThemeDefinition
52 +        {
53 +            bool ThemeLoaded { get; set; }
54 +            string MainImage { get; set; }
55 +            string ShutdownImage { get; set; }
56 +            string RestartImage { get; set; }
57 +            string LogoffImage { get; set; }
58 +            string LockScreenImage { get; set; }
59 +            string SleepImage { get; set; }
60 +            string CloseImage { get; set; }
61 +        }
62 +        public class ThemeDefinition : IThemeDefinition
63 +        {
64 +            private const string MissingImage = "missing.png";
65              private const string ThemeDirectory = "theme";
66 +            private const string ThemeFile = "theme.xml";
67  
68              public const string DefaultTheme = "AeroLowRes";
69              public ThemeDefinition() : this(ThemeDefinition.DefaultTheme) { }
70 <            public ThemeDefinition(string ThemeXml) { this.LoadTheme(ThemeXml); }
70 >            public ThemeDefinition(string ThemeXml) { this.init(); this.LoadTheme(ThemeXml); }
71 >
72 >            private void init()
73 >            {
74 >                this.ThemeLoaded = false;
75 >                this.MainImage = "";
76 >                this.ShutdownImage = "";
77 >                this.RestartImage = "";
78 >                this.LogoffImage = "";
79 >                this.LockScreenImage = "";
80 >                this.SleepImage = "";
81 >                this.CloseImage = "";
82 >            }
83  
84              private void LoadTheme(string theme) { if (theme.ToLower().Contains(".xml")) { this.LoadCustomTheme(theme); } else { this.LoadStandardTheme(theme); } }
85  
86              private void LoadStandardTheme(string theme)
87              {
88                  // load theme in format: theme/<themname>/theme.xml : will throw file not found excetion (use default)
89 +                string xml_file = string.Format(@"{0}\{1}\{2}", ThemeDirectory, theme, ThemeFile);
90 +                this.LoadCustomTheme(xml_file);
91              }
92              private void LoadCustomTheme(string theme)
93              {
94                  // load theme in format: <theme xml file>: will throw file not found excetion (use default)
95 +                try { if (!File.Exists(theme)) { throw new FileNotFoundException(theme); } else { try { this.ReadThemeXml(theme); } catch { this.ThemeLoaded = false; return; } } this.ThemeLoaded = true; }
96 +                catch (FileNotFoundException) { this.ThemeLoaded = false; }
97 +                catch (Exception) { this.ThemeLoaded = false; }
98              }
99 +
100 +            private void ReadThemeXml(string theme_xml)
101 +            {
102 +                FileStream fs = new FileStream(theme_xml, FileMode.Open, FileAccess.Read);
103 +                XmlReader r = XmlReader.Create(fs);
104 +                while (r.Read())
105 +                {
106 +                    if (r.NodeType == XmlNodeType.Element)
107 +                    {
108 +                        if (r.Name.ToLower() == "theme") { r.Read(); }
109 +                
110 +                        if (r.HasAttributes)
111 +                        {
112 +                            switch (r.Name.ToLower())
113 +                            {
114 +                                case ThemeElements.close: this.CloseImage = BuildThemeImageURI(theme_xml,r.GetAttribute("value")); break;
115 +                                case ThemeElements.lockscreen: this.LockScreenImage =BuildThemeImageURI(theme_xml,r.GetAttribute("value")); break;
116 +                                case ThemeElements.logoff: this.LogoffImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break;
117 +                                case ThemeElements.main: this.MainImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break;
118 +                                case ThemeElements.restart: this.RestartImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break;
119 +                                case ThemeElements.shutdown: this.ShutdownImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break;
120 +                                case ThemeElements.sleep: this.SleepImage = BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break;
121 +                                default: BuildThemeImageURI(ThemeDirectory,MissingImage); break;                                
122 +                            }
123 +                        }
124 +                    }
125 +                }
126 +            }
127 +
128 +            private string BuildThemeImageURI(string theme_xml, string image)
129 +            {
130 +                FileInfo fs = new FileInfo(theme_xml);
131 +                string path = fs.Directory.FullName;
132 +                string fullpath = string.Format(@"{0}\{1}", path, image);
133 +                fs = new FileInfo(fullpath);
134 +                if (!fs.Exists)
135 +                {
136 +                    fs = new FileInfo(ThemeDirectory);
137 +                    fullpath = string.Format(@"{0}\{1}\{2}", fs.DirectoryName, ThemeDirectory, MissingImage);
138 +                }
139 +                return fullpath;
140 +            }
141 +
142 +            #region IThemeDefinition Members
143              public bool ThemeLoaded { get; set; }
144 +            public string MainImage { get; set; }
145 +            public string ShutdownImage { get; set; }
146 +            public string RestartImage { get; set; }
147 +            public string LogoffImage { get; set; }
148 +            public string LockScreenImage { get; set; }
149 +            public string SleepImage { get; set; }
150 +            public string CloseImage { get; set; }
151 +            #endregion
152          }
153          #endregion
154      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines