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.
|
|
|
|
#
|
|
|
|
|
2019-07-16 13:48:37 +02:00
|
|
|
from gettext import gettext as _
|
2019-07-10 01:51:41 +02:00
|
|
|
from gi.repository import GObject, Gtk
|
|
|
|
from .drawing import Drawing
|
2019-07-16 04:56:02 +02:00
|
|
|
from .config import Config
|
2019-07-10 01:51:41 +02:00
|
|
|
|
2019-07-15 07:39:19 +02:00
|
|
|
import time
|
2019-07-10 01:51:41 +02:00
|
|
|
import gi
|
2019-07-16 07:28:51 +02:00
|
|
|
import logging
|
|
|
|
|
2019-07-10 01:51:41 +02:00
|
|
|
gi.require_version("Gtk", "3.0")
|
|
|
|
|
2019-07-11 12:17:38 +02:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2019-07-18 02:10:15 +02:00
|
|
|
logger = logging.getLogger('tuhi.gui.drawingperspective')
|
2019-07-11 12:17:38 +02:00
|
|
|
|
2019-07-16 07:28:51 +02:00
|
|
|
|
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:
|
2019-07-16 13:48:37 +02:00
|
|
|
return _('just now')
|
2019-07-15 07:39:19 +02:00
|
|
|
if seconds < 5 * MIN:
|
2019-07-16 13:48:37 +02:00
|
|
|
return _('a few minutes ago')
|
2019-07-15 07:39:19 +02:00
|
|
|
if seconds < H:
|
2019-07-16 13:48:37 +02:00
|
|
|
minutes = int(seconds / MIN / 10) * 10
|
|
|
|
return _(f'{minutes} minutes ago')
|
2019-07-15 07:39:19 +02:00
|
|
|
if seconds < DAY:
|
2019-07-16 13:48:37 +02:00
|
|
|
hours = int(seconds / H)
|
|
|
|
return _(f'{hours} hours ago')
|
2019-07-15 07:39:19 +02:00
|
|
|
if seconds < 4 * WEEK:
|
2019-07-16 13:48:37 +02:00
|
|
|
days = int(seconds / DAY)
|
|
|
|
return _(f'{days} days ago')
|
2019-07-16 06:49:30 +02:00
|
|
|
if seconds > 10 * 365 * DAY:
|
2019-07-17 12:16:55 +02:00
|
|
|
return _('not yet')
|
2019-07-16 07:28:51 +02:00
|
|
|
|
2019-07-16 13:48:37 +02:00
|
|
|
return _('a long time ago')
|
2019-07-15 07:39:19 +02:00
|
|
|
|
2019-07-10 01:51:41 +02:00
|
|
|
|
2019-07-17 13:10:36 +02:00
|
|
|
@Gtk.Template(resource_path="/org/freedesktop/Tuhi/ui/DrawingPerspective.ui")
|
2019-07-10 01:51:41 +02:00
|
|
|
class DrawingPerspective(Gtk.Stack):
|
|
|
|
__gtype_name__ = "DrawingPerspective"
|
|
|
|
|
|
|
|
flowbox_drawings = Gtk.Template.Child()
|
2019-07-16 06:40:37 +02:00
|
|
|
overlay_undo = Gtk.Template.Child()
|
|
|
|
notification_delete_undo = Gtk.Template.Child()
|
|
|
|
notification_delete_close = 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-10 01:51:41 +02:00
|
|
|
|
2019-07-16 05:52:54 +02:00
|
|
|
def _cache_drawings(self, device, pspec):
|
|
|
|
# The config backend filters duplicates anyway, so don't care here
|
|
|
|
for ts in self.device.drawings_available:
|
|
|
|
json_string = self.device.json(ts)
|
2019-07-16 13:09:02 +02:00
|
|
|
Config.instance().add_drawing(ts, json_string)
|
2019-07-16 05:52:54 +02:00
|
|
|
|
|
|
|
def _update_drawings(self, config, pspec):
|
|
|
|
for js in config.drawings:
|
|
|
|
if js in self.known_drawings:
|
2019-07-11 08:05:41 +02:00
|
|
|
continue
|
|
|
|
|
2019-07-16 05:52:54 +02:00
|
|
|
self.known_drawings.append(js)
|
|
|
|
|
2019-07-16 13:06:32 +02:00
|
|
|
drawing = Drawing(js)
|
2019-07-16 05:52:54 +02:00
|
|
|
|
|
|
|
# We don't know which order we get drawings from the device, so
|
|
|
|
# let's do a sorted insert here
|
|
|
|
index = 0
|
|
|
|
child = self.flowbox_drawings.get_child_at_index(index)
|
|
|
|
while child is not None:
|
|
|
|
if child.get_child().timestamp < drawing.timestamp:
|
|
|
|
break
|
|
|
|
index += 1
|
|
|
|
child = self.flowbox_drawings.get_child_at_index(index)
|
|
|
|
|
|
|
|
self.flowbox_drawings.insert(drawing, index)
|
2019-07-10 01:51:41 +02:00
|
|
|
|
2019-07-16 06:40:37 +02:00
|
|
|
# Remove deleted ones
|
|
|
|
deleted = [d for d in self.known_drawings if d not in config.drawings]
|
|
|
|
for d in deleted:
|
2019-07-16 12:54:01 +02:00
|
|
|
def delete_matching_child(child, drawing):
|
|
|
|
if child.get_child().timestamp == drawing['timestamp']:
|
2019-07-16 06:40:37 +02:00
|
|
|
self.flowbox_drawings.remove(child)
|
2019-07-16 12:54:01 +02:00
|
|
|
self.known_drawings.remove(drawing)
|
|
|
|
self.notification_delete_undo.deleted_drawing = drawing['timestamp']
|
2019-07-16 06:40:37 +02:00
|
|
|
self.overlay_undo.set_reveal_child(True)
|
2019-07-16 12:54:01 +02:00
|
|
|
self.flowbox_drawings.foreach(delete_matching_child, d)
|
2019-07-16 06:40:37 +02:00
|
|
|
|
2019-07-10 01:51:41 +02:00
|
|
|
@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-16 05:52:54 +02:00
|
|
|
|
|
|
|
# This is a bit convoluted. We need to cache all drawings
|
|
|
|
# because Tuhi doesn't have guaranteed storage. So any json that
|
|
|
|
# comes in from Tuhi, we pass to our config backend to save
|
|
|
|
# somewhere.
|
|
|
|
# The config backend adds the json file and emits a notify for the
|
|
|
|
# json itself (once cached) that we then actually use for SVG
|
|
|
|
# generation.
|
|
|
|
device.connect('notify::drawings-available', self._cache_drawings)
|
2019-07-16 13:09:02 +02:00
|
|
|
Config.instance().connect('notify::drawings', self._update_drawings)
|
2019-07-11 08:05:41 +02:00
|
|
|
|
2019-07-16 13:09:02 +02:00
|
|
|
self._update_drawings(Config.instance(), None)
|
2019-07-16 02:28:55 +02:00
|
|
|
|
|
|
|
# We always want to sync on startup
|
|
|
|
logger.debug(f'{device.name} - starting to listen')
|
|
|
|
device.start_listening()
|
|
|
|
|
|
|
|
@GObject.Property
|
|
|
|
def name(self):
|
|
|
|
return "drawing_perspective"
|
|
|
|
|
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()
|
2019-07-16 06:40:37 +02:00
|
|
|
|
|
|
|
@Gtk.Template.Callback('_on_undo_close_clicked')
|
|
|
|
def _on_undo_close_clicked(self, button):
|
|
|
|
self.overlay_undo.set_reveal_child(False)
|
|
|
|
|
|
|
|
@Gtk.Template.Callback('_on_undo_clicked')
|
|
|
|
def _on_undo_clicked(self, button):
|
2019-07-16 13:09:02 +02:00
|
|
|
Config.instance().undelete_drawing(button.deleted_drawing)
|
2019-07-16 06:40:37 +02:00
|
|
|
self.overlay_undo.set_reveal_child(False)
|