Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/shellservice.py
blob: dd5e224d4c846bee60a944d0970e8041f000347a (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright (C) 2006-2007 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

"""D-bus service providing access to the shell's functionality"""
import dbus

from model import bundleregistry

_DBUS_SERVICE = "org.laptop.Shell"
_DBUS_ACTIVITY_REGISTRY_IFACE = "org.laptop.Shell.ActivityRegistry"
_DBUS_SHELL_IFACE = "org.laptop.Shell"
_DBUS_OWNER_IFACE = "org.laptop.Shell.Owner"
_DBUS_PATH = "/org/laptop/Shell"

class ShellService(dbus.service.Object):
    """Provides d-bus service to script the shell's operations
    
    Uses a shell_model object to observe events such as changes to:
    
        * nickname 
        * colour
        * icon
        * currently active activity
    
    and pass the event off to the methods in the dbus signature.
    
    Key method here at the moment is add_bundle, which is used to 
    do a run-time registration of a bundle using it's application path.
    
    XXX At the moment the d-bus service methods do not appear to do
    anything other than add_bundle
    """
    def __init__(self, shell):
        self._shell = shell
        self._shell_model = shell.get_model()

        self._owner = self._shell_model.get_owner()
        self._owner.connect('nick-changed', self._owner_nick_changed_cb)
        self._owner.connect('icon-changed', self._owner_icon_changed_cb)
        self._owner.connect('color-changed', self._owner_color_changed_cb)

        self._home_model = self._shell_model.get_home()
        self._home_model.connect('active-activity-changed',
                                 self._cur_activity_changed_cb)

        self._shell_model.connect('notify::zoom-level',
                                  self._shell_model_notify_zoom_level_cb)
        
        bus = dbus.SessionBus()
        bus_name = dbus.service.BusName(_DBUS_SERVICE, bus=bus)
        dbus.service.Object.__init__(self, bus_name, _DBUS_PATH)

    @dbus.service.method(_DBUS_SHELL_IFACE,
                         in_signature="ss", out_signature="")
    def NotifyLaunch(self, bundle_id, activity_id):
        self._shell.notify_launch(bundle_id, activity_id)

    @dbus.service.method(_DBUS_SHELL_IFACE,
                         in_signature="s", out_signature="")
    def NotifyLaunchFailure(self, activity_id):
        self._shell.notify_launch_failure(activity_id)

    @dbus.service.method(_DBUS_ACTIVITY_REGISTRY_IFACE,
                         in_signature="s", out_signature="b")
    def AddBundle(self, bundle_path):
        """Register the activity bundle with the global registry 
        
        bundle_path -- path to the activity bundle's root directory,
            that is, the directory with activity/activity.info as a 
            child of the directory.
        
        The bundleregistry.BundleRegistry is responsible for setting 
        up a set of d-bus service mappings for each available activity.
        """
        registry = bundleregistry.get_registry()
        return registry.add_bundle(bundle_path)

    @dbus.service.method(_DBUS_ACTIVITY_REGISTRY_IFACE,
                         in_signature="s", out_signature="a{sv}")
    def GetActivity(self, service_name):
        registry = bundleregistry.get_registry()
        bundle = registry.get_bundle(service_name)
        if not bundle:
            return {}
        
        return self._bundle_to_dict(bundle)

    @dbus.service.method(_DBUS_ACTIVITY_REGISTRY_IFACE,
                         in_signature="s", out_signature="aa{sv}")
    def FindActivity(self, name):
        result = []
        key = name.lower()

        for bundle in bundleregistry.get_registry():
            name = bundle.get_name().lower()
            service_name = bundle.get_service_name().lower()
            if name.find(key) != -1 or service_name.find(key) != -1:
                result.append(self._bundle_to_dict(bundle))

        return result

    @dbus.service.method(_DBUS_ACTIVITY_REGISTRY_IFACE,
                         in_signature="s", out_signature="aa{sv}")
    def GetActivitiesForType(self, mime_type):
        result = []

        for bundle in bundleregistry.get_registry():
            if bundle.get_mime_types() and mime_type in bundle.get_mime_types():
                result.append(self._bundle_to_dict(bundle))

        return result

    @dbus.service.signal(_DBUS_OWNER_IFACE, signature="s")
    def ColorChanged(self, color):
        pass

    def _owner_color_changed_cb(self, new_color):
        self.ColorChanged(new_color.to_string())

    @dbus.service.signal(_DBUS_OWNER_IFACE, signature="s")
    def NickChanged(self, nick):
        pass

    def _owner_nick_changed_cb(self, new_nick):
        self.NickChanged(new_nick)

    @dbus.service.signal(_DBUS_OWNER_IFACE, signature="ay")
    def IconChanged(self, icon_data):
        pass

    def _owner_icon_changed_cb(self, new_icon):
        self.IconChanged(dbus.ByteArray(new_icon))

    @dbus.service.signal(_DBUS_OWNER_IFACE, signature="s")
    def CurrentActivityChanged(self, activity_id):
        pass

    @dbus.service.signal(_DBUS_SHELL_IFACE, signature="i")
    def ZoomLevelChanged(self, new_level):
        pass

    def _cur_activity_changed_cb(self, owner, new_activity):
        new_id = ""
        if new_activity:
            new_id = new_activity.get_activity_id()
        if new_id:
            self.CurrentActivityChanged(new_id)

    def _shell_model_notify_zoom_level_cb(self, shell_model, pspec):
        self.ZoomLevelChanged(shell_model.props.zoom_level)

    def _bundle_to_dict(self, bundle):
        return {'name': bundle.get_name(),
                'icon': bundle.get_icon(),
                'service_name': bundle.get_service_name(),
                'path': bundle.get_path()}