clPlaylists.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2009 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 import clPlaylistDialog
00022 from modules import clEntry
00023 import gtk
00024 import os
00025 from modules import functions
00026 from modules.clGstreamer010 import Track
00027 
00028 class main():
00029     def __init__(self, pyjama):
00030         self.pyjama = pyjama
00031 
00032         self.pyjama.Events.connect_event("alldone", self.ev_alldone)
00033         self.pyjama.Events.connect_event("populate_playlistmenu", self.ev_populate_playlistmenu)
00034         self.pyjama.Events.connect_event("populate_listmenu", self.ev_populate_listmenu)
00035 
00036     def ev_populate_listmenu(self, rootmenu):
00037         selection = self.pyjama.window.tvList.get_selection()
00038         model, tmpIter = selection.get_selected()
00039         if tmpIter is None: return
00040 
00041         path =  model.get_path(tmpIter)
00042         ret = self.pyjama.window.tvList.get_item(path)
00043         track = Track()
00044         track.id = ret[self.pyjama.window.tvList.COLUMN_TRACKID]
00045         if track.id < 0: return
00046 
00047 
00048         mnu = gtk.ImageMenuItem("Add track to playlist")
00049         rootmenu.append(mnu)
00050 
00051 
00052         try:
00053             img = gtk.Image()
00054             pix = gtk.gdk.pixbuf_new_from_file_at_size(os.path.join(functions.install_dir(), "images", "playlist.png"), 16, 16)
00055             img.set_from_pixbuf(pix)
00056             mnu.set_image(img)
00057         except:
00058             print ("Playlist image not found or corrupt")
00059 
00060 
00061 
00062         submenu = gtk.Menu()
00063         addnew = gtk.ImageMenuItem("Create new playlist")
00064         addnew.connect("activate", self.cb_addnew_playlist_activate, track)
00065         img = gtk.Image()
00066         img.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_MENU)
00067         addnew.set_image(img)
00068         addnew.show()
00069         submenu.append(addnew)
00070         sep = gtk.SeparatorMenuItem()
00071         sep.show()
00072         submenu.append(sep)
00073 
00074 #        if self.pyjama.settings.section_exists("Playlists"):
00075 #            playlists = self.pyjama.settings.config.items("Playlists")
00076         sql = "SELECT option, value FROM settings WHERE section='playlists'"
00077         playlists = self.pyjama.settingsdb.query(sql)
00078         if playlists:
00079             for name, ids in playlists:
00080                 tmp = gtk.ImageMenuItem(name.replace("_", "__"))
00081                 tmp.connect("activate", self.cb_append_to_playlist, name, track)
00082                 submenu.append(tmp)
00083                 tmp.show()
00084 
00085         mnu.set_submenu(submenu)
00086         submenu.show()
00087         mnu.show()
00088 
00089     def ev_populate_playlistmenu(self, rootmenu):
00090         model, tmpIter = self.pyjama.window.tvSelection.get_selected()
00091         if tmpIter is None: return
00092         path =  model.get_path(tmpIter)
00093         track = self.pyjama.player.playlist[path[0]]
00094 
00095         mnu = gtk.ImageMenuItem("Add track to playlist")
00096         rootmenu.append(mnu)
00097 
00098         try:
00099             img = gtk.Image()
00100             pix = gtk.gdk.pixbuf_new_from_file_at_size(os.path.join(functions.install_dir(), "images", "playlist.png"), 16, 16)
00101             img.set_from_pixbuf(pix)
00102             mnu.set_image(img)
00103         except:
00104             print ("Playlist image not found or corrupt")
00105 
00106         submenu = gtk.Menu()
00107         addnew = gtk.ImageMenuItem("Create new playlist")
00108         addnew.connect("activate", self.cb_addnew_playlist_activate, track)
00109         img = gtk.Image()
00110         img.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_MENU)
00111         addnew.set_image(img)
00112         addnew.show()
00113         submenu.append(addnew)
00114         sep = gtk.SeparatorMenuItem()
00115         sep.show()
00116         submenu.append(sep)
00117 
00118 #        if self.pyjama.settings.section_exists("Playlists"):
00119 #            playlists = self.pyjama.settings.config.items("Playlists")
00120         sql = "SELECT option, value FROM settings WHERE section='playlists'"
00121         playlists = self.pyjama.settingsdb.query(sql)
00122         if playlists:
00123             for name, ids in playlists:
00124                 tmp = gtk.ImageMenuItem(name.replace("_", "__"))
00125                 tmp.connect("activate", self.cb_append_to_playlist, name, track)
00126                 submenu.append(tmp)
00127                 tmp.show()
00128 
00129         mnu.set_submenu(submenu)
00130         submenu.show()
00131         mnu.show()
00132 
00133     def cb_addnew_playlist_activate(self, activate, track):
00134         # ask for name
00135         result = clEntry.input_box(title=_('Playlist'),
00136             message=_('Please enter a name for your new playlist.\nExisting playlists will be overwritten'),
00137             default_text="MyPlaylist")
00138         if result is None:
00139             return
00140         name = str(result)
00141 
00142         pl_string = "-SEP-%s" % str(track.id)
00143         # Append to bookmarks and write to config
00144         self.pyjama.settingsdb.set_value("playlists", name, pl_string)
00145 
00146         # this part needs to be fixed - the dict submenu
00147         # is not needed any more, use get_childs instead!
00148         if not name in self.pyjama.window.menubar.submenus:
00149             # Append menu entry
00150             menu = self.pyjama.window.menubar
00151             playlist = menu.append_entry(menu.get_rootmenu("Playlists"), name, name)
00152             playlist.connect("activate", self.cb_load_playlist, name)
00153         
00154 
00155     def cb_append_to_playlist(self, widget,  name, track):
00156         tracks = self.pyjama.settingsdb.get_value("playlists", name, None)
00157         if tracks is not None:
00158             tracks += "-SEP-%s" % str(track.id)
00159         else:
00160             tracks = "-SEP-%s" % str(track.id)
00161         self.pyjama.settingsdb.set_value("playlists", name, tracks)
00162 
00163     def ev_alldone(self):
00164         # Create Bookmarks menu
00165         menu = self.pyjama.window.menubar
00166         playlists = menu.insert_root_before("Extras", _("Playlists"), "Playlists")
00167 
00168         translation_array = [_("Import Playlist"), _("Save playlist"), _("Export Playlist"), _("Import Playlist from Jamendo"), _("Manage Playlists")]
00169 
00170         sub = ["Save playlist", "Import Playlist", "Import Playlist from Jamendo", "Export Playlist", "---", "Manage Playlists", "---"]  
00171         self.playlists_menu = menu.create_submenu(rootmenu=playlists, submenu=sub)
00172 
00173         save_playlist = menu.get_submenu("Save playlist")
00174         menu.set_item_image(save_playlist, gtk.STOCK_SAVE )
00175         save_playlist.connect("activate", self.cb_save_playlist)
00176 
00177         save_playlist_to_disc = menu.get_submenu("Export Playlist")
00178         menu.set_item_image(save_playlist_to_disc, gtk.STOCK_SAVE_AS )
00179         save_playlist_to_disc.connect("activate", self.pyjama.window.cb_export_playlist)
00180 
00181         load_playlist_from_disc = menu.get_submenu("Import Playlist")
00182         menu.set_item_image(load_playlist_from_disc, gtk.STOCK_OPEN )
00183         load_playlist_from_disc.connect("activate", self.cb_import_playlist)
00184 
00185         import_playlists = menu.get_submenu("Import Playlist from Jamendo")
00186         menu.set_item_image(import_playlists, gtk.STOCK_NETWORK )
00187         import_playlists.connect("activate", self.cb_import_playlists)
00188 
00189         manage_playlists = menu.get_submenu("Manage Playlists")
00190         menu.set_item_image(manage_playlists, gtk.STOCK_EDIT )
00191         manage_playlists.connect("activate", self.cb_manage_playlists)
00192 
00193 #        ret = self.pyjama.settingsdb.create_table("playlists")
00194 #        if ret == "created": print ("Created playlists table")
00195 
00196         # Append menu entrys
00197         menu = self.pyjama.window.menubar
00198         root = menu.get_rootmenu("Playlists")
00199         if self.pyjama.settings.section_exists("Playlists"):
00200             for name, ids in self.pyjama.settings.config.items("Playlists"):
00201 #                playlist = menu.append_entry(root, name, name)
00202 #                playlist.connect("activate", self.cb_load_playlist, name)
00203                 # move all playlists from configfile to settingsdb
00204                 print ("Playlist '%s' was moved from configfile to database"%name)
00205                 self.pyjama.settingsdb.set_value("playlists", name, ids.replace("+", "-SEP-"))
00206                 # delete the old entries
00207                 self.pyjama.settings.remove_option("Playlists", name)
00208 
00209         sql = "SELECT option, value FROM settings WHERE section='playlists'"
00210         playlists = self.pyjama.settingsdb.query(sql)
00211         if playlists:
00212             for name, ids in playlists:
00213                 playlist = menu.append_entry(root, name, name)
00214                 playlist.connect("activate", self.cb_load_playlist, name)
00215         
00216 
00217 #    def create_table(self):
00218 #        sql = "select count(name) from sqlite_master where name = 'playlists'" 
00219 #        result = self.pyjama.settingsdb.query(sql)
00220 #        if ret[0][0] == 0:
00221 #            sql = """
00222 #            CREATE TABLE playlists (
00223 #              uid INTEGER PRIMARY KEY,
00224 #              name STRING,
00225 #              track INTEGER,
00226 #            )
00227 #            """
00228 #            self.pyjama.settingsdb.query(sql)
00229 #            print ("Created table for playlists")
00230 #            return "created"
00231 #        else:
00232 #            return "existant"
00233 
00234     def cb_import_playlist(self, ev):
00235         #TO BE IMPROVED:
00236         #Right now pyjama queries every trackid
00237         #at the database on its own.
00238 
00239         buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN,   gtk.RESPONSE_OK)
00240         dialog = gtk.FileChooserDialog(_("Load Playlist"), None, action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=buttons, backend=None)
00241         filter1 = gtk.FileFilter()
00242         filter1.set_name("M3U Playlist Format")
00243         filter1.add_pattern("*.m3u")
00244         dialog.add_filter(filter1)
00245 #        filter2 = gtk.FileFilter()
00246 #        filter2.set_name("XSPF Playlist Format")
00247 #        filter2.add_pattern("*.xspf")
00248 #        dialog.add_filter(filter2)
00249 
00250         dialog.set_default_response(gtk.RESPONSE_OK)
00251         dialog.set_current_folder(os.getenv("HOME"))
00252 #        dialog.set_current_name("pyjama-playlist")
00253 
00254         response = dialog.run()
00255         dialog.hide()
00256         if response == gtk.RESPONSE_OK and dialog.get_filename() is not None:
00257             filename = dialog.get_filename()
00258             self.open_playlist_from_file(filename)
00259             dialog.destroy()
00260 
00261     def open_playlist_from_file(self, filename):
00262         tracks = self.pyjama.read_playlist(filename)
00263         if tracks is not None:
00264             ret = self.pyjama.db.get_multiple_trackinfos(tracks)
00265             if ret is None:
00266                 #~ self.pyjama.info("Playlist Error", "None of the tracks in the playlist were found in the local database")
00267                 return
00268             if ret == []:
00269                 gtk.gdk.threads_enter()
00270                 self.pyjama.info("Playlist Error", "None of the tracks in the playlist were found in the local database")
00271                 gtk.gdk.threads_leave()
00272                 return
00273             for track_id in tracks:
00274                 inlist = False
00275                 for track in ret:
00276                     if int(track.id) == int(track_id):
00277                         inlist = True
00278                         self.pyjama.add2playlist(track)
00279                 if not inlist:
00280                     print ("Track '%i' not in database, yet.")
00281 
00282     def cb_manage_playlists(self, widget):
00283         dialog = clPlaylistDialog.PlaylistManageDialog(self)
00284 
00285 #        if self.pyjama.settings.section_exists("Playlists"):
00286 #            playlists = self.pyjama.settings.config.items("Playlists")
00287         sql = "SELECT option, value FROM settings WHERE section='playlists'"
00288         playlists = self.pyjama.settingsdb.query(sql)
00289         if playlists:
00290             dialog.populate_list(playlists)
00291         result = dialog.run()
00292         model = dialog.treeview.get_model()
00293         dialog.destroy()
00294         if result == 1: # delete
00295             iter = model.get_iter_first()
00296             menu = self.pyjama.window.menubar
00297             while iter:
00298                 checked, name = ( model.get(iter, 0, 1) )
00299                 if checked == True:
00300                     mnu = menu.get_submenu(name).destroy()
00301                     sql = "DELETE FROM settings WHERE section='playlists' and option = '%s'" % name
00302                     playlists = self.pyjama.settingsdb.query(sql)
00303 
00304                     # this part needs to be fixed - the dict submenu
00305                     # is not needed any more, use get_childs instead!
00306                     del self.pyjama.window.menubar.submenus[name]
00307                 iter = model.iter_next(iter)
00308 
00309     def cb_import_playlists(self, widget):
00310         dialog = clPlaylistDialog.PlaylistDialog(self)
00311         result = dialog.run()
00312         model = dialog.treeview.get_model()
00313         dialog.destroy()
00314         if result == 1:
00315             iter = model.get_iter_first()
00316             while iter:
00317                 checked, name, id = ( model.get(iter, 0, 1, 2) )
00318                 if checked == True:
00319                     self.import_playlist(name, id)
00320                 iter = model.iter_next(iter)
00321 
00322     def cb_save_playlist(self, widget):
00323         dialog = clPlaylistDialog.SaveDialog(self)
00324         result = dialog.run()
00325         entry = dialog.entry
00326         dialog.destroy()
00327         if result == 1: #save
00328             name = entry.get_text()
00329             playlist = self.pyjama.player.playlist
00330             pl_string = ""
00331             for track in playlist:
00332                 pl_string += "-SEP-%s" % str(track.id)
00333                 # Append to bookmarks and write to config
00334                 self.pyjama.settingsdb.set_value("playlists", name, pl_string)
00335 
00336                 # this part needs to be fixed - the dict submenu
00337                 # is not needed any more, use get_childs instead!
00338                 if not name in self.pyjama.window.menubar.submenus:
00339                     # Append menu entry
00340                     menu = self.pyjama.window.menubar
00341                     playlist = menu.append_entry(menu.get_rootmenu("Playlists"), name, name)
00342                     playlist.connect("activate", self.cb_load_playlist, name)
00343 
00344 
00345     def cb_load_playlist(self, menu, name):
00346         
00347         tracks = self.pyjama.settingsdb.get_value("playlists", name, None)
00348         if tracks is not None:
00349             tracks = tracks[5:].split("-SEP-")
00350             tracks = self.pyjama.db.get_multiple_trackinfos(tracks)
00351             self.pyjama.appendtracks(tracks)
00352 
00353 
00354     def import_playlist(self, name, id):
00355         print "Going to import Playlist '%s' with the id '%i'" % (name, id)
00356         tracks = self.playlist_tracks(int(id))
00357         if tracks != []:
00358             track_string = ""
00359             for track in tracks:
00360                 track_string += "-SEP-%i" % track
00361 
00362 #            name = name.replace(" ", "_").replace("=", "_").replace("#", "_")
00363 
00364             # Append to bookmarks and write to config
00365             self.pyjama.settingsdb.set_value("playlists", name, track_string)
00366 
00367             # Append menu entry
00368             menu = self.pyjama.window.menubar
00369             playlist = menu.append_entry(menu.get_rootmenu("Playlists"), name, name)
00370             playlist.connect("activate", self.cb_load_playlist, name)
00371 
00372 
00373     def user_playlists(self, user):
00374         ret = self.pyjama.jamendo.query("name+id/playlist/json/?user_idstr=%s&n=all" % (user), self.pyjama.settings.get_value("JAMENDO", "CACHING_TIME_SHORT"))
00375         return ret
00376 
00377     def playlist_tracks(self, playlist_id):
00378         ret = self.pyjama.jamendo.query("track_id/playlist/json/?id=%i" % playlist_id)
00379         return ret

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8