From 515477b5d597b807b27d2f95950b937ab8684c00 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 11 Feb 2022 17:43:58 +0100 Subject: Add read local manga to automedia seen --- src/plugins/LocalManga.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/plugins/LocalManga.cpp') diff --git a/src/plugins/LocalManga.cpp b/src/plugins/LocalManga.cpp index 4deb229..3ad4ffa 100644 --- a/src/plugins/LocalManga.cpp +++ b/src/plugins/LocalManga.cpp @@ -4,6 +4,7 @@ #include "../../include/Theme.hpp" #include "../../include/StringUtils.hpp" #include "../../include/Storage.hpp" +#include namespace QuickMedia { // Pages are sorted from 1.png to n.png @@ -155,6 +156,47 @@ namespace QuickMedia { return PluginResult::OK; } + static std::unordered_set get_lines_in_file(const Path &filepath) { + std::unordered_set lines; + + std::string file_content; + if(file_get_content(filepath, file_content) != 0) + return lines; + + string_split(file_content, '\n', [&lines](const char *str_part, size_t size) { + lines.insert(std::string(str_part, size)); + return true; + }); + + return lines; + } + + static bool append_seen_manga_to_automedia_seen(const std::string &manga_chapter_name) { + Path automedia_config_dir = get_home_dir().join(".config").join("automedia"); + if(create_directory_recursive(automedia_config_dir) != 0) { + fprintf(stderr, "Warning: failed to create directory: %s\n", automedia_config_dir.data.c_str()); + return false; + } + + Path automedia_seen_filepath = automedia_config_dir; + automedia_seen_filepath.join("seen"); + + std::unordered_set lines = get_lines_in_file(automedia_seen_filepath); + if(lines.find(manga_chapter_name) != lines.end()) + return true; // Already seen + + FILE *file = fopen(automedia_seen_filepath.data.c_str(), "ab"); + if(!file) { + fprintf(stderr, "Warning: failed to open automedia seen file %s\n", automedia_seen_filepath.data.c_str()); + return false; + } + + std::string new_line_data = manga_chapter_name + "\n"; + fwrite(new_line_data.data(), 1, new_line_data.size(), file); + fclose(file); + return true; + } + PluginResult LocalMangaChaptersPage::submit(const SubmitArgs &args, std::vector &result_tabs) { if(get_config().local_manga_directory.empty()) { show_notification("QuickMedia", "local_manga_directory config is not set", Urgency::CRITICAL); @@ -169,6 +211,10 @@ namespace QuickMedia { Path chapter_url = Path(get_config().local_manga_directory).join(content_url).join(args.url); std::vector pages = get_images_in_manga(chapter_url); result_tabs.push_back(Tab{nullptr, std::make_unique(program, content_title, args.title, args.url, thumbnail_url, std::move(pages)), nullptr}); + + if(is_program_executable_by_name("automedia")) + append_seen_manga_to_automedia_seen(content_url + "/" + args.url); + return PluginResult::OK; } -- cgit v1.2.3