1
# Copyright (C) 2010 Johannes Schwarz
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 1, or (at your option)
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
from xl import event, providers
22
from xl.nls import gettext as _
23
from xlgui import icons
24
from xlgui.widgets import dialogs, menu
29
def __init__(self, exaile):
31
self.do_shutdown = False
33
# add menuitem to tools menu
34
providers.register('menubar-tools-menu',
35
menu.simple_separator('plugin-sep', ['track-properties']))
38
item = menu.check_menu_item('shutdown', ['plugin-sep'], _('Shutdown after Playback'),
39
# checked func # callback func
40
lambda *x: self.do_shutdown, lambda w, n, p, c: self.on_toggled(w))
41
providers.register('menubar-tools-menu', item)
46
self.message = dialogs.MessageBar(
47
parent=exaile.gui.builder.get_object('player_box'),
48
buttons=gtk.BUTTONS_CLOSE)
49
self.message.connect('response', self.on_response)
51
def on_toggled(self, menuitem):
53
Enables or disables defered shutdown
55
if menuitem.get_active():
56
self.do_shutdown = True
57
event.add_callback(self.on_playback_player_end, 'playback_player_end')
59
self.message.show_info(_('Shutdown scheduled'),
60
_('Computer will be shutdown at the end of playback.'))
62
self.disable_shutdown()
64
def disable_shutdown(self):
65
self.do_shutdown = False
66
event.remove_callback(self.on_playback_player_end, 'playback_player_end')
68
# Stop possible countdown
69
if self.countdown is not None:
70
glib.source_remove(self.countdown)
73
# Prepare for a new run
76
# Reset message button layout
78
self.message.clear_buttons()
79
self.message.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE)
81
def on_playback_player_end(self, event, player, track):
83
Tries to shutdown the computer
85
self.message.set_message_type(gtk.MESSAGE_INFO)
86
self.message.set_markup(_('Imminent Shutdown'))
87
self.message.clear_buttons()
88
self.message.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
90
if self.countdown is not None:
91
glib.source_remove(self.countdown)
94
self.countdown = glib.timeout_add_seconds(1, self.on_timeout)
96
def on_response(self, widget, response):
98
Cancels shutdown if requested
100
if response == gtk.RESPONSE_CANCEL:
101
self.disable_shutdown()
103
def on_timeout(self):
105
Tries to shutdown the computer
108
self.message.set_secondary_text(
109
_('The computer will be shut down in %d seconds.') % self.counter)
116
self.do_shutdown = False
118
bus = dbus.SystemBus()
121
proxy = bus.get_object('org.freedesktop.ConsoleKit',
122
'/org/freedesktop/ConsoleKit/Manager')
123
proxy.Stop(dbus_interface='org.freedesktop.ConsoleKit.Manager')
124
except dbus.exceptions.DBusException:
126
proxy = bus.get_object('org.freedesktop.Hal',
127
'/org/freedesktop/Hal/devices/computer')
128
proxy.Shutdown(dbus_interface='org.freedesktop.Hal.Device.SystemPowerManagement')
129
except dbus.exceptions.DBusException:
130
self.message.show_warning(_('Shutdown failed'),
131
_('Computer could not be shutdown using D-Bus.'))
137
if self.countdown is not None:
138
glib.source_remove(self.countdown)
140
event.remove_callback(self.on_playback_player_end, 'playback_player_end')
141
for item in providers.get('menubar-tools-menu'):
142
if item.name == 'shutdown':
143
providers.unregister('menubar-tools-menu', item)
148
event.add_callback(_enable, 'exaile_loaded')
150
_enable(None, exaile, None)
152
def _enable(eventname, exaile, nothing):
154
SHUTDOWN = Shutdown(exaile)