aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/LocalManga.cpp
blob: 3ad4ffa9586446b016ea4c175cd97c181f51064c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "../../plugins/LocalManga.hpp"
#include "../../include/Notification.hpp"
#include "../../include/Config.hpp"
#include "../../include/Theme.hpp"
#include "../../include/StringUtils.hpp"
#include "../../include/Storage.hpp"
#include <unordered_set>

namespace QuickMedia {
    // Pages are sorted from 1.png to n.png
    static std::vector<LocalMangaPage> get_images_in_manga(const Path &directory) {
        std::vector<LocalMangaPage> page_list;
        for_files_in_dir(directory, [&page_list](const Path &filepath) -> bool {
            if(get_file_type(filepath) != FileType::REGULAR)
                return true;

            std::string filname_no_ext = filepath.filename_no_ext();
            int page_number = 0;
            if(!to_num(filname_no_ext.c_str(), filname_no_ext.size(), page_number) || filepath.ext()[0] == '\0')
                return true;

            LocalMangaPage local_manga_page;
            local_manga_page.path = filepath;
            local_manga_page.number = page_number;
            page_list.push_back(std::move(local_manga_page));
            return true;
        });

        std::sort(page_list.begin(), page_list.end(), [](const LocalMangaPage &manga_page1, const LocalMangaPage &manga_page2) {
            return manga_page1.number < manga_page2.number;
        });
        return page_list;
    }

    static std::vector<LocalMangaChapter> get_chapters_in_manga(const Path &directory) {
        std::vector<LocalMangaChapter> chapter_list;
        auto callback = [&chapter_list](const Path &filepath) -> bool {
            if(get_file_type(filepath) != FileType::DIRECTORY)
                return true;

            LocalMangaChapter local_manga_chapter;
            local_manga_chapter.name = filepath.filename();
            local_manga_chapter.pages = get_images_in_manga(filepath);
            if(local_manga_chapter.pages.empty() || !file_get_last_modified_time_seconds(filepath.data.c_str(), &local_manga_chapter.modified_time_seconds))
                return true;

            chapter_list.push_back(std::move(local_manga_chapter));
            return true;
        };

        if(get_config().local_manga_sort_chapters_by_name)
            for_files_in_dir_sort_name(directory, std::move(callback), FileSortDirection::DESC);
        else
            for_files_in_dir_sort_last_modified(directory, std::move(callback));

        return chapter_list;
    }

    static std::vector<LocalManga> get_manga_in_directory(const Path &directory) {
        std::vector<LocalManga> manga_list;
        auto callback = [&manga_list](const Path &filepath) -> bool {
            if(get_file_type(filepath) != FileType::DIRECTORY)
                return true;

            LocalManga local_manga;
            local_manga.name = filepath.filename();
            local_manga.chapters = get_chapters_in_manga(filepath);
            if(local_manga.chapters.empty() || !file_get_last_modified_time_seconds(filepath.data.c_str(), &local_manga.modified_time_seconds))
                return true;

            manga_list.push_back(std::move(local_manga));
            return true;
        };

        if(get_config().local_manga_sort_by_name)
            for_files_in_dir_sort_name(directory, std::move(callback), FileSortDirection::ASC);
        else
            for_files_in_dir_sort_last_modified(directory, std::move(callback));

        return manga_list;
    }

    static std::shared_ptr<BodyItem> local_manga_to_body_item(const LocalManga &local_manga, time_t time_now) {
        auto body_item = BodyItem::create(local_manga.name);
        body_item->url = local_manga.name;
        body_item->set_description("Latest chapter: " + local_manga.chapters.front().name + "\nUpdated " + seconds_to_relative_time_str(time_now - local_manga.modified_time_seconds));
        body_item->set_description_color(get_theme().faded_text_color);
        body_item->thumbnail_url = local_manga.chapters.back().pages.front().path.data;
        body_item->thumbnail_is_local = true;
        body_item->thumbnail_size = {101, 141};
        return body_item;
    }

    SearchResult LocalMangaSearchPage::search(const std::string &str, BodyItems &result_items) {
        time_t time_now = time(nullptr);
        for(const LocalManga &local_manga : manga_list) {
            if(string_find_fuzzy_case_insensitive(local_manga.name, str))
                result_items.push_back(local_manga_to_body_item(local_manga, time_now));
        }
        return SearchResult::OK;
    }

    PluginResult LocalMangaSearchPage::submit(const SubmitArgs &args, std::vector<Tab> &result_tabs) {
        if(get_config().local_manga_directory.empty()) {
            show_notification("QuickMedia", "local_manga_directory config is not set", Urgency::CRITICAL);
            return PluginResult::OK;
        }

        if(get_file_type(get_config().local_manga_directory) != FileType::DIRECTORY) {
            show_notification("QuickMedia", "local_manga_directory config is not set to a valid directory", Urgency::CRITICAL);
            return PluginResult::OK;
        }

        Path manga_url = Path(get_config().local_manga_directory).join(args.url);
        std::vector<LocalMangaChapter> chapters = get_chapters_in_manga(manga_url);

        const time_t time_now = time(nullptr);
        BodyItems chapters_items;

        for(const LocalMangaChapter &local_manga_chapter : chapters) {
            auto body_item = BodyItem::create(local_manga_chapter.name);
            body_item->url = local_manga_chapter.name;
            body_item->set_description("Updated " + seconds_to_relative_time_str(time_now - local_manga_chapter.modified_time_seconds));
            body_item->set_description_color(get_theme().faded_text_color);
            chapters_items.push_back(std::move(body_item));
        }

        auto chapters_body = create_body();
        chapters_body->set_items(std::move(chapters_items));

        auto chapters_page = std::make_unique<LocalMangaChaptersPage>(program, args.title, args.url, args.thumbnail_url);
        result_tabs.push_back(Tab{std::move(chapters_body), std::move(chapters_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)});
        return PluginResult::OK;
    }

    PluginResult LocalMangaSearchPage::lazy_fetch(BodyItems &result_items) {
        manga_list.clear();

        if(get_config().local_manga_directory.empty()) {
            show_notification("QuickMedia", "local_manga_directory config is not set", Urgency::CRITICAL);
            return PluginResult::OK;
        }

        if(get_file_type(get_config().local_manga_directory) != FileType::DIRECTORY) {
            show_notification("QuickMedia", "local_manga_directory config is not set to a valid directory", Urgency::CRITICAL);
            return PluginResult::OK;
        }

        manga_list = get_manga_in_directory(get_config().local_manga_directory);

        const time_t time_now = time(nullptr);
        for(const LocalManga &local_manga : manga_list) {
            result_items.push_back(local_manga_to_body_item(local_manga, time_now));
        }

        return PluginResult::OK;
    }

    static std::unordered_set<std::string> get_lines_in_file(const Path &filepath) {
        std::unordered_set<std::string> 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<std::string> 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<Tab> &result_tabs) {
        if(get_config().local_manga_directory.empty()) {
            show_notification("QuickMedia", "local_manga_directory config is not set", Urgency::CRITICAL);
            return PluginResult::OK;
        }

        if(get_file_type(get_config().local_manga_directory) != FileType::DIRECTORY) {
            show_notification("QuickMedia", "local_manga_directory config is not set to a valid directory", Urgency::CRITICAL);
            return PluginResult::OK;
        }

        Path chapter_url = Path(get_config().local_manga_directory).join(content_url).join(args.url);
        std::vector<LocalMangaPage> pages = get_images_in_manga(chapter_url);
        result_tabs.push_back(Tab{nullptr, std::make_unique<LocalMangaImagesPage>(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;
    }

    bool LocalMangaChaptersPage::extract_id_from_url(const std::string &url, std::string &manga_id) const {
        manga_id = url;
        return true;
    }

    ImageResult LocalMangaImagesPage::get_number_of_images(int &num_images) {
        num_images = 0;
        ImageResult image_result = get_image_urls_for_chapter(url);
        if(image_result != ImageResult::OK) return image_result;
        num_images = chapter_image_urls.size();
        return ImageResult::OK;
    }

    ImageResult LocalMangaImagesPage::for_each_page_in_chapter(PageCallback callback) {
        ImageResult image_result = get_image_urls_for_chapter(url);
        if(image_result != ImageResult::OK) return image_result;
        for(const std::string &url : chapter_image_urls) {
            if(!callback(url))
                break;
        }
        return ImageResult::OK;
    }

    ImageResult LocalMangaImagesPage::get_image_urls_for_chapter(const std::string&) {
        if(!chapter_image_urls.empty())
            return ImageResult::OK;

        for(const LocalMangaPage &local_manga_page : pages) {
            chapter_image_urls.push_back(local_manga_page.path.data);
        }
        return ImageResult::OK;
    }
}