diff options
-rw-r--r-- | downloadmanager.py | 85 | ||||
-rwxr-xr-x | webactivity.py | 9 |
2 files changed, 48 insertions, 46 deletions
diff --git a/downloadmanager.py b/downloadmanager.py index a190d75..9f3dc8a 100644 --- a/downloadmanager.py +++ b/downloadmanager.py @@ -27,9 +27,10 @@ from xpcom.server.factory import Factory import dbus from sugar.datastore import datastore -from sugar.clipboard import clipboardservice from sugar import profile from sugar import objects +from sugar.graphics.alert import Alert, ContinueAlert +from sugar.graphics import style # #3903 - this constant can be removed and assumed to be 1 when dbus-python # 0.82.3 is the only version used @@ -47,10 +48,13 @@ DS_DBUS_PATH = '/org/laptop/sugar/DataStore' _browser = None _temp_path = '/tmp' -def init(browser, temp_path): +def init(browser, activity, temp_path): global _browser _browser = browser + global _activity + _activity = activity + global _temp_path _temp_path = temp_path @@ -97,9 +101,9 @@ components.registrar.registerFactory('{64355793-988d-40a5-ba8e-fcde78cac631}"', class Download: _com_interfaces_ = interfaces.nsITransfer - - def init(self, source, target, display_name, mime_info, start_time, temp_file, - cancelable): + + def init(self, source, target, display_name, mime_info, start_time, + temp_file, cancelable): self._source = source self._mime_type = mime_info.MIMEType self._temp_file = temp_file @@ -114,17 +118,27 @@ class Download: def onStateChange(self, web_progress, request, state_flags, status): if state_flags == interfaces.nsIWebProgressListener.STATE_START: - self._create_journal_object() - self._create_clipboard_object() + self._create_journal_object() + alert = ContinueAlert(9, 'Continue') + alert.props.title = _('Download started') + path, file_name = os.path.split(self._target_file.path) + alert.props.msg = _('%s'%(file_name)) + _activity.add_alert(alert) + alert.connect('response', self.__start_response_cb) + alert.show() elif state_flags == interfaces.nsIWebProgressListener.STATE_STOP: if NS_FAILED(status): # download cancelled return - - cb_service = clipboardservice.get_instance() - # Test if the object still exists in the clipboard as it could have - # been removed. - if cb_service.get_object(self._cb_object_id): - cb_service.set_object_percent(self._cb_object_id, 100) + alert = Alert() + alert.props.title = _('Download completed') + path, file_name = os.path.split(self._target_file.path) + alert.props.msg = _('%s'%(file_name)) + alert.add_button(0, _('Open')) + alert.add_button(1, _('Show')) + alert.add_button(2, _('Ok')) + _activity.add_alert(alert) + alert.connect('response', self.__stop_response_cb) + alert.show() path, file_name = os.path.split(self._target_file.path) @@ -144,6 +158,21 @@ class Download: error_handler=self._internal_save_error_cb, timeout=360 * DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND) + def __start_response_cb(self, alert, response_id): + if response_id == 0: + logging.debug('Download Canceled') + self._cancelable.cancel(NS_ERROR_FAILURE) + if self._dl_jobject is not None: + self._datastore_deleted_handler.remove() + datastore.delete(self._dl_jobject.object_id) + self._dl_jobject.destroy() + self._dl_jobject = None + _activity.remove_alert(alert) + + def __stop_response_cb(self, alert, response_id): + logging.debug('Download Completed %d'%response_id) + _activity.remove_alert(alert) + def _cleanup_datastore_write(self): global _active_ds_writes _active_ds_writes = _active_ds_writes - 1 @@ -180,12 +209,6 @@ class Download: self._dl_jobject.metadata['progress'] = str(percent) datastore.write(self._dl_jobject) - cb_service = clipboardservice.get_instance() - # Test if the object still exists in the clipboard as it could have - # been removed. - if cb_service.get_object(self._cb_object_id): - cb_service.set_object_percent(self._cb_object_id, percent) - def _create_journal_object(self): path, file_name = os.path.split(self._target_file.path) @@ -205,33 +228,15 @@ class Download: bus = dbus.SessionBus() obj = bus.get_object(DS_DBUS_SERVICE, DS_DBUS_PATH) datastore_dbus = dbus.Interface(obj, DS_DBUS_INTERFACE) - datastore_dbus.connect_to_signal('Deleted', self.__datastore_deleted_cb, - arg0=self._dl_jobject.object_id) + self._datastore_deleted_handler = datastore_dbus.connect_to_signal( + 'Deleted', self.__datastore_deleted_cb, + arg0=self._dl_jobject.object_id) def __datastore_deleted_cb(self, uid): logging.debug('Downloaded entry has been deleted from the datastore: %r' % uid) # TODO: Use NS_BINDING_ABORTED instead of NS_ERROR_FAILURE. self._cancelable.cancel(NS_ERROR_FAILURE) #NS_BINDING_ABORTED) - def _create_clipboard_object(self): - path, file_name = os.path.split(self._target_file.path) - - cb_service = clipboardservice.get_instance() - self._cb_object_id = cb_service.add_object(file_name) - - # TODO: avoid by now adding two formats, as they would create two - # identical files in /tmp - #cb_service.add_object_format(self._cb_object_id, - # self._mime_type, - # 'file://' + self._target_file.path.encode('utf8'), - # on_disk = True) - - # Also add the 'text/uri-list' target for the same file path. - cb_service.add_object_format(self._cb_object_id, - 'text/uri-list', - 'file://' + self._target_file.path.encode('utf8'), - on_disk = True) - components.registrar.registerFactory('{23c51569-e9a1-4a92-adeb-3723db82ef7c}"', 'Sugar Download', '@mozilla.org/transfer;1', diff --git a/webactivity.py b/webactivity.py index 629f714..61b2d0e 100755 --- a/webactivity.py +++ b/webactivity.py @@ -67,13 +67,10 @@ class WebActivity(activity.Activity): _logger.debug('Starting the web activity') - if browser: - self._browser = browser - else: - self._browser = Browser() + self._browser = Browser() temp_path = os.path.join(self.get_activity_root(), 'tmp') - downloadmanager.init(self._browser, temp_path) + downloadmanager.init(self._browser, self, temp_path) sessionhistory.init(self._browser) progresslistener.init(self._browser) @@ -118,7 +115,7 @@ class WebActivity(activity.Activity): if handle.uri: self._browser.load_uri(handle.uri) - elif not self._jobject.file_path and not browser: + elif not self._jobject.file_path: # TODO: we need this hack until we extend the activity API for # opening URIs and default docs. self._load_homepage() |