changeset 147:6fdf0632c003

track: use a bit of a more sane way to manage recognition it also works with the new animia API
author Paper <mrpapersonic@gmail.com>
date Tue, 14 Nov 2023 13:19:40 -0500
parents d8a61e7e2a36
children aa4df5a84338
files CMakeLists.txt include/core/config.h include/core/session.h include/gui/dialog/settings.h include/track/types.h src/core/config.cc src/gui/dialog/settings/recognition.cc src/gui/window.cc src/main.cc src/track/media.cc src/track/types.cc
diffstat 11 files changed, 51 insertions(+), 390 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Mon Nov 13 13:52:58 2023 -0500
+++ b/CMakeLists.txt	Tue Nov 14 13:19:40 2023 -0500
@@ -105,7 +105,6 @@
 	# Tracking
 	src/track/constants.cc
 	src/track/media.cc
-	src/track/types.cc
 
 	# Qt resources
 	rc/icons.qrc
--- a/include/core/config.h	Mon Nov 13 13:52:58 2023 -0500
+++ b/include/core/config.h	Tue Nov 14 13:19:40 2023 -0500
@@ -4,8 +4,12 @@
 #include "core/anime.h"
 #include "gui/theme.h"
 #include "gui/locale.h"
+
+#include "animia/player.h"
+
 #include <string>
 #include <vector>
