Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Tamagtochi Widget #852

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions data/org.sugarlabs.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<summary>A limit to the number of simultaneously open activities.</summary>
<description>This int is used to set a limit to the number of open activities. By default (0), there is no limit.</description>
</key>
<key name="variable-buddy-icon" type="b">
<default>false</default>
<summary>Enable/Disable variable appeareance of the buddy icon</summary>
<description>This will enable variable appearance of buddy icon on Sugar Desktop.</description>
</key>
<child name="user" schema="org.sugarlabs.user" />
<child name="journal" schema="org.sugarlabs.journal" />
<child name="sound" schema="org.sugarlabs.sound" />
Expand Down Expand Up @@ -218,6 +223,16 @@
<summary>Grace Time</summary>
<description>Defer forced shutdown to at least this many seconds since Sugar was started.</description>
</key>
<key name="battery-present" type="b">
<default>false</default>
<summary>Battery Presence</summary>
<description>Whether the machine is battery equipped or not.</description>
</key>
<key name="battery-level" type="d">
<default>100</default>
<summary>Battery Level</summary>
<description>Current battery level of the machine.</description>
</key>
</schema>
<schema id="org.sugarlabs.peripherals" path="/org/sugarlabs/peripherals/">
<child name="keyboard" schema="org.sugarlabs.peripherals.keyboard" />
Expand Down
8 changes: 7 additions & 1 deletion extensions/deviceicon/battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def _update_info(self):
self.palette.set_info(current_level, self._model.props.time_remaining,
status)

_settings.set_double('battery-level', current_level)

def __battery_status_changed_cb(self, model):
self._update_info()

Expand Down Expand Up @@ -315,4 +317,8 @@ def setup(tray):
device_prop_iface = dbus.Interface(device, dbus.PROPERTIES_IFACE)
device_type = device_prop_iface.Get(_UP_DEVICE_IFACE, 'Type')
if device_type == _UP_TYPE_BATTERY:
tray.add_device(DeviceView(device_path))
battery = DeviceView(device_path)
has_battery = battery._model.props.present
if has_battery:
_settings.set_boolean('battery-present', has_battery)
tray.add_device(battery)
102 changes: 102 additions & 0 deletions src/jarabe/view/buddyicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import gi
from gi.repository import Gio
from gi.repository import GLib

from sugar3.graphics import style
from sugar3.graphics.icon import CanvasIcon
from sugar3 import env
from sugar3.datastore import datastore

from jarabe.view.buddymenu import BuddyMenu
from jarabe.util.normalize import normalize_string

import os
import statvfs

_FILTERED_ALPHA = 0.33

Expand All @@ -39,6 +47,100 @@ def __init__(self, buddy, pixel_size=style.STANDARD_ICON_SIZE):

self._update_color()

settings = Gio.Settings('org.sugarlabs')
variable_apr = settings.get_boolean('variable-buddy-icon')

if variable_apr:
self.icon_dict = {'embryo': {'normal': 'embryo-test',
'disk_50': 'embryo-disk50',
'disk_90': 'embryo-disk90'},
'teen': {'normal': 'teen',
'disk_50': 'teen-disk50',
'disk_90': 'teen-disk90'},
'adult': {'normal': 'computer-xo',
'disk_50': 'adult-disk50',
'disk_90': 'adult-disk90'}
}

self.journal_entries = 0
self.has_battery = None
self.__tamagotchi_thread()
_, self.journal_entries = datastore.find({})
datastore.updated.connect(self.__datastore_listener_updated_cb)
datastore.deleted.connect(self.__datastore_listener_deleted_cb)

def __tamagotchi_thread(self):
GLib.timeout_add(60000, self.__tamagotchi_thread)

user_type = None
disk_space_type = None
_, self.total, self.used = self._get_space()

if self.journal_entries <= 10:
user_type = 'embryo'
elif self.journal_entries > 10 and self.journal_entries <= 50:
user_type = 'teen'
elif self.journal_entries >= 50:
user_type = 'adult'

diskspace_50 = int(self.total / 2)
diskspace_90 = int((90 * self.total) / 100)

if self.used > diskspace_90:
disk_space_type = 'disk_90'
elif self.used > diskspace_50:
disk_space_type = 'disk_50'
else:
disk_space_type = 'normal'

self.set_icon_name(self.icon_dict[user_type][disk_space_type])
self.__get_battery()
self.__status_tooltip(self.has_battery)

def __datastore_listener_updated_cb(self, **kwargs):
self.journal_entries += 1

def __datastore_listener_deleted_cb(self, **kwargs):
self.journal_entries -= 1

def __get_battery(self):
settings = Gio.Settings('org.sugarlabs.power')
self.has_battery = settings.get_boolean('battery-present')
self.level = settings.get_double('battery-level')
if self.has_battery:
if self.level == 100:
icon_name = 'battery-100'
else:
icon_name = 'battery-0' + str(int(self.level / 10)) + '0'
self.props.badge_name = icon_name

def __status_tooltip(self, has_battery=False):
disk_usage = (self.used * 100) / self.total
battery = ''
if self.has_battery:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your change here to use self.has_battery means the keyword argument is not needed?

battery = "\n{} Battery".format(str(self.level))
tooltip_str = "{}% Disk space used {}".format(str(disk_usage), battery)
self.set_tooltip_text(tooltip_str)

def __datastore_query(self):
_, entries = datastore.find({})
self.journal_entries = entries

def _get_space(self):
stat = os.statvfs(env.get_profile_path())
free_space = stat[statvfs.F_BSIZE] * stat[statvfs.F_BAVAIL]
total_space = stat[statvfs.F_BSIZE] * stat[statvfs.F_BLOCKS]

free_space = self._get_MBs(free_space)
total_space = self._get_MBs(total_space)
used_space = total_space - free_space

return free_space, total_space, used_space

def _get_MBs(self, space):
space = space / (1024 * 1024)
return space

def create_palette(self):
palette = BuddyMenu(self._buddy)
self.connect_to_palette_pop_events(palette)
Expand Down