Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/shell/clipboardservice.py
blob: fec546f5fa63e347a6d07ff357811f114c0476db (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
# Copyright (C) 2006, 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

import logging
import os
import shutil
import urlparse
import tempfile

import dbus
import dbus.service

from sugar import env
from sugar import util
from sugar import mime

from clipboardobject import ClipboardObject, Format

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'

class ClipboardService(dbus.service.Object):

    _CLIPBOARD_DBUS_INTERFACE = "org.laptop.Clipboard"
    _CLIPBOARD_OBJECT_PATH = "/org/laptop/Clipboard"
    _CLIPBOARD_OBJECTS_PATH = _CLIPBOARD_OBJECT_PATH + "/Objects/"

    def __init__(self):
        self._objects = {}
        self._next_id = 0

        bus = dbus.SessionBus()
        bus_name = dbus.service.BusName(self._CLIPBOARD_DBUS_INTERFACE, bus=bus)
        dbus.service.Object.__init__(self, bus_name, self._CLIPBOARD_OBJECT_PATH)

    def _get_next_object_id(self):
        self._next_id += 1
        return self._next_id

    # dbus methods        
    @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
                         in_signature="s", out_signature="o")
    def add_object(self, name):
        logging.debug('ClipboardService.add_object')
        op = self._CLIPBOARD_OBJECTS_PATH + "%d" % self._get_next_object_id()
        self._objects[op] = ClipboardObject(op, name)
        self.object_added(dbus.ObjectPath(op), name)
        logging.debug('Added object ' + op + ' with name ' + name)
        return dbus.ObjectPath(op)

    @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
                         in_signature="ssayb", out_signature="", byte_arrays=True)
    def add_object_format(self, object_path, format_type, data, on_disk):
        logging.debug('ClipboardService.add_object_format')
        cb_object = self._objects[str(object_path)]

        if format_type == 'XdndDirectSave0':
            format = Format('text/uri-list', data + '\r\n', on_disk)
            format.owns_disk_data = True
            cb_object.add_format(format)
        elif on_disk and cb_object.get_percent() == 100:
            new_uri = self._copy_file(data)
            cb_object.add_format(Format(format_type, new_uri, on_disk))
            logging.debug('Added format of type ' + format_type + ' with path at ' + new_uri)
        else:
            cb_object.add_format(Format(format_type, data, on_disk))
            logging.debug('Added in-memory format of type ' + format_type + '.')
                        
        self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(),
                                  PERCENT_KEY: cb_object.get_percent(),
                                  ICON_KEY: cb_object.get_icon(),
                                  PREVIEW_KEY: cb_object.get_preview(),
                                  ACTIVITIES_KEY: cb_object.get_activities()})

    @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
                         in_signature="o", out_signature="")
    def delete_object(self, object_path):
        cb_object = self._objects.pop(str(object_path))
        cb_object.destroy()
        self.object_deleted(object_path)
        logging.debug('Deleted object with object_id ' + object_path)
        
    @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
                         in_signature="oi", out_signature="")
    def set_object_percent(self, object_path, percent):
        cb_object = self._objects[str(object_path)]
        if percent < 0 or percent > 100:
            raise ValueError("invalid percentage")
        if cb_object.get_percent() > percent:
            raise ValueError("invalid percentage; less than current percent")
        if cb_object.get_percent() == percent:
            # ignore setting same percentage
            return

        cb_object.set_percent(percent)

        if percent == 100:
            formats = cb_object.get_formats()
            for format_name, format in formats.iteritems():
                if format.is_on_disk() and not format.owns_disk_data:
                    new_uri = self._copy_file(format.get_data())
                    format.set_data(new_uri)

            # Add a text/plain format to objects that are text but lack it
            if 'text/plain' not in formats.keys():
                if 'UTF8_STRING' in formats.keys():
                    self.add_object_format(object_path,
                                           'text/plain',
                                           data=formats['UTF8_STRING'].get_data(),
                                           on_disk=False)
                elif 'text/unicode' in formats.keys():
                    self.add_object_format(object_path,
                                           'text/plain',
                                           data=formats['UTF8_STRING'].get_data(),
                                           on_disk=False)

        self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(),
                                    PERCENT_KEY: percent,
                                    ICON_KEY: cb_object.get_icon(),
                                    PREVIEW_KEY: cb_object.get_preview(),
                                    ACTIVITIES_KEY: cb_object.get_activities()})

    @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
                         in_signature="o", out_signature="a{sv}")
    def get_object(self, object_path):
        logging.debug('ClipboardService.get_object')

        if not self._objects.has_key(str(object_path)):
            return dbus.Dictionary({}, signature='sv')

        cb_object = self._objects[str(object_path)]
        formats = cb_object.get_formats()
        format_types = dbus.Array([], signature='s')
        
        for type, format in formats.iteritems():
            format_types.append(type)
        
        result_dict = {NAME_KEY: cb_object.get_name(),
                PERCENT_KEY: cb_object.get_percent(),
                ICON_KEY: cb_object.get_icon(),
                PREVIEW_KEY: cb_object.get_preview(),
                ACTIVITIES_KEY: cb_object.get_activities(),
                FORMATS_KEY: format_types}
        return dbus.Dictionary(result_dict)

    @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
                         in_signature="os", out_signature="a{sv}")
    def get_object_data(self, object_path, format_type):   
        logging.debug('ClipboardService.get_object_data')
        cb_object = self._objects[str(object_path)]
        format = cb_object.get_formats()[format_type]
        result_dict = {TYPE_KEY: format.get_type(),
                DATA_KEY: dbus.ByteArray(format.get_data()),
                ON_DISK_KEY: format.is_on_disk()}
        return dbus.Dictionary(result_dict)

    # dbus signals
    @dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="os")
    def object_added(self, object_path, name):
        pass

    @dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="o")
    def object_deleted(self, object_path):
        pass

    @dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="oa{sv}")
    def object_state_changed(self, object_path, values):
        pass

    def _copy_file(self, original_uri):
        uri = urlparse.urlparse(original_uri)
        path, file_name = os.path.split(uri.path)

        root, ext = os.path.splitext(file_name)
        if not ext or ext == '.':
            mime_type = mime.get_for_file(uri.path)
            ext = '.' + mime.get_primary_extension(mime_type)

        f, new_file_path = tempfile.mkstemp(ext, root)
        del f
        shutil.copyfile(uri.path, new_file_path)

        return 'file://' + new_file_path

_instance = None

def get_instance():
    global _instance
    if not _instance:
        _instance = ClipboardService()
    return _instance