home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / serpentine / plugins / plugrhythmbox.py < prev    next >
Encoding:
Python Source  |  2006-08-23  |  2.9 KB  |  92 lines

  1. # -*- encoding: utf-8 -*-
  2. import gtk
  3. import os.path
  4. import weakref
  5.  
  6. from xml.xpath import Evaluate
  7. from xml.dom import minidom
  8. from gettext import gettext as _
  9.  
  10. if __name__ == '__main__':
  11.     import sys
  12.     basedir = os.path.join (os.path.join(os.path.dirname (__file__), ".."), "..")
  13.     sys.path.insert (0, basedir)
  14.  
  15. from serpentine import gtkutil
  16.  
  17. PLAYLISTS = os.path.join (os.path.expanduser("~"), ".gnome2", "rhythmbox", "playlists.xml")
  18.  
  19. def rhythmbox_list_names ():
  20.     root = minidom.parse (PLAYLISTS)
  21.     getvalue = lambda node: node.attributes["name"].value
  22.     return map(getvalue, Evaluate ("/rhythmdb-playlists/playlist", root))
  23.  
  24. def rhythmbox_get_playlist (playlist_name):
  25.     root = minidom.parse (PLAYLISTS)
  26.     path = "/rhythmdb-playlists/playlist[@name = '%s']/location/text()" % playlist_name
  27.     getvalue = lambda node: node.nodeValue
  28.     return map(getvalue, Evaluate (path, root))
  29.  
  30. class RhythmboxListener (object):
  31.     def __init__ (self, app):
  32.         self._app = weakref.ref (app)
  33.     
  34.     app = property (lambda self: self._app())
  35.     
  36.     def on_clicked (self, menu):
  37.         playlists = rhythmbox_list_names()
  38.  
  39.         if len(playlists) == 0:
  40.             gtkutil.dialog_warn(
  41.                 _("No Rhythmbox playlist found"),
  42.                 _("Please create a playlist using <i>Music→Playlist→New Playlist...</i>"),
  43.                 parent=self.app.window_widget
  44.             )
  45.             return
  46.         
  47.         indexes, response = gtkutil.choice_dialog(
  48.             _("Which playlist do you choose to open?"),
  49.             _("These are the playlists created on Rhythmbox."),
  50.             one_item_text = _("Do you want to open the playlist <i>%s</i>?"),
  51.             list_title = _("Rhythmbox playlists:"),
  52.             items = playlists,
  53.             parent = self.app.window_widget,
  54.             min_select = 1,
  55.             max_select = 1,
  56.             ok_button = gtk.STOCK_OPEN,
  57.         )
  58.         
  59.         if response == gtk.RESPONSE_OK:
  60.             self.app.music_list.clear ()
  61.             files = rhythmbox_get_playlist(playlists[indexes[0]])
  62.             self.app.add_files (files).start ()
  63.     
  64. def create_plugin (app):
  65.     if not hasattr (app, "window_widget"):
  66.         return
  67.     
  68.     window = app.window_widget
  69.     theme = gtk.icon_theme_get_default ()
  70.     rhyt = gtk.ImageMenuItem (_("Open Rhythmbox Playlist..."))
  71.  
  72.     if theme.has_icon("rhythmbox"):
  73.         img = gtk.image_new_from_icon_name("rhythmbox", gtk.ICON_SIZE_MENU)
  74.         img.show ()
  75.         rhyt.set_image (img)
  76.  
  77.     rhyt.show ()
  78.     listener = RhythmboxListener (app)
  79.     rhyt.connect ("activate", listener.on_clicked)
  80.     
  81.     file_menu = gtkutil.find_child_widget (window, "file_menu")
  82.     file_menu.add (rhyt)
  83.     file_menu.reorder_child (rhyt, 4)
  84.  
  85. if __name__ == '__main__':
  86.     import sys
  87.  
  88.     if len (sys.argv) != 2:
  89.         print rhythmbox_list_names ()
  90.     
  91.     else:
  92.         print rhythmbox_get_playlist (sys.argv[1])