diff src/gui/widgets/anime_info.cc @ 320:1b5c04268d6a

services/kitsu: ACTUALLY finish GetAnimeList there are some things the API just... doesn't provide. therefore we have to request the genres separately any time a new anime info box is opened...
author Paper <paper@paper.us.eu.org>
date Wed, 12 Jun 2024 19:49:19 -0400
parents b1f625b0227c
children 5d3c9b31aa6e
line wrap: on
line diff
--- a/src/gui/widgets/anime_info.cc	Wed Jun 12 17:52:26 2024 -0400
+++ b/src/gui/widgets/anime_info.cc	Wed Jun 12 19:49:19 2024 -0400
@@ -1,21 +1,61 @@
 #include "gui/widgets/anime_info.h"
 #include "core/anime.h"
+#include "core/anime_db.h"
 #include "core/strings.h"
 #include "gui/translate/anime.h"
 #include "gui/widgets/text.h"
+#include "services/services.h"
 #include <QHBoxLayout>
 #include <QTextStream>
 
+AnimeInfoWidgetGetMetadataThread::AnimeInfoWidgetGetMetadataThread(QObject* parent) : QThread(parent) {}
+
+void AnimeInfoWidgetGetMetadataThread::AddToQueue(int id) {
+	const std::lock_guard<std::mutex> guard(queue_mutex_);
+	queue_.push(id);
+}
+
+/* processes the queue... */
+void AnimeInfoWidgetGetMetadataThread::run() {
+	queue_mutex_.lock();
+	while (!queue_.empty() && !isInterruptionRequested()) {
+		int id = queue_.front();
+
+		queue_mutex_.unlock();
+
+		if (Services::RetrieveAnimeMetadata(id))
+			emit NeedRefresh(id);
+
+		queue_mutex_.lock();
+
+		queue_.pop();
+	}
+	queue_mutex_.unlock();
+}
+
+/* all widgets share this thread */
+static AnimeInfoWidgetGetMetadataThread get_metadata_thread;
+
 AnimeInfoWidget::AnimeInfoWidget(QWidget* parent)
 	: QWidget(parent)
 	, _title(tr("Alternative titles"), "")
-	, _details(tr("Details"), tr("Type:\nEpisodes:\nStatus:\nSeason:\nGenres:\nScore:"), "")
+	, _details(tr("Details"), tr("Type:\nEpisodes:\nStatus:\nSeason:\nGenres:\nProducers:\nScore:"), "")
 	, _synopsis(tr("Synopsis"), "") {
 	QVBoxLayout* layout = new QVBoxLayout(this);
 
 	layout->addWidget(&_title);
 	layout->addWidget(&_details);
 	layout->addWidget(&_synopsis);
+
+	/* ... */
+	connect(&get_metadata_thread, &AnimeInfoWidgetGetMetadataThread::NeedRefresh, this, [this](int id) {
+		setUpdatesEnabled(false);
+
+		if (id == id_)
+			RefreshGenres(Anime::db.items[id]);
+
+		setUpdatesEnabled(true);
+	});
 }
 
 AnimeInfoWidget::AnimeInfoWidget(const Anime::Anime& anime, QWidget* parent) : AnimeInfoWidget(parent) {
@@ -23,27 +63,45 @@
 }
 
 void AnimeInfoWidget::SetAnime(const Anime::Anime& anime) {
+	setUpdatesEnabled(false);
+
+	id_ = anime.GetId();
+
+	get_metadata_thread.AddToQueue(id_);
+	if (!get_metadata_thread.isRunning())
+		get_metadata_thread.start();
+
 	/* alt titles */
 	_title.GetLine()->SetText(Strings::ToQString(Strings::Implode(anime.GetTitleSynonyms(), ", ")));
 
+	RefreshGenres(anime);
+
+	_synopsis.GetParagraph()->SetText(Strings::ToQString(anime.GetSynopsis()));
+
+	setUpdatesEnabled(true);
+
+	updateGeometry();
+}
+
+void AnimeInfoWidget::RefreshGenres(const Anime::Anime& anime) {
 	/* details */
 	QString details_data;
 	QTextStream details_data_s(&details_data);
 
 	/* we have to convert ALL of these strings to
 	 * QString because QTextStream sucks and assumes
-	 * Latin1 (on Windows?) */
+	 * Latin-1 (on Windows?) */
 	const auto genres = anime.GetGenres();
+	const auto producers = anime.GetProducers();
+
 	details_data_s << Strings::ToQString(Translate::ToLocalString(anime.GetFormat())) << "\n"
 	               << anime.GetEpisodes() << "\n"
 	               << Strings::ToQString(Translate::ToLocalString(anime.GetAiringStatus())) << "\n"
 	               << Strings::ToQString(Translate::ToLocalString(anime.GetSeason())) << " "
 	               << anime.GetAirDate().GetYear().value_or(2000) << "\n"
-	               << Strings::ToQString((genres.size() > 1) ? Strings::Implode(genres, ", ") : "-") << "\n"
+	               << Strings::ToQString((genres.size() > 1)    ? Strings::Implode(genres, ", ")    : "-") << "\n"
+	               << Strings::ToQString((producers.size() > 1) ? Strings::Implode(producers, ", ") : "-") << "\n"
 	               << anime.GetAudienceScore() << "%";
-	_details.GetData()->setText(details_data);
 
-	_synopsis.GetParagraph()->SetText(Strings::ToQString(anime.GetSynopsis()));
-
-	updateGeometry();
+	_details.GetData()->setText(details_data);
 }