PageRenderTime 35ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/podpoochlib/playpod.py

https://github.com/timabell/mokopod
Python | 168 lines | 148 code | 5 blank | 15 comment | 0 complexity | b0f471233499d6b006dec1e9515b5f8b MD5 | raw file
Possible License(s): GPL-3.0
  1. # This program is free software: you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation, either version 3 of the License, or
  4. # (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. import gobject,gtk, os, sys, dbus
  14. import pymplayer, signal
  15. from time import strftime
  16. import pickle
  17. class view:
  18. def __init__(self, parent_window, episode):
  19. self.w = gtk.Window()
  20. self.w.set_modal(True)
  21. self.w.set_title("Playing podcast...")
  22. #self.w.set_decorated(False)
  23. #self.w.set_geometry_hints(None, 1000, 1000)
  24. self.w.maximize()
  25. # Main page
  26. mainvbox = gtk.VBox(False, 10)
  27. mainvbox.add(gtk.Label(episode.parentFeed.name))
  28. mainvbox.add(gtk.Label(episode.title))
  29. mainvbox.add(gtk.Label(strftime("%c", episode.pubDate)))
  30. self.positionLabel = gtk.Label("")
  31. mainvbox.add(self.positionLabel)
  32. self.scroll = gtk.HScrollbar(gtk.Adjustment(0,0,100,5,5,10))
  33. mainvbox.add(self.scroll)
  34. hbox = gtk.HBox(True, 5)
  35. self.backButton = gtk.Button("-5 sec")
  36. hbox.add(self.backButton)
  37. self.forwardButton = gtk.Button("+5 sec")
  38. hbox.add(self.forwardButton)
  39. mainvbox.add(hbox)
  40. hbox = gtk.HBox(True, 5)
  41. self.backButton30 = gtk.Button("-30 sec")
  42. hbox.add(self.backButton30)
  43. self.forwardButton30 = gtk.Button("+30 sec")
  44. hbox.add(self.forwardButton30)
  45. mainvbox.add(hbox)
  46. self.stopButton = gtk.Button("Stop")
  47. mainvbox.add(self.stopButton)
  48. self.w.add(mainvbox)
  49. self.w.show_all()
  50. class control:
  51. stateFileLoad = "/usr/share/openmoko/scenarios/stereoouthead.state"
  52. volumeFile = os.environ.get('HOME') + "/.mokorss/volume"
  53. def __init__(self, gui, episode, parent):
  54. try:
  55. bus = dbus.SystemBus()
  56. # Tell FSO we will use CPU
  57. usage_obj = bus.get_object('org.freesmartphone.ousaged', '/org/freesmartphone/Usage')
  58. usage_obj.RequestResource("CPU")
  59. except:
  60. pass
  61. if os.path.exists(self.stateFileLoad):
  62. restorePath = os.environ.get('HOME') + "/.mokorss/restore.state"
  63. os.system("alsactl -f "+restorePath+" store")
  64. os.system("alsactl -f "+self.stateFileLoad+" restore")
  65. if os.path.exists(self.volumeFile):
  66. f = open( self.volumeFile, 'r' )
  67. volume = pickle.load(f)
  68. f.close()
  69. gui.scroll.set_value(volume)
  70. else:
  71. gui.scroll.set_value(100)
  72. volume=100
  73. gui.stopButton.connect('clicked', self.stop)
  74. gui.w.connect("destroy", self.quit)
  75. gui.scroll.connect('value-changed', self.changeVolume)
  76. gui.backButton.connect('clicked', self.goBack, -5.0)
  77. gui.forwardButton.connect('clicked', self.goForward, 5.0)
  78. gui.backButton30.connect('clicked', self.goBack, -30.0)
  79. gui.forwardButton30.connect('clicked', self.goForward, 30.0)
  80. self.running = True
  81. self.gui = gui
  82. self.episode = episode
  83. self.parent = parent
  84. def handle_data(data):
  85. pass
  86. self.player = pymplayer.MPlayer()
  87. self.player.args = [episode.file]
  88. self.player.stdout.attach(handle_data)
  89. if self.player.start()==False:
  90. raise Exception("mplayer failed to start. :-(")
  91. self.player.command('volume', volume, 1)
  92. self.player.command('seek', episode.position, 2)
  93. signal.signal(signal.SIGTERM, lambda s, f: player.quit())
  94. signal.signal(signal.SIGINT, lambda s, f: player.quit())
  95. self.updateTime()
  96. #pymplayer.loop()
  97. def goForward(self, t, time):
  98. self.seekSec(time)
  99. def goBack(self, t, time):
  100. self.seekSec(time)
  101. def changeVolume(self, t):
  102. volume = self.gui.scroll.get_value()
  103. self.player.command('volume', volume, 1)
  104. f = open(self.volumeFile , 'w' )
  105. pickle.dump(volume, f)
  106. f.close()
  107. def seekSec(self, sec):
  108. self.player.command('seek', sec)
  109. self.updateTime(False)
  110. def updateTime(self, addTimeout = True):
  111. if self.running:
  112. if addTimeout:
  113. gobject.timeout_add (1000, self.updateTime)
  114. pos = self.player.query('get_time_pos')
  115. if pos==None:
  116. return
  117. pos = int(pos)
  118. if pos < 5:
  119. self.episode.position = 0
  120. else:
  121. self.episode.position = pos - 5
  122. length = int(self.player.query('get_time_length'))
  123. posStr = "%d:%02d / %d:%02d" % (int(pos/60), (pos % 60), int(length/60), (length % 60))
  124. self.gui.positionLabel.set_text(posStr)
  125. print "saving position as %i" % pos
  126. self.parent.savePosition(self.episode, pos)
  127. def stop(self, t):
  128. self.gui.w.destroy()
  129. self.quit(t)
  130. def quit(self, t):
  131. #gtk.main_quit(t)
  132. self.player.quit()
  133. self.running = False
  134. if os.path.exists(self.stateFileLoad):
  135. restorePath = os.environ.get('HOME') + "/.mokorss/restore.state"
  136. os.system("alsactl -f "+restorePath+" restore")
  137. try:
  138. bus = dbus.SystemBus()
  139. usage_obj = bus.get_object('org.freesmartphone.ousaged', '/org/freesmartphone/Usage')
  140. usage_obj.ReleaseResource("CPU")
  141. except:
  142. pass
  143. try:
  144. del self.gui
  145. except:
  146. pass