Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- **Cancel or Back during an NZBGet download asks first.** A menu offers
three choices: stop it and remove it and its files from NZBGet (what a press
used to do), go back but keep it downloading in NZBGet, or keep watching
the download
([Cancel and Back](https://appz4fun.github.io/NeNeTeePee-Stream-Kodi/features/nzbget-backend/#cancel-and-back)).
- **Read a release's NFO and MediaInfo from the picker.** Left or Right on a
release opens a viewer over the list
([NFO and MediaInfo viewer](https://appz4fun.github.io/NeNeTeePee-Stream-Kodi/features/nfo-viewer/)):
Expand Down
27 changes: 23 additions & 4 deletions docs-site/features/nzbget-backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ shared duplicate keys and descending scores. Only this legacy path can use the
existing one-shot FORCE rescue. Authentication, timeout, or other errors do not
trigger individual appends because a bulk submission may already have succeeded.

If you cancel the play, NeNeTeePee-Stream-Kodi stops what's downloading: it
If you cancel the play and choose to stop the download (see
[Cancel and Back](#cancel-and-back)), NeNeTeePee-Stream-Kodi stops what's downloading: it
removes your pick and any backup NZBGet promoted or queued. The backups NZBGet
parked in its history (status `dupe`) stay there: they don't download, and if
you play the same release again within a day they're reused instead of
Expand Down Expand Up @@ -433,9 +434,27 @@ One of those is still used if its upload's first listing fails: the add-on
then asks the next indexer that lists the same upload.

The download dialog is the add-on's own window. It has no progress bar, since
the first line carries the percent, and room for eight lines. Cancel, Back
and Escape stop the play. If the window cannot be opened, Kodi's own progress
dialog is used and shows the first four lines.
the first line carries the percent, and room for eight lines. If the window
cannot be opened, Kodi's own progress dialog is used and shows the first four
lines.

#### Cancel and Back

Cancel, Back, Escape and Stop don't stop the download straight away. They open
a menu that asks what you meant:

![The cancel menu over the download dialog](../images/download-cancel-menu.png)

| Choice | What happens |
| --- | --- |
| **Yes, stop it and remove it and its files from NZBGet** | The play stops and its downloads are removed from NZBGet, as described under [Smart Duplicates failover](#smart-duplicates-failover). |
| **Yes, go back but keep it downloading in NZBGet** | You go back to where you were. Nothing is removed: the download keeps running in NZBGet, and a toast says "Left downloading in NZBGet". Play the release again once it has finished and it plays from the completed folder. |
| **No, keep watching the download** | The menu closes and the download dialog carries on. Back on the menu itself does the same. |

If you go back while the copies are still being sent, the add-on finishes
sending them first, then leaves without removing anything.

![Back, keep watching, then Back again and leave it downloading](../images/download-cancel-menu.gif)

Once NZBGet has the copies, the dialog shows these lines:

Expand Down
Binary file added docs-site/images/download-cancel-menu.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs-site/images/download-cancel-menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2912,3 +2912,27 @@ msgstr ""
msgctxt "#30788"
msgid "{} posts that are not this release"
msgstr ""

msgctxt "#30789"
msgid "Stop this download?"
msgstr ""

msgctxt "#30790"
msgid "Yes, stop it and remove it and its files from NZBGet"
msgstr ""

msgctxt "#30791"
msgid "Yes, go back but keep it downloading in NZBGet"
msgstr ""

msgctxt "#30792"
msgid "No, keep watching the download"
msgstr ""

msgctxt "#30793"
msgid "Left downloading in NZBGet"
msgstr ""

msgctxt "#30794"
msgid "Going back. The download keeps running in NZBGet."
msgstr ""
6 changes: 6 additions & 0 deletions repo/plugin.video.nzbdav/resources/lib/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@
30693: "Repair needs {} parity blocks",
30694: "Cancel",
30695: "Cancelling...",
30789: "Stop this download?",
30790: "Yes, stop it and remove it and its files from NZBGet",
30791: "Yes, go back but keep it downloading in NZBGet",
30792: "No, keep watching the download",
30793: "Left downloading in NZBGet",
30794: "Going back. The download keeps running in NZBGet.",
30696: "not downloading 1 duplicate",
30697: "not downloading {} duplicates",
30700: "{} copies ({} listings on {} indexers)",
Expand Down
188 changes: 171 additions & 17 deletions repo/plugin.video.nzbdav/resources/lib/nzbget_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,29 @@
never stops a download.

Cancel is read from a Home window property the window's Cancel button sets
(on a click and on Back). That is a plain read, so it is seen at once from
any thread; a script callback would only arrive when the polling thread next
waits. ``onAction`` and ``onClick`` set the same flag for the keys the
property does not cover.
(on a click and on Back; the button always has the focus). That is a plain
read, so it is seen at once from any thread; a script callback would only
arrive when the polling thread next waits. ``onAction`` covers only the keys
the property does not: Escape (previous menu) and Stop. Back and a click also
reach ``onAction`` / ``onClick``, late; taking those too would count one press
twice and ask again after the menu was answered.

A press does not stop the download by itself: ``iscanceled`` first asks what
the press meant (``ask_cancel_choice``). Stop it and remove it from NZBGet,
go back and leave it downloading (``detached`` turns True, ``iscanceled``
stays False, and the resolve leaves without deleting anything), or carry on
watching (the press is forgotten). Only the thread that opened the dialog
asks; any other thread that polls it sees no cancel until the answer is in.
"""

import os
import threading

import xbmc
import xbmcgui

from resources.lib.i18n import string as _string

WINDOW_XML = "download-dialog.xml"
# How many lines the window has room for (``nzbget_progress`` asks).
MAX_LINES = 8
Expand All @@ -33,9 +45,80 @@
_HOME_WINDOW = 10000
_ACTION_PREVIOUS_MENU = 10
_ACTION_STOP = 13
_ACTION_NAV_BACK = 92
_CANCEL_ACTIONS = frozenset((_ACTION_PREVIOUS_MENU, _ACTION_STOP, _ACTION_NAV_BACK))
# Back (92) and a click on Cancel are seen through CANCEL_PROPERTY.
_CANCEL_ACTIONS = frozenset((_ACTION_PREVIOUS_MENU, _ACTION_STOP))
_LINE_BREAK = "[CR]"
# The cancel menu's rows, in the order it lists them.
CHOICE_STOP = 0
CHOICE_DETACH = 1
CHOICE_RESUME = 2
_STOPPED = "stop"
_DETACHED = "detach"


def ask_cancel_choice():
"""Ask what a Cancel or Back press meant; one of the ``CHOICE_*`` values.

Back on the menu itself (Kodi's ``-1``) is "no, carry on". A menu Kodi
refuses to show, or an answer that is not one of its rows, stops the
download as the press used to, so a cancel always stays possible.
"""
try:
choice = xbmcgui.Dialog().select(
_string(30789), [_string(30790), _string(30791), _string(30792)]
)
except Exception as exc: # pylint: disable=broad-except
xbmc.log(
"NeNeTeePee-Stream-Kodi: cancel menu unavailable, stopping the "
"download: {}".format(exc),
xbmc.LOGWARNING,
)
return CHOICE_STOP
if choice == -1:
return CHOICE_RESUME
if choice in (CHOICE_STOP, CHOICE_DETACH, CHOICE_RESUME):
return choice
return CHOICE_STOP


class _CancelGate:
"""Turns a raw Cancel/Back press into the answer from the cancel menu."""

def __init__(self):
self._lock = threading.Lock()
self._owner = threading.get_ident()
self.state = None

def claim(self):
"""The calling thread is the one that asks (the one showing the dialog)."""
self._owner = threading.get_ident()
self.state = None

def check(self, pressed, forget_press, on_detach=None):
"""True when the download is to stop; asks once per press."""
if self.state == _STOPPED:
return True
if self.state == _DETACHED or not pressed():
return False
if threading.get_ident() != self._owner:
return False
if not self._lock.acquire(False): # pylint: disable=consider-using-with
return False
try:
if self.state is not None or not pressed():
return self.state == _STOPPED
choice = ask_cancel_choice()
if choice == CHOICE_STOP:
self.state = _STOPPED
return True
forget_press()
if choice == CHOICE_DETACH:
self.state = _DETACHED
if on_detach is not None:
on_detach()
return False
finally:
self._lock.release()


def split_message(message):
Expand Down Expand Up @@ -64,11 +147,18 @@ def __init__(self, *args, **kwargs):
super().__init__(*args)
self._canceled = False
self._shown = False
self._gate = _CancelGate()

@property
def detached(self):
"""True once the user chose to go back and leave it downloading."""
return self._gate.state == _DETACHED

def create(self, heading, message=""):
"""Show the window with ``heading`` and the first ``message``."""
_home().clearProperty(CANCEL_PROPERTY)
self._canceled = False
self._gate.claim()
self.setProperty("heading", str(heading or ""))
self._set_message(message)
self.show()
Expand All @@ -86,13 +176,21 @@ def _set_message(self, message):
self.setProperty("body", body)

def iscanceled(self):
"""True once Cancel or Back was pressed."""
if self._canceled:
return True
if _home().getProperty(CANCEL_PROPERTY) == "1":
"""True once Cancel or Back was pressed and the menu said stop it."""
return self._gate.check(self._pressed, self._forget_press, self._on_detach)

def _pressed(self):
if not self._canceled and _home().getProperty(CANCEL_PROPERTY) == "1":
self._canceled = True
return self._canceled

def _forget_press(self):
self._canceled = False
_home().clearProperty(CANCEL_PROPERTY)

def _on_detach(self):
self._set_message(_string(30794))

def close(self):
"""Close the window and forget the cancel mark."""
try:
Expand All @@ -103,13 +201,13 @@ def close(self):
_home().clearProperty(CANCEL_PROPERTY)

def onAction(self, action): # pylint: disable=invalid-name
"""Back, Escape and Stop cancel; nothing else closes the window."""
if action.getId() in _CANCEL_ACTIONS:
self._canceled = True
"""Escape and Stop ask to cancel; nothing else closes the window.

def onClick(self, control_id): # pylint: disable=invalid-name
"""A click on Cancel."""
if control_id == CANCEL_BUTTON_ID:
Back is not taken here: the Cancel button's ``<onback>`` already set
the property for it, and this callback can arrive after the menu
for that press was answered.
"""
if action.getId() in _CANCEL_ACTIONS:
self._canceled = True


Expand Down Expand Up @@ -139,6 +237,62 @@ def open_dialog(heading, message=""):
"Kodi's progress dialog: {}".format(exc),
xbmc.LOGWARNING,
)
dialog = xbmcgui.DialogProgress()
dialog = KodiProgressDialog()
dialog.create(heading, message)
return dialog


class KodiProgressDialog:
"""Kodi's progress dialog, with the same cancel menu as the own window.

Kodi's dialog cannot forget a Cancel press, so "carry on" closes it and
opens a fresh one with the same text.
"""

def __init__(self):
self._dialog = xbmcgui.DialogProgress()
self._gate = _CancelGate()
self._text = ("", "", 0)

@property
def detached(self):
"""True once the user chose to go back and leave it downloading."""
return self._gate.state == _DETACHED

def create(self, heading, message=""):
"""Open Kodi's dialog with ``heading`` and ``message``."""
self._gate.claim()
self._text = (heading, message, 0)
self._dialog.create(heading, message)

def update(self, percent, message=None):
"""Move the bar and, when given, replace the text."""
heading, last, _percent = self._text
self._text = (heading, last if message is None else message, percent)
if message is None:
self._dialog.update(percent)
else:
self._dialog.update(percent, message)

def iscanceled(self):
"""True once Cancel was pressed and the menu said stop it."""
return self._gate.check(
lambda: self._dialog.iscanceled() is True, self._reopen, self._on_detach
)

def _reopen(self):
heading, message, percent = self._text
try:
self._dialog.close()
except Exception: # pylint: disable=broad-except
pass
self._dialog = xbmcgui.DialogProgress()
self._dialog.create(heading, message)
self._dialog.update(percent, message)

def _on_detach(self):
self.update(self._text[2], _string(30794))

def close(self):
"""Close Kodi's dialog."""
self._dialog.close()
Loading