Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib/sugar/clipboard/clipboardservice.py
blob: d975330dab5c86c9322273d348b37e7dcccead17 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""UI class to access system-level clipboard object"""
# Copyright (C) 2007, One Laptop Per Child
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import logging
import dbus
import gobject

NAME_KEY = 'NAME'
PERCENT_KEY = 'PERCENT'
ICON_KEY = 'ICON'
PREVIEW_KEY = 'PREVIEW'
ACTIVITIES_KEY = 'ACTIVITIES'
FORMATS_KEY = 'FORMATS'

TYPE_KEY = 'TYPE'
DATA_KEY = 'DATA'
ON_DISK_KEY = 'ON_DISK'

DBUS_SERVICE = "org.laptop.Clipboard"
DBUS_INTERFACE = "org.laptop.Clipboard"
DBUS_PATH = "/org/laptop/Clipboard"

class ClipboardService(gobject.GObject):
    """GUI interfaces for the system clipboard dbus service
    
    This object is used to provide convenient access to the clipboard
    service (see source/services/clipboard/clipboardservice.py).  It 
    provides utility methods for adding/getting/removing objects from 
    the clipboard as well as generating events when such events occur.
    
    Meaning is source/services/clipboard/clipboardobject.py 
    objects when describing "objects" on the clipboard.
    """
    __gsignals__ = {
        'object-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                        ([str, str])),
        'object-deleted': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                        ([str])),
        'object-state-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                        ([str, str, int, str, str, object])),
    }
    
    def __init__(self):
        """Initialise the ClipboardService instance
        
        If the service is not yet active in the background uses 
        a signal watcher to connect when the service appears.
        """
        gobject.GObject.__init__(self)

        self._dbus_service = None

        bus = dbus.SessionBus()
        self._nameOwnerChangedHandler = bus.add_signal_receiver(
                self._name_owner_changed_cb,
                signal_name="NameOwnerChanged",
                dbus_interface="org.freedesktop.DBus",
                arg0=DBUS_SERVICE)

        self._connected = False
        # Try to register to ClipboardService, if we fail, we'll try later.
        try:
            self._connect_clipboard_signals()
        except dbus.DBusException, exception:
            logging.debug(exception)

    def _connect_clipboard_signals(self):
        """Connect dbus signals to our GObject signal generating callbacks"""
        bus = dbus.SessionBus()
        if not self._connected:
            # NOTE: We need to follow_name_owner_changes here
            #       because we can not connect to a signal unless 
            #       we follow the changes or we start the service
            #       before we connect.  Starting the service here
            #       causes a major bottleneck during startup
            proxy_obj = bus.get_object(DBUS_SERVICE, 
                                       DBUS_PATH,
                                       follow_name_owner_changes=True)
            self._dbus_service = dbus.Interface(proxy_obj, DBUS_SERVICE)
            self._dbus_service.connect_to_signal('object_added',
                                                 self._object_added_cb)
            self._dbus_service.connect_to_signal('object_deleted',
                                                 self._object_deleted_cb)
            self._dbus_service.connect_to_signal('object_state_changed',
                                                 self._object_state_changed_cb)
            self._connected = True

        bus.remove_signal_receiver(self._nameOwnerChangedHandler)

    def _name_owner_changed_cb(self, name, old, new):
        """On backend service creation, connect to the server"""
        if not old and new:
            # ClipboardService started up
            self._connect_clipboard_signals()

    def _object_added_cb(self, object_id, name):
        """Emit an object-added GObject event when dbus event arrives"""
        self.emit('object-added', str(object_id), name)

    def _object_deleted_cb(self, object_id):
        """Emit an object-deleted GObject event when dbus event arrives"""
        self.emit('object-deleted', str(object_id))

    def _object_state_changed_cb(self, object_id, values):
        """Emit an object-state-changed GObject event when dbus event arrives
        
        GObject event has:
        
            object_id 
            name 
            percent 
            icon 
            preview
            activities
        
        From the ClipboardObject instance which is being described.
        """
        self.emit('object-state-changed', str(object_id), values[NAME_KEY],
                  values[PERCENT_KEY], values[ICON_KEY], values[PREVIEW_KEY],
                  values[ACTIVITIES_KEY])

    def add_object(self, name):
        """Add a new object to the path
        
        returns dbus path-name for the new object's cliboard service,
        this is used for all future references to the cliboard object.
        
        Note:
            That service is actually provided by the clipboard
            service object, not the ClipboardObject
        """
        return str(self._dbus_service.add_object(name))

    def add_object_format(self, object_id, formatType, data, on_disk):
        """Annotate given object on the clipboard with new information
        
        object_id -- dbus path as returned from add_object
        formatType -- XXX what should this be? mime type?
        data -- storage format for the clipped object?
        on_disk -- whether the data is on-disk (non-volatile) or in 
            memory (volatile)
        
        Last three arguments are just passed directly to the 
        clipboardobject.Format instance on the server side.
        
        returns None
        """
        self._dbus_service.add_object_format(dbus.ObjectPath(object_id),
                formatType,
                data,
                on_disk)
    
    def delete_object(self, object_id):
        """Remove the given object from the clipboard
        
        object_id -- dbus path as returned from add_object
        """
        self._dbus_service.delete_object(dbus.ObjectPath(object_id))
    
    def set_object_percent(self, object_id, percent):
        """Set the "percentage" for the given clipboard object 
        
        object_id -- dbus path as returned from add_object
        percentage -- numeric value from 0 to 100 inclusive
        
        Object percentages which are set to 100% trigger "file-completed"
        operations, see the backend ClipboardService's 
        _handle_file_completed method for details.
        
        returns None
        """
        self._dbus_service.set_object_percent(dbus.ObjectPath(object_id), percent)

    def get_object(self, object_id):
        """Retrieve the clipboard object structure for given object 
        
        object_id -- dbus path as returned from add_object
        
        Retrieves the metadata description of a given object, but 
        *not* the data for the object.  Use get_object_data passing 
        one of the values in the FORMATS_KEY value in order to 
        retrieve the data.
        
        returns dictionary with 
            NAME_KEY: str,
            PERCENT_KEY: number,
            ICON_KEY: str,
            PREVIEW_KEY: XXX what is it?,
            ACTIVITIES_KEY: activities that can open this object,
            FORMATS_KEY: list of XXX what is it?
        """
        return self._dbus_service.get_object(dbus.ObjectPath(object_id),)

    def get_object_data(self, object_id, formatType):    
        """Retrieve object's data in the given formatType
        
        object_id -- dbus path as returned from add_object
        formatType -- format specifier XXX of what description 
        
        returns dictionary with 
            TYPE_KEY: str,
            DATA_KEY: str,
            ON_DISK_KEY: bool
        """
        return self._dbus_service.get_object_data(dbus.ObjectPath(object_id),
                                                  formatType,
                                                  byte_arrays=True)

_clipboard_service = None
def get_instance():
    """Retrieve this process's interface to the clipboard service"""
    global _clipboard_service
    if not _clipboard_service:
        _clipboard_service = ClipboardService()
    return _clipboard_service