2019-07-10 01:51:41 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
|
|
|
|
from gi.repository import GObject, Gtk
|
|
|
|
from .drawing import Drawing
|
|
|
|
from .svg import JsonSvg
|
|
|
|
|
|
|
|
import json
|
2019-07-15 07:39:19 +02:00
|
|
|
import time
|
2019-07-10 01:51:41 +02:00
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
|
|
|
2019-07-11 12:17:38 +02:00
|
|
|
import logging
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logger = logging.getLogger('drawingperspective')
|
|
|
|
|
2019-07-15 07:39:19 +02:00
|
|
|
def relative_time(seconds):
|
|
|
|
MIN = 60
|
|
|
|
H = 60 * MIN
|
|
|
|
DAY = 24 * H
|
|
|
|
WEEK = 7 * DAY
|
|
|
|
|
|
|
|
if seconds < 30:
|
|
|
|
return 'just now'
|
|
|
|
if seconds < 5 * MIN:
|
|
|
|
return 'a few minutes ago'
|
|
|
|
if seconds < H:
|
|
|
|
return f'{int(seconds/MIN/10) * 10} minutes ago'
|
|
|
|
if seconds < DAY:
|
|
|
|
return f'{int(seconds/H)} hours ago'
|
|
|
|
if seconds < 4 * WEEK:
|
|
|
|
return f'{int(seconds/D)} days ago'
|
|
|
|
return 'a long time ago'
|
|
|
|
|
2019-07-10 01:51:41 +02:00
|
|
|
|
|
|
|
@Gtk.Template(resource_path="/org/freedesktop/TuhiGui/ui/DrawingPerspective.ui")
|
|
|
|
class DrawingPerspective(Gtk.Stack):
|
|
|
|
__gtype_name__ = "DrawingPerspective"
|
|
|
|
|
|
|
|
image_battery = Gtk.Template.Child()
|
|
|
|
flowbox_drawings = Gtk.Template.Child()
|
2019-07-15 07:39:19 +02:00
|
|
|
spinner_sync = Gtk.Template.Child()
|
|
|
|
label_last_sync = Gtk.Template.Child()
|
2019-07-10 01:51:41 +02:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-07-11 08:05:41 +02:00
|
|
|
self.known_drawings = []
|
2019-07-15 07:39:19 +02:00
|
|
|
self.last_sync_time = 0
|
|
|
|
self._sync_label_timer = GObject.timeout_add_seconds(60, self._update_sync_label)
|
|
|
|
self._update_sync_label()
|
2019-07-10 01:51:41 +02:00
|
|
|
|
2019-07-11 08:05:41 +02:00
|
|
|
def _update_drawings(self, device, pspec):
|
2019-07-10 01:51:41 +02:00
|
|
|
for ts in self.device.drawings_available:
|
2019-07-11 08:05:41 +02:00
|
|
|
if ts in self.known_drawings:
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.known_drawings.append(ts)
|
2019-07-10 01:51:41 +02:00
|
|
|
js = json.loads(self.device.json(ts))
|
|
|
|
svg = JsonSvg(js)
|
|
|
|
drawing = Drawing(svg)
|
|
|
|
self.flowbox_drawings.add(drawing)
|
|
|
|
|
|
|
|
@GObject.Property
|
|
|
|
def device(self):
|
|
|
|
return self._device
|
|
|
|
|
|
|
|
@device.setter
|
|
|
|
def device(self, device):
|
|
|
|
self._device = device
|
|
|
|
|
2019-07-11 08:05:41 +02:00
|
|
|
device.connect('notify::connected', self._on_connected)
|
|
|
|
device.connect('notify::listening', self._on_listening_stopped)
|
2019-07-15 07:39:19 +02:00
|
|
|
device.connect('notify::sync-state', self._on_sync_state)
|
2019-07-11 08:05:41 +02:00
|
|
|
self.device.connect('notify::drawings-available',
|
|
|
|
self._update_drawings)
|
|
|
|
|
2019-07-10 01:51:41 +02:00
|
|
|
# icon name is something like battery-020-charging, or battery-040
|
|
|
|
# in 20-step increments
|
|
|
|
if device.battery_state == 1:
|
|
|
|
state = '-charging'
|
|
|
|
else:
|
|
|
|
state = ''
|
2019-07-11 08:09:42 +02:00
|
|
|
percent = f'{int(device.battery_percent/20) * 20:03d}'
|
2019-07-10 01:51:41 +02:00
|
|
|
batt_icon_name = f'battery-{percent}{state}'
|
|
|
|
_, isize = self.image_battery.get_icon_name()
|
|
|
|
self.image_battery.set_from_icon_name(batt_icon_name, isize)
|
2019-07-11 08:05:41 +02:00
|
|
|
self._update_drawings(self.device, None)
|
|
|
|
|
|
|
|
# We always want to sync on startup
|
2019-07-11 12:17:38 +02:00
|
|
|
logger.debug(f'{device.name} - starting to listen')
|
2019-07-11 08:05:41 +02:00
|
|
|
device.start_listening()
|
2019-07-10 01:51:41 +02:00
|
|
|
|
|
|
|
@GObject.Property
|
|
|
|
def name(self):
|
|
|
|
return "drawing_perspective"
|
|
|
|
|
2019-07-15 07:39:19 +02:00
|
|
|
def _on_sync_state(self, device, pspec):
|
|
|
|
if device.sync_state:
|
|
|
|
self.spinner_sync.start()
|
|
|
|
else:
|
|
|
|
self.spinner_sync.stop()
|
|
|
|
self.last_sync_time = time.time()
|
|
|
|
self._update_sync_label()
|
|
|
|
|
|
|
|
def _update_sync_label(self):
|
|
|
|
now = time.time()
|
|
|
|
self.label_last_sync.set_text(f'{relative_time(now - self.last_sync_time)}')
|
|
|
|
return True
|
|
|
|
|
2019-07-11 08:05:41 +02:00
|
|
|
def _on_connected(self, device, pspec):
|
2019-07-11 12:18:17 +02:00
|
|
|
# Turns out we don't really care about whether the device is
|
|
|
|
# connected or not, it has little effect on how we work here
|
2019-07-11 08:05:41 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
def _on_listening_stopped(self, device, pspec):
|
2019-07-11 12:18:17 +02:00
|
|
|
if not device.listening:
|
|
|
|
logger.debug(f'{device.name} - listening stopped, restarting')
|
|
|
|
# We never want to stop listening
|
|
|
|
device.start_listening()
|