changeset 198:bc1ae1810855

dep/animia: switch from using classes to global functions the old idea was ok, but sort of hackish; this method doesn't use classes at all, and this way (especially important!) we can do wayland stuff AND x11 at the same time, which wasn't really possible without stupid workarounds in the other method
author Paper <mrpapersonic@gmail.com>
date Sun, 24 Dec 2023 02:59:42 -0500
parents c4ca035c565d
children 9f3534f6b8c4
files dep/animia/include/animia/fd.h dep/animia/include/animia/fd/libutil.h dep/animia/include/animia/fd/proc.h dep/animia/include/animia/fd/win32.h dep/animia/include/animia/fd/xnu.h dep/animia/include/animia/win.h dep/animia/include/animia/win/quartz.h dep/animia/include/animia/win/win32.h dep/animia/include/animia/win/x11.h dep/animia/src/animia.cc dep/animia/src/fd.cc dep/animia/src/fd/libutil.cc dep/animia/src/fd/proc.cc dep/animia/src/fd/win32.cc dep/animia/src/fd/xnu.cc dep/animia/src/strategist.cc dep/animia/src/win.cc dep/animia/src/win/quartz.cc dep/animia/src/win/win32.cc dep/animia/src/win/x11.cc include/core/json.h src/core/anime_db.cc src/core/config.cc src/core/date.cc src/gui/pages/anime_list.cc
diffstat 25 files changed, 154 insertions(+), 134 deletions(-) [+]
line wrap: on
line diff
--- a/dep/animia/include/animia/fd.h	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/include/animia/fd.h	Sun Dec 24 02:59:42 2023 -0500
@@ -22,14 +22,8 @@
 
 using open_file_proc_t = std::function<bool(const OpenFile&)>;
 
-class BaseFdTools {
-	public:
-		virtual ~BaseFdTools() {}
-		virtual bool EnumerateOpenProcesses(process_proc_t process_proc) { return false; }
-		virtual bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { return false; }
-};
-
-extern BaseFdTools& fd; // defined in fd.cc
+bool EnumerateOpenProcesses(process_proc_t process_proc);
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc);
 
 } // namespace internal
 
--- a/dep/animia/include/animia/fd/libutil.h	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/include/animia/fd/libutil.h	Sun Dec 24 02:59:42 2023 -0500
@@ -9,11 +9,8 @@
 
 namespace animia::internal::libutil {
 
-class LibutilFdTools final : public BaseFdTools {
-	public:
-		bool EnumerateOpenProcesses(process_proc_t process_proc) override;
-		bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) override;
-};
+bool EnumerateOpenProcesses(process_proc_t process_proc);
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc);
 
 } // namespace animia::internal::libutil
 
--- a/dep/animia/include/animia/fd/proc.h	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/include/animia/fd/proc.h	Sun Dec 24 02:59:42 2023 -0500
@@ -9,11 +9,8 @@
 
 namespace animia::internal::proc {
 
-class ProcFdTools final : public BaseFdTools {
-	public:
-		bool EnumerateOpenProcesses(process_proc_t process_proc) override;
-		bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) override;
-};
+bool EnumerateOpenProcesses(process_proc_t process_proc);
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc);
 
 } // namespace animia::internal::proc
 
--- a/dep/animia/include/animia/fd/win32.h	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/include/animia/fd/win32.h	Sun Dec 24 02:59:42 2023 -0500
@@ -11,11 +11,8 @@
 
 namespace animia::internal::win32 {
 
-class Win32FdTools final : public BaseFdTools {
-	public:
-		bool EnumerateOpenProcesses(process_proc_t process_proc) override;
-		bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) override;
-};
+bool EnumerateOpenProcesses(process_proc_t process_proc);
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc);
 
 } // namespace animia::internal::win32
 
