using System; using System.Collections.Generic; using System.IO; using Gtk; namespace AmicEditor { public static class OOConverter { /* public static int width; public static int height; static int border_Width; static int border_Height; static bool useBorder; static bool useCaption; static string GalName; */ public static int OOConvert(List pictogrames, string GalName, int width, int height, bool useBorder, int border_Width, int border_Height, bool useCaption, bool Convert2OO) { /*this.width=width; this.height=height; this.border_Width=border_Width; this.border_Height=border_Height; this.useBorder=useBorder; this.useCaption=useCaption; this.GalName=GalName;*/ // Passa una llista de pictogrames a imatges SVG per exportar-les a // una galeria d'OpenOffice. // 1. Creem els svgs... CreateSVG(pictogrames, GalName, width, height, useBorder, border_Width, border_Height, useCaption); // 2. Importació a Open Office (només si Convert2OO és cert...) if(Convert2OO){ // Comprovem que existisca l'arxiu conversor if (File.Exists("/usr/lib/openoffice/basis3.2/program/gengal")){ string HomeDir=Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); string path=HomeDir+"/.openoffice.org/3/user/gallery"; string filesPath=Preferences.dir_OO+"/"+GalName; Console.WriteLine(path); string command="/usr/lib/openoffice/basis3.2/program/gengal"; string args="--name "+GalName+" --path "+path+"/ "+filesPath+"/*"; Console.WriteLine(command+" "+args); //System.Threading.ThreadStart tstart=delegate{ // lliurex.utils.Commands.run("/usr/lib/openoffice/basis3.2/program/gengal", args); System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName ="bash"; proc.StartInfo.Arguments = "-c '"+command+" "+args+"'"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.Start(); //Work on gallery 'file:///var/lib/lliurex-amic/ImageGallery/Custom/Gallery4OO/'"); // }; //thread=new System.Threading.Thread(tstart); //thread.Start(); ///usr/lib/openoffice/basis3.2/program$ ./gengal --name AmicOO --path /var/lib/lliurex-amic/ImageGallery/Custom/Gallery4OO/ --number-from 4321 /var/lib/lliurex-amic/ImageGallery/Custom/Gallery4OO/* //Work on gallery 'file:///var/lib/lliurex-amic/ImageGallery/Custom/Gallery4OO/' MessageDialog md = new MessageDialog (null, DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Close, Mono.Unix.Catalog.GetString("Conversion has been done. You have a new Gallery in your OO. A new svg gallery has been created too.")); md.Run(); md.Destroy(); } else { MessageDialog md = new MessageDialog (null, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Close, Mono.Unix.Catalog.GetString("Couldn't find OpenOffice 3.2 files required for the conversion (maybe installing openoffice.org-dev it will run). You can use svg gallery created.")); md.Run(); md.Destroy(); } }// if convertOO else { MessageDialog md = new MessageDialog (null, DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Close, Mono.Unix.Catalog.GetString("A new svg gallery has been created.")); md.Run(); md.Destroy(); } return 0; } public static int CreateSVG(List pictogrames, string GalName, int width, int height, bool useBorder, int border_Width, int border_Height, bool useCaption) { // Passa la llista de pictogrames a arxius SVG foreach (Pictogram pic in pictogrames) { // TEMA DEFAULT: // TODO string pictimg=pic.getImageFromTheme("Default"); Console.WriteLine(pictimg); //MyImg=new Image(Preferences.dir_img+"/"+pictimg); string fileName=Preferences.dir_img+"/"+pictimg; string base64; FileStream fs=null; StreamWriter sw=null; // Grandària del svg //int height=300; //int width=300; //int border_Height=20; //int border_Width=20; // Apertura de l'arxiu XML Directory.CreateDirectory(Preferences.dir_OO+"/"+GalName); //Directory.CreateDirectory("/tmp/tralari"); string fileName2Save=pictimg.Replace("/","-"); fileName2Save=fileName2Save.Substring(0, fileName2Save.LastIndexOf(".")); fileName2Save=fileName2Save+".svg"; Console.WriteLine("SAVING: "+Preferences.dir_OO+"/"+GalName+"/"+fileName2Save); fs = new FileStream(Preferences.dir_OO+"/"+GalName+"/"+fileName2Save, FileMode.OpenOrCreate, FileAccess.Write); sw=new StreamWriter(fs); // Impressió de la capçalera XML sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine("image/svg+xml"); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); // Escriptura del rectangle exterior // Necessitem el color de fons if(useBorder){ string color=TransformColor(pic.background.ToString()); sw.WriteLine(""); } /********************************/ // Escriptura de la imatge (PNG!) /********************************/ base64=ImageToBase64(fileName); int lines=(int)(base64.Length/64); int extra=base64.Length%64; // Capçalera if(useBorder){ sw.WriteLine(""); sw.Flush(); // Escriptura del text int len=((pic.caption.Length)*int.Parse(pic.fontsize)); if(useCaption){ sw.WriteLine(""); sw.Flush(); sw.WriteLine(""+pic.caption+""); sw.Flush(); sw.WriteLine(""); } sw.Flush(); // Fi de l'arxiu sw.WriteLine(""); sw.Flush(); sw.Close(); } return 0; } public static string ImageToBase64(string fileName){ // Converteix la imatges especificada en fileName en una cadena // codificada en base 64, per guardar-la en arxiu xml. byte[] buff = null; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); long numBytes = new FileInfo(fileName).Length; buff = br.ReadBytes((int)numBytes); var stream = new MemoryStream(); stream.Write(buff, 0, (int)numBytes); //System.Drawing.Bitmap img=new System.Drawing.Bitmap(stream); //Console.WriteLine(Convert.ToBase64String(stream.ToArray())); fs.Close(); br.Close(); return Convert.ToBase64String(stream.ToArray()); } public static string TransformColor(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; } } } } }