diff --git a/shell/view/Shell.py b/shell/view/Shell.py index 7c7d7490..777c10c4 100644 --- a/shell/view/Shell.py +++ b/shell/view/Shell.py @@ -197,14 +197,20 @@ class Shell(gobject.GObject): screenshot.get_from_drawable(window, window.get_colormap(), x_orig, y_orig, 0, 0, width, height) screenshot.save(file_path, "png") - - jobject = datastore.create() - jobject.metadata['title'] = _('Screenshot') - jobject.metadata['keep'] = '0' - jobject.metadata['buddies'] = '' - jobject.metadata['preview'] = '' - jobject.metadata['icon-color'] = profile.get_color().to_string() - jobject.metadata['mime_type'] = 'image/png' - jobject.file_path = file_path - datastore.write(jobject) + try: + jobject = datastore.create() + try: + jobject.metadata['title'] = _('Screenshot') + jobject.metadata['keep'] = '0' + jobject.metadata['buddies'] = '' + jobject.metadata['preview'] = '' + jobject.metadata['icon-color'] = profile.get_color().to_string() + jobject.metadata['mime_type'] = 'image/png' + jobject.file_path = file_path + datastore.write(jobject) + finally: + datastore.destroy() + del datastore + finally: + os.remove(file_path) diff --git a/sugar/activity/activity.py b/sugar/activity/activity.py index ffa46d90..a616ccd8 100644 --- a/sugar/activity/activity.py +++ b/sugar/activity/activity.py @@ -234,6 +234,7 @@ class Activity(Window, gtk.Container): self._internal_joined_cb(self._shared_activity, True, None) self._bus = ActivityService(self) + self._owns_file = False if handle.object_id: self._jobject = datastore.get(handle.object_id) @@ -346,9 +347,13 @@ class Activity(Window, gtk.Container): else: self.metadata['preview'] = self._preview try: - file_path = os.path.join(tempfile.gettempdir(), '%i' % time.time()) - self.write_file(file_path) - self._jobject.file_path = file_path + if self._jobject.file_path: + self.write_file(self._jobject.file_path) + else: + file_path = os.path.join(tempfile.gettempdir(), '%i' % time.time()) + self.write_file(file_path) + self._owns_file = True + self._jobject.file_path = file_path except NotImplementedError: pass datastore.write(self._jobject, @@ -409,6 +414,13 @@ class Activity(Window, gtk.Container): self._preview = self._get_preview() self.save() + + if self._jobject: + if self._owns_file and os.path.isfile(self._jobject.file_path): + os.remove(self._jobject.file_path) + self._owns_file = False + self._jobject.destroy() + self._jobject = None def get_metadata(self): if self._jobject: diff --git a/sugar/datastore/datastore.py b/sugar/datastore/datastore.py index 4249239c..9fca07a6 100644 --- a/sugar/datastore/datastore.py +++ b/sugar/datastore/datastore.py @@ -17,6 +17,7 @@ import logging from datetime import datetime +import os import gobject @@ -62,11 +63,13 @@ class DSMetadata(gobject.GObject): def get_dictionary(self): return self._props -class DSObject: +class DSObject(object): def __init__(self, object_id, metadata=None, file_path=None): self.object_id = object_id self._metadata = metadata self._file_path = file_path + self._destroyed = False + self._owns_file = False def get_metadata(self): if self._metadata is None and not self.object_id is None: @@ -83,10 +86,15 @@ class DSObject: def get_file_path(self): if self._file_path is None and not self.object_id is None: self.set_file_path(dbus_helpers.get_filename(self.object_id)) + self._owns_file = True return self._file_path def set_file_path(self, file_path): if self._file_path != file_path: + if self._file_path and self._owns_file: + if os.path.isfile(self._file_path): + os.remove(self._file_path) + self._owns_file = False self._file_path = file_path file_path = property(get_file_path, set_file_path) @@ -126,6 +134,26 @@ class DSObject: activityfactory.create(service_name, handle) + def destroy(self): + logging.debug('DSObject.destroy() file_path: %r.' % self._file_path) + if self._destroyed: + logging.warning('This DSObject has already been destroyed!.') + import pdb;pdb.set_trace() + return + self._destroyed = True + if self._file_path and self._owns_file: + logging.debug('Removing temp file: %r' % self._file_path) + if os.path.isfile(self._file_path): + os.remove(self._file_path) + self._owns_file = False + self._file_path = None + + def __del__(self): + if not self._destroyed: + logging.warning('DSObject was deleted without cleaning up first. ' \ + 'Please call DSObject.destroy() before disposing it.') + self.destroy() + def get(object_id): logging.debug('datastore.get') metadata = dbus_helpers.get_properties(object_id) @@ -145,10 +173,6 @@ def write(ds_object, update_mtime=True, reply_handler=None, error_handler=None): logging.debug('datastore.write') properties = ds_object.metadata.get_dictionary().copy() - # The title property should be sent as a 'text' property so it gets indexed - if properties.has_key('title'): - properties['title:text'] = properties['title'] - del properties['title'] if update_mtime: properties['mtime'] = datetime.now().isoformat()