--- a/dep/animia/include/animia/fd/xnu.h	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/include/animia/fd/xnu.h	Sun Dec 24 02:59:42 2023 -0500
@@ -9,11 +9,8 @@
 
 namespace animia::internal::xnu {
 
-class XnuFdTools final : public BaseFdTools {
-	public:
-		bool EnumerateOpenProcesses(process_proc_t process_proc) override;
-		bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) override;
-};
+bool EnumerateOpenProcesses(process_proc_t process_proc);
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc);
 
 } // namespace animia::internal::xnu
 
--- a/dep/animia/include/animia/win.h	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/include/animia/win.h	Sun Dec 24 02:59:42 2023 -0500
@@ -13,15 +13,7 @@
 
 using window_proc_t = std::function<bool(const Process&, const Window&)>;
 
-class BaseWinTools {
-	public:
-		virtual bool EnumerateWindows(window_proc_t window_proc) {
-			return false;
-			(void)window_proc;
-		}
-};
-
-extern BaseWinTools& win;
+bool EnumerateWindows(window_proc_t window_proc);
 
 } // namespace internal
 
--- a/dep/animia/include/animia/win/quartz.h	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/include/animia/win/quartz.h	Sun Dec 24 02:59:42 2023 -0500
@@ -5,10 +5,7 @@
 
 namespace animia::internal::quartz {
 
-class QuartzWinTools final : public BaseWinTools {
-	public:
-		bool EnumerateWindows(window_proc_t window_proc) override;
-};
+bool EnumerateWindows(window_proc_t window_proc);
 
 } // namespace animia::internal::quartz
 
--- a/dep/animia/include/animia/win/win32.h	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/include/animia/win/win32.h	Sun Dec 24 02:59:42 2023 -0500
@@ -5,10 +5,7 @@
 
 namespace animia::internal::win32 {
 
-class Win32WinTools final : public BaseWinTools {
-	public:
-		bool EnumerateWindows(window_proc_t window_proc) override;
-};
+bool EnumerateWindows(window_proc_t window_proc);
 
 } // namespace animia::internal::win32
 
--- a/dep/animia/include/animia/win/x11.h	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/include/animia/win/x11.h	Sun Dec 24 02:59:42 2023 -0500
@@ -5,10 +5,7 @@
 
 namespace animia::internal::x11 {
 
-class X11WinTools final : public BaseWinTools {
-	public:
-		bool EnumerateWindows(window_proc_t window_proc) override;
-};
+bool EnumerateWindows(window_proc_t window_proc);
 
 } // namespace animia::internal::x11
 
--- a/dep/animia/src/animia.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/animia.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -61,7 +61,7 @@
 		return true;
 	};
 
