1 #!/usr/bin/python 2 3 import os 4 from gi.repository import Gtk 5 from random import shuffle 6 import urllib 7 8 9 filepattern = ( 10 ("MP3","*.mp3"), 11 ("AVI","*.avi"), 12 ("MPEG-4","*.mp4"), 13 ("FLV ","*.flv"), 14 ("OGG","*.ogg"), 15 ) 16 17 pattern = (".mp3",".mp4",".avi",".flv",".ogg") 18 19 class main: 20 21 def __init__(self): 22 # Crea la ventana de trabajo Principal y obtiene los objetos en Glade 23 builder = Gtk.Builder() 24 builder.add_from_file("Multiplay.ui") 25 self.addfile = builder.get_object("Add") 26 self.addfolder = builder.get_object("AddFolder") 27 self.save = builder.get_object("Save") 28 self.about = builder.get_object("About") 29 self.medialist = builder.get_object("MediaList") 30 self.combolist = builder.get_object("combobox1") 31 self.treeview = builder.get_object("TextView") 32 self.up = builder.get_object("Up") 33 self.down = builder.get_object("Down") 34 self.filterbox = Gtk.FileFilter() 35 36 # Diccionario de eventos y Conexion de los mismos. 37 dict = {"on_Add_clicked": self.showAddFile, 38 "on_AddFolder_clicked": self.showAddFolder, 39 "on_Saved_clicked": self.showSave, 40 "on_About_clicked": self.showAbout, 41 "on_Clearone_clicked": self.clearone, 42 "on_Clearlist_clicked": self.clearlist, 43 "on_Up_clicked": self.updown_move, 44 "on_Down_clicked": self.updown_move, 45 "on_ComboOrder_changed": self.newList, 46 47 } 48 builder.connect_signals(dict) 49 50 #Definicion del comando Agregar 51 def showAddFile(self, widget): 52 self.filterbox.set_name("Media Files") 53 for name, pattern in filepattern: 54 self.filterbox.add_pattern(pattern) 55 56 self.addfile.add_filter(self.filterbox) 57 58 respt = self.addfile.run() 59 self.addfile.remove_filter(self.filterbox) 60 self.addfile.hide() 61 if respt == -5: 62 fileselected = self.addfile.get_filenames() 63 for files in fileselected: 64 (dirs,files)= os.path.split(files) 65 self.medialist.append([files,dirs]) 66 67 68 #Definicion del comando Abrir Carpeta 69 def showAddFolder(self, widget): 70 self.filterbox.set_name("All Media Files") 71 for names, patterns in filepattern: 72 self.filterbox.add_pattern(patterns) 73 74 self.addfolder.add_filter(self.filterbox) 75 76 respt = self.addfolder.run() 77 self.addfolder.remove_filter(self.filterbox) 78 self.addfolder.hide() 79 80 if respt == -5: 81 addmultiple = self.addfolder.get_filename() 82 83 for root, dirs, filelist in os.walk(addmultiple): 84 for allfiles in [f for f in filelist if f.endswith(pattern)]: 85 self.medialist.append([allfiles,root]) 86 87 #Definicion del comando Borrar un Archivo 88 def clearone(self, *args): 89 clearone = self.treeview.get_selection() 90 (model, rows) = clearone.get_selected_rows() 91 for i in rows: 92 iteration=model.get_iter(i) 93 print iteration 94 self.medialist.remove(iteration) 95 96 #Definicion del comando Borrar Lista 97 def clearlist(self, *args): 98 self.medialist.clear() 99 100 #Definicion del comando Arriba y Abajo 101 def updown_move(self, button): 102 line = self.treeview.get_selection() 103 (model, rows) = line.get_selected_rows() 104 if button == self.up: 105 for path1 in rows: 106 path2 = int(str(path1))-1 107 if path2 < 0: 108 return 109 else: 110 iter1 = model.get_iter(path1) 111 iter2 = model.get_iter(path2) 112 model.swap(iter1,iter2) 113 114 if button == self.down: 115 for path1 in rows: 116 iter1 = model.get_iter(path1) 117 iter2 = model.iter_next(iter1) 118 if iter2 == None: 119 return 120 else: 121 model.swap(iter1,iter2) 122 123 #Definicion para ordernar la lista 124 def newList(self, widget): 125 var = self.combolist.get_active() 126 127 #Si se activa Aleatorio 128 if var == 0: 129 n = 0 130 for musicfiles in self.medialist: 131 n += 1 132 index = range(0, n) 133 shuffle(index) 134 #Reorganiza la lista de musica llamad medialist 135 if len(index) > 1: 136 self.medialist.reorder(index) 137 138 #Si se activa Alfabetico 139 if var == 1: 140 iter=self.medialist.get_iter_first() 141 newlistfiles = [] 142 while iter: 143 files = self.medialist.get_value(iter,0) 144 source = self.medialist.get_value(iter,1) 145 newlistfiles.append([files,source]) 146 iter = self.medialist.iter_next(iter) 147 newlistfiles = sorted(newlistfiles) 148 self.medialist.clear() 149 for files,source in newlistfiles: 150 self.medialist.append([files,source]) 151 152 153 154 #Definicion del comando Guardar Permite guardar una lista 155 def showSave(self, widget): 156 self.filterbox.set_name("Lista Multimedia") 157 self.filterbox.add_pattern("*.pls") 158 self.save.add_filter(self.filterbox) 159 respt = self.save.run() 160 self.save.remove_filter(self.filterbox) 161 self.save.hide() 162 163 if respt == -5: 164 n = 0 165 filename = self.save.get_filename() 166 playlistsaved = open(filename + '.pls', 'w') 167 playlistsaved.write("[playlist]" + "\n") 168 169 iter=self.medialist.get_iter_first() 170 171 while iter: 172 n+=1 173 rutas = self.medialist.get_value(iter,1) 174 archivos = self.medialist.get_value(iter,0) 175 allmusicfiles = rutas+"/"+archivos 176 pathname = urllib.pathname2url(os.path.abspath(allmusicfiles)) 177 playlistsaved.write("File"+str(n)+"=file://"+pathname.encode('utf-8') + "\n") 178 iter = self.medialist.iter_next(iter) 179 180 #Definicion del Comando Acerca de: 181 def showAbout(self, widget, data=None): 182 self.about.run() 183 self.about.hide() 184 185 186 #Ejecucion del programa 187 if __name__ == "__main__": 188 main() 189 Gtk.main() 190