using System; using Gtk; using Glade; namespace AmicEditor { public class SaveFileWindow : Dialog { //[Widget] Gtk.ColorButton CatFontColor; [Widget] Gtk.Dialog ChooseFile; //[Widget] Gtk.Widget dialogvbox1; //[Widget] Gtk.FileChooserWidget dialogvbox1; [Widget] Gtk.Button btCancel; [Widget] Gtk.Button BtOk; [Widget] Gtk.Image ImgSel; [Widget] Gtk.Button BtDefaultImg; [Widget] Gtk.Button BtCustomImg; [Widget] Gtk.Button BtHome; [Widget] Gtk.Entry PathEntry; [Widget] Gtk.TreeView FilesTreeView; // Directori actual string CurrentDir=Preferences.dir_img_custom+"/Default"; string CurrentFile=""; // Pila de directoris pels que s'ha passat... System.Collections.Generic.Stack PathStack; //string orig_img=""; // Ruta completa de la imatge original //bool ImageIsValid; //string pathGeneral=""; //string pathCustom=""; //string filename=""; //string linkDefault=""; //string linkCustom=""; bool asc_desc=true; public SaveFileWindow (string _filename) { Application.Init(); Glade.XML gxml = new Glade.XML(null, "SaveFileWindow.glade", "ChooseFile", null); gxml.Autoconnect(this); // Imatges als botons AddImageToButton("CustomImageFolder.png", null, BtCustomImg, 50, 50); AddImageToButton("DefaultImageFolder.png", null, BtDefaultImg , 50, 50); AddImageToButton("UserHome.png", null, BtHome , 50, 50); // Afegim el path complet if (System.IO.File.Exists(Preferences.dir_img_custom+"/"+_filename)) _filename=Preferences.dir_img_custom+"/"+_filename; else if (System.IO.File.Exists(Preferences.dir_img+"/"+_filename)) _filename=Preferences.dir_img+"/"+_filename; else _filename=""; System.Collections.Generic.List FolderList=new System.Collections.Generic.List(); PathStack=new System.Collections.Generic.Stack(); // FolderList.Add(Preferences.dir_img); // FolderList.Add(Preferences.dir_img_custom); FolderList.Add(Preferences.dir_img+"/Default"); FolderList.Add(Preferences.dir_img_custom+"/Custom"); Console.WriteLine("Filename: "+_filename); /*********** Patch: Per si la ruta de la imatge és una subcarpeta *******************/ if (_filename.LastIndexOf("/")>0){ CurrentDir=_filename.Substring(0,_filename.LastIndexOf("/")); Console.WriteLine("Currentdir: "+CurrentDir); System.Collections.Generic.List paths=new System.Collections.Generic.List(); paths.Add(Preferences.dir_img+"/Default"); paths.Add(Preferences.dir_img_custom+"/Custom"); string CurrentPath=GeneralUtils.GetPathFrom(_filename, paths); // Inicialització de l'etiqueta CurrentFile=_filename; PathEntry.Text=CurrentFile; // Si no es troba en una ruta arrel, cal afegir la ruta a la pila Console.WriteLine("CurrentPath.Length+1: "+CurrentPath.Length+1); Console.WriteLine("CurrentDir.Length-CurrentPath.Length-1: "+(CurrentDir.Length-CurrentPath.Length-1)); Console.WriteLine("Filename.length:"+_filename.Length); Console.WriteLine(_filename); string path_to_file=_filename.Substring(CurrentPath.Length+1, CurrentDir.Length-CurrentPath.Length); Console.WriteLine(path_to_file); string [] subdirs=path_to_file.Split(new Char [] {'/'}); string path_for_stack=CurrentPath; if(subdirs.Length>0) { Console.WriteLine("APILANT: *"+CurrentPath+"*"); PathStack.Push(CurrentPath); } foreach(string dir in subdirs){ path_for_stack=path_for_stack+"/"+dir; Console.WriteLine("APILANT: *"+path_for_stack+"*"); PathStack.Push(path_for_stack); } // I desapilem l'últim, que és l'actual Console.WriteLine("DESAPILANT: *"+PathStack.Peek()); PathStack.Pop(); Console.WriteLine("DESAPILANT: *"+PathStack.Peek()); PathStack.Pop(); } /**********************************/ TreeViewColumn colIcon=new TreeViewColumn("", new Gtk.CellRendererPixbuf (), "pixbuf", 0); colIcon.Clickable=true; //colIcon.SortColumnId=1; //colIcon.SortIndicator=true; FilesTreeView.AppendColumn(colIcon); TreeViewColumn colFile=new TreeViewColumn("File", new Gtk.CellRendererText (), "text", 1); colFile.Clickable=true; //colFile.SortColumnId=1; //colFile.SortIndicator=true; FilesTreeView.AppendColumn(colFile); FilesTreeView.AppendColumn(null, null, "text", 2); // Omplim la llista PopulateList(); // Dibuixem la imatge DrawImage(CurrentFile); // Event Handlers FilesTreeView.Selection.Changed += HandleFilesTreeViewSelectionChanged; FilesTreeView.RowActivated += HandleFilesTreeViewRowActivated; BtDefaultImg.Clicked += HandleBtDefaultImgClicked; BtCustomImg.Clicked += HandleBtCustomImgClicked; BtHome.Clicked += HandleBtHomeClicked; colIcon.Clicked+= HandleColFileClicked; // Fa el mateix que File colFile.Clicked+= HandleColFileClicked; BtOk.Clicked += HandleBtOkClicked; btCancel.Clicked += HandleBtCancelClicked; } void HandleColFileClicked (object sender, EventArgs e) { asc_desc=!asc_desc; PopulateList(); } void HandleBtHomeClicked (object sender, EventArgs e) { CurrentDir=Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); Console.WriteLine(System.Environment.SpecialFolder.Personal); PathStack.Clear(); PopulateList(); PathEntry.Text=CurrentDir; } void HandleBtCustomImgClicked (object sender, EventArgs e) { // PATCHED CurrentDir=Preferences.dir_img_custom; CurrentDir=Preferences.dir_img_custom+"/Custom"; PathStack.Clear(); PopulateList(); PathEntry.Text=CurrentDir; } void HandleBtDefaultImgClicked (object sender, EventArgs e) { // PATCHED CurrentDir=Preferences.dir_img; CurrentDir=Preferences.dir_img+"/Default"; PathStack.Clear(); PopulateList(); PathEntry.Text=CurrentDir; } void PopulateList(){ // llista per emmagatzemar els fitxers //Gtk.ListStore llista=new Gtk.ListStore(typeof(Gdk.Pixbuf), typeof(string), typeof(string)); Gtk.TreeStore llista=new Gtk.TreeStore(typeof(Gdk.Pixbuf), typeof(string), typeof(string)); Gdk.Pixbuf PixBufImage=new Gdk.Pixbuf (Preferences.dir_default+"/IconFile.png", 20, 20); Gdk.Pixbuf PixBufFolder=new Gdk.Pixbuf (Preferences.dir_default+"/IconFolder.png", 20, 20); Gdk.Pixbuf PixBufParent=new Gdk.Pixbuf (Preferences.dir_default+"/IconGoParent.png", 20, 20); // Si hi ha carpeta pare, la posem primer if(PathStack.Count>0){ llista.AppendValues (PixBufParent,"..", PathStack.Peek()); } // Ordenació de la llista de carpetes string [] FilesSorted=System.IO.Directory.GetFiles(CurrentDir); System.Collections.Generic.List FileList=new System.Collections.Generic.List(); foreach (string file in FilesSorted) FileList.Add(file); FileList.Sort(); if (asc_desc) FileList.Reverse(); foreach (string file in FileList){ if (IsImage(file)&&(GetFileName(file))[0]!='.') llista.AppendValues (PixBufImage,GetFileName(file), file); } // Per deixar l'ordenació normal... // foreach (string file in System.IO.Directory.GetFiles(CurrentDir)) // if (IsImage(file)&&(GetFileName(file))[0]!='.') // llista.AppendValues (PixBufImage,GetFileName(file), file); // Ordenació de les carpetes string [] FolderSorted=System.IO.Directory.GetDirectories(CurrentDir); System.Collections.Generic.List FolderList=new System.Collections.Generic.List(); foreach (string file in FolderSorted) FolderList.Add(file); FolderList.Sort(); if (asc_desc) FolderList.Reverse(); foreach (string file in FolderList){ if ((GetFileName(file))[0]!='.') llista.AppendValues (PixBufFolder,GetFileName(file), file); } // Per deixar l'ordenació normal //foreach (string file in System.IO.Directory.GetDirectories(CurrentDir)) // if ((GetFileName(file))[0]!='.') // llista.AppendValues (PixBufFolder,GetFileName(file), file); // Creem un treemodelfilter, que actúa com a controlador, entre la llista i el treeview TreeModelFilter TMFilter=new TreeModelFilter(llista, null); TreeModelSort sortAdapter=new TreeModelSort(TMFilter); //FilesTreeView.Model=TMFilter; FilesTreeView.Model=sortAdapter; } public Gtk.TreeView GetFilesTreeView(){ return FilesTreeView; } void HandleFilesTreeViewRowActivated (object o, RowActivatedArgs args) { TreeIter iter; string dirpath=""; GetFilesTreeView().Model.GetIter(out iter, args.Path); dirpath=(GetFilesTreeView().Model.GetValue(iter, 2)).ToString(); if(System.IO.Directory.Exists(dirpath)){ Console.WriteLine("Entering Path... "+dirpath); // Actualitzem el directori if (PathStack.Count>0 && PathStack.Peek()==dirpath) // Es tracta del mateix directori! { Console.WriteLine("DESAPILANT: *"+PathStack.Peek()); PathStack.Pop(); CurrentDir=dirpath; } else { // és un subdirectori Console.WriteLine("APILANT: *"+CurrentDir+"*"); PathStack.Push(CurrentDir); CurrentDir=dirpath; } PopulateList(); PathEntry.Text=CurrentDir; } } void HandleFilesTreeViewSelectionChanged (object sender, EventArgs e) { try{ TreeModel model; TreeIter iterSelected; //string actualSelectedItem=""; string actualSelectedPath=""; if(((TreeSelection)sender).GetSelected (out model, out iterSelected)) { //actualSelectedItem = (string) model.GetValue (iterSelected, 1); actualSelectedPath = (string) model.GetValue (iterSelected, 2); } if((System.IO.File.Exists(actualSelectedPath)&&IsImage(actualSelectedPath))){ DrawImage(actualSelectedPath); CurrentFile=actualSelectedPath; PathEntry.Text=actualSelectedPath; } } catch (Exception exc){ Console.WriteLine("Excepcio: "+exc); } } void DrawImage(string file_to_show){ Gtk.Image MyImage=new Gtk.Image(); if(System.IO.File.Exists(file_to_show)) { Console.WriteLine("Selector: "+file_to_show); try{ //Console.WriteLine("Magic ImageMagic=new Magic();"); Magic ImageMagic=new Magic(); if(ImageMagic.isImageFile(file_to_show)){ MyImage=new Gtk.Image(file_to_show); // ImageIsValid=true; Console.WriteLine("LA IMATGE ES CORRECTA"); } else { // ImageIsValid=false; Console.WriteLine("LA IMATGE NO ES BONA"); } } catch (Exception ex) { Console.WriteLine("Lliurex Amic Exception: "+ex); } } else MyImage=new Gtk.Image("rsrc/image-missing.png"); /* if (System.IO.File.Exists(dir_img+"/"+img_source)) { Console.WriteLine("Trobe "+dir_img+"/"+img_source); MiImage = new Gtk.Image(dir_img+"/"+img_source); } else { Console.WriteLine("No trobe "+dir_img+"/"+img_source); MiImage = new Gtk.Image("rsrc/image-missing.png"); } */ try{ int new_width=150; int new_height=150; MyImage.Pixbuf = MyImage.Pixbuf.ScaleSimple(new_width, new_height, Gdk.InterpType.Bilinear); this.ImgSel.Pixbuf=MyImage.Pixbuf; }catch (Exception ex){ Console.WriteLine("Lliurex Amic Exception: "+ex); } } void HandleBtOkClicked (object sender, EventArgs e) { Console.WriteLine(CurrentFile); // Comprovem si està en les carpetes Default o Custom if((CurrentFile.Length>Preferences.dir_img.Length && CurrentFile.Substring(0,Preferences.dir_img.Length)==Preferences.dir_img) || (CurrentFile.Length>Preferences.dir_img_custom.Length && CurrentFile.Substring(0,Preferences.dir_img_custom.Length)==Preferences.dir_img_custom)) { Console.WriteLine("Esta en Default o Custom"); AmicEditor.SelectedImageFile=CurrentFile; Application.Quit(); ChooseFile.Destroy(); } else { Console.WriteLine("Esta en Home"); // Comprovem si l'arxiu que ha triat està dins de la galeria string filename; filename=CurrentFile.Substring(CurrentFile.LastIndexOf('/')+1, CurrentFile.Length-CurrentFile.LastIndexOf('/')-1); if (System.IO.File.Exists(Preferences.dir_img_custom+"/Custom/"+filename)) { // Ja es troba a la galeria MessageDialog md = new MessageDialog (ChooseFile, DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.YesNo, Mono.Unix.Catalog.GetString("The image is already in the Custom Gallery. Do you want to overwrite it?")); ResponseType result = (ResponseType)md.Run(); md.Destroy(); if (result==ResponseType.Yes){ // Si diu que si, eliminem l'arxiu i el tornem a copiar Console.WriteLine("Eliminant: "+Preferences.dir_img_custom+"/Custom/"+filename); System.IO.File.Delete(Preferences.dir_img_custom+"/Custom/"+filename); Console.WriteLine("Copiant "+CurrentFile+" a "+Preferences.dir_img_custom+"/Custom/"+filename); System.IO.File.Copy(CurrentFile, Preferences.dir_img_custom+"/Custom/"+filename); // I l'actualitzem i tanquem la finestra AmicEditor.SelectedImageFile=Preferences.dir_img_custom+"/Custom/"+filename; Application.Quit(); ChooseFile.Destroy(); } } else { // Si no existeix, preguntem si vol copiar-lo (*1) MessageDialog md = new MessageDialog (ChooseFile, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.YesNo, Mono.Unix.Catalog.GetString("The image is not included in the your Custom Gallery. Do you want to import it there?")); ResponseType result = (ResponseType)md.Run(); md.Destroy(); if (result==ResponseType.Yes){ // Si vol importar-la, la copiem a la galeria Console.WriteLine("Copiant "+CurrentFile+" a "+Preferences.dir_img_custom+"/Custom/"+filename); System.IO.File.Copy(CurrentFile, Preferences.dir_img_custom+"/Custom/"+filename); // I l'actualitzem i tanquem la finestra AmicEditor.SelectedImageFile=Preferences.dir_img_custom+"/Custom/"+filename; Application.Quit(); ChooseFile.Destroy(); } } // del else (*1) } } void HandleBtCancelClicked (object sender, EventArgs e) { Application.Quit(); ChooseFile.Destroy(); } public new void Show(){ ChooseFile.ShowAll(); Application.Run(); } public string GetFileName(string path){ string [] parts=path.Split(new Char [] {'/'}); return parts[parts.Length-1]; } public bool IsImage(string path){ string [] parts=path.Split(new Char [] {'.'}); System.Collections.Generic.List Validfiles=new System.Collections.Generic.List(); Validfiles.Add("png"); Validfiles.Add("jpg"); if(Validfiles.Contains(parts[parts.Length-1])) return true; return false; } public void AddImageToButton(string image, string stock, Button Boto, int tamX, int tamY){ // Utilitat per assignar una imatge a un botó. Console.WriteLine("**"+image); Gtk.Image MiImage=new Gtk.Image(); if (System.IO.File.Exists(Preferences.dir_rsrc+"/"+image)) { Console.WriteLine("*1*"+image); MiImage = new Gtk.Image(Preferences.dir_rsrc+"/"+image); if (tamX!=0) MiImage.Pixbuf=MiImage.Pixbuf.ScaleSimple(tamX, tamY, Gdk.InterpType.Bilinear); Boto.Image=MiImage; } else { Console.WriteLine("*2*"+image); MiImage = new Image(stock, IconSize.Button); Boto.Image=MiImage; } Console.WriteLine("*****"); } } }