~ubuntu-branches/debian/sid/exaile/sid

« back to all changes in this revision

Viewing changes to .pc/20_use-logind-to-shutdown.patch/plugins/shutdown/__init__.py

  • Committer: Package Import Robot
  • Author(s): Vincent Cheng
  • Date: 2013-10-26 16:55:00 UTC
  • mfrom: (0.2.3)
  • Revision ID: package-import@ubuntu.com-20131026165500-6x29udlb6vxt6q3s
Tags: 3.3.2-1
* New upstream release.
* Add debian/patches/20_use-logind-to-shutdown.patch: allow shutdown plugin
  to support both logind in addition to ConsoleKit. Thanks to Iain Lane.
* Remove references to Shoutcast in package descriptions. (Closes: #721682)
* (Re-)add lintian override for executable-not-elf-or-script.
* Fix lintian warning vcs-field-not-canonical.
* Remove deprecated DMUA flag in debian/control.
* Update Standards version from 3.9.3 to 3.9.4, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Johannes Schwarz
 
2
#
 
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)
 
6
# any later version.
 
7
#
 
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.
 
12
#
 
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.
 
16
 
 
17
import dbus
 
18
import glib
 
19
import gtk
 
20
 
 
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
 
25
 
 
26
SHUTDOWN = None
 
27
 
 
28
class Shutdown():
 
29
    def __init__(self, exaile):
 
30
        self.exaile = exaile
 
31
        self.do_shutdown = False
 
32
        
 
33
        # add menuitem to tools menu
 
34
        providers.register('menubar-tools-menu', 
 
35
            menu.simple_separator('plugin-sep', ['track-properties']))
 
36
 
 
37
             
 
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)
 
42
        
 
43
        self.countdown = None
 
44
        self.counter = 10
 
45
 
 
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)
 
50
 
 
51
    def on_toggled(self, menuitem):
 
52
        """
 
53
            Enables or disables defered shutdown
 
54
        """
 
55
        if menuitem.get_active():
 
56
            self.do_shutdown = True
 
57
            event.add_callback(self.on_playback_player_end, 'playback_player_end')
 
58
 
 
59
            self.message.show_info(_('Shutdown scheduled'),
 
60
                _('Computer will be shutdown at the end of playback.'))
 
61
        else:
 
62
            self.disable_shutdown()
 
63
            
 
64
    def disable_shutdown(self):
 
65
        self.do_shutdown = False
 
66
        event.remove_callback(self.on_playback_player_end, 'playback_player_end')
 
67
 
 
68
        # Stop possible countdown
 
69
        if self.countdown is not None:
 
70
            glib.source_remove(self.countdown)
 
71
            self.countdown = None
 
72
 
 
73
        # Prepare for a new run
 
74
        self.counter = 10
 
75
 
 
76
        # Reset message button layout
 
77
        self.message.hide()
 
78
        self.message.clear_buttons()
 
79
        self.message.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE)
 
80
 
 
81
    def on_playback_player_end(self, event, player, track):
 
82
        """
 
83
            Tries to shutdown the computer
 
84
        """
 
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)
 
89
 
 
90
        if self.countdown is not None:
 
91
            glib.source_remove(self.countdown)
 
92
 
 
93
        self.counter = 10
 
94
        self.countdown = glib.timeout_add_seconds(1, self.on_timeout)
 
95
 
 
96
    def on_response(self, widget, response):
 
97
        """
 
98
            Cancels shutdown if requested
 
99
        """
 
100
        if response == gtk.RESPONSE_CANCEL:
 
101
            self.disable_shutdown()
 
102
 
 
103
    def on_timeout(self):
 
104
        """
 
105
            Tries to shutdown the computer
 
106
        """
 
107
        if self.counter > 0:
 
108
            self.message.set_secondary_text(
 
109
                _('The computer will be shut down in %d seconds.') % self.counter)
 
110
            self.message.show()
 
111
 
 
112
            self.counter -= 1;
 
113
 
 
114
            return True
 
115
 
 
116
        self.do_shutdown = False
 
117
 
 
118
        bus = dbus.SystemBus()
 
119
 
 
120
        try:
 
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:
 
125
            try:
 
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.'))
 
132
 
 
133
    def destroy(self):
 
134
        """
 
135
            Cleans up
 
136
        """
 
137
        if self.countdown is not None:
 
138
            glib.source_remove(self.countdown)
 
139
 
 
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)
 
144
                break
 
145
 
 
146
def enable(exaile):
 
147
    if (exaile.loading):
 
148
        event.add_callback(_enable, 'exaile_loaded')
 
149
    else:
 
150
        _enable(None, exaile, None)
 
151
 
 
152
def _enable(eventname, exaile, nothing):
 
153
    global SHUTDOWN
 
154
    SHUTDOWN = Shutdown(exaile)
 
155
 
 
156
def disable(exaile):
 
157
    global SHUTDOWN
 
158
    SHUTDOWN.destroy()
 
159