-	if (!internal::fd.EnumerateOpenProcesses(process_proc))
+	if (!internal::EnumerateOpenProcesses(process_proc))
 		return false;
 
 	/* Then add our cool windows.
@@ -83,7 +83,7 @@
 		return true;
 	};
 
-	if (!internal::win.EnumerateWindows(window_proc))
+	if (!internal::EnumerateWindows(window_proc))
 		return false;
 
 	return internal::ApplyStrategies(results);
--- a/dep/animia/src/fd.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/fd.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -2,28 +2,64 @@
 
 #ifdef WIN32
 #	include "animia/fd/win32.h"
-#elif defined(LINUX) || defined(FREEBSD)
+#endif
+
+#ifdef LINUX
 #	include "animia/fd/proc.h"
-#elif defined(MACOSX)
+#endif
+
+#ifdef MACOSX
 #	include "animia/fd/xnu.h"
-#elif defined(LIBUTIL)
-#   include "animia/fd/libutil.h"
+#endif
+
+#ifdef LIBUTIL
+#	include "animia/fd/libutil.h"
 #endif
 
 namespace animia::internal {
 
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
+	bool success = false;
+
 #ifdef WIN32
-win32::Win32FdTools os_fd;
-#elif defined(LINUX) || defined(FREEBSD)
-proc::ProcFdTools os_fd;
-#elif defined(MACOSX)
-xnu::XnuFdTools os_fd;
-#elif defined(LIBUTIL)
-libutil::LibutilFdTools os_fd;
-#else
-BaseFdTools os_fd;
+	success ^= win32::EnumerateOpenFiles(pids, open_file_proc);
+#endif
+
+#ifdef LINUX
+	success ^= proc::EnumerateOpenFiles(pids, open_file_proc);
+#endif
+
+#ifdef MACOSX
+	success ^= xnu::EnumerateOpenFiles(pids, open_file_proc);
+#endif
+
+#ifdef LIBUTIL
+	success ^= libutil::EnumerateOpenFiles(pids, open_file_proc);
 #endif
 
-BaseFdTools& fd = os_fd;
+	return success;
+}
+
+bool EnumerateOpenProcesses(process_proc_t process_proc) {
+	bool success = false;
+
+#ifdef WIN32
+	success ^= win32::EnumerateOpenProcesses(process_proc);
+#endif
+
+#ifdef LINUX
+	success ^= proc::EnumerateOpenProcesses(process_proc);
+#endif
+
+#ifdef MACOSX
+	success ^= xnu::EnumerateOpenProcesses(process_proc);
+#endif
+
+#ifdef LIBUTIL
+	success ^= libutil::EnumerateOpenProcesses(process_proc);
+#endif
+
+	return success;
+}
 
 } // namespace animia::internal
--- a/dep/animia/src/fd/libutil.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/fd/libutil.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -19,7 +19,7 @@
 		    !file.find("/proc"));
 }
 
-bool LibutilFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
+bool EnumerateOpenProcesses(process_proc_t process_proc) {
 	static const int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
 	size_t length = 0;
 
@@ -47,7 +47,7 @@
 	return true;
 }
 
-bool LibutilFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
 	for (const auto& pid : pids) {
 		int cnt;
 		std::unique_ptr<struct kinfo_file[]> files;
@@ -68,4 +68,4 @@
 	return true;
 }
 
-}
\ No newline at end of file
+}
--- a/dep/animia/src/fd/proc.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/fd/proc.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -100,7 +100,7 @@
 	return result;
 }
 
-bool ProcFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
+bool EnumerateOpenProcesses(process_proc_t process_proc) {
 	bool success = false;
 	for (const auto& dir : GetAllFilesInDir(std::string(PROC_LOCATION))) {
 		pid_t pid;
@@ -116,7 +116,7 @@
 	return success;
 }
 
-bool ProcFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
 	if (!open_file_proc)
 		return false;
 
--- a/dep/animia/src/fd/win32.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/fd/win32.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -172,7 +172,7 @@
 	return true;
 }
 
-bool Win32FdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
+bool EnumerateOpenProcesses(process_proc_t process_proc) {
 	HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 	if (hProcessSnap == INVALID_HANDLE_VALUE)
 		return false;
@@ -196,7 +196,7 @@
 }
 
 /* this could be changed to being a callback, but... I'm too lazy right now :) */
-bool Win32FdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
 	if (!open_file_proc)
 		return false;
 
--- a/dep/animia/src/fd/xnu.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/fd/xnu.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -16,7 +16,7 @@
 
 namespace animia::internal::xnu {
 
-bool XnuFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
+bool EnumerateOpenProcesses(process_proc_t process_proc) {
 	size_t pids_size = 512;
 	std::unique_ptr<pid_t[]> pids;
 
@@ -38,7 +38,7 @@
 	return true;
 }
 
-bool XnuFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
+bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
 	if (!open_file_proc)
 		return false;
 
--- a/dep/animia/src/strategist.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/strategist.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -94,7 +94,7 @@
 		return true;
 	};
 
-	fd.EnumerateOpenFiles(pids, open_file_proc);
+	EnumerateOpenFiles(pids, open_file_proc);
 
 	return success;
 }
--- a/dep/animia/src/win.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/win.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -2,24 +2,34 @@
 
 #ifdef WIN32
 #	include "animia/win/win32.h"
-#elif defined(MACOSX)
+#endif
+
+#ifdef MACOSX
 #	include "animia/win/quartz.h"
