Commit 57924ca8 authored by Daniel Brötzmann's avatar Daniel Brötzmann
Browse files

feat: Preview: Make download cancellable

parent bedd7dc2
......@@ -80,6 +80,8 @@ def __init__(self,
self.file_size: int = 0
self._received_size: int = 0
self._soup_message: Optional[Soup.Message] = None
self.key: Optional[bytes] = None
self.iv: Optional[bytes] = None
if self.is_aes_encrypted and urlparts is not None:
......@@ -114,6 +116,10 @@ def context(self) -> Optional[str]:
def filename(self) -> str:
return self._filename
@property
def soup_message(self) -> Soup.Message:
return self._soup_message
@property
def request_uri(self) -> Optional[str]:
if self._urlparts is None:
......@@ -150,7 +156,8 @@ def create_thumbnail(self, data: bytes) -> bool:
def update_widget(self, data: Optional[GdkPixbufType] = None) -> None:
self._widget.update(self, data)
def update_progress(self, size: int) -> None:
def update_progress(self, size: int, message: Soup.Message) -> None:
self._soup_message = message
self._received_size += size
if self.file_size == 0 or self._received_size == 0:
return
......@@ -420,11 +427,11 @@ def _on_content_sniffed(self,
preview.update_widget()
def _on_got_chunk(self,
_message: Soup.Message,
message: Soup.Message,
chunk: Soup.Buffer,
preview: Preview
) -> None:
preview.update_progress(len(chunk.get_data()))
preview.update_progress(len(chunk.get_data()), message)
def _on_finished(self,
_session: Soup.Session,
......@@ -507,3 +514,7 @@ def _on_thumb_write_finished(_result: bool,
return
preview.update_widget(data=pixbuf)
def cancel_download(self, preview: Preview) -> None:
session = self._get_session(preview.account)
session.cancel_message(preview.soup_message, Soup.Status.CANCELLED)
......@@ -72,10 +72,43 @@
</packing>
</child>
<child>
<object class="GtkProgressBar" id="progressbar">
<object class="GtkBox" id="progress_box">
<property name="can-focus">False</property>
<property name="no-show-all">True</property>
<property name="show-text">True</property>
<property name="spacing">12</property>
<child>
<object class="GtkProgressBar" id="progressbar">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="show-text">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="tooltip-text" translatable="yes">Cancel</property>
<signal name="clicked" handler="_on_cancel_download_clicked" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="icon-name">process-stop-symbolic</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
......
......@@ -718,6 +718,7 @@ class PreviewBuilder(Builder):
right_box: Gtk.Box
image_event_box: Gtk.EventBox
image_button: Gtk.Button
progress_box: Gtk.Box
progressbar: Gtk.ProgressBar
button_box: Gtk.Box
download_button: Gtk.Button
......
......@@ -67,12 +67,12 @@ def get_text(self) -> str:
return self._preview.uri
def update_progress(self, _preview: Preview, progress: float) -> None:
self._ui.progressbar.show()
self._ui.progress_box.show()
self._ui.progressbar.set_fraction(progress)
def update(self, preview: Preview, data: Optional[GdkPixbufType]) -> None:
self._preview = preview
self._ui.progressbar.hide()
self._ui.progress_box.hide()
if preview.is_geo_uri:
data = load_icon_pixbuf('map', size=preview.size)
......@@ -274,6 +274,10 @@ def _on_button_press_event(self,
menu = self._get_context_menu()
menu.popup_at_pointer(event)
def _on_cancel_download_clicked(self, _button: Gtk.Button) -> None:
assert self._preview is not None
app.interface.preview_manager.cancel_download(self._preview)
@staticmethod
def _on_realize(event_box: Gtk.EventBox) -> None:
window = event_box.get_window()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment