using System; using System.Xml; using System.IO; using System.Xml.XPath; using System.Xml.Schema; using System.Collections.Generic; //using System; //using System; namespace AmicEditor { public class Theme { /* * Conté informació sobre un tema concret * * */ public string name {get; set;} public string description {get; set;} //public string folder {get; set;} //public Theme (string _name, string _desc, string _folder) public Theme (string _name, string _desc) { /* * Constructor: Crea un tema a partir del nom, la descripció i la carpeta indicades * * */ this.name=_name; this.description=_desc; //this.folder=_folder; } } public class Themes { /* * Conté la llista de temes en què es poden representar els pictogrames * * */ //string dir_themes; public List themes {get;set;} public List getThemes(){ /* * Retorna la llista de temes * * */ return this.themes; } public Themes () { /* * Constructor: Crea la llista de temes a partir del directori corresponent * */ this.ReadThemes(); } public void ReadThemes(string filename){ /* * Omple la llista de temes a partir de l'arxiu indicat en filename * * */ themes=new List(); string name, desc; if(XmlUtils.ValidaXSD(filename,Preferences.dir_themes+"/xsd/themes.xsd")) { XmlDocument xDoc = new XmlDocument(); xDoc.Load(filename); XPathNavigator ThemeNav = xDoc.CreateNavigator(); XPathNodeIterator ThemeIter = ThemeNav.Select("themes/theme"); foreach (XPathNavigator nodeTheme in ThemeIter) { // Llegim el nom XPathNodeIterator nodeTh = nodeTheme.Select("name"); nodeTh.MoveNext(); Console.WriteLine(" Theme Name: "+nodeTh.Current.Value); name=nodeTh.Current.Value; // Llegim la descripcio nodeTh = nodeTheme.Select("description"); nodeTh.MoveNext(); Console.WriteLine(" Theme Desc: "+nodeTh.Current.Value); desc=nodeTh.Current.Value; // Llegim el folder /*nodeTh = nodeTheme.Select("folder"); nodeTh.MoveNext(); Console.WriteLine(" Theme Folde: "+nodeTh.Current.Value); fol=nodeTh.Current.Value; */ //themes.Add(new Theme(name, desc, fol)); themes.Add(new Theme(name, desc)); } } } public void ReadThemes(){ /* * Omple la llista de temes a partir de l'arxiu indicat en dir_themes * * */ /* themes=new List(); //string name, desc, fol; string name, desc; if(XmlUtils.ValidaXSD(Preferences.dir_themes+"/themes.xml",Preferences.dir_themes+"/xsd/themes.xsd")) { XmlDocument xDoc = new XmlDocument(); xDoc.Load(Preferences.dir_themes+"/themes.xml"); XPathNavigator ThemeNav = xDoc.CreateNavigator(); XPathNodeIterator ThemeIter = ThemeNav.Select("themes/theme"); foreach (XPathNavigator nodeTheme in ThemeIter) { // Llegim el nom XPathNodeIterator nodeTh = nodeTheme.Select("name"); nodeTh.MoveNext(); Console.WriteLine(" Theme Name: "+nodeTh.Current.Value); name=nodeTh.Current.Value; // Llegim la descripcio nodeTh = nodeTheme.Select("description"); nodeTh.MoveNext(); Console.WriteLine(" Theme Desc: "+nodeTh.Current.Value); desc=nodeTh.Current.Value; // Llegim el folder /*nodeTh = nodeTheme.Select("folder"); nodeTh.MoveNext(); Console.WriteLine(" Theme Folde: "+nodeTh.Current.Value); fol=nodeTh.Current.Value; *n/ //themes.Add(new Theme(name, desc, fol)); themes.Add(new Theme(name, desc)); } } */ ReadThemes(Preferences.dir_themes+"/themes.xml"); } // Fi mètode ReadThemes public bool Add(Theme tema){ themes.Add(tema); return this.Save(); }// Fi del mètode Add public bool Save(){ /* * Guarda l'arxiu xml corresponent al tema * */ FileStream fs=null; StreamWriter sw=null; try{ //Console.WriteLine("Saving:"+Preferences.dir_themes+"/themes.xml"); // I ara escrivim l'xml... // Capçalera if(File.Exists(Preferences.dir_themes+"/themes.xml")) File.Delete(Preferences.dir_themes+"/themes.xml"); fs = new FileStream(Preferences.dir_themes+"/themes.xml", FileMode.OpenOrCreate, FileAccess.Write); sw=new StreamWriter(fs); sw.WriteLine(""); sw.WriteLine(""); foreach(Theme Tem in themes){ sw.WriteLine("\t"); sw.WriteLine("\t\t"+Tem.name+""); sw.WriteLine("\t\t"+Tem.description+""); sw.WriteLine("\t"); } sw.WriteLine(""); sw.Close(); return true; } catch (Exception exc){ Console.WriteLine("Excepcio escrivint: "+exc); if (sw!=null) sw.Close(); return false; } } public bool Exists(string ThemeName){ foreach(Theme t in themes) if (t.name==ThemeName) return true; return false; } } }