Advertisement
gregwa

Article 22

Mar 21st, 2011
584
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import sys
  3. from mutagen.mp3 import MP3
  4. try:
  5.     import pygtk
  6.     pygtk.require("2.0")
  7. except:
  8.     pass
  9. try:
  10.     import gtk
  11.     import gtk.glade
  12. except:
  13.     sys.exit(1)
  14.    
  15. class PlayListCreator:
  16.     def __init__(self):
  17.         self.gladefile = "playlistmaker.glade"
  18.         self.wTree = gtk.glade.XML(self.gladefile,"MainWindow")
  19.         self.SetEventDictionary()
  20.         self.SetWidgetReferences()  
  21.         self.SetupTreeview()      
  22.         self.CurrentPath = ""
  23.         self.CurrentRow = 0
  24.         self.RowCount = 0        
  25.        
  26.     def SetEventDictionary(self):
  27.         dict = {"on_MainWindow_destroy": gtk.main_quit,
  28.                 "on_tbtnQuit_clicked": gtk.main_quit,
  29.                 "on_tbtnAdd_clicked": self.on_tbtnAdd_clicked,
  30.                 "on_tbtnDelete_clicked": self.on_tbtnDelete_clicked,
  31.                 "on_tbtnClearAll_clicked": self.on_tbtnClearAll_clicked,
  32.                 "on_tbtnMoveToTop_clicked": self.on_tbtnMoveToTop_clicked,
  33.                 "on_tbtnMoveUp_clicked": self.on_tbtnMoveUp_clicked,
  34.                 "on_tbtnMoveDown_clicked": self.on_tbtnMoveDown_clicked,
  35.                 "on_tbtnMoveToBottom_clicked": self.on_tbtnMoveToBottom_clicked,                                                
  36.                 "on_tbtnAbout_clicked": self.on_tbtnAbout_clicked,
  37.                 "on_btnGetFolder_clicked": self.on_btnGetFolder_clicked,
  38.                 "on_btnSavePlaylist_clicked": self.on_btnSavePlaylist_clicked}
  39.         self.wTree.signal_autoconnect(dict)
  40.  
  41.     def SetWidgetReferences(self):
  42.         self.txtFilename = self.wTree.get_widget("txtFilename")
  43.         self.txtPath = self.wTree.get_widget("txtPath")
  44.         self.tbtnAdd = self.wTree.get_widget("tbtnAdd")
  45.         self.tbtnDelete = self.wTree.get_widget("tbtnDelete")
  46.         self.tbtnClearAll = self.wTree.get_widget("tbtnClearAll")
  47.         self.tbtnQuit = self.wTree.get_widget("tbtnQuit")
  48.         self.tbtnAbout = self.wTree.get_widget("tbtnAbout")
  49.         self.tbtnMoveToTop = self.wTree.get_widget("tbtnMoveToTop")
  50.         self.tbtnMoveUp = self.wTree.get_widget("tbtnMoveUp")
  51.         self.tbtnMoveDown = self.wTree.get_widget("tbtnMoveDown")
  52.         self.tbtnMoveToBottom = self.wTree.get_widget("tbtnMoveToBottom")
  53.         self.btnGetFolder = self.wTree.get_widget("btnGetFolder")
  54.         self.btnSavePlaylist = self.wTree.get_widget("btnSavePlaylist")        
  55.         self.sbar = self.wTree.get_widget("statusbar1")
  56.         self.context_id = self.sbar.get_context_id("Statusbar")
  57.            
  58.     def SetupTreeview(self):
  59.         self.cFName = 0
  60.         self.cFType = 1
  61.         self.cFPath = 2
  62.         self.sFName = "Filename"
  63.         self.sFType = "Type"
  64.         self.sFPath = "Folder"
  65.         self.treeview = self.wTree.get_widget("treeview1")
  66.         self.AddPlaylistColumn(self.sFName,self.cFName)
  67.         self.AddPlaylistColumn(self.sFType,self.cFType)
  68.         self.AddPlaylistColumn(self.sFPath,self.cFPath)
  69.         self.playList = gtk.ListStore(str,str,str)
  70.         self.treeview.set_model(self.playList)
  71.         self.treeview.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_BOTH)
  72.        
  73.     def AddPlaylistColumn(self,title,columnId):
  74.         column = gtk.TreeViewColumn(title,gtk.CellRendererText(),text=columnId)
  75.         column.set_resizable(True)
  76.         column.set_sort_column_id(columnId)
  77.         self.treeview.append_column(column)
  78.                
  79.     def on_tbtnAdd_clicked(self,widget):
  80.         #self.MessageBox("info","Button Add was clicked...")
  81.         fd = FileDialog()
  82.         selectedfiles,self.CurrentPath = fd.ShowDialog(0,self.CurrentPath)        
  83. ##        for f in selectedfiles:
  84. ##            print "User selected %s" % f
  85. ##        print "Current path is %s" % self.CurrentPath
  86.         self.AddFilesToTreeview(selectedfiles)        
  87.                
  88.     def on_tbtnDelete_clicked(self,widget):
  89.         self.MessageBox("info","Button Delete was clicked...")
  90.     def on_tbtnClearAll_clicked(self,widget):
  91.         self.MessageBox("info","Button ClearAll was clicked...")
  92.     def on_tbtnMoveToTop_clicked(self,widget):
  93.         self.MessageBox("info","Button MoveToTop was clicked...")
  94.     def on_tbtnMoveUp_clicked(self,widget):
  95.         self.MessageBox("info","Button MoveUp was clicked...")
  96.     def on_tbtnMoveDown_clicked(self,widget):
  97.         self.MessageBox("info","Button MoveDown was clicked...")
  98.     def on_tbtnMoveToBottom_clicked(self,widget):
  99.         self.MessageBox("info","Button MoveToBottom was clicked...")
  100.     def on_tbtnAbout_clicked(self,widget):
  101.         #self.MessageBox("info","Button About was clicked...")
  102.         self.ShowAbout()
  103.        
  104.     def on_btnGetFolder_clicked(self,widget):
  105.         self.MessageBox("info","Button GetFolder was clicked...")
  106.     def on_btnSavePlaylist_clicked(self,widget):
  107.         self.MessageBox("info","Button SavePlaylist was clicked...")
  108.    
  109.     def AddFilesToTreeview(self,FileList):
  110.         counter = 0
  111.         for f in FileList:
  112.             extStart = f.rfind(".")
  113.             fnameStart = f.rfind("/")
  114.             extension = f[extStart+1:]
  115.             fname = f[fnameStart+1:extStart]
  116.             fpath = f[:fnameStart]
  117.             data = [fname,extension,fpath]
  118.             self.playList.append(data)
  119.             counter += 1
  120.         self.RowCount += counter
  121.         self.sbar.push(self.context_id,"%s files added for a total of %d" % (counter,self.RowCount))    
  122.     def MessageBox(self,level,text):
  123.         if level == "info":
  124.             dlg = gtk.MessageDialog(None,0,gtk.MESSAGE_INFO,gtk.BUTTONS_OK,text)
  125.         elif level == "warning":
  126.             dlg = gtk.MessageDialog(None,0,gtk.MESSAGE_WARNING,gtk.BUTTONS_OK,text)
  127.         elif level == "error":
  128.             dlg = gtk.MessageDialog(None,0,gtk.MESSAGE_ERROR,gtk.BUTTONS_OK,text)
  129.         elif level == "question":
  130.             dlg = gtk.MessageDialog(None,0,gtk.MESSAGE_QUESTION,gtk.BUTTONS_YES_NO,text)
  131.         if level == "question":
  132.             resp = dlg.run()
  133.             dlg.destroy()
  134.             return resp
  135.         else:
  136.             resp = dlg.run()
  137.             dlg.destroy()    
  138.  
  139.     def ShowAbout(self):
  140.         about = gtk.AboutDialog()
  141.         about.set_program_name("Playlist Maker")
  142.         about.set_version("1.0")
  143.         about.set_copyright("(c) 2011 by Greg Walters")
  144.         about.set_comments("Written for Full Circle Magazine")
  145.         about.set_website("http://thedesignatedgeek.com")
  146.         about.run()
  147.         about.destroy()            
  148.            
  149. class FileDialog:
  150.     def ShowDialog(self,which,CurrentPath):
  151.         if which == 0: # file chooser
  152.             dialog = gtk.FileChooserDialog("Select files to add...",None,
  153.                                gtk.FILE_CHOOSER_ACTION_OPEN,
  154.                                (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
  155.                                 gtk.STOCK_OPEN, gtk.RESPONSE_OK))
  156.             filter = gtk.FileFilter()
  157.             filter.set_name("Music Files")
  158.             filter.add_pattern("*.mp3")
  159.             filter.add_pattern("*.ogg")
  160.             filter.add_pattern("*.wav")
  161.             dialog.add_filter(filter)
  162.             filter = gtk.FileFilter()
  163.             filter.set_name("All files")
  164.             filter.add_pattern("*")
  165.             dialog.add_filter(filter)
  166.                                
  167.         else:          # folder chooser
  168.             dialog = gtk.FileChooserDialog("Select Save Folder..",None,
  169.                                 gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
  170.                                (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
  171.                                 gtk.STOCK_OPEN, gtk.RESPONSE_OK))
  172.            
  173.         dialog.set_default_response(gtk.RESPONSE_OK)
  174.         dialog.set_select_multiple(True)
  175.         if CurrentPath != "":
  176.             dialog.set_current_folder(CurrentPath)
  177.         response = dialog.run()
  178.         if response == gtk.RESPONSE_OK:
  179.             fileselection = dialog.get_filenames()
  180.             CurrentPath = dialog.get_current_folder()
  181.             dialog.destroy()
  182.             return (fileselection,CurrentPath)
  183.         elif response == gtk.RESPONSE_CANCEL:
  184.             print 'Closed, no files selected'
  185.             dialog.destroy()                
  186.                            
  187. if __name__ == "__main__":
  188.     plc = PlayListCreator()
  189.     gtk.main()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement