#1984 Fix removing items from the clipboard.

This commit is contained in:
Tomeu Vizoso 2007-07-10 20:35:51 +02:00
parent f182201b34
commit e4bce9271a
4 changed files with 36 additions and 49 deletions

2
NEWS
View File

@ -1,3 +1,5 @@
* #1984 Fix removing items from the clipboard. (tomeu)
Snapshot b83a9ec27d Snapshot b83a9ec27d
* Fix font size on the XO. (marco) * Fix font size on the XO. (marco)

View File

@ -141,7 +141,7 @@ class Format:
def get_data(self): def get_data(self):
return self._data return self._data
def _set_data(self, data): def set_data(self, data):
self._data = data self._data = data
def is_on_disk(self): def is_on_disk(self):

View File

@ -17,10 +17,16 @@
import logging import logging
import os import os
import shutil import shutil
import urlparse
import tempfile
import dbus import dbus
import dbus.service import dbus.service
from sugar import env from sugar import env
from sugar import util from sugar import util
from sugar.objects import mime
from clipboardobject import ClipboardObject, Format from clipboardobject import ClipboardObject, Format
NAME_KEY = 'NAME' NAME_KEY = 'NAME'
@ -52,34 +58,6 @@ class ClipboardService(dbus.service.Object):
self._next_id += 1 self._next_id += 1
return self._next_id return self._next_id
def _handle_file_completed(self, cb_object):
"""If the object is an on-disk file, and it's at 100%, and we care about
it's file type, copy that file to $HOME and upate the clipboard object's
data to point to the new location"""
formats = cb_object.get_formats()
if not len(formats) or len(formats) > 1:
return
format = formats.values()[0]
if not format.is_on_disk():
return
if not len(cb_object.get_activity()):
# no activity to handle this, don't autosave it
return
# copy to homedir
src = format.get_data()
if not os.path.exists(src):
logging.debug("File %s doesn't appear to exist" % src)
return
dst = os.path.join(os.path.expanduser("~"), os.path.basename(src))
try:
shutil.move(src, dst)
format._set_data(dst)
except IOError, e:
logging.debug("Couldn't move file %s to %s: %s" % (src, dst, e))
# dbus methods # dbus methods
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE, @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="s", out_signature="o") in_signature="s", out_signature="o")
@ -96,11 +74,13 @@ class ClipboardService(dbus.service.Object):
def add_object_format(self, object_path, format_type, data, on_disk): def add_object_format(self, object_path, format_type, data, on_disk):
logging.debug('ClipboardService.add_object_format') logging.debug('ClipboardService.add_object_format')
cb_object = self._objects[str(object_path)] cb_object = self._objects[str(object_path)]
cb_object.add_format(Format(format_type, data, on_disk))
if on_disk and cb_object.get_percent() == 100:
if on_disk: new_uri = self._copy_file(data)
logging.debug('Added format of type ' + format_type + ' with path at ' + 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: else:
cb_object.add_format(Format(format_type, data, on_disk))
logging.debug('Added in-memory format of type ' + format_type + '.') logging.debug('Added in-memory format of type ' + format_type + '.')
self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(), self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(),
@ -132,7 +112,10 @@ class ClipboardService(dbus.service.Object):
cb_object.set_percent(percent) cb_object.set_percent(percent)
if percent == 100: if percent == 100:
self._handle_file_completed(cb_object) for format_name, format in cb_object.get_formats().iteritems():
if format.is_on_disk():
new_uri = self._copy_file(format.get_data())
format.set_data(new_uri)
self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(), self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(),
PERCENT_KEY: percent, PERCENT_KEY: percent,
@ -183,6 +166,21 @@ class ClipboardService(dbus.service.Object):
def object_state_changed(self, object_path, values): def object_state_changed(self, object_path, values):
pass 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 _instance = None
def get_instance(): def get_instance():

View File

@ -13,16 +13,14 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import shutil
import os import os
import logging import logging
import urlparse
import hippo import hippo
import gtk import gtk
from sugar import util from sugar import util
from sugar.objects import mime
from view.clipboardicon import ClipboardIcon from view.clipboardicon import ClipboardIcon
from sugar.clipboard import clipboardservice from sugar.clipboard import clipboardservice
@ -101,21 +99,10 @@ class ClipboardBox(hippo.CanvasBox):
uris = selection.data.split('\n') uris = selection.data.split('\n')
if len(uris) > 1: if len(uris) > 1:
raise NotImplementedError('Multiple uris in text/uri-list still not supported.') raise NotImplementedError('Multiple uris in text/uri-list still not supported.')
uri = urlparse.urlparse(uris[0])
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)
file_name = root + '.' + mime.get_primary_extension(mime_type)
# Copy the file, as it will be deleted when the dnd operation finishes.
new_file_path = os.path.join(path, 'cb' + file_name)
shutil.copyfile(uri.path, new_file_path)
cb_service.add_object_format(object_id, cb_service.add_object_format(object_id,
selection.type, selection.type,
"file://" + new_file_path, uris[0],
on_disk=True) on_disk=True)
else: else:
cb_service.add_object_format(object_id, cb_service.add_object_format(object_id,