-#elif defined(X11)
+#endif
+
+#ifdef X11
 #	include "animia/win/x11.h"
 #endif
 
 namespace animia::internal {
 
+bool EnumerateWindows(window_proc_t window_proc) {
+	bool success = false;
+
 #ifdef WIN32
-win32::Win32WinTools os_win;
-#elif defined(MACOSX)
-quartz::QuartzWinTools os_win;
-#elif defined(X11)
-x11::X11WinTools os_win;
-#else
-BaseWinTools os_win;
+	success ^= win32::EnumerateWindows(window_proc);
 #endif
 
-BaseWinTools& win = os_win;
+#ifdef MACOSX
+	success ^= quartz::EnumerateWindows(window_proc);
+#endif
+
+#ifdef X11
+	success ^= x11::EnumerateWindows(window_proc);
+#endif
+
+	return success;
+}
 
 } // namespace animia::internal
--- a/dep/animia/src/win/quartz.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/win/quartz.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -77,7 +77,7 @@
 	return true;
 }
 
-bool QuartzWinTools::EnumerateWindows(window_proc_t window_proc) {
+bool EnumerateWindows(window_proc_t window_proc) {
 	if (!window_proc)
 		return false;
 
--- a/dep/animia/src/win/win32.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/win/win32.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -167,7 +167,7 @@
 	return TRUE;
 }
 
-bool Win32WinTools::EnumerateWindows(window_proc_t window_proc) {
+bool EnumerateWindows(window_proc_t window_proc) {
 	if (!window_proc)
 		return false;
 
@@ -178,4 +178,4 @@
 	return ::EnumWindows(EnumWindowsProc, param) != FALSE;
 }
 
-} // namespace animia::internal::win32
\ No newline at end of file
+} // namespace animia::internal::win32
--- a/dep/animia/src/win/x11.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/dep/animia/src/win/x11.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -130,7 +130,7 @@
 	return true;
 }
 
