From: <fr...@us...> - 2008-05-19 17:06:22
|
Revision: 1792 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=1792&view=rev Author: framug Date: 2008-05-19 10:06:10 -0700 (Mon, 19 May 2008) Log Message: ----------- - XmlConfig for setup Modified Paths: -------------- trunk/plugins/MyClickmania/AssemblyInfo.cs trunk/plugins/MyClickmania/ClickManiaSetup.cs trunk/plugins/MyClickmania/GUIClickMania.csproj Added Paths: ----------- trunk/plugins/MyClickmania/Properties/ trunk/plugins/MyClickmania/XmlConfig.cs Modified: trunk/plugins/MyClickmania/AssemblyInfo.cs =================================================================== --- trunk/plugins/MyClickmania/AssemblyInfo.cs 2008-05-19 16:58:17 UTC (rev 1791) +++ trunk/plugins/MyClickmania/AssemblyInfo.cs 2008-05-19 17:06:10 UTC (rev 1792) @@ -26,7 +26,7 @@ // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.1.*")] // // In order to sign your assembly you must specify a key to use. Refer to the Modified: trunk/plugins/MyClickmania/ClickManiaSetup.cs =================================================================== --- trunk/plugins/MyClickmania/ClickManiaSetup.cs 2008-05-19 16:58:17 UTC (rev 1791) +++ trunk/plugins/MyClickmania/ClickManiaSetup.cs 2008-05-19 17:06:10 UTC (rev 1792) @@ -16,11 +16,16 @@ private void ClickManiaSetup_Load(object sender, EventArgs e) { - using (MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.Settings(Config.GetFile(Config.Dir.Config, "MyClickMania.xml"))) +/* using (MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.Settings(Config.GetFile(Config.Dir.Config, "MyClickMania.xml"))) { textBox1.Text = xmlreader.GetValueAsString("clickmania", "pluginName", PluginName()); textBox2.Text = xmlreader.GetValueAsString("clickmania", "Title", "Click Mania"); - } + } */ + + XmlConfig XmlConfig = new XmlConfig(); + textBox1.Text = XmlConfig.ReadXmlConfig("MyClickMania", "clickmania", "pluginName", PluginName()); + textBox2.Text = XmlConfig.ReadXmlConfig("MyClickMania", "clickmania", "Title", "Click Mania"); + } private void ButOK_Click(object sender, EventArgs e) { @@ -36,11 +41,15 @@ textBox2.Focus(); return; } - using (MediaPortal.Profile.Settings xmlwriter = new MediaPortal.Profile.Settings(Config.GetFile(Config.Dir.Config, "MyClickMania.xml"))) +/* using (MediaPortal.Profile.Settings xmlwriter = new MediaPortal.Profile.Settings(Config.GetFile(Config.Dir.Config, "MyClickMania.xml"))) { xmlwriter.SetValue("clickmania", "pluginName", textBox1.Text.ToString()); xmlwriter.SetValue("clickmania", "Title", textBox2.Text.ToString()); - } + } */ + + XmlConfig XmlConfig = new XmlConfig(); + XmlConfig.WriteXmlConfig("MyClickMania", "clickmania", "pluginName", textBox1.Text.ToString()); + XmlConfig.WriteXmlConfig("MyClickMania", "clickmania", "Title", textBox2.Text.ToString()); this.Close(); } } Modified: trunk/plugins/MyClickmania/GUIClickMania.csproj =================================================================== --- trunk/plugins/MyClickmania/GUIClickMania.csproj 2008-05-19 16:58:17 UTC (rev 1791) +++ trunk/plugins/MyClickmania/GUIClickMania.csproj 2008-05-19 17:06:10 UTC (rev 1792) @@ -134,6 +134,7 @@ <Compile Include="myClickMania.cs"> <SubType>Code</SubType> </Compile> + <Compile Include="XmlConfig.cs" /> </ItemGroup> <ItemGroup> <EmbeddedResource Include="ClickManiaSetup.resx"> Added: trunk/plugins/MyClickmania/XmlConfig.cs =================================================================== --- trunk/plugins/MyClickmania/XmlConfig.cs (rev 0) +++ trunk/plugins/MyClickmania/XmlConfig.cs 2008-05-19 17:06:10 UTC (rev 1792) @@ -0,0 +1,320 @@ +#region Banneer +/* + * Copyright (C) 2008 framug + * + * This Program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This Program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ +#endregion + +#region HowToUse +/* + * Purpose of this class is to manage a xml config file. + * The generated file is MediaPortal xml config file compatible. + * It has been written in case of you have your own plugin save button or, you don't want + * to use MediaPortal.Configuration and MediaPortal.Profile in your plugin classes. + * ie : using (Settings ReadOrWriteXml = new Settings(Config.GetFile(Config.Dir.Config, PluginName()))) + * { ReadOrWriteXml = ... } + * + * How to use : + * - Add this class to your project. + * - Change namespace of this class with your own plugin name. + * + * - Call methods in your own plugin this way for writing : + * XmlConfig XmlConfig = new XmlConfig(); + * XmlConfig.WriteXmlConfig(StringFileName, StringSectionName, StringEntryName, StringValueName); + * XmlConfig.WriteXmlConfig(StringFileName, StringSectionName, StringEntryName, BoolValueName); + * XmlConfig.WriteXmlConfig(StringFileName, StringSectionName, StringEntryName, IntValueName); + * + * - Call methods in your own plugin this way for reading : + * XmlConfig XmlConfig = new XmlConfig(); + * YourString = XmlConfig.ReadXmlConfig(StringFileName, StringSectionName, StringEntryName, StringValueDefaultName); + * YourBool = XmlConfig.ReadXmlConfig(StringFileName, StringSectionName, StringEntryName, BoolValueDefaultName); + * YourInt = XmlConfig.ReadXmlConfig(StringFileName, StringSectionName, StringEntryName, IntValueDefaultName); + * + * - Call methods in your own plugin this way for removing entry : + * XmlConfig XmlConfig = new XmlConfig(); + * XmlConfig.RemoveEntry(StringFileName, StringSectionName, StringEntryName); + * + */ +#endregion + +#region using +using System; +using System.IO; // For I/O file +using System.Collections.Generic; +using System.Text; +using System.Xml; // For XmlDocument +using MediaPortal.Configuration; // For recover install MediaPortal path +#endregion + +namespace MyClickMania +{ + class XmlConfig + { + +#region <<DECLARATION>> + XmlDocument configxml = new XmlDocument(); +#endregion + +#region <<public>> + + // Recover install MediaPortal path + public string PathInstalMP() + { + string path = Config.GetFolder(Config.Dir.Config); + return path; + } + + // Build entire filename of config file + public string EntireFilenameConfig(string FileName) + { + string entirefilename = PathInstalMP() + @"\" + FileName + ".xml"; + return entirefilename; + } + + // Called with bool type + public void WriteXmlConfig(string FileName, string Section, string Entry, bool Value) + { + string value = ""; + // Change true by "yes" and false by "no" for xml MediaPortal compatibility + if (Value) + { + value = "yes"; + } + else + { + value = "no"; + } + + WriteXmlConfig(FileName, Section, Entry, value); + } + + // Called with decimal type + public void WriteXmlConfig(string FileName, string Section, string Entry, decimal Value) + { + string value = Value.ToString(); + + WriteXmlConfig(FileName, Section, Entry, value); + } + + // Write a config file with XmlDocument + public void WriteXmlConfig(string FileName, string Section, string Entry, string Value) + { + // Create file if doesn't exist + if (!File.Exists(EntireFilenameConfig(FileName))) + { + CreateXmlConfig(FileName); + } + + //Open xml document + configxml.Load(EntireFilenameConfig(FileName)); + //Recover profile node + XmlElement profile = configxml.DocumentElement; + //Create section if doesn't exist + String XPath = @"/profile/section[@name='" + Section + "']"; + XmlNodeList ListSection = configxml.SelectNodes(XPath); + if (ListSection.Count < 1) + { + CreateSection(Section); + } + //Posit on section node + XmlNode section = profile.SelectSingleNode("section[@name='" + Section + "']"); + + //Create Entry if doesn't exist + XPath = @"/profile/section[@name='" + Section + "']/entry[@name='" + Entry + "']"; + XmlNodeList ListEntry = configxml.SelectNodes(XPath); + if (ListEntry.Count < 1) + { + CreateEntry(Section, Entry); + } + //Posit on entry node + XmlNode entry = section.SelectSingleNode("entry[@name='" + Entry + "']"); + + //Store entry value + entry.InnerText = Value; + + //Save xml config file + configxml.Save(EntireFilenameConfig(FileName)); + } + + // Remove an Entry + public void RemoveEntry(string FileName, string Section, string Entry) + { + // Return if xml file doesn't exist + if (!File.Exists(EntireFilenameConfig(FileName))) + { + return; + } + + //Open xml document + configxml.Load(EntireFilenameConfig(FileName)); + //Recover profile node + XmlElement profile = configxml.DocumentElement; + + //Posit on value + String XPath = @"/profile/section[@name='" + Section + "']/entry[@name='" + Entry + "']"; + XmlNodeList ListEntry = configxml.SelectNodes(XPath); + + // If value exist, remove it otherwise, return + if (ListEntry.Count > 0) + { + //Posit on section node + XmlNode section = profile.SelectSingleNode("section[@name='" + Section + "']"); + //Posit on entry node + XmlNode entry = section.SelectSingleNode("entry[@name='" + Entry + "']"); + //Remove the entry node for section + section.RemoveChild(entry); + //Save xml config file + configxml.Save(EntireFilenameConfig(FileName)); + } + return; + } + + // Called with bool type + public bool ReadXmlConfig(string FileName, string Section, string Entry, bool Value) + { + // Change true by "yes" and false by "no" for xml MediaPortal compatibility + string value = Value.ToString(); + if (Value) + { + value = "yes"; + } + else + { + value = "no"; + } + + string result = ReadXmlConfig(FileName, Section, Entry, value); + + // Change "yes" by true and "no" by false for xml MediaPortal compatibility + if (result == "yes") + { + Value = true; + } + else + { + Value = false; + } + + return Value; + } + + // Called with int type + public int ReadXmlConfig(string FileName, string Section, string Entry, int Value) + { + string value = Value.ToString(); + + string result = ReadXmlConfig(FileName, Section, Entry, value); + + Value = Convert.ToInt32(result); + + return Value; + } + + // Read xml config file with XmlDocument + public string ReadXmlConfig(string FileName, string Section, string Entry, string Value) + { + // Default value if xml file doesn't exist + if (!File.Exists(EntireFilenameConfig(FileName))) + { + return Value; + } + + //Open xml document + configxml.Load(EntireFilenameConfig(FileName)); + //Recover profile node + XmlElement profile = configxml.DocumentElement; + + //Posit on value + String XPath = @"/profile/section[@name='" + Section + "']/entry[@name='" + Entry + "']"; + XmlNodeList ListEntry = configxml.SelectNodes(XPath); + + // If value exist, return it otherwise, return default value + if (ListEntry.Count > 0) + { + //Posit on section node + XmlNode section = profile.SelectSingleNode("section[@name='" + Section + "']"); + //Posit on entry node + XmlNode entry = section.SelectSingleNode("entry[@name='" + Entry + "']"); + //Recover value with entry data + Value = entry.InnerText; + } + + return Value; + } + +#endregion + +#region <<private>> + + // Create xml config file with profile node + private void CreateXmlConfig(string FileName) + { + XmlDocument configxml = new XmlDocument(); + //Declaration of XML document type (utf-8, same as MediaPortal) + XmlDeclaration declaration = configxml.CreateXmlDeclaration("1.0", "utf-8", ""); + //Add declaration to document + configxml.AppendChild(declaration); + //Create profile node + XmlNode profile = configxml.CreateNode(System.Xml.XmlNodeType.Element, "profile", ""); + //Add node to document + configxml.AppendChild(profile); + + //Save xml config file + configxml.Save(EntireFilenameConfig(FileName)); + } + + // create section node + private void CreateSection(string Section) + { + //Recover profile node + XmlElement profile = configxml.DocumentElement; + //Create new section node + XmlNode section = configxml.CreateElement("section"); + //Add "name" attribute to section node + XmlAttribute name = configxml.CreateAttribute("name"); + //value is section name + name.Value = Section; + //Add value to section + section.Attributes.Append(name); + //Add section to document + profile.AppendChild(section); + } + + // create entry node + private void CreateEntry(string Section, string Entry) + { + //Recover profile node + XmlElement profile = configxml.DocumentElement; + //Posit on section node + XmlNode section = profile.SelectSingleNode("section[@name='" + Section + "']"); + //Create new node for entry + XmlNode entry = configxml.CreateElement("entry"); + //Add "name" attribute to entry node + XmlAttribute name = configxml.CreateAttribute("name"); + //value is entry name + name.Value = Entry; + //Add value to entry + entry.Attributes.Append(name); + //Add entry to document + section.AppendChild(entry); + } + +#endregion + + } // end of class +} // end of namespace This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |