using Gtk; using System; using System.IO; using System.Collections.Generic; using ICSharpCode.SharpZipLib; namespace AmicEditor { public static class Archiver { //public static Archiver () //{} public static bool CreateArchive(List FileList, List CatList, string filename){ bool using_default_audio_gallery=false; // Per indicar que cal instal·lar la galeria per defecte bool using_default_image_gallery=false; Console.WriteLine("Saving..."); try{ // Creem els directoris temporals... if (Directory.Exists("/tmp/lliurex-amic")) Directory.Delete("/tmp/lliurex-amic", true); // true -> Eliminat recursiu... Directory.CreateDirectory("/tmp/lliurex-amic"); Directory.CreateDirectory("/tmp/lliurex-amic/themes"); // Per guardar els temes... Directory.CreateDirectory("/tmp/lliurex-amic/themes/xsd"); // Per guardar els temes... Directory.CreateDirectory("/tmp/lliurex-amic/galleries"); // Per guardar les galeries... Directory.CreateDirectory("/tmp/lliurex-amic/images"); // Per guardar les imatges que no estan a la galeria Directory.CreateDirectory("/tmp/lliurex-amic/audio"); // Per guardar l'audio que no és a la llibreria // Anem copiant-ho tot... string filename_no_path=""; string [] parts; // Copiem l'arxiu de temes... File.Copy(Preferences.dir_themes+"/themes.xml" , "/tmp/lliurex-amic/themes/themes.xml"); File.Copy(Preferences.dir_themes+"/xsd/themes.xsd" , "/tmp/lliurex-amic/themes/xsd/themes.xsd"); foreach(string file in FileList){ // Obtenim el nom e l'arxiu sense la ruta parts=file.Split(new Char [] {'/'}); filename_no_path=parts[parts.Length-1]; // I el gravem File.Copy(file, "/tmp/lliurex-amic/galleries/"+filename_no_path); Console.WriteLine("Copying... "+file); }// I rastregem les imatges de les categories, per vore si es troben al dir. custom o no... Gallery g=new Gallery(); Cathegory MyCat=new Cathegory(); foreach(string cat in CatList){ MyCat=g.GetPictogramsFrom(cat); foreach (Pictogram pic in MyCat.GetPictograms()){ foreach(PictImage pictimg in pic.images){ Console.WriteLine(pictimg.theme+" - "+ pictimg.source); // Cal comprovar també els temes que apareixen!!!!!!!!!!! // I incloure'ls en un themes.xml nou, per integrar-lo amb el themes de l'ordinador destí... if (File.Exists(Preferences.dir_img_custom+"/"+pictimg.source)) { Console.WriteLine("Copying... "+Preferences.dir_img_custom+"/"+pictimg.source); File.Copy(Preferences.dir_img_custom+"/"+pictimg.source, "/tmp/lliurex-amic/images/"+pictimg.source); } else{ // L'arxiu no és a la galeria custom, per tant, està en default... if (pictimg.source!="") using_default_image_gallery=true; } // Mirem els arxius d'àudio... if (File.Exists(Preferences.dir_audio_custom+"/"+pic.actionSound)){ Console.WriteLine("Copying... "+Preferences.dir_audio_custom+"/"+pic.actionSound); File.Copy(Preferences.dir_audio_custom+"/"+pic.actionSound, "/tmp/lliurex-amic/audio/"+pic.actionSound); } else /// L'arxiu no és a la galeria custom, per tant, està en default... if (pic.actionSound!="") using_default_audio_gallery=true; } } } // PER EVITAR WARNINFS Console.WriteLine("Using default audio gallery: "+using_default_audio_gallery.ToString()); Console.WriteLine("Using default image gallery: "+using_default_image_gallery.ToString()); // Escrivim l'arxiu de metainformació FileStream fs = new FileStream("/tmp/lliurex-amic/meta", FileMode.Create, FileAccess.Write); StreamWriter sw=new StreamWriter(fs); sw.WriteLine("Lliurex Amic Gallery Zip Archive (.agz)"); sw.WriteLine("version 0.2"); if (using_default_image_gallery) sw.WriteLine("UsesDefaultImageGallery=yes"); else sw.WriteLine("UsesDefaultImageGallery=no"); if (using_default_audio_gallery) sw.WriteLine("UsesDefaultAudioGallery=yes"); else sw.WriteLine("UsesDefaultAudioGallery=no"); sw.Close(); // Finalment, comprimim l'arxiu System.IO.Directory.SetCurrentDirectory("/tmp/"); Stream stmout=new FileStream(filename, FileMode.Create); ICSharpCode.SharpZipLib.Tar.TarArchive TA=ICSharpCode.SharpZipLib.Tar.TarArchive.CreateOutputTarArchive(stmout); ICSharpCode.SharpZipLib.Tar.TarEntry TE=ICSharpCode.SharpZipLib.Tar.TarEntry.CreateEntryFromFile("lliurex-amic/"); TA.WriteEntry(TE,true); TA.CloseArchive(); stmout.Close(); Console.WriteLine(" in "+filename); return true; }catch(Exception ex){ Console.WriteLine("Exception Saving! - "+ex.Message); return false; } //return false; } public static bool ImportArchive(string filename){ bool using_default_audio_gallery=false; // Per indicar que cal instal·lar la galeria per defecte bool using_default_image_gallery=false; // Per fer el recompte del que s'instal·la int NumNewImages=0; int NumNewAudios=0; int NumNewCathegories=0; int NumNewThemes=0; Console.WriteLine("Importing... "+ filename); try{ // Creem els directoris temporals... if (Directory.Exists("/tmp/lliurex-amic")) Directory.Delete("/tmp/lliurex-amic", true); // true -> Eliminat recursiu... //Directory.CreateDirectory("/tmp/lliurex-amic"); Stream stmin=new FileStream(filename, FileMode.Open); ICSharpCode.SharpZipLib.Tar.TarArchive TA; TA=ICSharpCode.SharpZipLib.Tar.TarArchive.CreateInputTarArchive(stmin); TA.ExtractContents("/tmp"); TA.CloseArchive(); stmin.Close(); // Extraiem la informació de meta... bool has_errors=false; string text_error=""; string linia; if(!File.Exists("/tmp/lliurex-amic/meta")){ has_errors=true; text_error=Mono.Unix.Catalog.GetString("Archive corrupt. Not found meta info.");; } else { // TODO: // Caldrà fer una comprovació més consistent... // - Existència dels directoris, temes, i una comprovació més acurada dels camps de meta. FileStream fs = new FileStream("/tmp/lliurex-amic/meta", FileMode.Open, FileAccess.Read); StreamReader sr=new StreamReader(fs); linia=sr.ReadLine(); // 1a línia if (linia!="Lliurex Amic Gallery Zip Archive (.agz)"){ has_errors=true; text_error=Mono.Unix.Catalog.GetString("Archive not seems a valid Amic Gallery Zip Archive."); } linia=sr.ReadLine(); // 2a línia if (linia!="version 0.2"){ has_errors=true; text_error=Mono.Unix.Catalog.GetString("Archive version not valid."); } linia=sr.ReadLine(); // 3a línia if((linia.Split(new Char [] {'='})[1])=="yes") using_default_image_gallery=true; linia=sr.ReadLine(); if((linia.Split(new Char [] {'='})[1])=="yes") using_default_audio_gallery=true; sr.Close(); } // PER EVITAR WARNINGS Console.WriteLine("Using default audio gallery: "+using_default_audio_gallery.ToString()); Console.WriteLine("Using default image gallery: "+using_default_image_gallery.ToString()); if(has_errors){ MessageDialog md=new MessageDialog(null, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "Error: "+text_error); md.Run(); md.Destroy(); return false; } // Una vegada descomprimit... // 1. Copiem les imatges i l'audio foreach(string file in Directory.GetFiles("/tmp/lliurex-amic/images")){ string [] parts=file.Split(new Char [] {'/'}); Console.WriteLine("Extract... "+parts[parts.Length-1]+ " to "+Preferences.dir_img_custom+"/"+parts[parts.Length-1]); // Comprovem abans si existeix l'arxiu if (File.Exists(Preferences.dir_img_custom+"/"+parts[parts.Length-1])){ // Es el mateix? //FileInfo FileInfo f1=new FileInfo(file); FileInfo f2=new FileInfo(Preferences.dir_img_custom+"/"+parts[parts.Length-1]); string message=Mono.Unix.Catalog.GetString("Overwrite ")+ parts[parts.Length-1]+ " ("+f2.Length+" bytes)" + Mono.Unix.Catalog.GetString("with ")+ parts[parts.Length-1]+ " ("+f1.Length+" bytes)?"; MessageDialog md=new MessageDialog(null, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo, message); ResponseType result = (ResponseType)md.Run(); md.Destroy(); if(result==ResponseType.Yes){ // Sobreescriptura File.Delete(Preferences.dir_img_custom+"/"+parts[parts.Length-1]); File.Copy(file, Preferences.dir_img_custom+"/"+parts[parts.Length-1]); NumNewImages++; // Incrementem el nº d'imatges copiades } } else // Si no existeix, simplement el copiem { File.Copy(file, Preferences.dir_img_custom+"/"+parts[parts.Length-1]); NumNewImages++; } } // Passem als arxius d'audio foreach(string file in Directory.GetFiles("/tmp/lliurex-amic/audio")){ string [] parts=file.Split(new Char [] {'/'}); Console.WriteLine("Extract... "+parts[parts.Length-1]+ " to "+Preferences.dir_audio_custom+"/"+parts[parts.Length-1]); // Comprovem abans si existeix l'arxiu if (File.Exists(Preferences.dir_audio_custom+"/"+parts[parts.Length-1])){ // Es el mateix? //FileInfo FileInfo f1=new FileInfo(file); FileInfo f2=new FileInfo(Preferences.dir_audio_custom+"/"+parts[parts.Length-1]); string message=Mono.Unix.Catalog.GetString("Overwrite ")+ parts[parts.Length-1]+ " ("+f2.Length+" bytes)" + Mono.Unix.Catalog.GetString("with ")+ parts[parts.Length-1]+ " ("+f1.Length+" bytes)?"; MessageDialog md=new MessageDialog(null, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo, message); ResponseType result = (ResponseType)md.Run(); md.Destroy(); if(result==ResponseType.Yes){ // Sobreescriptura File.Delete(Preferences.dir_audio_custom+"/"+parts[parts.Length-1]); File.Copy(file, Preferences.dir_audio_custom+"/"+parts[parts.Length-1]); NumNewAudios++; } } else {// Si no existeix, simplement el copiem File.Copy(file, Preferences.dir_audio_custom+"/"+parts[parts.Length-1]); NumNewAudios++; } } // 2. Copiem les categories foreach(string file in Directory.GetFiles("/tmp/lliurex-amic/galleries")){ string [] parts=file.Split(new Char [] {'/'}); Console.WriteLine("Extract... "+parts[parts.Length-1]+ " to "+Preferences.dir_gallery+"/"+parts[parts.Length-1]); if (File.Exists(Preferences.dir_gallery +"/"+parts[parts.Length-1])){ // Es el mateix? FileInfo f1=new FileInfo(file); FileInfo f2=new FileInfo(Preferences.dir_gallery+"/"+parts[parts.Length-1]); string message=Mono.Unix.Catalog.GetString("Overwrite ")+ parts[parts.Length-1]+ " ("+f2.Length+" bytes)" + Mono.Unix.Catalog.GetString("with ")+ parts[parts.Length-1]+ " ("+f1.Length+" bytes)?"; MessageDialog md=new MessageDialog(null, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo, message); ResponseType result = (ResponseType)md.Run(); md.Destroy(); if(result==ResponseType.Yes){ // Sobreescriptura File.Delete(Preferences.dir_gallery+"/"+parts[parts.Length-1]); File.Copy(file, Preferences.dir_gallery+"/"+parts[parts.Length-1]); NumNewCathegories++; } } else {// Si no existeix, simplement el copiem File.Copy(file, Preferences.dir_gallery+"/"+parts[parts.Length-1]); NumNewCathegories++; } } // 3. Fem el merge dels temes Themes Mythemes=new Themes(); Themes NewThemes=new Themes(); Mythemes.ReadThemes(); NewThemes.ReadThemes("/tmp/lliurex-amic/themes/themes.xml"); foreach (Theme t in NewThemes.getThemes()){ if (Mythemes.Exists(t.name)){ Console.WriteLine("Theme "+t.name+" already exists."); } else { Mythemes.Add(t); NumNewThemes++; } } // Mostrem els resultats Console.WriteLine("Finished!"); Console.WriteLine(NumNewCathegories+" new cathegories."); Console.WriteLine(NumNewThemes + " new themes."); Console.WriteLine(NumNewImages+ " new images."); Console.WriteLine(NumNewAudios+" new audio files."); string msg; msg="Process successful!\n\n"; msg=msg+NumNewCathegories+" new cathegories.\n"; msg=msg+NumNewThemes + " new themes.\n"; msg=msg+NumNewImages+ " new images.\n"; msg=msg+NumNewAudios+" new audio files.\n"; msg=msg+"\n Has been added."; MessageDialog finished=new MessageDialog(null, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, msg); finished.Run(); finished.Destroy(); return true; }catch(Exception ex){ Console.WriteLine("Exception on Import! - "+ex.Message); return false; } //return false; } } }