Advertisement
Guest User

list.vala

a guest
Jun 9th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 7.07 KB | None | 0 0
  1. using Gtk;
  2. using TagLib;
  3.  
  4.  
  5. namespace BabeList
  6. {
  7.    
  8.  private const Gtk.TargetEntry[] targets = {
  9.         {"text/uri-list",0,0}
  10.     };
  11.  
  12. public class BList : Gtk.ScrolledWindow
  13. {
  14.     public signal void click ();   // definition of the signal
  15.  
  16.     public void method () {
  17.         click ();
  18.         print("seññññal");               // emitting the signal (callbacks get invoked)
  19.     }
  20.    
  21.     public int c;
  22.     private Gtk.ListStore main_list;
  23.     private Gtk.CellRendererText main_list_cell;
  24.     private Gtk.TreeView main_list_view;
  25.     public Gtk.TreeIter iter;
  26.     private Gtk.FileChooserDialog chooser;
  27.     public Gtk.EventBox add_music_event;
  28.     public Gtk.Stack stack;
  29.     public string title;
  30.     public string artist;
  31.     public string album;
  32.     public string song;
  33.     public BList(bool state, string playlist_path)//whether the list has to be populate from start//useful for saved playlists(true=populate/false=start empty, the path to the playlist)
  34.     {                  
  35.         Object(hadjustment: null, vadjustment: null);
  36.  
  37.         c=0;
  38.         this.set_min_content_height(200);
  39.         this.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
  40.        
  41.         Gtk.drag_dest_set (this,Gtk.DestDefaults.ALL, targets, Gdk.DragAction.COPY);
  42.         this.drag_data_received.connect(on_drag_data_received);
  43.  
  44.         main_list = new Gtk.ListStore (5, typeof (string), typeof (string), typeof (string), typeof (string), typeof (string));
  45.         main_list_cell = new Gtk.CellRendererText ();
  46.         main_list_view= new Gtk.TreeView.with_model (main_list);
  47.  
  48.         main_list_view.insert_column_with_attributes (-1, "Title", main_list_cell, "text", 0);         
  49.         main_list_view.set_grid_lines (TreeViewGridLines.BOTH);
  50.         main_list_view.set_reorderable(true);
  51.         main_list_view.set_headers_visible(false);
  52.         main_list_view.set_enable_search(true);    
  53.  
  54.         main_list_view.row_activated.connect(this.on_row_activated);
  55.         //var label=new Gtk.Label("Add Music");// use this in future if i can make it use the font wanted
  56.        
  57.         var add_music_img = new Gtk.Image();
  58.         add_music_img.set_from_file("img/add.png");
  59.        
  60.         add_music_event = new Gtk.EventBox();
  61.         add_music_event.add(add_music_img);
  62.         add_music_event.button_press_event.connect (() => {
  63.                     on_open();
  64.                     return true;
  65.         });
  66.        
  67.         stack=new Gtk.Stack();
  68.         stack.set_vexpand(true);//main list
  69.         stack.set_vexpand(true);//add music event/message      
  70.         stack.add_named(add_music_event, "add");
  71.         stack.add_named(main_list_view, "list");
  72.  
  73.        
  74.         if((state)&&playlist_path.strip().length>0)
  75.         {
  76.             populate_playlist( playlist_path);
  77.         }
  78.         GLib.Timeout.add (1000, (SourceFunc) this.check_list);
  79.  
  80.        
  81.         this.add(stack);
  82.     }
  83.        
  84.     private void on_row_activated (Gtk.TreeView treeview, Gtk.TreePath path, Gtk.TreeViewColumn column) //double click starts playback
  85.     {
  86.         var model=treeview.get_model();
  87.  
  88.         if (treeview.model.get_iter (out iter, path)) {
  89.         model.get (iter,
  90.                         4, out title,
  91.                         1, out artist,
  92.                         2, out song,
  93.                         3, out album);
  94.       }
  95.      
  96.       //print(title+" by "+artist+" @ "+song);
  97.     }
  98.  
  99.     public string get_song()
  100.     {
  101.         return song;
  102.     }
  103.  
  104.     public void on_open()
  105.     {
  106.         chooser = new Gtk.FileChooserDialog (
  107.                 "Select your favorite file", null, Gtk.FileChooserAction.OPEN,
  108.                 "_Cancel",
  109.                 Gtk.ResponseType.CANCEL,
  110.                 "_Open",
  111.                 Gtk.ResponseType.ACCEPT);
  112.  
  113.         chooser.select_multiple = true;
  114.         Gtk.FileFilter filter = new Gtk.FileFilter ();
  115.         filter.set_filter_name ("Audio");
  116.         filter.add_pattern ("*.mp3");
  117.         filter.add_pattern ("*.flac");
  118.         chooser.add_filter (filter);
  119.  
  120.         if (chooser.run () == Gtk.ResponseType.ACCEPT)
  121.         {
  122.             SList<string> uris = chooser.get_uris ();
  123.             //stdout.printf ("Selection on_open:\n");
  124.             foreach (unowned string uri in uris)
  125.             {
  126.                 populate(uri);
  127.             }          
  128.         }
  129.  
  130.         chooser.close ();
  131.     }
  132.    
  133.     public void modify_c()
  134.     {
  135.         c--;
  136.         check_list();  
  137.     }
  138.    
  139.     public int get_c()
  140.     {
  141.         return c;
  142.     }
  143.     public async void populate(string uri)
  144.     {    
  145.         var file = GLib.File.new_for_uri (uri); //check if file exists
  146.         var type = GLib.ContentType.guess(uri, null,null);  //check if file is audio file  
  147.        
  148.         if (file.query_exists()&& type.contains("audio"))
  149.         {
  150.             main_list.append (out iter);
  151.             main_list.set (iter, 0, get_song_info(uri).tag.title+"\nby "+ get_song_info(uri).tag.artist, 1, get_song_info(uri).tag.artist, 2, uri, 3,get_song_info(uri).tag.album,4,get_song_info(uri).tag.title);
  152.             c++;
  153.         }else
  154.         {
  155.             warning("no existe");          
  156.         }
  157.     check_list();
  158.     }
  159.        
  160.     public TagLib.File get_song_info(string uri)//it actually turns a uri into a path to be able to get the tags
  161.     {                  
  162.         var gfile = GLib.File.new_for_uri (uri);
  163.         string nm=gfile.get_path() ;
  164.         var info =  new TagLib.File(nm);
  165.         return info;
  166.     }  
  167.        
  168.     public Gtk.TreeView get_treeview()
  169.     {
  170.         return main_list_view;
  171.     }  
  172.    
  173.     public Gtk.ListStore get_liststore()
  174.     {
  175.         return main_list;
  176.     }
  177.        
  178.    
  179.     public bool list_is_empty()
  180.     {
  181.         if(c==0)
  182.         {
  183.             return true;
  184.            
  185.         }else
  186.         {
  187.             //print(c.to_string()+"//");
  188.             return false;
  189.         }
  190.        
  191.     }
  192.    
  193.     public void check_list()
  194.     {
  195.         if(list_is_empty())
  196.         {
  197.             stack.set_visible_child_name("add");
  198.            
  199.         }else
  200.         {
  201.             stack.set_visible_child_name("list");
  202.         }
  203.        
  204.     }
  205.    
  206.     private async void on_drag_data_received (Gdk.DragContext drag_context, int x, int y,
  207.                                         Gtk.SelectionData data, uint info, uint time)
  208.     {
  209.         //loop through list of URIs
  210.         foreach(string uri in data.get_uris ())
  211.         {
  212.             var file = GLib.File.new_for_uri (uri);
  213.             if(file.query_file_type (0) == GLib.FileType.REGULAR )
  214.             {
  215.                 populate(uri);
  216.             }
  217.             if(file.query_file_type (0) == GLib.FileType.DIRECTORY )
  218.             {
  219.                 print("es un directorio");
  220.                 try
  221.                 {
  222.                     var directory = GLib.File.new_for_uri (uri);           
  223.                     var enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0);
  224.                     FileInfo file_info;
  225.                     while ((file_info = enumerator.next_file ()) != null)
  226.                     {        
  227.                         GLib.File child = enumerator.get_child (file_info);
  228.                         populate(child.get_uri());             
  229.                     }
  230.  
  231.                 } catch (Error e)
  232.                 {
  233.                     stderr.printf ("Error: %s\n", e.message);
  234.      
  235.                 }              
  236.             }
  237.         }
  238.  
  239.         Gtk.drag_finish (drag_context, true, false, time);
  240.     }
  241.    
  242.     public void populate_playlist(string playlist_path)
  243.     {
  244.         //babes_list.clear();
  245.         //var file = File.new_for_path (".Babes.txt");
  246.         var file = GLib.File.new_for_path (playlist_path);
  247.  
  248.         if (!(file.query_exists()))
  249.         {
  250.            file.create (FileCreateFlags.NONE);
  251.         }
  252.         var dis = new DataInputStream (file.read());
  253.         string line;
  254.         // Read lines until end of file (null) is reached
  255.         while ((line = dis.read_line (null)) != null) {
  256.             //stdout.printf ("%s\n", line);
  257.             file=GLib.File.new_for_uri(line);
  258.             if(file.query_exists())
  259.             {
  260.             populate(line);        
  261.             }else
  262.             {
  263.                 print("File missing: "+line+"\n");
  264.                
  265.             }
  266.         }      
  267.     }
  268.     public void clean_list()
  269.     {
  270.         get_liststore().clear();
  271.         c=0;
  272.     }
  273.    
  274. }
  275. }
  276. /*
  277. static int main(string[] args)
  278. {
  279.     Gtk.init(ref args);
  280.     BList empty_list=new BList(false, "");
  281.     var babe_list= new BList(true, ".Babes.txt");
  282.     var window=new Gtk.Window();
  283.     var box=new Gtk.Box(Gtk.Orientation.VERTICAL,0);
  284.     empty_list.playlist();
  285.     box.add(empty_list);
  286.     box.add(babe_list);
  287.     window.add(box);
  288.     window.show_all();
  289.     Gtk.main();
  290.     return 0;
  291. }
  292. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement