changeset 154:d43d68408d3c

dep/animia: fix XnuFdTools also we use anitomy directly now. HG Enter commit message. Lines beginning with 'HG:' are removed.
author Paper <mrpapersonic@gmail.com>
date Wed, 15 Nov 2023 14:14:17 -0500
parents bd439dd6ffc5
children d2bbb5773616
files dep/animia/CMakeLists.txt dep/animia/include/animia/fd.h dep/animia/src/fd/linux.cc dep/animia/src/fd/xnu.cc dep/animia/src/win.cc include/gui/pages/now_playing.h include/track/media.h src/gui/pages/now_playing.cc src/gui/pages/torrents.cc src/gui/window.cc src/track/media.cc
diffstat 11 files changed, 55 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/dep/animia/CMakeLists.txt	Wed Nov 15 13:28:18 2023 -0500
+++ b/dep/animia/CMakeLists.txt	Wed Nov 15 14:14:17 2023 -0500
@@ -10,12 +10,13 @@
 	src/win.cc
 )
 
+# FD
 if(LINUX)
 	list(APPEND SRC_FILES
 		# linux
 		src/fd/linux.cc
 	)
-elseif(MACOSX) # this won't run on Linux
+elseif(APPLE) # this won't run on Linux
 	list(APPEND SRC_FILES
 		# xnu stuff
 		src/fd/xnu.cc
@@ -27,6 +28,7 @@
 	)
 endif()
 
+# Windows
 if(WIN32)
 	list(APPEND SRC_FILES
 		src/win/win32.cc
--- a/dep/animia/include/animia/fd.h	Wed Nov 15 13:28:18 2023 -0500
+++ b/dep/animia/include/animia/fd.h	Wed Nov 15 14:14:17 2023 -0500
@@ -24,6 +24,7 @@
 
 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; }
 };
--- a/dep/animia/src/fd/linux.cc	Wed Nov 15 13:28:18 2023 -0500
+++ b/dep/animia/src/fd/linux.cc	Wed Nov 15 14:14:17 2023 -0500
@@ -99,14 +99,17 @@
 	return ret.c_str();
 }
 
-static bool GetProcessName(pid_t pid, std::string& result) {
+static std::string GetProcessName(pid_t pid) {
 	const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm";
 
+	std::string result;
+
 	if (!util::ReadFile(path, result))
-		return false;
+		return "";
 
 	result.erase(std::remove(result.begin(), result.end(), '\n'), result.end());
-	return true;
+
+	return result;
 }
 
 bool LinuxFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
--- a/dep/animia/src/fd/xnu.cc	Wed Nov 15 13:28:18 2023 -0500
+++ b/dep/animia/src/fd/xnu.cc	Wed Nov 15 14:14:17 2023 -0500
@@ -8,6 +8,7 @@
 #include <unordered_map>
 #include <vector>
 #include <string>
+#include <cassert>
 
 #include <fcntl.h>
 #include <sys/sysctl.h>
@@ -17,17 +18,16 @@
 
 namespace animia::internal::xnu {
 
-static bool GetProcessName(pid_t pid, std::string& result) {
+static std::string GetProcessName(pid_t pid) {
 	struct proc_bsdinfo proc;
 
 	int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE);
 	if (st != PROC_PIDTBSDINFO_SIZE)
-		return false;
+		return "";
 
 	/* fixme: is this right? pbi_comm is an alternative, but it reduces the string size to
 	   16 chars. does pbi_name do the same, or is it different? */
-	result = proc.pbi_name;
-	return true;
+	return proc.pbi_name;
 }
 
 /* this is a cleaned up version of a function from... Apple?
@@ -60,6 +60,8 @@
 		if (!process_proc({pid, GetProcessName(pid)}))
 			return false;
 	}
+
+	return true;
 }
 
 bool XnuFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
@@ -95,6 +97,7 @@
 			}
 		}
 	}
+
 	return true;
 }
 
--- a/dep/animia/src/win.cc	Wed Nov 15 13:28:18 2023 -0500
+++ b/dep/animia/src/win.cc	Wed Nov 15 14:14:17 2023 -0500
@@ -1,4 +1,4 @@
-#include "animia/fd.h"
+#include "animia/win.h"
 
 #ifdef WIN32
 #	include "animia/win/win32.h"
--- a/include/gui/pages/now_playing.h	Wed Nov 15 13:28:18 2023 -0500
+++ b/include/gui/pages/now_playing.h	Wed Nov 15 14:14:17 2023 -0500
@@ -10,13 +10,17 @@
 class Anime;
 }
 
+namespace anitomy {
+class Elements;
+}
+
 class NowPlayingPage final : public QFrame {
 		Q_OBJECT
 
 	public:
 		NowPlayingPage(QWidget* parent = nullptr);
 		void SetDefault();
-		void SetPlaying(const Anime::Anime& anime, const std::unordered_map<std::string, std::string>& episodes);
+		void SetPlaying(const Anime::Anime& anime, const anitomy::Elements& episodes);
 		int GetPlayingId();
 
 	private:
--- a/include/track/media.h	Wed Nov 15 13:28:18 2023 -0500
+++ b/include/track/media.h	Wed Nov 15 14:14:17 2023 -0500
@@ -1,6 +1,5 @@
 #ifndef __track__media_h
 #define __track__media_h
-#include "core/filesystem.h"
 #include <unordered_map>
 #include <string>
 #include <vector>
@@ -9,8 +8,6 @@
 namespace Media {
 
 bool GetCurrentlyPlaying(std::vector<std::string>& vec);
-std::unordered_map<std::string, std::string> GetFileElements(const std::string& basename);
-//std::unordered_map<std::string, std::string> GetFileElements(const std::filesystem::path& path);
 
 } // namespace Media
 } // namespace Track
--- a/src/gui/pages/now_playing.cc	Wed Nov 15 13:28:18 2023 -0500
+++ b/src/gui/pages/now_playing.cc	Wed Nov 15 14:14:17 2023 -0500
@@ -10,11 +10,8 @@
 #include <QVBoxLayout>
 #include <QWidget>
 
-/* WARNING: HACKY STUFF HERE
-   
-   The Now Playing page is designed to "switch" between these two sub-pages,
-   using a QStackedWidget. This *could* be the best way to do this, but this
-   feels very very stupid and I really don't like it */
+#include "anitomy/anitomy.h"
+
 namespace NowPlayingPages {
 
 class Default : public QWidget {
@@ -29,7 +26,7 @@
 
 	public:
 		Playing(QWidget* parent = nullptr);
-		void SetPlayingAnime(const Anime::Anime& anime, const std::unordered_map<std::string, std::string>& info);
+		void SetPlayingAnime(const Anime::Anime& anime, const anitomy::Elements& info);
 		int GetPlayingAnime();
 
 	private:
@@ -89,11 +86,11 @@
 	return _id;
 }
 
-void Playing::SetPlayingAnime(const Anime::Anime& anime, const std::unordered_map<std::string, std::string>& info) {
-	if (_id == anime.GetId() && std::to_string(_episode) == info.at("episode"))
+void Playing::SetPlayingAnime(const Anime::Anime& anime, const anitomy::Elements& info) {
+	if (_id == anime.GetId() && _episode == Strings::ToInt(Strings::ToUtf8String(info.get(anitomy::kElementEpisodeNumber))))
 		return;
 	_id = anime.GetId();
-	_episode = Strings::ToInt(info.at("episode"));
+	_episode = Strings::ToInt(Strings::ToUtf8String(info.get(anitomy::kElementEpisodeNumber)));
 	_title->SetText(Strings::ToQString(anime.GetUserPreferredTitle()));
 	_info->SetAnime(anime);
 	_poster->SetAnime(anime);
@@ -126,7 +123,7 @@
 	return reinterpret_cast<NowPlayingPages::Playing*>(stack->widget(1))->GetPlayingAnime();
 }
 
-void NowPlayingPage::SetPlaying(const Anime::Anime& anime, const std::unordered_map<std::string, std::string>& info) {
+void NowPlayingPage::SetPlaying(const Anime::Anime& anime, const anitomy::Elements& info) {
 	reinterpret_cast<NowPlayingPages::Playing*>(stack->widget(1))->SetPlayingAnime(anime, info);
 	stack->setCurrentIndex(1);
 	updateGeometry();
--- a/src/gui/pages/torrents.cc	Wed Nov 15 13:28:18 2023 -0500
+++ b/src/gui/pages/torrents.cc	Wed Nov 15 14:14:17 2023 -0500
@@ -17,6 +17,8 @@
 #include <sstream>
 #include <algorithm>
 
+#include "anitomy/anitomy.h"
+
 /* This file is very, very similar to the anime list page.
 
    It differs from Taiga in that it uses tabs instead of
@@ -71,12 +73,16 @@
 		TorrentModelItem torrent;
 		torrent.SetFilename(item.child_value("title")); /* "title" == filename */
 		{
-			/* Use Anitomy to parse the file's elements (we should *really* not be doing this this way!) */
-			std::unordered_map<std::string, std::string> elements = Track::Media::GetFileElements(torrent.GetFilename());
-			torrent.SetTitle(elements["title"]);
-			torrent.SetEpisode(Strings::RemoveLeadingChars(elements["episode"], '0'));
-			torrent.SetGroup(elements["group"]);
-			torrent.SetResolution(elements["resolution"]);
+			anitomy::Anitomy anitomy;
+			anitomy.Parse(Strings::ToWstring(torrent.GetFilename()));
+
+			const auto& elements = anitomy.elements();
+
+			/* todo: patch Anitomy so that it doesn't use wide strings */
+			torrent.SetTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle)));
+			torrent.SetEpisode(Strings::RemoveLeadingChars(Strings::ToUtf8String(elements.get(anitomy::kElementEpisodeNumber)), '0'));
+			torrent.SetGroup(Strings::ToUtf8String(elements.get(anitomy::kElementReleaseGroup)));
+			torrent.SetResolution(Strings::ToUtf8String(elements.get(anitomy::kElementVideoResolution)));
 		}
 		torrent.SetDescription(Strings::TextifySynopsis(item.child_value("description")));
 		{
--- a/src/gui/window.cc	Wed Nov 15 13:28:18 2023 -0500
+++ b/src/gui/window.cc	Wed Nov 15 14:14:17 2023 -0500
@@ -17,6 +17,8 @@
 #include "services/services.h"
 #include "track/media.h"
 
+#include "anitomy/anitomy.h"
+
 #include <QActionGroup>
 #include <QApplication>
 #include <QDebug>
@@ -76,9 +78,12 @@
 
 		/* this should really be more intertwined with anitomy */
 		for (const auto& file : files) {
-			std::filesystem::path path(file); // in the future it will not be guaranteed this is a path!
-			std::unordered_map<std::string, std::string> elements = Track::Media::GetFileElements(path.filename().string());
-			int id = Anime::db.GetAnimeFromTitle(elements["title"]);
+			anitomy::Anitomy anitomy;
+			anitomy.Parse(Strings::ToWstring(file));
+
+			const auto& elements = anitomy.elements();
+
+			int id = Anime::db.GetAnimeFromTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle)));
 			if (id <= 0)
 				continue;
 
--- a/src/track/media.cc	Wed Nov 15 13:28:18 2023 -0500
+++ b/src/track/media.cc	Wed Nov 15 14:14:17 2023 -0500
@@ -8,7 +8,7 @@
 #include <string>
 #include <unordered_map>
 #include <vector>
-#include <iostream>
+#include <filesystem>
 #include <QFile>
 #include <QTextStream>
 
@@ -42,11 +42,11 @@
 		for (const auto& media : result.media) {
 			for (const auto& info : media.information) {
 				switch (info.type) {
-					case 
+					case animia::MediaInfoType::File:
+					case animia::MediaInfoType::Title:
+						vec.push_back(std::filesystem::path(info.value).filename().string());
+						success |= true;
 				}
-				std::cout << static_cast<int>(info.type) << ": " << info.value << std::endl;
-				vec.push_back(info.value);
-				success |= true;
 			}
 		}
 	}