using System; using Gtk; using Glade; namespace AmicEditor { public class SaveSoundWindow : Dialog { [Widget] Gtk.Dialog ChooseFile; [Widget] Gtk.Button btCancel; [Widget] Gtk.Button BtOk; [Widget] Gtk.Button BtDefaultSound; [Widget] Gtk.Button BtCustomSound; [Widget] Gtk.Button BtHome; [Widget] Gtk.Button BtRec; [Widget] Gtk.Entry PathEntry; [Widget] Gtk.Button BtPlay; [Widget] Gtk.Button BtStop; [Widget] Gtk.TreeView FilesTreeView; // Directori actual string CurrentDir=Preferences.dir_audio_custom+"/Default"; string CurrentFile=""; // Paràmetres de l'audio int id_audio=-1; IAudioServer server; // Pila de directoris pels que s'ha passat... System.Collections.Generic.Stack PathStack; bool asc_desc=true; public SaveSoundWindow (string _filename) { Application.Init(); Glade.XML gxml = new Glade.XML(null, "SaveSoundWindow.glade", "ChooseFile", null); gxml.Autoconnect(this); // INICIALITZACIÓ DE L'AUDIO NDesk.DBus.ObjectPath path = new NDesk.DBus.ObjectPath("/net/Lliurex/Media/AudioServer"); server = NDesk.DBus.Bus.Session.GetObject("net.Lliurex.Media.AudioServer",path); // Imatges als botons AddImageToButton("CustomAudioFolder.png", null, BtCustomSound, 50, 50); AddImageToButton("DefaultAudioFolder.png", null, BtDefaultSound , 50, 50); AddImageToButton("UserHome.png", null, BtHome , 50, 50); AddImageToButton("RecordSound.png", null, BtRec , 50, 50); AddImageToButton("play.png", null, BtPlay , 20, 20); AddImageToButton("stop.png", null, BtStop , 20, 20); // Afegim el path complet if (System.IO.File.Exists(Preferences.dir_audio_custom+"/"+_filename)) _filename=Preferences.dir_audio_custom+"/"+_filename; else if (System.IO.File.Exists(Preferences.dir_audio+"/"+_filename)) _filename=Preferences.dir_audio+"/"+_filename; else _filename=""; System.Collections.Generic.List FolderList=new System.Collections.Generic.List(); PathStack=new System.Collections.Generic.Stack(); //FolderList.Add(Preferences.dir_audio); //FolderList.Add(Preferences.dir_audio_custom); FolderList.Add(Preferences.dir_audio+"/Default"); FolderList.Add(Preferences.dir_audio_custom+"/Custom"); /*********** Patch: Per si la ruta de la imatge és una subcarpeta *******************/ if (_filename.LastIndexOf("/")>0){ CurrentDir=_filename.Substring(0,_filename.LastIndexOf("/")); System.Collections.Generic.List paths=new System.Collections.Generic.List(); paths.Add(Preferences.dir_audio+"/Default"); paths.Add(Preferences.dir_audio_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) PathStack.Push(CurrentPath); foreach(string dir in subdirs){ path_for_stack=path_for_stack+"/"+dir; PathStack.Push(path_for_stack); } // I desapilem l'últim, que és l'actual (2 vegades pq vaja bé) PathStack.Pop(); 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(); // Event Handlers FilesTreeView.Selection.Changed += HandleFilesTreeViewSelectionChanged; FilesTreeView.RowActivated += HandleFilesTreeViewRowActivated; BtPlay.Clicked += HandleBtPlayClicked; BtStop.Clicked += HandleBtStopClicked; BtDefaultSound.Clicked += HandleBtDefaultSoundClicked; BtCustomSound.Clicked += HandleBtCustomSoundClicked; BtHome.Clicked += HandleBtHomeClicked; BtRec.Clicked += HandleBtRecClicked; colIcon.Clicked+= HandleColFileClicked; // Fa el mateix que File colFile.Clicked+= HandleColFileClicked; BtOk.Clicked += HandleBtOkClicked; btCancel.Clicked += HandleBtCancelClicked; } void HandleBtRecClicked (object sender, EventArgs e) { System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName ="bash"; proc.StartInfo.Arguments = "-c 'gnome-sound-recorder'"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.Start(); } void HandleBtStopClicked (object sender, EventArgs e) { if (id_audio!=-1) { try{ server.Stop(id_audio); }catch (Exception ex){ Console.WriteLine("Exception: "+ex); } } } void HandleBtPlayClicked (object sender, EventArgs e) { // Magic AudioMagic=new Magic(); Console.WriteLine("Play... "+ CurrentFile); if(CurrentFile!=null){ Console.WriteLine(CurrentFile); try{ //carga id_audio = server.LoadMedia(CurrentFile); //play server.Play(id_audio); }catch (Exception exc){ Console.WriteLine("Exception: "+exc); } } } 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 HandleBtCustomSoundClicked (object sender, EventArgs e) { CurrentDir=Preferences.dir_audio_custom+"/Custom"; PathStack.Clear(); PopulateList(); PathEntry.Text=CurrentDir; } void HandleBtDefaultSoundClicked (object sender, EventArgs e) { CurrentDir=Preferences.dir_audio+"/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 (IsAudio(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 (IsAudio(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! { PathStack.Pop(); CurrentDir=dirpath; } else { // és un subdirectori 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)&&IsAudio(actualSelectedPath))){ CurrentFile=actualSelectedPath; PathEntry.Text=actualSelectedPath; } } catch (Exception exc){ Console.WriteLine("Excepcio: "+exc); } } void HandleBtOkClicked (object sender, EventArgs e) { Console.WriteLine(CurrentFile); // Comprovem si està en les carpetes Default o Custom if((CurrentFile.Length>Preferences.dir_audio.Length && CurrentFile.Substring(0,Preferences.dir_audio.Length)==Preferences.dir_audio) || (CurrentFile.Length>Preferences.dir_audio_custom.Length && CurrentFile.Substring(0,Preferences.dir_audio_custom.Length)==Preferences.dir_audio_custom)) { Console.WriteLine("Esta en Default o Custom"); AmicEditor.SelectedSoundFile=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_audio_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_audio_custom+"/Custom/"+filename); System.IO.File.Delete(Preferences.dir_audio_custom+"/Custom/"+filename); Console.WriteLine("Copiant "+CurrentFile+" a "+Preferences.dir_audio_custom+"/Custom/"+filename); System.IO.File.Copy(CurrentFile, Preferences.dir_audio_custom+"/Custom/"+filename); // I l'actualitzem i tanquem la finestra AmicEditor.SelectedSoundFile=Preferences.dir_audio_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_audio_custom+"/Custom/"+filename); System.IO.File.Copy(CurrentFile, Preferences.dir_audio_custom+"/Custom/"+filename); // I l'actualitzem i tanquem la finestra AmicEditor.SelectedSoundFile=Preferences.dir_audio_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 IsAudio(string path){ string [] parts=path.Split(new Char [] {'.'}); System.Collections.Generic.List Validfiles=new System.Collections.Generic.List(); Validfiles.Add("mp3"); Validfiles.Add("ogg"); Validfiles.Add("wav"); 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ó. Gtk.Image MiImage=new Gtk.Image(); if (System.IO.File.Exists(Preferences.dir_rsrc+"/"+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 { MiImage = new Image(stock, IconSize.Button); Boto.Image=MiImage; } } } }