+#include <utility>
 
 class Config {
 	public:
@@ -40,7 +44,7 @@
 		struct {
 			public:
 				bool detect_media_players;
-				//bool detect_streaming_media;
+				std::vector<std::pair<bool, animia::Player>> players;
 		} recognition;
 
 		struct {
--- a/include/core/session.h	Mon Nov 13 13:52:58 2023 -0500
+++ b/include/core/session.h	Tue Nov 14 13:19:40 2023 -0500
@@ -2,7 +2,6 @@
 #define __core__session_h
 
 #include "core/config.h"
-#include "track/types.h"
 #include "gui/locale.h"
 #include <QElapsedTimer>
 
@@ -16,11 +15,6 @@
 
 		Config config;
 
-		struct {
-			std::vector<Track::Types::MediaPlayer> players;
-			std::vector<Track::Types::MediaExtension> extensions;
-		} recognition;
-
 	private:
 		uint32_t requests = 0;
 		QElapsedTimer timer;
--- a/include/gui/dialog/settings.h	Mon Nov 13 13:52:58 2023 -0500
+++ b/include/gui/dialog/settings.h	Tue Nov 14 13:19:40 2023 -0500
@@ -81,8 +81,7 @@
 	private:
 		QWidget* CreatePlayersWidget();
 		decltype(session.config.recognition.detect_media_players) detect_media_players;
-		decltype(session.recognition.players) players;
-		decltype(session.recognition.extensions) extensions;
+		decltype(session.config.recognition.players) players;
 };
 
 class SettingsDialog final : public QDialog {
--- a/include/track/types.h	Mon Nov 13 13:52:58 2023 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-#ifndef __track__types_h
-#define __track__types_h
-
-#include <string>
-#include <vector>
-
-namespace Track {
-namespace Types {
-
-struct MediaPlayer {
-	public:
-		std::string GetName() const { return _name; };
-		std::string GetExecutable() const { return _executable; };
-		bool GetEnabled() const { return _enabled; };
-
-		void SetName(const std::string& name) { _name = name; };
-		void SetExecutable(const std::string& executable) { _executable = executable; };
-		void SetEnabled(const bool enabled) { _enabled = enabled; };
-
-	private:
-		std::string _name;
-		std::string _executable;
-		bool _enabled;
-};
-
-struct MediaExtension {
-	public:
-		std::string GetExtension() const { return _extension; };
-		bool GetEnabled() const { return _enabled; };
-
-		void SetExtension(const std::string& extension) { _extension = extension; };
-		void SetEnabled(const bool enabled) { _enabled = enabled; };
-
-	private:
-		std::string _extension;
-		bool _enabled;
-};
-
-void LoadPlayers(std::vector<MediaPlayer>& players);
-void LoadExtensions(std::vector<MediaExtension>& extensions);
-
-void SavePlayers(const std::vector<MediaPlayer>& players);
-void SaveExtensions(const std::vector<MediaExtension>& extensions);
-
-}
-}
-
-#endif // __track__types_h
\ No newline at end of file
--- a/src/core/config.cc	Mon Nov 13 13:52:58 2023 -0500
+++ b/src/core/config.cc	Tue Nov 14 13:19:40 2023 -0500
@@ -10,6 +10,9 @@
 #include "core/json.h"
 #include "gui/translate/anime.h"
 #include "gui/translate/config.h"
+
+#include "animia/player.h"
+
 #include <algorithm>
 #include <cstdlib>
 #include <cstring>
@@ -17,6 +20,9 @@
 #include <fstream>
 #include <limits.h>
 
+#include <QFile>
+#include <QTextStream>
+
 /* I'll use an INI-based config file instead of using an
    XML file like Taiga. */
 
@@ -42,6 +48,29 @@
 
 	recognition.detect_media_players = INI::GetIniValue<bool>(ini, "Recognition", "Detect media players", true);
 
+	/* lots of dumb logic to import the player data */
+	{
+		/* load the player data */
+		QFile f(":/players.anisthesia");
+		if (!f.exists())
+			return false;
+
+		f.open(QFile::ReadOnly | QFile::Text);
+		QTextStream ts(&f);
+
+		std::vector<animia::Player> players;
+
+		if (!animia::ParsePlayersData(Strings::ToUtf8String(ts.readAll()), players))
+			return false;
+
+		recognition.players.reserve(players.size());
+		for (const auto& player : players)
+			recognition.players.push_back({true, player});
+	}
+
+	for (auto& player : recognition.players)
+		player.first = INI::GetIniValue<bool>(ini, "Recognition/Players", player.second.name, true);
+
 	/* ew */
 	locale.SetActiveLocale(QLocale(Strings::ToQString(INI::GetIniValue<std::string>(ini, "General", "Locale", "en_US"))));
 
@@ -75,6 +104,9 @@
 
 	INI::SetIniValue(ini, "Recognition", "Detect media players", recognition.detect_media_players);
 
+	for (const auto& player : recognition.players)
+		INI::SetIniValue(ini, "Recognition/Players", player.second.name, player.first);
+
 	file.write(ini);
 
 	return 0;
--- a/src/gui/dialog/settings/recognition.cc	Mon Nov 13 13:52:58 2023 -0500
+++ b/src/gui/dialog/settings/recognition.cc	Tue Nov 14 13:19:40 2023 -0500
@@ -1,7 +1,7 @@
 #include "core/session.h"
 #include "core/strings.h"
 #include "gui/dialog/settings.h"
-#include "track/types.h"
+
 #include <QListWidget>
 #include <QListWidgetItem>
 #include <QGroupBox>
@@ -9,10 +9,9 @@
 #include <QLabel>
 #include <QSizePolicy>
 #include <QVBoxLayout>
+
 #include <algorithm>
 
-Q_DECLARE_METATYPE(Track::Types::MediaPlayer);
-
 QWidget* SettingsPageRecognition::CreatePlayersWidget() {
 	QWidget* result = new QWidget(this);
 	result->setAutoFillBackground(true);
@@ -41,8 +40,8 @@
 				const auto& player = players[i];
 				{
 					QListWidgetItem* item = new QListWidgetItem(listwidget);
-					item->setCheckState(player.GetEnabled() ? Qt::Checked : Qt::Unchecked);
-					item->setText(Strings::ToQString(player.GetName() + " (" + player.GetExecutable() + ")"));
+					item->setCheckState(player.first ? Qt::Checked : Qt::Unchecked);
+					item->setText(Strings::ToQString(player.second.name));
 					item->setData(Qt::UserRole, QVariant::fromValue(i));
 				}
 			}
@@ -50,7 +49,7 @@
 				if (!item)
 					return;
 				size_t i = item->data(Qt::UserRole).toUInt();
-				players[i].SetEnabled(item->checkState());
+				players[i].first = item->checkState();
 			});
 			/* this is down here because the listwidget state depends on it */
 			connect(checkbox, &QCheckBox::stateChanged, this, [this, listwidget](int state) {
@@ -65,40 +64,6 @@
 		full_layout->addWidget(widget);
 	}
 
-	{
-		/* Feed link */
-		QWidget* widget = new QWidget(result);
-		QVBoxLayout* widget_layout = new QVBoxLayout(widget);
-
-		{
-			QLabel* label = new QLabel(tr("Allowed file extensions:"), widget);
-			widget_layout->addWidget(label);
-		}
-
-		{
-			QListWidget* listwidget = new QListWidget(widget);
-			for (size_t i = 0; i < extensions.size(); i++) {
-				const auto& extension = extensions[i];
-				{
-					QListWidgetItem* item = new QListWidgetItem(listwidget);
-					item->setCheckState(extension.GetEnabled() ? Qt::Checked : Qt::Unchecked);
-					item->setText(Strings::ToQString("." + extension.GetExtension()));
-					item->setData(Qt::UserRole, QVariant::fromValue(i));
-				}
-			}
-			connect(listwidget, &QListWidget::itemChanged, this, [this](QListWidgetItem* item){
-				if (!item)
-					return;
-				size_t i = item->data(Qt::UserRole).toUInt();
-				extensions[i].SetEnabled(item->checkState());
-			});
-
-			widget_layout->addWidget(listwidget);
-		}
-
-		full_layout->addWidget(widget);
-	}
-
 	full_layout->setSpacing(10);
 
 	return result;
@@ -106,14 +71,12 @@
 
 void SettingsPageRecognition::SaveInfo() {
 	session.config.recognition.detect_media_players = detect_media_players;
-	session.recognition.players = players;
-	session.recognition.extensions = extensions;
+	session.config.recognition.players = players;
 }
 
 SettingsPageRecognition::SettingsPageRecognition(QWidget* parent)
 	: SettingsPage(parent, tr("Recognition")),
-	  players(session.recognition.players),
-	  extensions(session.recognition.extensions) {
+	  players(session.config.recognition.players) {
 	detect_media_players = session.config.recognition.detect_media_players;
 	AddTab(CreatePlayersWidget(), tr("Media players"));
 }
--- a/src/gui/window.cc	Mon Nov 13 13:52:58 2023 -0500
+++ b/src/gui/window.cc	Tue Nov 14 13:19:40 2023 -0500
@@ -16,7 +16,7 @@
 #include "gui/widgets/sidebar.h"
 #include "services/services.h"
 #include "track/media.h"
-#include "track/types.h"
+
 #include <QActionGroup>
 #include <QApplication>
 #include <QDebug>
@@ -32,7 +32,7 @@
 #include <QTimer>
 #include <QToolBar>
 #include <QToolButton>
-#include <iostream>
+
 #ifdef MACOSX
 #	include "sys/osx/dark_theme.h"
 #elif defined(WIN32)
@@ -463,8 +463,6 @@
 
 void MainWindow::closeEvent(QCloseEvent* event) {
 	session.config.Save();
-	Track::Types::SavePlayers(session.recognition.players);
-	Track::Types::SaveExtensions(session.recognition.extensions);
 	event->accept();
 }
 
--- a/src/main.cc	Mon Nov 13 13:52:58 2023 -0500
+++ b/src/main.cc	Tue Nov 14 13:19:40 2023 -0500
@@ -1,6 +1,5 @@
 #include "core/session.h"
 #include "gui/window.h"
-#include "track/types.h"
 #include <QApplication>
 #include <QStyleFactory>
 #include <QTranslator>
@@ -12,8 +11,6 @@
 	QApplication app(argc, argv);
 
 	session.config.Load();
-	Track::Types::LoadPlayers(session.recognition.players);
-	Track::Types::LoadExtensions(session.recognition.extensions);
 
 	MainWindow window;
 
--- a/src/track/media.cc	Mon Nov 13 13:52:58 2023 -0500
+++ b/src/track/media.cc	Tue Nov 14 13:19:40 2023 -0500
@@ -18,17 +18,10 @@
 static bool GetCurrentlyPlayingResults(std::vector<animia::Result>& results) {
 	std::vector<animia::Player> players;
 
-	{
-		QFile f(":/players.anisthesia");
-		if (!f.exists())
-			return false;
-
-		f.open(QFile::ReadOnly | QFile::Text);
-		QTextStream ts(&f);
-
-		if (!animia::ParsePlayersData(Strings::ToUtf8String(ts.readAll()), players))
-			return false;
-	}
+	players.reserve(session.config.recognition.players.size());
+	for (const auto& [enabled, player] : session.config.recognition.players)
+		if (enabled)
+			players.push_back(player);
 
 	if (!animia::GetResults(players, results))
 		return false;
--- a/src/track/types.cc	Mon Nov 13 13:52:58 2023 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,270 +0,0 @@
-#include "track/types.h"
-#include "core/filesystem.h"
-#include "core/json.h"
-#include <fstream>
-#include <vector>
-#include <string>
-
-using namespace nlohmann::literals::json_literals;
-
-namespace Track {
-namespace Types {
-
-static nlohmann::json default_players = {
-	{
-		{"name", "VLC"},
-#ifdef MACOSX
-		{"executable", "VLC"},
-#elif defined(WIN32)
-		{"executable", "vlc.exe"},
-#else
-		{"executable", "vlc"},
-#endif
-		{"enabled", true}
-	},
-	{
-		{"name", "mpv"},
-#ifdef WIN32
-		{"executable", "mpv.exe"},
-#else
-		{"executable", "mpv"},
-#endif
-		{"enabled", true}
-	},
-#ifdef WIN32
-	{
-		{"name", "MPC-HC x64"},
-		{"executable", "mpc-hc64.exe"},
-		{"enabled", true}
-	},
-	{
-		{"name", "MPC-HC"},
-		{"executable", "mpc-hc.exe"},
-		{"enabled", true}
-	},
-	{
-		{"name", "Windows Media Player"},
-		{"executable", "wmplayer.exe"},
-		{"enabled", true}
-	}
-#elif defined(MACOSX)
-	{
-		{"name", "IINA"},
-		{"executable", "IINA"},
-		{"enabled", true}
-	},
-	{
-		{"name", "QuickTime Player"},
-		{"executable", "QuickTime Player"},
-		{"enabled", false}
-	}
-#else
-	{
-		{"name", "MPC-Qt"},
-		{"executable", "mpc-qt"},
-		{"enabled", true}
-	}
-#endif
-};
-
-static nlohmann::json default_extensions = {
-	/* These are the four most common file extensions
-	   according to... me. */
-	{
-		{"extension", "mkv"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "mp4"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "m4v"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "avi"},
-		{"enabled", true}
-	},
-	/* Matroska's retarded inbred cousin */
-	{
-		{"extension", "webm"},
-		{"enabled", true}
-	},
-	/* QuickTime */
-	{
-		{"extension", "mov"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "qt"},
-		{"enabled", true}
-	},
-	/* MPEG transport stream */
-	{
-		{"extension", "mts"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "m2ts"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "ts"},
-		{"enabled", true}
-	},
-	/* MPEG-1 */
-	{
-		{"extension", "mpg"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "mpeg"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "mpe"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "mpv"},
-		{"enabled", true}
-	},
-	/* MPEG-2 */
-	{
-		{"extension", "m2v"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "mp2"},
-		{"enabled", true}
-	},
-	/* 3GPP */
-	{
-		{"extension", "3gp"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "3g2"},
-		{"enabled", true}
-	},
-	/* Windows Media */
-	{
-		{"extension", "asf"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "wmv"},
-		{"enabled", true}
-	},
-	/* Adobe Flash */
-	{
-		{"extension", "flv"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "swf"}, // lol
-		{"enabled", false}
-	},
-	/* Ogg Video */
-	{
-		{"extension", "ogv"},
-		{"enabled", true}
-	},
-	/* RealPlayer... LOL */
-	{
-		{"extension", "rm"},
-		{"enabled", true}
-	},
-	{
-		{"extension", "rmvb"},
-		{"enabled", true}
-	},
-	/* Nullsoft Streaming Video (Winamp) */
-	{
-		{"extension", "nsv"},
-		{"enabled", true}
-	},
-	/* Material Exchange Format (Sony) */
-	{
-		{"extension", "mxf"},
-		{"enabled", true}
-	},
-};
-
-void LoadPlayers(std::vector<MediaPlayer>& players) {
-	nlohmann::json json;
-	{	
-		std::ifstream is(Filesystem::GetPlayersPath().string());
-		if (!is.is_open())
-			json = default_players;
-		else
-			is >> json;
-	}
-
-	players.reserve(json.size());
-	for (const auto& item : json) {
-		MediaPlayer player;
-		player.SetName(JSON::GetString(item, "/name"_json_pointer));
-		player.SetExecutable(JSON::GetString(item, "/executable"_json_pointer));
-		player.SetEnabled(JSON::GetBoolean(item, "/enabled"_json_pointer));
-		players.push_back(player);
-	}
-}
-
-void LoadExtensions(std::vector<MediaExtension>& extensions) {
-	nlohmann::json json;
-	{	
-		std::ifstream is(Filesystem::GetExtensionsPath().string());
-		if (!is.is_open())
-			json = default_extensions;
-		else
-			is >> json;
-	}
-
-	extensions.reserve(json.size());
-	for (const auto& item : json) {
-		MediaExtension extension;
-		extension.SetExtension(JSON::GetString(item, "/extension"_json_pointer));
-		extension.SetEnabled(JSON::GetBoolean(item, "/enabled"_json_pointer));
-		extensions.push_back(extension);
-	}
-}
-
-void SavePlayers(const std::vector<MediaPlayer>& players) {
-	nlohmann::json json = {};
-	for (const auto& player : players) {
-		json.push_back({
-			{"name", player.GetName()},
-			{"executable", player.GetExecutable()},
-			{"enabled", player.GetEnabled()}
-		});
-	}
-
-	{
-		std::ofstream os(Filesystem::GetPlayersPath().string());
-		if (!os.is_open())
-			return;
-		os << std::setw(4) << json << std::endl;
-	}
-}
-
-void SaveExtensions(const std::vector<MediaExtension>& extensions) {
-	nlohmann::json json = {};
-	for (const auto& extension : extensions) {
-		json.push_back({
-			{"extension", extension.GetExtension()},
-			{"enabled", extension.GetEnabled()}
-		});
-	}
-
-	{
-		std::ofstream os(Filesystem::GetExtensionsPath().string());
-		if (!os.is_open())
-			return;
-		os << std::setw(4) << json << std::endl;
-	}
-}
-
-}
-}