-bool X11WinTools::EnumerateWindows(window_proc_t window_proc) {
+bool EnumerateWindows(window_proc_t window_proc) {
 	if (!window_proc)
 		return false;
 
--- a/include/core/json.h	Fri Dec 08 11:19:54 2023 -0500
+++ b/include/core/json.h	Sun Dec 24 02:59:42 2023 -0500
@@ -3,6 +3,27 @@
 
 #include "json/json.hpp"
 
+#include <optional>
+
+namespace nlohmann {
+
+template<typename T>
+void to_json(nlohmann::json& j, const std::optional<T>& v)
+{
+	if (v.has_value())
+		j = v.value();
+	else
+		j = nullptr;
+}
+
+template<typename T>
+void from_json(const nlohmann::json& j, std::optional<T>& v)
+{
+	return j.is_null() ? std::nullopt : j.get<T>();
+}
+
+} // namespace nlohmann
+
 namespace JSON {
 
 template<typename T = std::string>
--- a/src/core/anime_db.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/src/core/anime_db.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -122,29 +122,29 @@
 	return id;
 }
 
-/* This is really fugly but WHO CARES :P
-
-   This fairly basic algorithm is only in effect because
-   there are some special cases, e.g. Another and Re:ZERO, where 
-   we get the wrong match, so we have to create Advanced Techniques
-   to solve this
-
-   This algorithm:
-     1. searches each anime item for a match to the preferred title
-        AND all synonyms and marks those matches with
-          `synonym.length() - (synonym.find(needle) + needle.length());`
-        which should never be less than zero and will be zero if, and only if
-        the titles match exactly.
-     2. returns the id of the match that is the lowest, which will most
-        definitely match anything that exactly matches the title of the
-        filename */
+/* 
+ * This fairly basic algorithm is only in effect because
+ * there are some special cases, e.g. Another and Re:ZERO, where 
+ * we get the wrong match, so we have to create Advanced Techniques
+ * to solve this
+ *
+ * This algorithm:
+ *   1. searches each anime item for a match to the preferred title
+ *      AND all synonyms and marks those matches with
+ *        `synonym.length() - (synonym.find(needle) + needle.length());`
+ *      which should never be less than zero and will be zero if, and only if
+ *      the titles match exactly.
+ *   2. returns the id of the match that is the lowest, which will most
+ *      definitely match anything that exactly matches the title of the
+ *      filename
+*/
 int Database::GetAnimeFromTitle(const std::string& title) {
 	if (title.empty())
 		return 0;
 
 	std::unordered_map<int, size_t> map;
 
-	auto process_title = [&map](const Anime& anime, const std::string& title, const std::string& needle) -> bool {
+	static const auto process_title = [&map](const Anime& anime, const std::string& title, const std::string& needle) -> bool {
 		size_t ret = title.find(needle);
 		if (ret == std::string::npos)
 			return false;
--- a/src/core/config.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/src/core/config.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -85,7 +85,6 @@
 		}
 	}
 
-	/* ew */
 	locale.SetActiveLocale(QLocale(Strings::ToQString(INI::GetIniValue<std::string>(ini, "General", "Locale", "en_US"))));
 
 	theme.SetTheme(Translate::ToTheme(INI::GetIniValue<std::string>(ini, "Appearance", "Theme", "Default")));
--- a/src/core/date.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/src/core/date.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -31,12 +31,13 @@
 	/* NOTE: this constructor is made for use with
 	   AniList FuzzyDate-style JSON. In the future, some other
 	   methods may be parsed and whatnot if necessary. */
-	if (json.contains("year") && json.at("year").is_number())
-		SetYear(json.at("year").get<unsigned int>());
-	if (json.contains("month") && json.at("month").is_number())
-		SetMonth(json.at("month").get<unsigned int>());
-	if (json.contains("day") && json.at("day").is_number())
-		SetDay(json.at("day").get<unsigned int>());
+
+	if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number())
+		SetYear(json.at("/year"_json_pointer).get<unsigned int>());
+	if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number())
+		SetMonth(json.at("/month"_json_pointer).get<unsigned int>());
+	if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number())
+		SetDay(json.at("/day"_json_pointer).get<unsigned int>());
 }
 
 void Date::VoidYear() {
@@ -88,22 +89,9 @@
 }
 
 nlohmann::json Date::GetAsAniListJson() const {
-	nlohmann::json result = {};
-
-	if (year.has_value())
-		result["year"] = year.value();
-	else
-		result["year"] = nullptr;
-
-	if (month.has_value())
-		result["month"] = month.value();
-	else
-		result["month"] = nullptr;
-
-	if (day.has_value())
-		result["day"] = day.value();
-	else
-		result["day"] = nullptr;
-
-	return result;
+	return {
+		{"year", year},
+		{"month", month},
+		{"day", day}
+	};
 }
--- a/src/gui/pages/anime_list.cc	Fri Dec 08 11:19:54 2023 -0500
+++ b/src/gui/pages/anime_list.cc	Sun Dec 24 02:59:42 2023 -0500
@@ -116,9 +116,10 @@
 				case AL_SCORE: return Strings::ToQString(list[index.row()].GetUserPresentableScore());
 				case AL_TYPE: return Strings::ToQString(Translate::ToString(list[index.row()].GetFormat()));
 				case AL_SEASON: {
-					const std::optional<unsigned int> year = list[index.row()].GetAirDate().GetYear();
-					if (year.has_value())
-						return Strings::ToQString(Translate::ToString(list[index.row()].GetSeason()) + " " + std::to_string(year.value()));
+					std::optional<unsigned int> year = list[index.row()].GetAirDate().GetYear();
+					if (!year)
+						return "Unknown Unknown";
+					return Strings::ToQString(Translate::ToLocalString(list[index.row()].GetSeason()) + " " + std::to_string(year.value()));
 				}
 				case AL_AVG_SCORE: return QString::number(list[index.row()].GetAudienceScore()) + "%";
 				case AL_STARTED: return list[index.row()].GetUserDateStarted().GetAsQDate();