~dobey/changeup/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python

# changeup-dispatcher - Queue and Dispatch application restart requests
#
# Author: Rodney Dawes <rodney.dawes@canonical.com>
#
# Copyright 2009 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
import dbus.service
import sys

from changeup import CHANGEUP_BUS_NAME
from dbus.mainloop.glib import DBusGMainLoop
from gobject import MainLoop

DBusGMainLoop(set_as_default=True)


class Dispatcher(dbus.service.Object):
    '''Service object for queuing requests.'''

    def __init__(self, *args, **kwargs):
        '''Initialize ourselves.'''
        self.queue = []
        self.path = '/queue'
        self.bus = dbus.SystemBus()
        self.__main = MainLoop()
        name = dbus.service.BusName(CHANGEUP_BUS_NAME,
                                    bus=self.bus)
        dbus.service.Object.__init__(self, bus_name=name,
                                     object_path=self.path)

    def main(self):
        """Main loop.method."""
        self.__main.run()

    @dbus.service.method(CHANGEUP_BUS_NAME,
                         in_signature='', out_signature='')
    def dispatch_requests(self):
        '''Dispatch all the requests'''
        for appname in self.queue:
            self.RestartDispatched(appname)
        self.__main.quit()

    @dbus.service.method(CHANGEUP_BUS_NAME,
                         in_signature='s', out_signature='')
    def queue_restart(self, appname):
        '''Queue an application restart.'''
        if appname not in self.queue:
            self.queue.append(appname)

    @dbus.service.signal(dbus_interface=CHANGEUP_BUS_NAME, signature='s')
    def RestartDispatched(self, appname):
        '''Signal for dispatching requests.'''
        self.queue.remove(appname)
        return appname


if __name__ == "__main__":
    bus = dbus.SystemBus()
    if bus.request_name(CHANGEUP_BUS_NAME, dbus.bus.NAME_FLAG_DO_NOT_QUEUE) == dbus.bus.REQUEST_NAME_REPLY_EXISTS:
        sys.exit(0)

    dispatcher = Dispatcher()
    dispatcher.main()