From 96288cb00f11b2334c4d876a7ec250dc0a78834b Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Fri, 29 Nov 2024 06:58:26 +0100
Subject: [PATCH] To /files view, add "play" button for immediate playing of
 (present) files.

---
 src/templates/files.tmpl |  5 +++--
 src/ytplom/misc.py       | 35 ++++++++++++++++++++++++++++-------
 2 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/src/templates/files.tmpl b/src/templates/files.tmpl
index 0dda3db..a44066b 100644
--- a/src/templates/files.tmpl
+++ b/src/templates/files.tmpl
@@ -9,13 +9,14 @@ show absent: <input type="checkbox" name="show_absent" {% if show_absent %}check
 <input type="submit" value="filter" />
 </form>
 <p>known files (shown: {{files|length}}):</p>
+<form action="files" method="POST">
 <table>
-<tr><th>present</th><th>path</th></td>
 {% for file in files %}
 <tr>
-<td><input type="checkbox"{% if file.present %} checked{% endif %} disabled/></td>
+<td><input type="submit" name="play_{{file.rel_path_b64}}" value="play" {% if not file.present %}disabled {% endif %}/></td>
 <td><a href="/{{page_names.file}}/{{file.rel_path_b64}}">{{file.rel_path}}</a></td>
 </tr>
 {% endfor %}
 </table>
+</form>
 {% endblock %}
diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py
index 96cabd6..2bdb0c3 100644
--- a/src/ytplom/misc.py
+++ b/src/ytplom/misc.py
@@ -529,7 +529,7 @@ class Player:
             """
             self._kill_queue.put(True)
 
-        self._mpv.command('playlist-play-index', self._idx)
+        self._play_at_index()
 
     def _kill_mpv(self) -> None:
         if self._mpv:
@@ -537,6 +537,10 @@ class Player:
             self._mpv = None
         self._signal_update()
 
+    def _play_at_index(self):
+        if self._mpv:
+            self._mpv.command('playlist-play-index', self._idx)
+
     @property
     def empty(self) -> bool:
         """Return if playlist empty."""
@@ -593,21 +597,18 @@ class Player:
         """Move player to previous item in playlist."""
         if self._idx > 0:
             self._idx -= 1
-        if self._mpv:
-            self._mpv.command('playlist-play-index', self._idx)
+        self._play_at_index()
 
     def next(self) -> None:
         """Move player to next item in playlist."""
         if self._idx < len(self._files) - 1:
             self._idx += 1
-        if self._mpv:
-            self._mpv.command('playlist-play-index', self._idx)
+        self._play_at_index()
 
     def jump_to(self, target_idx: int) -> None:
         """Move player to target_idx position in playlist."""
         self._idx = target_idx
-        if self._mpv:
-            self._mpv.command('playlist-play-index', self._idx)
+        self._play_at_index()
 
     def move_entry(self, start_idx: int, upwards=True) -> None:
         """Move playlist entry at start_idx up or down one step."""
@@ -633,6 +634,16 @@ class Player:
         self._start_mpv()
         self._signal_update()
 
+    def inject_and_play(self, file: VideoFile) -> None:
+        """Inject file after current title, then jump to it."""
+        if self._files:
+            self._idx += 1
+        self._files.insert(self._idx, file)
+        if self._mpv:
+            self._mpv.command('loadfile', file.full_path,
+                              'insert-at', self._idx)
+        self._play_at_index()
+
 
 class DownloadsManager:
     """Manages downloading and downloads access."""
@@ -744,6 +755,8 @@ class TaskHandler(BaseHTTPRequestHandler):
         postvars = parse_qs(self.rfile.read(body_length).decode())
         if PAGE_NAMES['playlist'] == page_name:
             self._receive_player_command(list(postvars.keys())[0])
+        if PAGE_NAMES['files'] == page_name:
+            self._receive_files_command(list(postvars.keys())[0])
         elif PAGE_NAMES['file'] == page_name:
             self._receive_video_flag(B64Str(toks_url[2]),
                                      [FlagName(k) for k in postvars])
@@ -770,6 +783,14 @@ class TaskHandler(BaseHTTPRequestHandler):
         sleep(0.5)  # avoid redir happening before current_file update
         self._redirect(Path('/'))
 
+    def _receive_files_command(self, command: str) -> None:
+        if command.startswith('play_'):
+            conn = DbConnection()
+            file = VideoFile.get_by_b64(conn, B64Str(command.split('_', 1)[1]))
+            conn.commit_close()
+            self.server.player.inject_and_play(file)
+        self._redirect(Path('/'))
+
     def _receive_video_flag(self,
                             rel_path_b64: B64Str,
                             flag_names: list[FlagName]
-- 
2.30.2