using System; using System.Xml; using System.IO; using System.Xml.XPath; using System.Xml.Schema; using System.Collections.Generic; using Gtk; namespace AmicEditor { public class Cathegory { /* * Representa una categoria d'imatges, amb la llista de pictogrames que conté * * */ public string cathegoryname {get;set;} public int numpicts{get;set;} public string dir_cathegory {get;set;} public string file{get;set;} public Gdk.Color BackgroundColor {get;set;} public Gdk.Color DefaultFontColor {get; set;} public string DefaultFont {get;set;} public List pictograms {get;set;} public Cathegory () { /* * Constructor genèric * */ this.pictograms=new List(); this.numpicts=0; } public Cathegory (string name, Gdk.Color color, Gdk.Color CreatedCathegoryFontColor, string CreatedCathegoryFont) //string CreatedCathegoryFont, string path) { /* * Constructor quan li donem a nova categoria. * */ string nameToSave=""; string catname; string [] nom = name.Split(new Char [] {'.'}); if(nom.Length>0&&nom[nom.Length-1]=="xml") // Detectem si acaba amb .xml { catname=name.Substring(0,name.Length-4); // Si acaba amb .xml eliminem l'extensió nameToSave=name; // I l'arxiu a guardar sí que conté .xml } else { catname=name; // Si no han posat .xml, guardem la categoria amb name nameToSave=name+".xml"; // I guardem amb .xml } //Console.WriteLine("Crea nova categoria..."+catname+" a l'arxiu "+nameToSave); this.cathegoryname=catname; this.BackgroundColor=color; this.file=nameToSave; this.dir_cathegory=Preferences.dir_gallery; this.pictograms=new List(); this.DefaultFont=CreatedCathegoryFont; this.DefaultFontColor=CreatedCathegoryFontColor; this.numpicts=0; this.Save(); } public string TansformColor(string gtkcolor){ /* * Torna un string tipus #rrggbb a partir d'un gtkcolor * * */ string [] color = gtkcolor.Split(new Char [] {':'}); if (color.Length<1) return "#000000"; else{ string [] components=color[1].Split(new Char [] {'/'}); if (components.Length!=3) return "#000000"; else{ string r,g,b; if (components[0].Length>2) r=components[0].Substring(0,2); else r="0"+components[0].Substring(0,1); if (components[1].Length>2) g=components[1].Substring(0,2); else g="0"+components[1].Substring(0,1); if (components[2].Length>2) b=components[2].Substring(0,2); else b="0"+components[2].Substring(0,1); return "#"+r+g+b; } } } public void Add(string name, string file, string path){ /* * Funció que omple la informació d'una categoria donada per name, * a partir de l'arxiu file ubicat al path indicat. * * */ this.cathegoryname=name; this.dir_cathegory=path; this.file=file; this.LoadPictograms(file); } public void Del(string _pictname){ /* * Elimina un pictograma donat el nom * */ Pictogram to_delete=null; foreach(Pictogram pic in pictograms) if (pic.pictname==_pictname) to_delete=pic; if(to_delete!=null){ this.numpicts--; pictograms.Remove(to_delete); } } public void AddPictogram(Pictogram pic){ /* * Afig un pictograma donat a la llista de pictogrames * * */ this.pictograms.Add(pic); this.numpicts++; } public bool ExistsPicWithName(string name){ /* * Torna cert si ja existeix un pictograma amb el mateix nom a la categoria * */ bool ret=false; foreach(Pictogram pic in pictograms) { if (pic.pictname==name) return true; } return ret; } public List GetPictograms(){ /* * Retorna una llista d'elements de tipus pictograma * */ //Console.WriteLine("GetPictograms..."+this.cathegoryname+"-"+ this.pictograms.Count); return this.pictograms; } public Pictogram GetPictogram(string name){ /* * Retorna el pictograma donat el nom, o null en cas que no existisca * */ foreach(Pictogram pic in pictograms) if(pic.pictname==name) return pic; return null; } public List LoadPictograms(string FullName){ /* * Carrega la llista de Pictogrames a partir de l'arxiu XML indicat per FullName * * */ Console.WriteLine("PICTOGRAMES DE: "+FullName); string sel_cathegory; try{ XmlDocument Pictograms=new XmlDocument(); Pictograms=LoadXML(FullName, this.dir_cathegory+"/xsd/cathegory.xsd"); XPathNavigator PictNav = Pictograms.CreateNavigator(); // Capturem el color de fons per defecte Gdk.Color backcol = new Gdk.Color(); XPathNodeIterator CatColor = PictNav.Select("cathegory/defaultColor"); CatColor.MoveNext(); Gdk.Color.Parse(CatColor.Current.Value, ref backcol); this.BackgroundColor=backcol; Console.Write("Background: "+this.BackgroundColor); // Capturem el color de la lletra per defecte Gdk.Color fontcol = new Gdk.Color(); XPathNodeIterator CatFontColor = PictNav.Select("cathegory/defaultFontColor"); CatFontColor.MoveNext(); Gdk.Color.Parse(CatFontColor.Current.Value, ref fontcol); this.DefaultFontColor=fontcol; Console.Write(" - Color: "+this.DefaultFontColor); // Capturem la font per defecte XPathNodeIterator CatFont = PictNav.Select("cathegory/defaultFont"); CatFont.MoveNext(); this.DefaultFont=CatFont.Current.Value; Console.Write("Font : "+this.DefaultFont); // Llegim la categoria XPathNodeIterator CatName = PictNav.Select("cathegory/cathegoryname"); CatName.MoveNext(); sel_cathegory=CatName.Current.Value; this.cathegoryname=sel_cathegory; ///////// Console.Write("Catname: "+this.cathegoryname); XPathNodeIterator iterPicts = PictNav.Select("cathegory/pictograms/pictogram"); foreach (XPathNavigator nodePict in iterPicts) { this.numpicts++; Pictogram pictograma=new Pictogram(); // Llegim el nom XPathNodeIterator nodePicts = nodePict.Select("pictname"); nodePicts.MoveNext(); pictograma.pictname=nodePicts.Current.Value; Console.WriteLine(""); Console.Write(" Pictname: "+pictograma.pictname); // Llegim el background XPathNodeIterator nodePicts2 = nodePict.Select("background"); nodePicts2.MoveNext(); //pictograma.background=nodePicts2.Current.Value; Gdk.Color back = new Gdk.Color(); Gdk.Color.Parse(nodePicts2.Current.Value, ref back); pictograma.background=back; Console.Write(" - Background: "+pictograma.background); // Agafem les imatges XPathNodeIterator nodeImages = nodePict.Select("images/image"); int i=0; foreach (XPathNavigator nodeImage in nodeImages) { Console.WriteLine(i); PictImage img=new PictImage(); Console.WriteLine(""); Console.Write(" Images: "); XPathNodeIterator Themes = nodeImage.Select("theme"); Themes.MoveNext(); img.theme=Themes.Current.Value; Console.Write(" - theme: {0}", img.theme); XPathNodeIterator Sources = nodeImage.Select("source"); Sources.MoveNext(); img.source=Sources.Current.Value; Console.Write(" -> Source: "+img.source); try{ pictograma.Add(img); i++; //pictograma.images.Add(img); }catch (Exception e) {Console.WriteLine(e);} } // Agafem la resta d'atributs: Font XPathNodeIterator nodePictsFont = nodePict.Select("caption"); nodePictsFont.MoveNext(); pictograma.caption=nodePictsFont.Current.Value; pictograma.fontface=nodePictsFont.Current.GetAttribute("fontface", String.Empty); pictograma.fontsize=nodePictsFont.Current.GetAttribute("size", String.Empty); pictograma.typeface=nodePictsFont.Current.GetAttribute("typeface", String.Empty); //pictograma.fontcolor=nodePictsFont.Current.GetAttribute("color", String.Empty); Gdk.Color col = new Gdk.Color(); Gdk.Color.Parse(nodePictsFont.Current.GetAttribute("color", String.Empty), ref col); pictograma.fontcolor=col; Console.WriteLine(""); Console.Write(" Caption: "+pictograma.caption); Console.Write(" - Fontface: "+pictograma.fontface); Console.Write(" - size: "+pictograma.fontsize); Console.Write(" - typeface: "+pictograma.typeface); Console.Write(" - Color: "+pictograma.fontcolor); // Accions XPathNodeIterator nodePictsActionSpeech = nodePict.Select("action/speech"); nodePictsActionSpeech.MoveNext(); pictograma.actionSpeech=nodePictsActionSpeech.Current.Value; Console.WriteLine(""); Console.Write(" Action Speech: "+pictograma.actionSpeech); XPathNodeIterator nodePictsActionSound = nodePict.Select("action/sound"); nodePictsActionSound.MoveNext(); pictograma.actionSound=nodePictsActionSound.Current.Value; Console.Write(" Action Sound: "+pictograma.actionSound); pictograms.Add(pictograma); } // foreach xpath... iterpicts return pictograms; }catch (Exception e){ Console.WriteLine("******************** "+e); return null; } } public XmlDocument LoadXML(string file, string xsd){ /** * Retorna el document XML corresponent a File, validat amb l'xsd indicat * */ XmlDocument xDoc = new XmlDocument(); if (XmlUtils.ValidaXSD(file, xsd)) { xDoc.Load(file); return xDoc; } else { return null; }; } public XmlDocument LoadPictogramsFrom(string Path){ /* * Torna un XMLDocument amb els pictogrames indicats al path * * */ try{ XmlDocument Pictograms=new XmlDocument(); //System.Console.WriteLine("PATH VAL... "+Path); Pictograms=LoadXML(Path, this.dir_cathegory+"/xsd/cathegory.xsd"); return Pictograms; }catch{ return null; } } public bool Contains(string picname) { // Comprova no es repeteix el nom del pictograma donat //int repeat=0; // Indica el nombre de repeticions foreach(Pictogram pic in pictograms){ Console.WriteLine("provant "+picname+" amb... "+pic.pictname); if(picname==pic.pictname) //{ //repeat++; //Console.WriteLine("REPETAT "+repeat); //if (repeat>1) return true; //} } return false; } public bool Save(){ /* * Guarda l'arxiu xml corresponent a la categoria * */ FileStream fs=null; StreamWriter sw=null; try{ //Console.WriteLine("Saving:"+dir_cathegory+"/"+cathegoryname); Console.WriteLine("Saving:"+Preferences.dir_gallery+"/"+cathegoryname+".xml"); // I ara escrivim l'xml... // Capçalera if(File.Exists(Preferences.dir_gallery+"/"+cathegoryname+".xml")) File.Delete(Preferences.dir_gallery+"/"+cathegoryname+".xml"); fs = new FileStream(Preferences.dir_gallery+"/"+cathegoryname+".xml", FileMode.OpenOrCreate, FileAccess.Write); sw=new StreamWriter(fs); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""+cathegoryname+""); sw.WriteLine(""+TansformColor(BackgroundColor.ToString())+""); sw.WriteLine(""+this.DefaultFont+""); sw.WriteLine(""+TansformColor(this.DefaultFontColor.ToString())+""); sw.WriteLine(""); foreach(Pictogram pic in pictograms){ sw.WriteLine("\t"); sw.WriteLine("\t"+pic.pictname+""); sw.WriteLine("\t"+TansformColor(pic.background.ToString())+""); sw.WriteLine("\t"); // LlistaAfegits.RemoveRange(0, LlistaAfegits.Count); // PATCH foreach(PictImage img in pic.images){ // PATCH //if(!Conte(LlistaAfegits, img)) // Només els imprimim si no està. // END PATCH //{ //Console.WriteLine("NUMPICS:"+pic.images.Count); //Console.WriteLine("Length:"+LlistaAfegits.Count); sw.WriteLine("\t\t"); sw.WriteLine("\t\t\t"+img.theme+""); sw.WriteLine("\t\t\t"+img.source+""); sw.WriteLine("\t\t"); // LlistaAfegits.Add(img); //PATCH //} // PATCH } sw.WriteLine("\t"); sw.WriteLine("\t"+ pic.caption+""); sw.WriteLine("\t"); sw.WriteLine("\t\t"+pic.actionSpeech+""); sw.WriteLine("\t\t"+pic.actionSound+""); sw.WriteLine("\t"); sw.WriteLine("\t"); } sw.WriteLine(""); sw.WriteLine(""); sw.Close(); return true; } catch (Exception exc){ Console.WriteLine("Excepcio escrivint: "+exc); if (sw!=null) sw.Close(); return false; } } public void PrintMe(){ /* * Mostra l'arxiu xml corresponent a la categoria * */ Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(""+cathegoryname+""); Console.WriteLine(""+TansformColor(BackgroundColor.ToString())+""); Console.WriteLine(""+this.DefaultFont+""); Console.WriteLine(""+TansformColor(this.DefaultFontColor.ToString())+""); Console.WriteLine(""); foreach(Pictogram pic in pictograms){ Console.WriteLine("\t"); Console.WriteLine("\t"+pic.pictname+""); Console.WriteLine("\t"+TansformColor(pic.background.ToString())+""); Console.WriteLine("\t"); foreach(PictImage img in pic.images){ Console.WriteLine("\t\t"); Console.WriteLine("\t\t\t"+img.theme+""); Console.WriteLine("\t\t\t"+img.source+""); Console.WriteLine("\t\t"); } Console.WriteLine("\t"); Console.WriteLine("\t"+ pic.caption+""); Console.WriteLine("\t"); Console.WriteLine("\t\t"+pic.actionSpeech+""); Console.WriteLine("\t\t"+pic.actionSound+""); Console.WriteLine("\t"); Console.WriteLine("\t"); } Console.WriteLine(""); Console.WriteLine(""); } /*public bool Conte(List LlistaAfegits, PictImage _img){ // PATCH PER COMPROVAR EL DE DALT... foreach(PictImage img in LlistaAfegits) if (img.theme==_img.theme) return true; return false; }*/ } }