aboutsummaryrefslogtreecommitdiff
path: root/src/QuickMedia.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-11-23 18:54:18 +0100
committerdec05eba <dec05eba@protonmail.com>2022-11-23 18:54:22 +0100
commit1de2ff02bb746607727900180b6f32ded0cd7856 (patch)
tree0f9f634674d5f48a65e84c8067461e53d83700e2 /src/QuickMedia.cpp
parent89c41c1488854858e02ff6bd48a6518161fa05a5 (diff)
Allow opening 4chan post directly
Diffstat (limited to 'src/QuickMedia.cpp')
-rw-r--r--src/QuickMedia.cpp123
1 files changed, 58 insertions, 65 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index 2e7feff..ca69248 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -368,44 +368,6 @@ namespace QuickMedia {
return true;
}
- // |comment_id| is optional
- static bool fourchan_extract_url(const std::string &url, std::string &board_id, std::string &thread_id, std::string &comment_id) {
- size_t len = 10;
- size_t index = url.find("4chan.org/");
- if(index == std::string::npos) {
- len = 13;
- index = url.find("4channel.org/");
- }
-
- if(index == std::string::npos)
- return false;
-
- index += len;
- size_t board_end = url.find('/', index);
- if(board_end == std::string::npos)
- return false;
-
- board_id = url.substr(index, board_end - index);
- index = board_end + 1;
-
- const std::string_view remaining(url.data() + index, url.size() - index);
- if(remaining.size() <= 7 || remaining.substr(0, 7) != "thread/")
- return false;
-
- index += 7;
- size_t thread_id_end = url.find('#', index);
- if(thread_id_end == std::string::npos)
- thread_id_end = url.size();
-
- thread_id = url.substr(index, thread_id_end - index);
- if(thread_id.empty())
- return false;
-
- index = thread_id_end;
- comment_id = url.substr(index);
- return true;
- }
-
int Program::run(int argc, char **argv) {
mgl_init();
@@ -1330,11 +1292,10 @@ namespace QuickMedia {
tabs.push_back(Tab{std::move(categories_sukebei_body), std::make_unique<NyaaSiCategoryPage>(this, true), create_search_bar("Search...", SEARCH_DELAY_FILTER)});
} else if(strcmp(plugin_name, "4chan") == 0) {
if(launch_url_type == LaunchUrlType::FOURCHAN_THREAD) {
- // TODO: Use comment id
- std::string board_id, thread_id, comment_id;
- fourchan_extract_url(launch_url, board_id, thread_id, comment_id);
+ std::string board_id, thread_id, post_id;
+ fourchan_extract_url(launch_url, board_id, thread_id, post_id);
auto body = create_body();
- auto thread_page = std::make_unique<FourchanThreadPage>(this, std::move(board_id), std::move(thread_id), "");
+ auto thread_page = std::make_unique<FourchanThreadPage>(this, std::move(board_id), std::move(thread_id), std::move(post_id), "");
page_stack.push(current_page);
current_page = PageType::IMAGE_BOARD_THREAD;
image_board_thread_page(thread_page.get(), body.get());
@@ -3461,8 +3422,6 @@ namespace QuickMedia {
const bool is_resume_go_back = !start_time.empty();
if(start_time.empty())
start_time = video_page->get_url_timestamp();
-
- fprintf(stderr, "start time: %s\n", start_time.c_str());
watched_videos.insert(video_page->get_url());
// TODO: Sync sequences
@@ -4553,6 +4512,46 @@ namespace QuickMedia {
thread_body->set_items(std::move(result_items));
+ std::deque<int> comment_navigation_stack;
+ std::deque<int> comment_page_scroll_stack;
+
+ auto focus_selected_post = [&]() {
+ BodyItem *selected_item = thread_body->get_selected();
+ if(!selected_item)
+ return;
+
+ thread_body->for_each_item([](std::shared_ptr<BodyItem> &body_item) {
+ body_item->visible = false;
+ });
+
+ selected_item->visible = true;
+ ImageBoardBodyItemData *image_board_post_data = static_cast<ImageBoardBodyItemData*>(selected_item->extra.get());
+
+ for(size_t reply_to_index : image_board_post_data->replies_to) {
+ thread_body->get_item_by_index(reply_to_index)->visible = true;
+ }
+
+ for(size_t reply_index : image_board_post_data->replies) {
+ thread_body->get_item_by_index(reply_index)->visible = true;
+ }
+
+ comment_navigation_stack.push_back(thread_body->get_selected_item());
+ comment_page_scroll_stack.push_back(thread_body->get_page_scroll());
+ //thread_body->clamp_selection();
+ thread_body->set_page_scroll(0.0f);
+ };
+
+ int64_t navigate_to_post_id = 0;
+ if(!thread_page->post_id.empty() && to_num(thread_page->post_id.c_str(), thread_page->post_id.size(), navigate_to_post_id)) {
+ const int found_body_item_index = thread_body->find_item_index([&](auto &body_item) {
+ const ImageBoardBodyItemData *image_board_post_data = static_cast<ImageBoardBodyItemData*>(body_item->extra.get());
+ return image_board_post_data->post_id == navigate_to_post_id;
+ });
+
+ if(found_body_item_index != -1)
+ thread_body->set_selected_item(found_body_item_index);
+ }
+
// TODO: Instead of using stage here, use different pages for each stage
enum class NavigationStage {
VIEWING_COMMENTS,
@@ -4713,9 +4712,6 @@ namespace QuickMedia {
mgl::vec2f body_size;
mgl::Event event;
- std::deque<int> comment_navigation_stack;
- std::deque<int> comment_page_scroll_stack;
-
bool moving_captcha_left = false;
bool moving_captcha_right = false;
@@ -4866,21 +4862,9 @@ namespace QuickMedia {
}
BodyItem *selected_item = thread_body->get_selected();
- if(event.key.code == mgl::Keyboard::Enter && selected_item && (comment_navigation_stack.empty() || thread_body->get_selected_item() != comment_navigation_stack.back()) && (!selected_item->replies_to.empty() || !selected_item->replies.empty())) {
- thread_body->for_each_item([](std::shared_ptr<BodyItem> &body_item) {
- body_item->visible = false;
- });
- selected_item->visible = true;
- for(size_t reply_to_index : selected_item->replies_to) {
- thread_body->get_item_by_index(reply_to_index)->visible = true;
- }
- for(size_t reply_index : selected_item->replies) {
- thread_body->get_item_by_index(reply_index)->visible = true;
- }
- comment_navigation_stack.push_back(thread_body->get_selected_item());
- comment_page_scroll_stack.push_back(thread_body->get_page_scroll());
- //thread_body->clamp_selection();
- thread_body->set_page_scroll(0.0f);
+ ImageBoardBodyItemData *image_board_post_data = static_cast<ImageBoardBodyItemData*>(selected_item->extra.get());
+ if(event.key.code == mgl::Keyboard::Enter && selected_item && (comment_navigation_stack.empty() || thread_body->get_selected_item() != comment_navigation_stack.back()) && (!image_board_post_data->replies_to.empty() || !image_board_post_data->replies.empty())) {
+ focus_selected_post();
} else if(event.key.code == mgl::Keyboard::Backspace && !comment_navigation_stack.empty()) {
size_t previous_selected = comment_navigation_stack.back();
float previous_page_scroll = comment_page_scroll_stack.back();
@@ -4899,17 +4883,17 @@ namespace QuickMedia {
thread_body->set_selected_item(previous_selected);
selected_item = thread_body->get_item_by_index(comment_navigation_stack.back()).get();
selected_item->visible = true;
- for(size_t reply_to_index : selected_item->replies_to) {
+ for(size_t reply_to_index : image_board_post_data->replies_to) {
thread_body->get_item_by_index(reply_to_index)->visible = true;
}
- for(size_t reply_index : selected_item->replies) {
+ for(size_t reply_index : image_board_post_data->replies) {
thread_body->get_item_by_index(reply_index)->visible = true;
}
thread_body->clamp_selection();
}
thread_body->set_page_scroll(previous_page_scroll);
} else if(event.key.code == mgl::Keyboard::R && selected_item) {
- std::string text_to_add = ">>" + selected_item->post_number + "\n";
+ std::string text_to_add = ">>" + std::to_string(image_board_post_data->post_id) + "\n";
comment_input.insert_text_at_caret_position(std::move(text_to_add));
comment_input.move_caret_to_end();
}
@@ -6578,6 +6562,7 @@ namespace QuickMedia {
std::string youtube_channel_id;
std::string youtube_channel_url;
std::string video_id;
+ std::string board_id, thread_id, post_id;
if(youtube_url_extract_channel_id(url, youtube_channel_id, youtube_channel_url)) {
std::vector<Tab> tabs;
YoutubeChannelPage::create_each_type(this, youtube_channel_url, "", "Channel", tabs);
@@ -6593,6 +6578,14 @@ namespace QuickMedia {
video_content_page(matrix_chat_page, youtube_video_page.get(), "", false, tabs[MESSAGES_TAB_INDEX].body.get(), tabs[MESSAGES_TAB_INDEX].body->get_selected_item());
redraw = true;
avatar_applied = false;
+ } else if(fourchan_extract_url(url, board_id, thread_id, post_id)) {
+ auto body = create_body();
+ auto thread_page = std::make_unique<FourchanThreadPage>(this, std::move(board_id), std::move(thread_id), std::move(post_id), "");
+ page_stack.push(current_page);
+ current_page = PageType::IMAGE_BOARD_THREAD;
+ image_board_thread_page(thread_page.get(), body.get());
+ redraw = true;
+ avatar_applied = false;
} else {
const char *launch_program = "xdg-open";
if(!is_program_executable_by_name("xdg-open")) {