home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / upnp_coherence / __init__.py next >
Encoding:
Python Source  |  2009-04-07  |  6.0 KB  |  184 lines

  1. # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
  2. #
  3. # Copyright 2008, Frank Scholz <coherence@beebits.net>
  4. # Copyright 2008, James Livingston <doclivingston@gmail.com>
  5. #
  6. # Licensed under the MIT license
  7. # http://opensource.org/licenses/mit-license.php
  8.  
  9.  
  10. import rhythmdb, rb
  11. import gobject, gtk
  12.  
  13. import coherence.extern.louie as louie
  14.  
  15. from coherence import log
  16.  
  17. # For the icon
  18. import os.path, urllib, gtk.gdk
  19.  
  20. class CoherencePlugin(rb.Plugin,log.Loggable):
  21.  
  22.     logCategory = 'rb_coherence_plugin'
  23.  
  24.     def __init__(self):
  25.         rb.Plugin.__init__(self)
  26.         self.coherence = None
  27.  
  28.     def activate(self, shell):
  29.         from twisted.internet import gtk2reactor
  30.         try:
  31.             gtk2reactor.install()
  32.         except AssertionError, e:
  33.             # sometimes it's already installed
  34.             print e
  35.  
  36.         self.coherence = self.get_coherence()
  37.         if self.coherence is None:
  38.             print "Coherence is not installed or too old, aborting"
  39.             return
  40.  
  41.         print "coherence UPnP plugin activated"
  42.         self.shell = shell
  43.         self.sources = {}
  44.  
  45.         # Set up our icon
  46.         the_icon = None
  47.         face_path = os.path.join(os.path.expanduser('~'), ".face")
  48.         if os.path.exists(face_path):
  49.             url = "file://" + urllib.pathname2url(face_path)
  50.         try:
  51.             import gio
  52.             f = gio.File(url)
  53.             fi = f.query_info(gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE)
  54.             ctype = fi.get_attribute_string(gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE)
  55.             mimetype = gio.content_type_get_mime_type(ctype)
  56.         except:
  57.             import gnomevfs
  58.             mimetype = gnomevfs.get_mime_type(url)
  59.  
  60.             pixbuf = gtk.gdk.pixbuf_new_from_file(face_path)
  61.             width = "%s" % pixbuf.get_width()
  62.             height = "%s" % pixbuf.get_height()
  63.             depth = '24'
  64.             the_icon = {
  65.                 'url':url,
  66.                 'mimetype':mimetype,
  67.                 'width':width,
  68.                 'height':height,
  69.                 'depth':depth
  70.                 }
  71.         else:
  72.             the_icon = None
  73.  
  74.         # create our own media server
  75.         from coherence.upnp.devices.media_server import MediaServer
  76.         from MediaStore import MediaStore
  77.         if the_icon:
  78.             server = MediaServer(self.coherence, MediaStore, no_thread_needed=True, db=self.shell.props.db, plugin=self, icon=the_icon)
  79.         else:
  80.             server = MediaServer(self.coherence, MediaStore, no_thread_needed=True, db=self.shell.props.db, plugin=self)
  81.  
  82.         self.uuid = str(server.uuid)
  83.  
  84.         if self.coherence_version >= (0,5,2):
  85.             # create our own media renderer
  86.             # but only if we have a matching Coherence package installed
  87.             from coherence.upnp.devices.media_renderer import MediaRenderer
  88.             from MediaPlayer import RhythmboxPlayer
  89.             if the_icon:
  90.                 MediaRenderer(self.coherence, RhythmboxPlayer, no_thread_needed=True, shell=self.shell, icon=the_icon)
  91.             else:
  92.                 MediaRenderer(self.coherence, RhythmboxPlayer, no_thread_needed=True, shell=self.shell)
  93.  
  94.         # watch for media servers
  95.         louie.connect(self.detected_media_server,
  96.                 'Coherence.UPnP.ControlPoint.MediaServer.detected',
  97.                 louie.Any)
  98.         louie.connect(self.removed_media_server,
  99.                 'Coherence.UPnP.ControlPoint.MediaServer.removed',
  100.                 louie.Any)
  101.  
  102.  
  103.     def deactivate(self, shell):
  104.         print "coherence UPnP plugin deactivated"
  105.         if self.coherence is None:
  106.             return
  107.  
  108.         self.coherence.shutdown()
  109.  
  110.         louie.disconnect(self.detected_media_server,
  111.                 'Coherence.UPnP.ControlPoint.MediaServer.detected',
  112.                 louie.Any)
  113.         louie.disconnect(self.removed_media_server,
  114.                 'Coherence.UPnP.ControlPoint.MediaServer.removed',
  115.                 louie.Any)
  116.  
  117.         del self.shell
  118.         del self.coherence
  119.  
  120.         for usn, source in self.sources.iteritems():
  121.             source.delete_thyself()
  122.         del self.sources
  123.  
  124.         # uninstall twisted reactor? probably not, since other things may have used it
  125.  
  126.  
  127.     def get_coherence (self):
  128.         coherence_instance = None
  129.         required_version = (0, 5, 7)
  130.  
  131.         try:
  132.             from coherence.base import Coherence
  133.             from coherence import __version_info__
  134.         except ImportError, e:
  135.             print "Coherence not found"
  136.             return None
  137.  
  138.         if __version_info__ < required_version:
  139.             required = '.'.join([str(i) for i in required_version])
  140.             found = '.'.join([str(i) for i in __version_info__])
  141.             print "Coherence %s required. %s found. Please upgrade" % (required, found)
  142.             return None
  143.  
  144.         self.coherence_version = __version_info__
  145.  
  146.         coherence_config = {
  147.             #'logmode': 'info',
  148.             'controlpoint': 'yes',
  149.             'plugins': {},
  150.         }
  151.         coherence_instance = Coherence(coherence_config)
  152.  
  153.         return coherence_instance
  154.  
  155.     def removed_media_server(self, udn):
  156.         print "upnp server went away %s" % udn
  157.         if self.sources.has_key(udn):
  158.             self.sources[udn].delete_thyself()
  159.             del self.sources[udn]
  160.  
  161.     def detected_media_server(self, client, udn):
  162.         print "found upnp server %s (%s)"  %  (client.device.get_friendly_name(), udn)
  163.         self.warning("found upnp server %s (%s)"  %  (client.device.get_friendly_name(), udn))
  164.         if client.device.get_id() == self.uuid:
  165.             """ don't react on our own MediaServer"""
  166.             return
  167.  
  168.         db = self.shell.props.db
  169.         group = rb.rb_source_group_get_by_name ("shared")
  170.         entry_type = db.entry_register_type("CoherenceUpnp:" + client.device.get_id()[5:])
  171.  
  172.         from UpnpSource import UpnpSource
  173.         source = gobject.new (UpnpSource,
  174.                     shell=self.shell,
  175.                     entry_type=entry_type,
  176.                     source_group=group,
  177.                     plugin=self,
  178.                     client=client,
  179.                     udn=udn)
  180.  
  181.         self.sources[udn] = source
  182.  
  183.         self.shell.append_source (source, None)
  184.