From d7a681b69153ea99f03c128fce5d297ff1f635f6 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 5 Jun 2021 17:36:54 +0200 Subject: Fix incorrect times, add time to manga history --- src/Body.cpp | 8 +----- src/QuickMedia.cpp | 59 +++++++++++++++++++++++++++------------------ src/Storage.cpp | 9 +++++++ src/plugins/FileManager.cpp | 34 ++++++++++---------------- src/plugins/NyaaSi.cpp | 2 +- src/plugins/Soundcloud.cpp | 2 +- src/plugins/Youtube.cpp | 2 +- 7 files changed, 61 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/Body.cpp b/src/Body.cpp index 01ae8b7..4833741 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -841,13 +841,7 @@ namespace QuickMedia { //bool is_same_year = message_tm->tm_year == now_tm->tm_year; char time_str[128] = {0}; - /* - if(is_same_year) - strftime(time_str, sizeof(time_str) - 1, "%a %b %d %H:%M:%S", message_tm); - else - strftime(time_str, sizeof(time_str) - 1, "%a %b %d %H:%M:%S %Y", message_tm); - */ - strftime(time_str, sizeof(time_str) - 1, "%a %b %d %H:%M", &message_tm); + strftime(time_str, sizeof(time_str) - 1, "%Y %b %d, %a %H:%M", &message_tm); if(body_item->timestamp_text) { body_item->timestamp_text->setString(time_str); diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 48d73d5..dccdf11 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -1286,7 +1286,6 @@ namespace QuickMedia { return std::to_string(seconds) + " second" + (seconds == 1 ? "" : "s") + " ago"; } - // TODO: Make asynchronous static void fill_history_items_from_json(const Json::Value &history_json, BodyItems &history_items) { assert(history_json.isArray()); @@ -1355,8 +1354,9 @@ namespace QuickMedia { show_notification("QuickMedia", "Failed to create directory: " + content_storage_dir.data, Urgency::CRITICAL); exit(1); } - // TODO: Make asynchronous - for_files_in_dir_sort_last_modified(content_storage_dir, [&history_items, plugin_name](const std::filesystem::path &filepath) { + + time_t now = time(NULL); + for_files_in_dir_sort_last_modified(content_storage_dir, [&history_items, plugin_name, now](const std::filesystem::path &filepath) { // This can happen when QuickMedia crashes/is killed while writing to storage. // In that case, the storage wont be corrupt but there will be .tmp files. // TODO: Remove these .tmp files if they exist during startup @@ -1373,28 +1373,39 @@ namespace QuickMedia { // TODO: Manga combined auto filename = filepath.filename(); + if(filename.empty()) + return true; + const Json::Value &manga_name = body["name"]; - if(!filename.empty() && manga_name.isString()) { - // TODO: Add thumbnail - auto body_item = BodyItem::create(manga_name.asString()); - if(strcmp(plugin_name, "manganelo") == 0) - body_item->url = "https://manganelo.com/manga/" + base64_decode(filename.string()); - else if(strcmp(plugin_name, "manganelos") == 0) - body_item->url = "http://manganelos.com/manga/" + base64_decode(filename.string()); - else if(strcmp(plugin_name, "mangadex") == 0) - body_item->url = base64_decode(filename.string()); - else if(strcmp(plugin_name, "mangatown") == 0) - body_item->url = "https://mangatown.com/manga/" + base64_decode(filename.string()); - else if(strcmp(plugin_name, "mangakatana") == 0) - body_item->url = "https://mangakatana.com/manga/" + base64_decode(filename.string()); - else if(strcmp(plugin_name, "onimanga") == 0) - body_item->url = "https://onimanga.com/" + base64_decode(filename.string()); - else if(strcmp(plugin_name, "readm") == 0) - body_item->url = "https://readm.org/manga/" + base64_decode(filename.string()); - else - fprintf(stderr, "Error: Not implemented: filename to manga chapter list\n"); - history_items.push_back(std::move(body_item)); - } + if(!manga_name.isString()) + return true; + + time_t last_modified_time = 0; + file_get_last_modified_time_seconds(filepath.c_str(), &last_modified_time); + + // TODO: Add thumbnail + auto body_item = BodyItem::create(manga_name.asString()); + body_item->set_description("Last read " + seconds_to_relative_time_str(now - last_modified_time)); + body_item->set_description_color(sf::Color(179, 179, 179)); + + if(strcmp(plugin_name, "manganelo") == 0) + body_item->url = "https://manganelo.com/manga/" + base64_decode(filename.string()); + else if(strcmp(plugin_name, "manganelos") == 0) + body_item->url = "http://manganelos.com/manga/" + base64_decode(filename.string()); + else if(strcmp(plugin_name, "mangadex") == 0) + body_item->url = base64_decode(filename.string()); + else if(strcmp(plugin_name, "mangatown") == 0) + body_item->url = "https://mangatown.com/manga/" + base64_decode(filename.string()); + else if(strcmp(plugin_name, "mangakatana") == 0) + body_item->url = "https://mangakatana.com/manga/" + base64_decode(filename.string()); + else if(strcmp(plugin_name, "onimanga") == 0) + body_item->url = "https://onimanga.com/" + base64_decode(filename.string()); + else if(strcmp(plugin_name, "readm") == 0) + body_item->url = "https://readm.org/manga/" + base64_decode(filename.string()); + else + fprintf(stderr, "Error: Not implemented: filename to manga chapter list\n"); + + history_items.push_back(std::move(body_item)); return true; }); } diff --git a/src/Storage.cpp b/src/Storage.cpp index 85a150c..7103b7a 100644 --- a/src/Storage.cpp +++ b/src/Storage.cpp @@ -118,6 +118,15 @@ namespace QuickMedia { return -1; } + bool file_get_last_modified_time_seconds(const char *path, time_t *result) { + struct stat file_stat; + if(stat(path, &file_stat) == 0) { + *result = file_stat.st_mtim.tv_sec; + return true; + } + return false; + } + static int file_overwrite(const Path &path, const char *str, size_t size) { FILE *file = fopen(path.data.c_str(), "wb"); if(!file) { diff --git a/src/plugins/FileManager.cpp b/src/plugins/FileManager.cpp index 04e284a..6532f3a 100644 --- a/src/plugins/FileManager.cpp +++ b/src/plugins/FileManager.cpp @@ -25,15 +25,6 @@ namespace QuickMedia { return ""; } - static std::filesystem::file_time_type file_get_last_modified_time(const std::filesystem::directory_entry &path, std::filesystem::file_time_type default_value) { - std::error_code err; - auto last_write_time = path.last_write_time(err); - if(err) - return default_value; - else - return last_write_time; - } - PluginResult FileManagerPage::submit(const std::string&, const std::string &url, std::vector &result_tabs) { std::filesystem::path new_path; if(url == "..") @@ -72,18 +63,20 @@ namespace QuickMedia { } PluginResult FileManagerPage::get_files_in_directory(BodyItems &result_items) { - std::vector paths; + std::vector> paths; try { for(auto &p : std::filesystem::directory_iterator(current_dir)) { - paths.push_back(p); + time_t last_modified_time = 0; + file_get_last_modified_time_seconds(p.path().c_str(), &last_modified_time); + paths.push_back(std::make_pair(p, last_modified_time)); } } catch(const std::filesystem::filesystem_error &err) { fprintf(stderr, "Failed to list files in directory %s, error: %s\n", current_dir.c_str(), err.what()); return PluginResult::ERR; } - std::sort(paths.begin(), paths.end(), [](const std::filesystem::directory_entry &path1, std::filesystem::directory_entry &path2) { - return file_get_last_modified_time(path1, std::filesystem::file_time_type::min()) > file_get_last_modified_time(path2, std::filesystem::file_time_type::min()); + std::sort(paths.begin(), paths.end(), [](const std::pair &path1, std::pair &path2) { + return path1.second > path2.second; }); if(current_dir != "/") { @@ -95,12 +88,12 @@ namespace QuickMedia { char time_str[128] = {0}; for(auto &p : paths) { std::error_code regular_file_err; - bool is_regular_file = p.is_regular_file(regular_file_err); + bool is_regular_file = p.first.is_regular_file(regular_file_err); if(regular_file_err) is_regular_file = true; // TODO: Check file magic number instead of extension? - const char *ext = get_ext(p.path()); + const char *ext = get_ext(p.first.path()); FileManagerMimeType file_mime_type = FILE_MANAGER_MIME_TYPE_OTHER; if(is_regular_file) { if(is_image_ext(ext)) @@ -112,11 +105,11 @@ namespace QuickMedia { if(is_regular_file && !(mime_type & file_mime_type)) continue; - auto body_item = BodyItem::create(p.path().filename().string()); + auto body_item = BodyItem::create(p.first.path().filename().string()); body_item->url = body_item->get_title(); if(file_mime_type == FILE_MANAGER_MIME_TYPE_IMAGE || file_mime_type == FILE_MANAGER_MIME_TYPE_VIDEO) { body_item->thumbnail_is_local = true; - body_item->thumbnail_url = p.path().string(); + body_item->thumbnail_url = p.first.path().string(); } else { body_item->thumbnail_is_local = true; if(is_regular_file) { @@ -128,13 +121,12 @@ namespace QuickMedia { } } - time_t last_modified_time = std::chrono::duration_cast(file_get_last_modified_time(p, std::filesystem::file_time_type::min()).time_since_epoch()).count(); struct tm last_modified_tm; - localtime_r(&last_modified_time, &last_modified_tm); - strftime(time_str, sizeof(time_str) - 1, "%a %b %d %H:%M", &last_modified_tm); + localtime_r(&p.second, &last_modified_tm); + strftime(time_str, sizeof(time_str) - 1, "%Y %b %d, %a %H:%M", &last_modified_tm); std::error_code file_size_err; - size_t file_size = p.file_size(file_size_err); + size_t file_size = p.first.file_size(file_size_err); if(file_size_err) file_size = 0; diff --git a/src/plugins/NyaaSi.cpp b/src/plugins/NyaaSi.cpp index a8cd2b4..5d9e41b 100644 --- a/src/plugins/NyaaSi.cpp +++ b/src/plugins/NyaaSi.cpp @@ -103,7 +103,7 @@ namespace QuickMedia { struct tm time_tm; localtime_r(&unix_time, &time_tm); char time_str[128] = {0}; - strftime(time_str, sizeof(time_str) - 1, "%Y-%m-%d %H:%M", &time_tm); + strftime(time_str, sizeof(time_str) - 1, "%Y %b %d, %a %H:%M", &time_tm); return time_str; } diff --git a/src/plugins/Soundcloud.cpp b/src/plugins/Soundcloud.cpp index f4d01b6..e66f1f4 100644 --- a/src/plugins/Soundcloud.cpp +++ b/src/plugins/Soundcloud.cpp @@ -69,7 +69,7 @@ namespace QuickMedia { struct tm time_tm; localtime_r(&unix_time, &time_tm); char time_str[128] = {0}; - strftime(time_str, sizeof(time_str) - 1, "%Y-%m-%d %H:%M", &time_tm); + strftime(time_str, sizeof(time_str) - 1, "%Y %b %d, %a %H:%M", &time_tm); return time_str; } diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index 9b7c5ba..d9df409 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -157,7 +157,7 @@ namespace QuickMedia { struct tm message_tm; localtime_r(&start_time, &message_tm); char time_str[128] = {0}; - strftime(time_str, sizeof(time_str) - 1, "%a %b %d %H:%M", &message_tm); + strftime(time_str, sizeof(time_str) - 1, "%Y %b %d, %a %H:%M", &message_tm); string_replace_all(upcoming_event_text.value(), "DATE_PLACEHOLDER", time_str); scheduled_text = std::move(upcoming_event_text.value()); } -- cgit v1.2.3