4 Copyright 2004 Kenneth Hayber <ken@hayber.us>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License.
11 This program is distributed in the hope that it will be useful
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 from __future__
import generators
23 import os
, sys
, re
, string
, threading
, pango
, gtk
, gobject
24 from threading
import *
27 from rox
import Menu
, app_options
, loading
, saving
, InfoWin
, OptionsBox
, filer
28 from rox
.options
import Option
32 import player
, playlist
, playlistui
, xsoap
, plugins
34 rox
.report_exception()
37 #Who am I and how did I get here?
40 APP_DOMAIN
= 'hayber.us'
44 #Toolbar button indexes
56 #Bitmaps (potentially) used in this application
57 factory
= gtk
.IconFactory()
59 'gtk-media-stop', 'gtk-media-pause', 'gtk-media-play',
60 'gtk-media-next', 'gtk-media-previous', 'gtk-media-forward', 'gtk-media-rewind',
61 'gtk-media-eject', 'gtk-media-repeat', 'gtk-media-shuffle',
62 # 'gtk-media_track', 'gtk-media-record', 'stock_playlist',
63 # 'stock_volume-max', 'stock_volume-med', 'stock_volume-min',
64 # 'stock_volume-mute', 'stock_volume-0'
66 path
= os
.path
.join(rox
.app_dir
, "images", name
+ ".svg")
67 pixbuf
= gtk
.gdk
.pixbuf_new_from_file(path
)
69 print >>sys
.stderr
, "Can't load stock icon '%s'" % name
71 gtk
.stock_add([(name
, name
, 0, 0, "")])
72 factory
.add(name
, gtk
.IconSet(pixbuf
= pixbuf
))
76 #Options.xml processing
77 from rox
import choices
78 choices
.migrate(APP_NAME
, APP_DOMAIN
)
79 rox
.setup_app_options(APP_NAME
, site
=APP_DOMAIN
)
80 Menu
.set_save_name(APP_NAME
, site
=APP_DOMAIN
)
82 #assume that everyone puts their music in ~/Music
83 LIBRARY
= Option('library', os
.path
.expanduser('~')+'/Music')
85 #how to parse each library leaf to get artist, album, title...
86 LIBRARY_RE
= Option('library_re', '^.*/(?P<artist>.*)/(?P<album>.*)/(?P<title>.*)')
88 #the ao driver type you want to use (esd, oss, alsa, alsa09, ...)
89 DRIVER_ID
= Option('driver_id', 'esd')
90 MIXER_DEVICE
= Option('mixer_device', '/dev/mixer')
92 SHUFFLE
= Option('shuffle', 0)
93 REPEAT
= Option('repeat', 0)
95 #Don't replay any of the last n songs in shuffle mode
96 SHUFFLE_CACHE_SIZE
= Option('shuffle_cache', 10)
98 #Buffer size used by audio device read/write
99 AUDIO_BUFFER_SIZE
= Option('audio_buffer', 4096)
102 SONG_FONT
= Option('song_font', None)
103 BASE_FONT
= Option('base_font', None)
104 BG_COLOR
= Option('bg_color', '#A6A699')
105 FG_COLOR
= Option('fg_color', '#000000')
108 SH_TOOLBAR
= Option('toolbar', True)
109 SH_VOLUME
= Option('volume', True)
110 SH_SEEKBAR
= Option('seekbar', True)
113 WORDWRAP
= Option('word_wrap', False)
114 MINITOOLS
= Option('mini_toolbar', False)
115 TIMEDISPLAY
= Option('time_display', False)
116 ALBUM_ART
= Option('album_art', True)
118 tooltips
= gtk
.Tooltips()
121 def build_tool_options(box
, node
, label
, option
):
122 """Custom Option widget to allow show/hide each toolbar button"""
124 toolbar
= gtk
.Toolbar()
125 toolbar
.set_style(gtk
.TOOLBAR_ICONS
)
128 def item_changed(thing
):
129 if done
: #supress widget updates until later
130 box
.check_widget(option
)
132 type1
= gtk
.TOOLBAR_CHILD_TOGGLEBUTTON
134 #(type, text, icon, callback)
135 (type1
, _("Close"), gtk
.STOCK_CLOSE
, item_changed
),
136 (type1
, _("Prev"), gtk
.STOCK_MEDIA_PREVIOUS
, item_changed
),
137 (type1
, _("Play"), gtk
.STOCK_MEDIA_PLAY
, item_changed
),
138 (type1
, _("Stop"), gtk
.STOCK_MEDIA_STOP
, item_changed
),
139 (type1
, _("Next"), gtk
.STOCK_MEDIA_NEXT
, item_changed
),
140 (type1
, _("Repeat"), 'gtk-media-repeat', item_changed
),
141 (type1
, _("Shuffle"), 'gtk-media-shuffle', item_changed
),
142 (type1
, _("Playlist"), gtk
.STOCK_INDEX
, item_changed
),
143 (type1
, _("Options"), gtk
.STOCK_PREFERENCES
, item_changed
),
147 for (type, text
, icon
, callback
) in buttons
:
148 image
= gtk
.image_new_from_stock(icon
, gtk
.ICON_SIZE_LARGE_TOOLBAR
)
149 controls
.append(toolbar
.insert_element(type, None, text
, text
,
150 None, image
, callback
, None, -1))
152 #build a bitmask of which buttons are active
155 for i
in range(len(controls
)):
156 if controls
[i
].get_active():
159 def set_values(): pass
160 box
.handlers
[option
] = (get_values
, set_values
)
162 #initialize the button states
163 for i
in range(len(controls
)):
164 if option
.int_value
& (1 << i
):
165 controls
[i
].set_active(True)
166 done
= True #OK, updates activated.
168 box
.may_add_tip(toolbar
, node
)
171 OptionsBox
.widget_registry
['tool_options'] = build_tool_options
172 TOOLOPTIONS
= Option('toolbar_disable', -1)
174 rox
.app_options
.notify()
177 class MusicBox(rox
.Window
, loading
.XDSLoader
):
178 """A Music Player for mp3 and ogg - main class"""
185 """Constructor for MusicBox"""
186 rox
.Window
.__init
__(self
)
187 loading
.XDSLoader
.__init
__(self
, plugins
.TYPE_LIST
)
189 # Main window settings
190 self
.set_title(APP_NAME
)
191 self
.set_role("MainWindow")
194 rox
.app_options
.add_notify(self
.get_options
)
195 self
.connect('delete_event', self
.delete_event
)
196 self
.connect('window-state-event', self
.window_state_event
)
197 self
.connect('drag-motion', self
.xds_drag_motion
)
200 self
.replace_library
= False
201 self
.library
= LIBRARY
.value
.split(':')
203 self
.playlistUI
= None
204 self
.current_song
= None
206 self
.shuffle
= bool(SHUFFLE
.int_value
)
207 self
.repeat
= bool(REPEAT
.int_value
)
209 # Build and Init everything
210 #GTK2.4 self.uimanager = gtk.UIManager()
211 #GTK2.4 self.uimanager.insert_action_group(self.build_actions(), 0)
212 #GTK2.4 self.uimanager.add_ui_from_file('ui.xml')
213 #GTK2.4 self.menu = self.uimanager.get_widget('/ui/popup')
214 #GTK2.4 self.toolbar = self.uimanager.get_widget('/ui/toolbar')
215 #GTK2.4 self.add_events(gtk.gdk.BUTTON_PRESS_MASK)
216 #GTK2.4 self.connect('button-press-event', self.button_press)
217 #GTK2.4 self.connect('popup-menu', self.menukey_press)
227 # Pack and show widgets
228 self
.vbox
= gtk
.VBox()
229 self
.hbox
= gtk
.HBox()
231 self
.vbox
.add(self
.hbox
)
233 self
.hbox
.pack_start(self
.display
, True, True, 0)
234 self
.hbox
.pack_end(self
.volume_control
, False, True, 0)
235 self
.vbox
.pack_end(self
.toolbar
, False, True, 0)
236 self
.vbox
.pack_end(self
.seek_bar_control
, False, True, 0)
239 self
.show_hide_controls()
240 self
.show_hide_buttons()
242 if not ALBUM_ART
.int_value
:
243 self
.album_img
.hide()
245 self
.display
.set_size_request(ALBUM_COVER_SIZE
, ALBUM_COVER_SIZE
)
252 self
.playlist
= playlist
.Playlist(SHUFFLE_CACHE_SIZE
.int_value
, LIBRARY_RE
.value
)
253 self
.player
= player
.Player(DRIVER_ID
.value
, AUDIO_BUFFER_SIZE
.int_value
, MIXER_DEVICE
.value
)
254 self
.foo
= Thread(name
='player', target
=self
.player
.run
)
255 self
.foo
.setDaemon(True)
257 self
.volume
.set_value(self
.player
.get_volume(MIXER_DEVICE
.value
))
259 if len(sys
.argv
) > 1:
260 self
.load_args(sys
.argv
[1:], True)
262 self
.load_args([], False)
264 gobject
.timeout_add(500, self
.display_update
)
266 #GTK2.4 def build_actions(self):
267 #GTK2.4 actions = gtk.ActionGroup('main')
268 #GTK2.4 actions.add_action(gtk.Action('quit', _("Quit"), _("Quit the application"), gtk.STOCK_QUIT))
269 #GTK2.4 actions.add_action(gtk.Action('close', _("Close"), _("Close this window"), gtk.STOCK_CLOSE))
271 #GTK2.4 actions.add_action(gtk.Action('options', _("Options"), _("Edit Options"), gtk.STOCK_PREFERENCES))
272 #GTK2.4 actions.add_action(gtk.Action('info', _("Info"), _("Show program info"), gtk.STOCK_DIALOG_INFO))
274 #GTK2.4 actions.add_action(gtk.Action('play', _("Play"), _("Play"), 'media-play'))
275 #GTK2.4 actions.add_action(gtk.Action('stop', _("Stop"), _("Stop"), 'media-stop'))
276 #GTK2.4 actions.add_action(gtk.Action('prev', _("Prev"), _("Prev"), 'media-prev'))
277 #GTK2.4 actions.add_action(gtk.Action('next', _("Next"), _("Next"), 'media-next'))
279 #GTK2.4 actions.add_action(gtk.Action('playlist', _("Playlist"), _("Show Playlist"), gtk.STOCK_INDEX))
280 #GTK2.4 actions.add_action(gtk.Action('open', _("Open"), _("Open Location"), gtk.STOCK_GO_UP))
281 #GTK2.4 actions.add_action(gtk.Action('save', _("Save"), _("Save Playlist"), gtk.STOCK_SAVE))
283 #GTK2.4 actions.add_action(gtk.Action('shuffle', _("Shuffle"), _("Shuffle"), 'media-shuffle'))
284 #GTK2.4 actions.add_action(gtk.Action('repeat', _("Repeat"), _("Repeat"), 'media-repeat'))
286 #GTK2.4 actions.add_action(gtk.Action('none', None, None, 0))
287 #GTK2.4 return actions
290 def build_menu(self
):
291 self
.add_events(gtk
.gdk
.BUTTON_PRESS_MASK
)
292 self
.connect('button-press-event', self
.button_press
)
293 self
.connect('popup-menu', self
.menukey_press
)
294 self
.menu
= Menu
.Menu('main', [
295 Menu
.Action(_("Play")+'\/'+_("Pause"), 'play_pause', '', gtk
.STOCK_MEDIA_PLAY
),
296 Menu
.Action(_("Stop"), 'stop', '', gtk
.STOCK_MEDIA_STOP
),
299 Menu
.Action(_("Back"), 'prev', '', gtk
.STOCK_MEDIA_PREVIOUS
),
300 Menu
.Action(_("Next"), 'next', '', gtk
.STOCK_MEDIA_NEXT
),
303 Menu
.SubMenu(_('Playlist'), [
304 Menu
.Action(_("Show"), 'show_playlist', '', gtk
.STOCK_INDEX
),
305 Menu
.Action(_("Open location"), 'show_dir', '', gtk
.STOCK_GO_UP
),
306 Menu
.Action(_("Save"), 'save', '', gtk
.STOCK_SAVE
),
307 Menu
.ToggleItem(_("Shuffle"), 'shuffle'),
308 Menu
.ToggleItem(_("Repeat"), 'repeat'),
312 Menu
.Action(_("Options"), 'show_options', '', gtk
.STOCK_PREFERENCES
),
313 Menu
.Action(_('Info'), 'get_info', '', gtk
.STOCK_DIALOG_INFO
),
316 Menu
.Action(_("Quit"), 'close', '', gtk
.STOCK_CLOSE
),
318 self
.menu
.attach(self
,self
)
321 def build_toolbar(self
):
322 self
.toolbar
= gtk
.Toolbar()
323 self
.toolbar
.set_style(gtk
.TOOLBAR_ICONS
)
324 if bool(MINITOOLS
.int_value
):
325 self
.toolbar
.set_icon_size(gtk
.ICON_SIZE_SMALL_TOOLBAR
)
327 self
.toolbar
.set_icon_size(gtk
.ICON_SIZE_LARGE_TOOLBAR
)
329 type1
= gtk
.TOOLBAR_CHILD_BUTTON
330 type2
= gtk
.TOOLBAR_CHILD_TOGGLEBUTTON
333 #(type, text, icon, callback)
334 (type1
, _("Close"), gtk
.STOCK_CLOSE
, self
.close
),
335 (type1
, _("Prev"), gtk
.STOCK_MEDIA_PREVIOUS
, self
.prev
),
336 (type1
, _("Play"), gtk
.STOCK_MEDIA_PLAY
, self
.play_pause
),
337 (type1
, _("Stop"), gtk
.STOCK_MEDIA_STOP
, self
.stop
),
338 (type1
, _("Next"), gtk
.STOCK_MEDIA_NEXT
, self
.next
),
339 (type2
, _("Repeat"), 'gtk-media-repeat', lambda b
: self
.set_repeat(b
.get_active())),
340 (type2
, _("Shuffle"), 'gtk-media-shuffle', lambda b
: self
.set_shuffle(b
.get_active())),
341 (type1
, _("Playlist"), gtk
.STOCK_INDEX
, self
.show_playlist
),
342 (type1
, _("Options"), gtk
.STOCK_PREFERENCES
, self
.show_options
),
346 for (type, text
, icon
, callback
) in items
:
347 image
= gtk
.image_new_from_stock(icon
, self
.toolbar
.get_icon_size())
348 buttons
.append(self
.toolbar
.insert_element(type, None, text
, text
,
349 None, image
, callback
, None, -1))
350 if text
== _("Play"): #this one changes later, so we have to save it
351 self
.image_play
= image
353 self
.buttons
= buttons
355 buttons
[BTN_REPEAT
].set_active(self
.repeat
)
356 buttons
[BTN_SHUFFLE
].set_active(self
.shuffle
)
357 buttons
[BTN_PLAYLIST
].set_sensitive(False)
360 def build_labels(self
):
361 self
.display
= gtk
.Layout()
362 self
.display_size
= (0, 0)
363 self
.display
.connect('size-allocate', self
.resize
)
365 self
.album_img
= gtk
.Image()
366 self
.album_img
.set_alignment(0.0, 0.0)
368 hbox
= gtk
.HBox(False, 5)
370 hbox
.pack_start(self
.album_img
, False, False, 0)
371 hbox
.pack_end(vbox
, True, True, 0)
372 self
.display
.put(hbox
, 0, 0)
373 self
.display_box
= vbox
375 self
.display_song
= gtk
.Label()
376 self
.display_song
.set_alignment(0.0, 0.0)
378 self
.display_album
= gtk
.Label()
379 self
.display_album
.set_alignment(0.0, 0.0)
381 self
.display_artist
= gtk
.Label()
382 self
.display_artist
.set_alignment(0.0, 0.0)
384 self
.display_status
= gtk
.Label()
385 self
.display_status
.set_alignment(0.0, 0.0)
387 vbox
.pack_start(self
.display_song
, False, True, 0)
388 vbox
.pack_start(self
.display_album
, False, True, 0)
389 vbox
.pack_start(self
.display_artist
, False, True, 0)
390 vbox
.pack_start(self
.display_status
, False, True, 0)
393 def set_line_wrap(self
):
394 self
.display_song
.set_line_wrap(bool(WORDWRAP
.int_value
))
395 self
.display_album
.set_line_wrap(bool(WORDWRAP
.int_value
))
396 self
.display_artist
.set_line_wrap(bool(WORDWRAP
.int_value
))
397 self
.display_status
.set_line_wrap(bool(WORDWRAP
.int_value
))
400 def build_misc(self
):
401 self
.volume
= gtk
.Adjustment(50.0, 0.0, 100.0, 1.0, 10.0, 0.0)
402 self
.volume
.connect('value_changed', self
.adjust_volume
)
403 self
.volume_control
= gtk
.VScale(self
.volume
)
404 self
.volume_control
.set_draw_value(False)
405 self
.volume_control
.set_inverted(True)
406 self
.volume_control
.set_size_request(-1, 90)
408 self
.seek_bar
= gtk
.Adjustment(0.0, 0.0, 1.0, 0.01, 0.1, 0.0)
409 self
.seek_id
= self
.seek_bar
.connect('value_changed', self
.adjust_seek_bar
)
410 self
.seek_bar_control
= gtk
.HScale(self
.seek_bar
)
411 self
.seek_bar_control
.set_update_policy(gtk
.UPDATE_DELAYED
)
412 self
.seek_bar_control
.set_draw_value(False)
413 self
.seek_bar_control
.set_size_request(100, -1)
417 song_font
= pango
.FontDescription(SONG_FONT
.value
)
418 base_font
= pango
.FontDescription(BASE_FONT
.value
)
419 self
.display_song
.modify_font(song_font
)
420 self
.display_album
.modify_font(base_font
)
421 self
.display_artist
.modify_font(base_font
)
422 self
.display_status
.modify_font(base_font
)
425 def set_colors(self
):
426 fg_color
= gtk
.gdk
.color_parse(FG_COLOR
.value
)
427 bg_color
= gtk
.gdk
.color_parse(BG_COLOR
.value
)
428 self
.display
.modify_bg(gtk
.STATE_NORMAL
, bg_color
)
429 self
.display_song
.modify_fg(gtk
.STATE_NORMAL
, fg_color
)
430 self
.display_album
.modify_fg(gtk
.STATE_NORMAL
, fg_color
)
431 self
.display_artist
.modify_fg(gtk
.STATE_NORMAL
, fg_color
)
432 self
.display_status
.modify_fg(gtk
.STATE_NORMAL
, fg_color
)
435 def show_hide_controls(self
):
436 for (option
, control
) in [
437 (SH_VOLUME
, self
.volume_control
), (SH_TOOLBAR
, self
.toolbar
),
438 (SH_SEEKBAR
, self
.seek_bar_control
)]:
439 if bool(option
.int_value
):
445 def show_hide_buttons(self
):
446 for i
in range(len(self
.buttons
)):
447 if TOOLOPTIONS
.int_value
& (1 << i
):
448 self
.buttons
[i
].show()
450 self
.buttons
[i
].hide()
453 def set_shuffle(self
, value
):
455 self
._shuffle
= value
456 self
.buttons
[BTN_SHUFFLE
].set_active(self
._shuffle
)
458 pass #this gets called before everything is built
459 shuffle
= property(lambda self
: self
._shuffle
, set_shuffle
)
462 def set_repeat(self
, value
):
465 self
.buttons
[BTN_REPEAT
].set_active(self
._repeat
)
467 pass #this gets called before everything is built
468 repeat
= property(lambda self
: self
._repeat
, set_repeat
)
471 def resize(self
, widget
, rectangle
):
472 """Called when the window resizes."""
474 height
= rectangle
[3]
477 self
.album_img
.get_image() #raises an exception if not valid
479 if ALBUM_ART
.int_value
:
480 awidth
= width
- ALBUM_COVER_SIZE
482 width
= max(width
, -1)
483 awidth
= max(awidth
, -1)
485 if self
.display_size
!= (width
, height
):
486 self
.display_size
= (width
, height
)
487 self
.display_box
.set_size_request(awidth
, -1)
488 self
.display_song
.set_size_request(awidth
, -1)
489 self
.display_album
.set_size_request(awidth
, -1)
490 self
.display_artist
.set_size_request(awidth
, -1)
491 self
.display_status
.set_size_request(awidth
, -1)
494 def set_sensitive(self
, state
):
495 self
.buttons
[BTN_PLAYLIST
].set_sensitive(state
)
496 self
.buttons
[BTN_PLAY
].set_sensitive(state
)
497 self
.buttons
[BTN_NEXT
].set_sensitive(state
)
498 self
.buttons
[BTN_STOP
].set_sensitive(state
)
502 """process external/remote commands"""
503 def callback(window
, cmd
, args
):
514 elif cmd
== 'add_songs':
516 elif cmd
== 'load_songs':
517 self
.load_songs(args
)
519 rox
.info("Bad rpc message")
521 xsoap
.register_server("_ROX_MUSICBOX")
522 xsoap
.register_callback(callback
)
525 def update_thd(self
, button
=None):
526 """load songs from source dirs"""
531 self
.library
= [LIBRARY
.value
]
536 self
.display_status
.set_text(_("Loading")+': '+str(len(self
.playlist
)))
537 if len(self
.playlist
):
538 self
.set_sensitive(True)
540 self
.set_sensitive(False)
544 """Load the playlist either from a saved xml file, or from source dirs"""
545 self
.display_status
.set_text(_("Loading songs, please wait..."))
546 self
.playlist
.get_songs(self
.library
, self
.loading
, self
.replace_library
)
547 self
.display_status
.set_text(_("Ready")+': '+_("loaded ")+str(len(self
.playlist
))+_(" songs"))
549 if len(self
.playlist
):
550 self
.set_sensitive(True)
552 self
.set_sensitive(False)
554 if self
.replace_library
and len(self
.playlist
):
562 """Save the current list"""
563 # box = saving.SaveBox(self.playlist, rox.choices.save(APP_NAME, 'Library.xml'), 'text/xml')
564 file = 'MyMusic.music'
565 path
= os
.path
.join(rox
.basedir
.save_config_path(APP_NAME
, APP_DOMAIN
), file)
566 box
= saving
.SaveBox(self
.playlist
, path
, 'application/x-music-playlist')
570 def load_args(self
, args
, replace
=True):
571 """Accept files and folders from the command line (or dropped on our icon)"""
572 self
.replace_library
= replace
581 def add_songs(self
, args
):
582 self
.load_args(args
, False)
585 def load_songs(self
, args
):
586 self
.load_args(args
, True)
590 """Play the current song"""
591 size
= self
.toolbar
.get_icon_size()
595 self
.current_song
= self
.playlist
.get()
596 self
.player
.play(self
.current_song
.filename
, self
.current_song
.type)
597 self
.image_play
.set_from_stock(gtk
.STOCK_MEDIA_PAUSE
, size
)
598 self
.buttons
[BTN_PREV
].set_sensitive(self
.playlist
.get_previous())
599 self
.display_song
.set_text(self
.current_song
.title
)
600 self
.display_artist
.set_text(self
.current_song
.artist
)
601 self
.display_album
.set_text(self
.current_song
.album
)
603 tooltips
.set_tip(self
.buttons
[BTN_PLAY
], 'Play ['+self
.current_song
.title
+']', tip_private
=None)
605 except TypeError, detail
:
606 rox
.alert(str(detail
))
608 rox
.alert(_("Failed to start playing %s") % self
.current_song
.filename
)
611 self
.playlistUI
.sync()
614 folder
= os
.path
.dirname(self
.current_song
.filename
)
616 for filename
in ['.DirIcon',
617 'Folder.jpg', 'folder.jpg', '.folder.jpg',
618 'Folder.png', 'folder.png', '.folder.png',
619 'Album.jpg', 'album.jpg', '.album.jpg',
620 'Album.png', 'album.png', '.album.png',
621 'Cover.jpg', 'cover.jpg', '.cover.jpg',
622 'Cover.png', 'cover.png', '.cover.png',
624 image
= os
.path
.join(folder
, filename
)
625 if os
.access(image
, os
.R_OK
):
626 pixbuf
= gtk
.gdk
.pixbuf_new_from_file_at_size(image
, ALBUM_COVER_SIZE
, ALBUM_COVER_SIZE
)
628 self
.album_img
.set_from_pixbuf(pixbuf
)
632 #force a resize because the labels may have changed
633 self
.resize(None, [0, 0, 0, 0])
636 def play_pause(self
, button
=None):
637 """Play button handler (toggle between play and pause)"""
638 if (self
.player
.state
== 'play') or (self
.player
.state
== 'pause'):
644 def prev(self
, button
=None):
645 """Skip to previous song and play it"""
646 self
.current_song
= self
.playlist
.prev()
650 def next(self
, button
=None):
651 """Skip to next song and play it (with shuffle and repeat)"""
653 self
.playlist
.shuffle()
654 self
.current_song
= self
.playlist
.get()
657 self
.current_song
= self
.playlist
.next()
658 except StopIteration:
660 self
.current_song
= self
.playlist
.first()
667 def stop(self
, button
=None):
669 size
= self
.toolbar
.get_icon_size()
671 self
.current_song
= None
672 self
.image_play
.set_from_stock(gtk
.STOCK_MEDIA_PLAY
, size
)
673 self
.seek_bar
.set_value(0.0)
676 def pause(self
, button
=None):
677 """Pause playing (toggle)"""
678 size
= self
.toolbar
.get_icon_size()
680 if (self
.player
.state
== 'play'):
681 self
.image_play
.set_from_stock(gtk
.STOCK_MEDIA_PAUSE
, size
)
683 self
.image_play
.set_from_stock(gtk
.STOCK_MEDIA_PLAY
, size
)
686 def display_update(self
):
687 duration
= int(self
.player
.remain
+ self
.player
.elapse
)
689 progress
= float(self
.player
.elapse
)/duration
693 min = string
.zfill(str(int(duration
)%3600/60),2)
694 sec
= string
.zfill(str(int(duration
)%3600%60),2)
697 minremain
= string
.zfill(str(self
.player
.remain
%3600/60),2)
698 secremain
= string
.zfill(str(self
.player
.remain
%3600%60),2)
699 remain
= minremain
+':'+secremain
701 minelapse
= string
.zfill(str(self
.player
.elapse
%3600/60),2)
702 secelapse
= string
.zfill(str(self
.player
.elapse
%3600%60),2)
703 elapse
= minelapse
+':'+secelapse
705 show_remain
= bool(TIMEDISPLAY
.int_value
)
707 self
.time_string
= remain
+' / '+total
709 self
.time_string
= elapse
+' / '+total
712 if self
.player
.state
== 'play':
713 self
.display_status
.set_text(_("Playing")+': '+self
.time_string
)
714 self
.seek_bar
.handler_block(self
.seek_id
)
715 self
.seek_bar
.set_value(progress
)
716 self
.seek_bar
.handler_unblock(self
.seek_id
)
717 elif self
.player
.state
== 'pause':
718 self
.display_status
.set_text(_("Paused")+': '+self
.time_string
)
719 elif self
.player
.state
== 'stop':
720 self
.display_status
.set_text(_("Stopped"))
721 elif self
.player
.state
== 'eof':
722 self
.display_status
.set_text("")
725 if (self
.window_state
& gtk
.gdk
.WINDOW_STATE_ICONIFIED
):
726 self
.set_title(self
.current_song
.title
+' - '+self
.time_string
)
728 tooltips
.set_tip(self
.seek_bar_control
, self
.time_string
, tip_private
=None)
731 #update the volume control if something other than us changed it
732 self
.volume
.set_value(self
.player
.get_volume(MIXER_DEVICE
.value
))
734 return True #keep running
737 def delete_event(self
, ev
, e1
):
738 """Same as close, but called from the window manager"""
742 def window_state_event(self
, window
, event
):
743 """Track changes in window state and such..."""
744 self
.my_gdk_window
= event
.window
745 self
.window_state
= event
.new_window_state
746 if not (self
.window_state
& gtk
.gdk
.WINDOW_STATE_ICONIFIED
):
747 self
.set_title(APP_NAME
)
750 def close(self
, button
= None):
751 """Stop playing, kill the player and exit"""
754 self
.playlistUI
.close()
756 xsoap
.unregister_server("_ROX_MUSICBOX")
760 def get_options(self
):
761 """Used as the notify callback when options change"""
762 if SHUFFLE
.has_changed
:
763 self
.shuffle
= SHUFFLE
.int_value
765 if REPEAT
.has_changed
:
766 self
.repeat
= REPEAT
.int_value
768 if SONG_FONT
.has_changed
or BASE_FONT
.has_changed
:
771 if FG_COLOR
.has_changed
or BG_COLOR
.has_changed
:
774 if TOOLOPTIONS
.has_changed
:
775 self
.show_hide_buttons()
777 if SH_TOOLBAR
.has_changed
or SH_VOLUME
.has_changed
or SH_SEEKBAR
.has_changed
:
778 self
.show_hide_controls()
780 if MINITOOLS
.has_changed
:
781 if bool(MINITOOLS
.int_value
):
782 self
.toolbar
.set_icon_size(gtk
.ICON_SIZE_SMALL_TOOLBAR
)
784 self
.toolbar
.set_icon_size(gtk
.ICON_SIZE_LARGE_TOOLBAR
)
786 if WORDWRAP
.has_changed
:
789 if ALBUM_ART
.has_changed
:
790 if ALBUM_ART
.int_value
:
791 self
.album_img
.show()
792 self
.display
.set_size_request(ALBUM_COVER_SIZE
, ALBUM_COVER_SIZE
)
794 self
.album_img
.hide()
795 self
.display
.set_size_request(-1, -1)
798 def show_options(self
, button
=None):
799 """Options edit dialog"""
803 def show_playlist(self
, button
=None):
804 """Display the playlist window"""
805 if not self
.playlistUI
:
806 self
.playlistUI
= playlistui
.PlaylistUI(self
.playlist
, self
)
807 self
.playlistUI
.connect('destroy', self
.playlist_close
)
808 self
.buttons
[BTN_PLAYLIST
].set_sensitive(False)
811 def show_dir(self
, *dummy
):
812 ''' Pops up a filer window containing the current song, or the library location '''
813 if self
.current_song
:
814 filer
.show_file(self
.playlist
.get().filename
)
816 filer
.show_file(os
.path
.expanduser(LIBRARY
.value
))
819 def playlist_close(self
, item
=None):
820 """Notice when the playlistUI goes away (so we don't crash)"""
821 self
.playlistUI
= None
822 self
.buttons
[BTN_PLAYLIST
].set_sensitive(True)
825 def button_press(self
, text
, event
):
826 """Popup menu handler"""
827 if event
.button
!= 3:
829 self
.menu
.popup(self
, event
)
830 #GTK2.4 self.menu.popup(None, None, None, event.button, 0)
833 def menukey_press(self
, widget
):
834 ''' Called when the user hits the menu key on their keyboard. '''
835 self
.menu
.popup(self
, None)
838 InfoWin
.infowin(APP_NAME
)
841 def adjust_seek_bar(self
, pos
):
842 """Set the playback position (seek)"""
843 self
.player
.seek(pos
.get_value())
846 def adjust_volume(self
, vol
):
847 """Set the playback volume"""
848 self
.player
.set_volume(vol
.get_value(), MIXER_DEVICE
.value
)
851 def xds_drag_motion(self
, widget
, context
, x
, y
, timestamp
):
855 def xds_drag_drop(self
, widget
, context
, data
, info
, time
):
856 """Check if the Shift key is pressed or not when Dropping files"""
857 if context
.actions
& gtk
.gdk
.ACTION_MOVE
:
858 self
.replace_library
= True
860 self
.replace_library
= False
861 return loading
.XDSLoader
.xds_drag_drop(self
, widget
, context
, data
, info
, time
)
864 def xds_load_uris(self
, uris
):
865 """Accept files and folders dropped on us as new Library"""
867 #strip off the 'file://' part and concatenate them
869 path
.append(rox
.get_local_path(s
))