changeset 328:71396ecb6f7e

library: convert to class + database it also stores std::filesystem::paths now...
author Paper <paper@paper.us.eu.org>
date Fri, 14 Jun 2024 00:37:45 -0400
parents b5d6c27c308f
children 4aeffed717ef
files include/library/library.h src/gui/window.cc src/library/library.cc
diffstat 3 files changed, 15 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/include/library/library.h	Thu Jun 13 01:49:18 2024 -0400
+++ b/include/library/library.h	Fri Jun 14 00:37:45 2024 -0400
@@ -3,15 +3,19 @@
 
 #include "library/library.h"
 
-#include <string>
+#include <filesystem>
 #include <unordered_map>
 
 namespace Library {
 
-// int = anime id, map = episode, paths
-extern std::unordered_map<int, std::unordered_map<int, std::string>> library;
+class Database {
+public:
+	void Refresh();
 
-void SearchLibraryFolders();
+	std::unordered_map<int, std::unordered_map<int, std::filesystem::path>> items;
+};
+
+extern Database db;
 
 } // namespace Library
 
--- a/src/gui/window.cc	Thu Jun 13 01:49:18 2024 -0400
+++ b/src/gui/window.cc	Fri Jun 14 00:37:45 2024 -0400
@@ -176,7 +176,7 @@
 		}
 
 		{
-			menu->addAction(tr("&Scan available episodes"), [] { Library::SearchLibraryFolders(); });
+			menu->addAction(tr("&Scan available episodes"), [] { Library::db.Refresh(); });
 		}
 
 		menu->addSeparator();
--- a/src/library/library.cc	Thu Jun 13 01:49:18 2024 -0400
+++ b/src/library/library.cc	Fri Jun 14 00:37:45 2024 -0400
@@ -13,11 +13,8 @@
 
 namespace Library {
 
-// int = anime id, map = episode, paths
-std::unordered_map<int, std::unordered_map<int, std::string>> library;
-
-void SearchLibraryFolders() {
-	library.clear();
+void Database::Refresh() {
+	items.clear();
 
 	for (const auto& folder : session.config.library.paths) {
 		for (const auto& entry : std::filesystem::recursive_directory_iterator(folder)) {
@@ -38,12 +35,14 @@
 			if (id <= 0)
 				continue;
 
-			const int episode = Strings::ToInt(Strings::ToUtf8String(elements.get(anitomy::kElementEpisodeNumber)));
+			const int episode = Strings::ToInt<int>(Strings::ToUtf8String(elements.get(anitomy::kElementEpisodeNumber)));
 
 			// we have an ID now!
-			library[id][episode] = path.u8string();
+			items[id][episode] = path;
 		}
 	}
 }
 
+Database db;
+
 } // namespace Library