don't crash when failing to load the icon mask

This commit is contained in:
Daniel Pavel 2013-05-19 03:52:01 +03:00
parent 87bc9a5431
commit ac0eafe6ab
4 changed files with 21 additions and 10 deletions

View File

@ -15,6 +15,7 @@ def _look_for_application_icons():
src_share = _path.normpath(_path.join(_path.realpath(_sys.path[0]), '..', 'share'))
local_share = _os.environ.get('XDG_DATA_HOME', _path.expanduser('~/.local/share'))
data_dirs = _os.environ.get('XDG_DATA_DIRS', '/usr/local/share:/usr/share')
del _sys
share_solaar = [prefix_share] + list(_path.join(x, 'solaar') for x in [src_share, local_share] + data_dirs.split(':'))
for location in share_solaar:
@ -25,7 +26,6 @@ def _look_for_application_icons():
# print ('XDG_DATA_DIRS=%s' % _os.environ['XDG_DATA_DIRS'])
break
del _sys
del _os
# del _path

View File

@ -27,7 +27,7 @@ APP_ICON = { 1: 'solaar', 2: 'solaar-mask', 0: 'solaar-init', -1: 'solaar-fail'
#
#
def battery(level=None):
def battery(level=None, charging=False):
if level is None or level < 0:
return 'battery_unknown'
return 'battery_%03d' % (10 * ((level + 5) // 10))
@ -88,4 +88,7 @@ def device_icon_name(name, kind=None):
def icon_file(name, size=_LARGE_SIZE):
theme = Gtk.IconTheme.get_default()
if theme.has_icon(name):
return theme.lookup_icon(name, size, 0).get_filename()
theme_icon = theme.lookup_icon(name, size, 0)
file_name = theme_icon.get_filename()
# print ("icon", name, "->", theme_icon, file_name)
return file_name

View File

@ -306,11 +306,11 @@ def _create(receiver):
window = Gtk.Window()
window.set_title(NAME + ': ' + receiver.name)
icon_file = _icons.icon_file(_icons.APP_ICON[1])
icon_file = _icons.icon_file(NAME.lower())
if icon_file:
window.set_icon_from_file(icon_file)
else:
window.set_icon_name(_icons.APP_ICON[1])
window.set_icon_name(NAME.lower())
window.set_role('status-window')
window.set_type_hint(Gdk.WindowTypeHint.UTILITY)
@ -429,7 +429,8 @@ def _update_device_box(frame, dev):
battery_label.set_markup('<small>no status</small>')
battery_label.set_sensitive(True)
else:
battery_icon.set_from_icon_name(_icons.battery(battery_level), _STATUS_ICON_SIZE)
icon_name = _icons.battery(battery_level)
battery_icon.set_from_icon_name(icon_name, _STATUS_ICON_SIZE)
battery_icon.set_sensitive(True)
battery_label.set_text('%d%%' % battery_level)
battery_label.set_sensitive(True)
@ -442,7 +443,7 @@ def _update_device_box(frame, dev):
light_icon.set_visible(False)
light_label.set_visible(False)
else:
icon_name = 'light_%03d' % (20 * ((light_level + 50) // 100))
icon_name = _icons.lux(light_level)
light_icon.set_from_icon_name(icon_name, _STATUS_ICON_SIZE)
light_icon.set_visible(True)
light_label.set_text('%d lux' % light_level)

View File

@ -75,7 +75,8 @@ def _icon_with_battery(level, active):
name = '%s-%s' % (battery_icon, active)
if name not in _PIXMAPS:
mask = _icons.icon_file(_icons.APP_ICON[2], 128)
assert mask
if not mask:
return
mask = GdkPixbuf.Pixbuf.new_from_file(mask)
assert mask.get_width() == 128 and mask.get_height() == 128
mask.saturate_and_pixelate(mask, 0.7, False)
@ -110,7 +111,11 @@ def _update_image(icon):
if battery_status is None:
icon.set_from_icon_name(_icons.APP_ICON[1])
else:
icon.set_from_pixbuf(_icon_with_battery(battery_level, bool(battery_status)))
pixbuf = _icon_with_battery(battery_level, bool(battery_status))
if pixbuf:
icon.set_from_pixbuf(pixbuf)
else:
icon.set_from_icon_name(_icons.APP_ICON[1])
#
#
@ -122,6 +127,8 @@ def _device_index(icon, device):
if rserial == device.receiver.serial and serial == device.serial:
return index
# print ("== device", device, device.receiver.serial, "not found in", icon._receivers, "/", icon._devices_info)
def _add_device(icon, device):
index = len(icon._devices_info)
@ -162,7 +169,7 @@ def _update_menu_item(icon, index, device_status):
menu_item = menu_items[index]
image = menu_item.get_image()
battery_level = device_status.get(_status.BATTERY_LEVEL) if device_status else None
battery_level = device_status.get(_status.BATTERY_LEVEL)
image.set_from_icon_name(_icons.battery(battery_level), Gtk.IconSize.LARGE_TOOLBAR)
image.set_sensitive(bool(device_status))
# menu_item.set_sensitive(bool(device_status))