changeset 274:f6a756c19bfb

anime_list.cc: use mutexes so we don't sex the stack strings.cc: use Qt to convert from HTML to plain text.
author Paper <paper@paper.us.eu.org>
date Thu, 18 Apr 2024 17:24:42 -0400
parents f31305b9f60a
children 22f9aacf6ac1 657fda1b9cac
files include/gui/pages/anime_list.h src/core/strings.cc src/gui/pages/anime_list.cc
diffstat 3 files changed, 31 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/include/gui/pages/anime_list.h	Thu Apr 18 16:53:17 2024 -0400
+++ b/include/gui/pages/anime_list.h	Thu Apr 18 17:24:42 2024 -0400
@@ -25,6 +25,7 @@
 
 private:
 	AnimeListPage* page_ = nullptr;
+	std::mutex _queue_mutex;
 	std::queue<int> queue_;
 };
 
--- a/src/core/strings.cc	Thu Apr 18 16:53:17 2024 -0400
+++ b/src/core/strings.cc	Thu Apr 18 17:24:42 2024 -0400
@@ -8,6 +8,7 @@
 #include <QDebug>
 #include <QLocale>
 #include <QString>
+#include <QTextDocument>
 
 #include <algorithm>
 #include <cctype>
@@ -148,24 +149,20 @@
 
 /* e.g. "&lt;" for "<" */
 void ParseHtmlEntities(std::string& string) {
-	/* The only one of these I can understand using are the first
-	 * three. why do the rest of these exist?
-	 *
-	 * probably mojibake.
-	 */
 	const std::unordered_map<std::string, std::string> map = {
-	    {"&lt;",    "<"   },
-        {"&rt;",    ">"   },
+	    {"&lt;",    "<"},
+        {"&rt;",    ">"},
         {"&nbsp;",  "\xA0"},
-        {"&amp;",   "&"   },
-        {"&quot;",  "\""  },
-	    {"&apos;",  "'"   },
-        {"&cent;",  "¢"  },
-        {"&pound;", "£"  },
-        {"&euro;",  "€" },
-        {"&yen;",   "¥"  },
-	    {"&copy;",  "©"  },
-        {"&reg;",   "®"  },
+        {"&amp;",   "&"},
+        {"&quot;",  "\""},
+	    {"&apos;",  "'"},
+        {"&cent;",  "¢"},
+        {"&pound;", "£"},
+        {"&euro;",  "€"},
+        {"&yen;",   "¥"},
+	    {"&copy;",  "©"},
+        {"&reg;",   "®"},
+        {"&times;", "×"},
         {"&rsquo;", "’" }  // Haibane Renmei, AniList
 	};
 
@@ -175,9 +172,17 @@
 
 /* removes stupid HTML stuff */
 void TextifySynopsis(std::string& string) {
+#if 1
+	/* Just let Qt deal with it. */
+	QTextDocument text;
+	text.setHtml(Strings::ToQString(string));
+	string = Strings::ToUtf8String(text.toPlainText());
+#else
+	/* old implementation here... beware of jankiness */
 	SanitizeLineEndings(string);
 	RemoveHtmlTags(string);
 	ParseHtmlEntities(string);
+#endif
 }
 
 /* let Qt handle the heavy lifting of locale shit
@@ -273,7 +278,7 @@
 std::string BytesToHumanReadableSize(uint64_t bytes, int precision) {
 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
 	/* QLocale in Qt >= 5.10.0 has a function for this */
-	return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes), precision);
+	return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes, precision));
 #else
 	static const std::unordered_map<uint64_t, std::string> map = {
 		{1ull << 10, "KiB"},
--- a/src/gui/pages/anime_list.cc	Thu Apr 18 16:53:17 2024 -0400
+++ b/src/gui/pages/anime_list.cc	Thu Apr 18 17:24:42 2024 -0400
@@ -38,17 +38,18 @@
 }
 
 void AnimeListPageUpdateEntryThread::AddToQueue(int id) {
-	if (isRunning())
-		return; /* don't let us fuck ourselves */
-
+	const std::lock_guard<std::mutex> guard(_queue_mutex);
 	queue_.push(id);
 }
 
 /* processes the queue... */
 void AnimeListPageUpdateEntryThread::run() {
-	while (!queue_.empty() && !isInterruptionRequested()) {
-		Services::UpdateAnimeEntry(queue_.front());
-		queue_.pop();
+	{
+		const std::lock_guard<std::mutex> guard(_queue_mutex);
+		while (!queue_.empty() && !isInterruptionRequested()) {
+			Services::UpdateAnimeEntry(queue_.front());
+			queue_.pop();
+		}
 	}
 	page_->Refresh();
 }
@@ -241,10 +242,8 @@
 
 void AnimeListPage::UpdateAnime(int id) {
 	/* this ought to just add to the thread's buffer. */
-	if (update_entry_thread_.isRunning()) {
+	if (update_entry_thread_.isRunning())
 		update_entry_thread_.requestInterruption();
-		update_entry_thread_.wait();
-	}
 
 	update_entry_thread_.AddToQueue(id);
 	update_entry_thread_.start();