From 653191806fc3739819773df050b2682026524009 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 14 Mar 2007 13:04:10 -0400 Subject: [PATCH] Copy interesting files to /home/dcbw after download --- services/clipboard/clipboardobject.py | 6 +++++ services/clipboard/clipboardservice.py | 36 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/services/clipboard/clipboardobject.py b/services/clipboard/clipboardobject.py index af8ba5f9..919acd00 100644 --- a/services/clipboard/clipboardobject.py +++ b/services/clipboard/clipboardobject.py @@ -54,3 +54,9 @@ class Format: def get_data(self): return self._data + + def _set_data(self, data): + self._data = data + + def get_on_disk(self): + return self._on_disk diff --git a/services/clipboard/clipboardservice.py b/services/clipboard/clipboardservice.py index bd932c5b..0ed423b5 100644 --- a/services/clipboard/clipboardservice.py +++ b/services/clipboard/clipboardservice.py @@ -16,11 +16,14 @@ import logging import gobject +import os +import shutil import dbus import dbus.service from sugar import env from sugar import util from clipboardobject import ClipboardObject, Format +import typeregistry NAME_KEY = 'NAME' PERCENT_KEY = 'PERCENT' @@ -48,6 +51,34 @@ class ClipboardDBusServiceHelper(dbus.service.Object): self._next_id += 1 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.get_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.service.method(_CLIPBOARD_DBUS_INTERFACE, in_signature="s", out_signature="o") @@ -93,7 +124,12 @@ class ClipboardDBusServiceHelper(dbus.service.Object): if cb_object.get_percent() == percent: # ignore setting same percentage return + cb_object.set_percent(percent) + + if percent == 100: + self._handle_file_completed(cb_object) + self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(), PERCENT_KEY: percent, ICON_KEY: cb_object.get_icon(),