From e51f1f2c7962781bf153896e5e2fa87e9273835e Mon Sep 17 00:00:00 2001 From: miruka Date: Fri, 6 Dec 2019 08:44:45 -0400 Subject: [PATCH] Implement upload pause function --- src/python/matrix_client.py | 8 +++++--- src/python/models/items.py | 7 +++---- src/qml/Chat/UploadsBar.qml | 6 ++++-- src/qml/Python.qml | 4 ++++ 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/python/matrix_client.py b/src/python/matrix_client.py index 13831e9c..ee5f0fac 100644 --- a/src/python/matrix_client.py +++ b/src/python/matrix_client.py @@ -216,6 +216,7 @@ class MatrixClient(nio.AsyncClient): try: await self._send_file(item_uuid, room_id, path) except (nio.TransferCancelledError, asyncio.CancelledError): + log.info("Deleting item for cancelled upload %s", item_uuid) del self.models[Upload, room_id][str(item_uuid)] @@ -233,8 +234,8 @@ class MatrixClient(nio.AsyncClient): # This error will be caught again by the try block later below size = 0 - task = asyncio.Task.current_task() - upload_item = Upload(item_uuid, task, path, total_size=size) + monitor = nio.TransferMonitor(size) + upload_item = Upload(item_uuid, monitor, path, total_size=size) self.models[Upload, room_id][str(item_uuid)] = upload_item def on_transfered(transfered: int) -> None: @@ -244,7 +245,8 @@ class MatrixClient(nio.AsyncClient): def on_speed_change(speed: float) -> None: upload_item.speed = speed - monitor = nio.TransferMonitor(size, on_transfered, on_speed_change) + monitor.on_transfered = on_transfered + monitor.on_speed_change = on_speed_change try: url, mime, crypt_dict = await self.upload( diff --git a/src/python/models/items.py b/src/python/models/items.py index 43bb7cba..10452fc2 100644 --- a/src/python/models/items.py +++ b/src/python/models/items.py @@ -1,4 +1,3 @@ -import asyncio import re from dataclasses import dataclass, field from datetime import datetime, timedelta @@ -114,9 +113,9 @@ class UploadStatus(AutoStrEnum): @dataclass class Upload(ModelItem): - uuid: UUID = field() - task: asyncio.Task = field() - filepath: Path = field() + uuid: UUID = field() + monitor: nio.TransferMonitor = field() + filepath: Path = field() total_size: int = 0 uploaded: int = 0 diff --git a/src/qml/Chat/UploadsBar.qml b/src/qml/Chat/UploadsBar.qml index f14e6bcf..350a3c9f 100644 --- a/src/qml/Chat/UploadsBar.qml +++ b/src/qml/Chat/UploadsBar.qml @@ -61,7 +61,7 @@ Rectangle { // immediate visual feedback hideBind.when = true // Python will delete this model item on cancel - py.call(py.getattr(model.task, "cancel")) + py.setattr(model.monitor, "cancel", true) } Layout.preferredWidth: theme.baseElementsHeight @@ -187,7 +187,9 @@ Rectangle { onClicked: { delegate.guiPaused = ! delegate.guiPaused - // py.ev("model.task. TODO + py.setattr( + model.monitor, "pause", model.status !== "Paused", + ) } Layout.preferredWidth: diff --git a/src/qml/Python.qml b/src/qml/Python.qml index aa9a87dd..1433f9ca 100644 --- a/src/qml/Python.qml +++ b/src/qml/Python.qml @@ -10,6 +10,10 @@ Python { property bool startupAnyAccountsSaved: false property var pendingCoroutines: ({}) + function setattr(obj, attr, value, callback=null) { + py.call(py.getattr(obj, "__setattr__"), [attr, value], callback) + } + function callSync(name, args=[]) { return call_sync("APP.backend." + name, args) }