/plugins_base/currentSong/Amarok.py

https://github.com/csuarez/emesene-1.6.3-fixed · Python · 120 lines · 77 code · 25 blank · 18 comment · 29 complexity · 2c9f81b7c65ffffcc06ea10e34d6eb10 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. # This file is part of emesene.
  3. #
  4. # Emesene is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # emesene is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with emesene; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. VERSION = '0.3'
  18. import os
  19. import CurrentSong
  20. error = False
  21. dcopext = None
  22. pydcop = None
  23. try:
  24. import dcopext
  25. except:
  26. try:
  27. import pydcop
  28. except:
  29. error = True
  30. class Amarok( CurrentSong.CurrentSong ):
  31. def __init__( self ):
  32. CurrentSong.CurrentSong.__init__( self )
  33. self.amarok = None
  34. if dcopext:
  35. self.client = dcopext.DCOPClient()
  36. self.client.attach()
  37. self.check()
  38. def _isServerRunning( self ):
  39. try:
  40. assert os.system( "ps aux|grep dcopserver|grep -v grep" ) == 0
  41. except:
  42. return False
  43. else:
  44. return True
  45. def isRunning( self ):
  46. if pydcop:
  47. return bool(self._isServerRunning() and "amarok" in pydcop.apps())
  48. elif dcopext:
  49. return bool(self.client.isApplicationRegistered('amarok'))
  50. def isPlaying( self ):
  51. if self.amarok:
  52. if pydcop and self.amarok.player.isPlaying():
  53. return True
  54. elif dcopext and self.amarok.player.isPlaying()[1]:
  55. return True
  56. return False
  57. def getStatus( self ):
  58. '''check if everything is OK to start the plugin
  59. return a tuple whith a boolean and a message
  60. if OK -> ( True , 'some message' )
  61. else -> ( False , 'error message' )'''
  62. global error
  63. if error:
  64. return ( False, _( "Can't import dcopext or python-dcop!" ) )
  65. if pydcop and not self._isServerRunning():
  66. return ( False, _( "dcop not running" ) )
  67. if os.name != 'posix':
  68. return ( False, _( 'This plugin only works in posix systems' ) )
  69. if pydcop and not self.is_on_path( 'dcop' ):
  70. return ( False, _( 'Dcop not found' ) )
  71. return ( True, 'Ok' )
  72. def check( self ):
  73. global error
  74. if error:
  75. return False
  76. if self.amarok == None and self.isRunning():
  77. if dcopext:
  78. self.amarok = dcopext.DCOPApp("amarok", self.client)
  79. elif pydcop:
  80. self.amarok = pydcop.DCOPApplication( "amarok" )
  81. if self.isPlaying():
  82. if dcopext:
  83. artist = self.amarok.player.artist()[1]
  84. title = self.amarok.player.title()[1]
  85. album = self.amarok.player.album()[1]
  86. elif pydcop:
  87. artist = self.amarok.player.artist()
  88. title = self.amarok.player.title()
  89. album = self.amarok.player.album()
  90. if self.artist != artist or self.title != title:
  91. self.artist = artist
  92. self.title = title
  93. self.album = album
  94. return True
  95. return False