[Sugar-devel] [PATCH] Journal:show error message on write failure: #1842

2010-07-02 Thread anishmangal2002
volumestoolbar.py now catches the IOError and ValueError
exceptions and emits 'volume-error'signal. This signal is
caught in journalactivity.py which displays the error as
an ErrorAlert message.

Signed-off-by: anishmangal2002 
---
 src/jarabe/journal/journalactivity.py |   16 
 src/jarabe/journal/volumestoolbar.py  |   22 +-
 2 files changed, 37 insertions(+), 1 deletions(-)

diff --git a/src/jarabe/journal/journalactivity.py 
b/src/jarabe/journal/journalactivity.py
index 0559560..eab292b 100644
--- a/src/jarabe/journal/journalactivity.py
+++ b/src/jarabe/journal/journalactivity.py
@@ -27,6 +27,9 @@ import statvfs
 import os
 
 from sugar.graphics.window import Window
+from sugar.graphics.alert import ErrorAlert
+from sugar.graphics.icon import Icon
+
 from sugar.bundle.bundle import ZipExtractException, RegistrationException
 from sugar import env
 from sugar.activity import activityfactory
@@ -138,6 +141,17 @@ class JournalActivity(Window):
 self._critical_space_alert = None
 self._check_available_space()
 
+def _alert_notify_cb(self, gobject, strerror, severity):
+alert = ErrorAlert()
+alert.props.title= severity
+alert.props.msg = strerror
+alert.connect('response', self._alert_response_cb)
+self.add_alert(alert)
+alert.show()
+
+def _alert_response_cb(self, alert, response_id):
+self.remove_alert(alert)
+
 def __realize_cb(self, window):
 wm.set_bundle_id(window.window, _BUNDLE_ID)
 activity_id = activityfactory.create_activity_id()
@@ -161,6 +175,8 @@ class JournalActivity(Window):
 self._volumes_toolbar = VolumesToolbar()
 self._volumes_toolbar.connect('volume-changed',
   self.__volume_changed_cb)
+self._volumes_toolbar.connect('volume-error', self._alert_notify_cb,
+  'Error')
 self._main_view.pack_start(self._volumes_toolbar, expand=False)
 
 search_toolbar = self._main_toolbox.search_toolbar
diff --git a/src/jarabe/journal/volumestoolbar.py 
b/src/jarabe/journal/volumestoolbar.py
index 74b974c..a6acebe 100644
--- a/src/jarabe/journal/volumestoolbar.py
+++ b/src/jarabe/journal/volumestoolbar.py
@@ -35,6 +35,9 @@ class VolumesToolbar(gtk.Toolbar):
 __gsignals__ = {
 'volume-changed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
+   ([str])),
+'volume-error': (gobject.SIGNAL_RUN_FIRST,
+   gobject.TYPE_NONE,
([str]))
 }
 
@@ -81,6 +84,7 @@ class VolumesToolbar(gtk.Toolbar):
 button = VolumeButton(mount)
 button.props.group = self._volume_buttons[0]
 button.connect('toggled', self._button_toggled_cb)
+button.connect('volume-error', self._volume_error_cb)
 position = self.get_item_index(self._volume_buttons[-1]) + 1
 self.insert(button, position)
 button.show()
@@ -90,6 +94,9 @@ class VolumesToolbar(gtk.Toolbar):
 if len(self.get_children()) > 1:
 self.show()
 
+def _volume_error_cb(self, button, strerror):
+self.emit('volume-error', strerror)
+
 def _button_toggled_cb(self, button):
 if button.props.active:
 self.emit('volume-changed', button.mount_point)
@@ -123,6 +130,12 @@ class VolumesToolbar(gtk.Toolbar):
 button.props.active = True
 
 class BaseButton(RadioToolButton):
+__gsignals__ = {
+'volume-error': (gobject.SIGNAL_RUN_FIRST,
+   gobject.TYPE_NONE,
+   ([str]))
+}
+
 def __init__(self, mount_point):
 RadioToolButton.__init__(self)
 
@@ -137,7 +150,14 @@ class BaseButton(RadioToolButton):
info, timestamp):
 object_id = selection_data.data
 metadata = model.get(object_id)
-model.copy(metadata, self.mount_point)
+try:
+model.copy(metadata, self.mount_point)
+except IOError as (errno, strerror):
+logging.error('BaseButton._drag_data_received_cb: IOError: %s; %s' 
% (errno, strerror))
+self.emit('volume-error', strerror)
+except ValueError as (strerror):
+logging.error('BaseButton._drag_data_received_cb: ValueError: %s' 
% (strerror))
+self.emit('volume-error', strerror)
 
 class VolumeButton(BaseButton):
 def __init__(self, mount):
-- 
1.7.0.1

___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


Re: [Sugar-devel] [PATCH] Journal:show error message on write failure: #1842

2010-07-03 Thread Sascha Silbe
Excerpts from anishmangal2002's message of Fri Jul 02 19:25:56 + 2010:


After this patch, the information flow will be the following:

* model.copy throws *Error 
* BaseButton emits volume-error (strerror)
* VolumesToolbar emits volume-error (strerror)
* JournalActivity instantiates NotifyAlert

This looks a bit convoluted to me, but I don't know whether there's a
better way.


[src/jarabe/journal/journalactivity.py]
> +def _alert_notify_cb(self, gobject, strerror, severity):
> +alert = ErrorAlert()
> +alert.props.title= severity
> +alert.props.msg = strerror
Please consider passing these directly to the constructor.

> @@ -161,6 +175,8 @@ class JournalActivity(Window):
>  self._volumes_toolbar = VolumesToolbar()
>  self._volumes_toolbar.connect('volume-changed',
>self.__volume_changed_cb)
> +self._volumes_toolbar.connect('volume-error', self._alert_notify_cb,
> +  'Error')
Why do you add an additional parameter ('Error')? AFAICT all signal
handlers should pass through strerror.


[src/jarabe/journal/volumestoolbar.py]
> @@ -137,7 +150,14 @@ class BaseButton(RadioToolButton):
> info, timestamp):
>  object_id = selection_data.data
>  metadata = model.get(object_id)
> -model.copy(metadata, self.mount_point)
> +try:
> +model.copy(metadata, self.mount_point)
> +except IOError as (errno, strerror):
Please don't use the except ... as ... syntax. It has been introduced in Python 
2.6, but we still support 2.5.

> +logging.error('BaseButton._drag_data_received_cb: IOError: %s; 
> %s' % (errno, strerror))
If you leave the exception intact while catching it (i.e. except IOError, 
exception:), it will do most of the formatting for you. You might even get 
below the 80 character line length limit this way. ;)

> +except ValueError as (strerror):
In what cases does model.copy() throw a ValueError? This sounds more like an 
internal error we should avoid...
If it does make sense to catch this here, please combine the two except blocks 
(which will be possible after the logging change mentioned above).

Sascha

--
http://sascha.silbe.org/
http://www.infra-silbe.de/


signature.asc
Description: PGP signature
___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel