changeset 383:27c462bc7815

make the "Now Playing" page actually work
author Paper <paper@tflc.us>
date Thu, 06 Nov 2025 07:10:58 -0500
parents 0265e125f680
children 0393debc0ed2
files include/core/session.h include/gui/pages/now_playing.h src/core/anime.cc src/core/anime_db.cc src/core/session.cc src/gui/pages/now_playing.cc src/gui/window.cc src/library/library.cc
diffstat 8 files changed, 53 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/include/core/session.h	Thu Nov 06 03:16:55 2025 -0500
+++ b/include/core/session.h	Thu Nov 06 07:10:58 2025 -0500
@@ -30,7 +30,7 @@
 	/* we literally *cannot* be lying to the user by doing this */
 	void IncrementRequests();
 	std::uint32_t GetRequests();
-	int uptime();
+	std::int64_t uptime();
 
 	Config config;
 
--- a/include/gui/pages/now_playing.h	Thu Nov 06 03:16:55 2025 -0500
+++ b/include/gui/pages/now_playing.h	Thu Nov 06 07:10:58 2025 -0500
@@ -12,6 +12,7 @@
 
 #include <memory>
 #include <unordered_map>
+#include <cstdint>
 
 
 namespace Anime {
@@ -82,7 +83,9 @@
 	};
 
 	QStackedWidget stack_;
-	QTimer timer_;
+	/* Time the episode started playing (roughly)
+	 * This is -1 if there is no episode playing */
+	std::int64_t timer_ = -1;
 
 	NowPlayingPages::Default default_;
 	NowPlayingPages::Playing playing_;
--- a/src/core/anime.cc	Thu Nov 06 03:16:55 2025 -0500
+++ b/src/core/anime.cc	Thu Nov 06 07:10:58 2025 -0500
@@ -16,6 +16,21 @@
 #include <iostream>
 
 namespace Anime {
+/* FIXME need to parse relations data as well.
+ *
+ * For example
+ *   A Certain Magical Index - 2x13.mkv
+ * How do we know which anime is the second season of
+ * "A Certain Magical Index"? We can't possibly know
+ * without relations data!
+ *
+ * A way to solve this would be to store prequel/sequel.
+ * This is sorta-kinda anime speak for seasons. We can
+ * then find when the prequels stop. HOWEVER, this isn't
+ * exactly fool proof, because there are shows with a
+ * second "season" that is actually a prequel, and may
+ * show up as one in relations data. So it's not as
+ * straightforward as one may think. */
 
 /* User list data */
 bool Anime::IsInUserList() const
--- a/src/core/anime_db.cc	Thu Nov 06 03:16:55 2025 -0500
+++ b/src/core/anime_db.cc	Thu Nov 06 07:10:58 2025 -0500
@@ -382,7 +382,8 @@
 
 QImage Database::GetAnimePoster(int id)
 {
-	/* Creates an anime poster and then saves it to disk */
+	/* Creates an anime poster and then saves it to disk
+	 * Sorry this is kind of spaghetti... */
 	QImage res;
 	QString posterfile;
 
--- a/src/core/session.cc	Thu Nov 06 03:16:55 2025 -0500
+++ b/src/core/session.cc	Thu Nov 06 07:10:58 2025 -0500
@@ -40,7 +40,7 @@
 	return requests_;
 };
 
-int Session::uptime()
+std::int64_t Session::uptime()
 {
 	return timer_.elapsed();
 }
--- a/src/gui/pages/now_playing.cc	Thu Nov 06 03:16:55 2025 -0500
+++ b/src/gui/pages/now_playing.cc	Thu Nov 06 07:10:58 2025 -0500
@@ -5,6 +5,7 @@
 #include "gui/widgets/poster.h"
 #include "gui/widgets/text.h"
 #include "services/services.h"
+#include "core/session.h"
 
 #include <QHBoxLayout>
 #include <QLabel>
@@ -12,9 +13,14 @@
 #include <QVBoxLayout>
 #include <QWidget>
 
+#include <iostream>
+
 #include "anitomy/anitomy.h"
 
 namespace NowPlayingPages {
+// TODO the "now playing" logic should NOT be located here; this should
+// only be a frontent. THAT logic should at the very least be in the
+// "core" subdirectory
 
 Default::Default(QWidget *parent) : QWidget(parent)
 {
@@ -94,17 +100,27 @@
 	stack_.addWidget(&default_);
 	stack_.addWidget(&playing_);
 	layout->addWidget(&stack_);
-	timer_.setSingleShot(true);
-
-	QObject::connect(&timer_, &QTimer::timeout, this, &NowPlayingPage::TimerDone);
 
 	SetDefault();
 }
 
 void NowPlayingPage::SetDefault()
 {
-	/* stop any timer */
-	timer_.stop();
+	/* 'timer_' is the time elapsed since we were set to the 'playing'
+	 * mode. Otherwise it is -1. */
+	if (timer_ != -1) {
+		std::int64_t elapsed = session.uptime() - timer_;
+
+		/* FIXME hardcoding the interval length is a hack at best
+		 * (18 * 60 * 1000), is 18 minutes. */
+		if (elapsed > (18ll * 60ll * 1000ll)) {
+			/* Episode has finished playing */
+			TimerDone();
+		}
+
+		/* Reset the timer */
+		timer_ = -1;
+	}
 
 	stack_.setCurrentIndex(static_cast<int>(Subpages::Default));
 }
@@ -116,18 +132,11 @@
 
 void NowPlayingPage::SetPlaying(const Anime::Anime &anime, const anitomy::Elements &info)
 {
-	/* ok */
-	timer_.stop();
-
 	playing_.SetPlayingAnime(anime, info);
 	stack_.setCurrentIndex(static_cast<int>(Subpages::Playing));
 	updateGeometry();
 
-	/* now, start the timer
-	 * FIXME hardcoding the interval length is a hack at best
-	 * (12 * 60 * 1000) == 720000 msec, which is 12 minutes. */
-	timer_.setInterval(12 * 60 * 1000);
-	timer_.start();
+	timer_ = session.uptime();
 }
 
 void NowPlayingPage::TimerDone(void)
--- a/src/gui/window.cc	Thu Nov 06 03:16:55 2025 -0500
+++ b/src/gui/window.cc	Thu Nov 06 07:10:58 2025 -0500
@@ -107,9 +107,15 @@
 	sidebar_.SetCurrentItem(static_cast<int>(Pages::ANIME_LIST));
 	setCentralWidget(&main_widget_);
 
+	/* This is disgusting */
 	NowPlayingPage *page = reinterpret_cast<NowPlayingPage *>(stack_.widget(static_cast<int>(Pages::NOW_PLAYING)));
 
 	connect(&playing_thread_, &MainWindowPlayingThread::Done, this, [page](const std::vector<std::string> &files) {
+		if (!files.size()) {
+			page->SetDefault();
+			return;
+		}
+
 		for (const auto &file : files) {
 			anitomy::Anitomy anitomy;
 			anitomy.Parse(file);
@@ -135,6 +141,7 @@
 	});
 
 #ifdef MACOSX
+	/* Anitomy needs specific permissions for window title grabbing */
 	if (!osx::AskForPermissions())
 		return;
 #endif
--- a/src/library/library.cc	Thu Nov 06 03:16:55 2025 -0500
+++ b/src/library/library.cc	Thu Nov 06 07:10:58 2025 -0500
@@ -56,8 +56,6 @@
 	std::string bname = path.filename().u8string();
 	int aid, ep;
 
-	std::cout << path << '\n';
-
 	if (!GetPathAnimeAndEpisode(bname, &aid, &ep))
 		return;
 
@@ -124,7 +122,7 @@
 	Refresh(std::optional<int>(id));
 }
 
-// TODO export to JSON
+// TODO export to JSON on exit
 
 Database db;