Skip to content

Commit

Permalink
urls to posts window finished
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmetmertguduz committed May 28, 2024
1 parent d766fdd commit 920d7d3
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/msg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ set(SOURCES
access_error_msg.c
color_comp_error_msg.c
window_create_error_msg.c
posts_not_array_error_msg.c
post_error_msg.c
)

add_library(yiffy-messages STATIC ${SOURCES})
20 changes: 20 additions & 0 deletions src/msg/post_error_msg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file post_error_msg.c
*
* @brief This file is used to output the error when the parsing is faced with an error in the post section.
*
* @author Mehmet Mert Gunduz ([email protected])
*
* @date 28/05/2024
*/

#include "yiffy_messages.h"

/**
* @brief Outputs the error when the parsing is faced with an error in the post section.
*/
void post_error_msg()
{
fprintf(stderr, "yiffy: post data is not in correct format, not able to parse JSON.\n");
fprintf(stderr, "yiffy: please try giving different tags.\n");
}
20 changes: 20 additions & 0 deletions src/msg/posts_not_array_error_msg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file posts_not_array_error_msg.c
*
* @brief This file is used to output the error when the parsing is faced with an error in the posts section.
*
* @author Mehmet Mert Gunduz ([email protected])
*
* @date 28/05/2024
*/

#include "yiffy_messages.h"

/**
* @brief Outputs the error when the parsing is faced with an error in the posts section.
*/
void posts_not_array_error_msg()
{
fprintf(stderr, "yiffy: format is not in array type, not able to parse JSON.\n");
fprintf(stderr, "yiffy: please try giving different tags.\n");
}
2 changes: 2 additions & 0 deletions src/msg/yiffy_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ void conf_successful_msg(char *option, char *argv);
void access_error_msg();
void color_comp_error_msg();
void window_create_error_msg();
void posts_not_array_error_msg();
void post_error_msg();

#endif /* YIFFY_MESSAGES_H */
1 change: 1 addition & 0 deletions src/search/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(SOURCES
create_post_tags_window.c
create_controls_window.c
search.c
write_post.c
)

add_library(yiffy-search STATIC ${SOURCES})
10 changes: 5 additions & 5 deletions src/search/create_posts_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
/// @brief Creates a new window to display posts and adds it to the specified position.
/// @param window A pointer to the WINDOW where posts will be displayed.
/// @param posts_panel_height A pointer to an integer representing the height of the posts panel.
void create_posts_window(WINDOW *window, int *posts_panel_height)
void create_posts_window(WINDOW **window, int *posts_panel_height)
{
/* Create the posts window until information, taking 12 lines from it because the info and controls window line is 12. */
*posts_panel_height = LINES - 12;

window = newwin(*posts_panel_height, COLS, 3, 0);
box(window, 0, 0);
*window = newwin(*posts_panel_height, COLS, 3, 0);
box(*window, 0, 0);

set_window_title(window, "POSTS");
set_window_title(*window, "POSTS");

refresh();
wrefresh(window);
wrefresh(*window);
}
82 changes: 80 additions & 2 deletions src/search/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#include <ncurses.h>

#include "yiffy_search.h"
#include "../fetch/yiffy_fetch.h"

#define MAX_FILE_PATH 256 ///< This macro is used to set the default size for getting the home directory file.
#define MAX_BUFFER_SIZE 512 ///< This macro is used to set the default size for reading the config file.
#define CONTENT_SIZE 524288 ///< This macro is used to set the default size for reading the JSON file that comes as a response from the API.

control controls[] =
{
Expand All @@ -36,6 +36,9 @@ bool is_nsfw;
/// @brief Height of the posts panel in the user interface.
int posts_panel_height;

/// @brief The string for storing posts.json data.
char json_string[CONTENT_SIZE];

/// @brief Creates a ncurses-based user interface to show, download, search and travel the e621/e926 by using curl api calls and aria2c tool.
/// @param tags These are the e621-e926 tags prompted by the user as an argument value. Example: yiffy --search "anthro+fur+male+smile".
void search(char *tags)
Expand Down Expand Up @@ -91,13 +94,88 @@ void search(char *tags)

/* Create the ncurses-based user interfaces. */
create_top_window(top_window, is_nsfw);
create_posts_window(posts_window, &posts_panel_height);
create_posts_window(&posts_window, &posts_panel_height);
create_post_tags_window(post_tags_window, &posts_panel_height);
create_controls_window(controls_window, &posts_panel_height, controls, sizeof(controls) / sizeof(controls[0]));

/* Download the first page of posts with prompted tags. */
aria2_download(tags, 1, is_nsfw, posts_panel_height);

/* Read the posts.json file. */
FILE *posts_json_file = fopen("posts.json", "r");

if (posts_json_file == NULL)
{
no_json_error_msg();
goto END;
}

fgets(json_string, CONTENT_SIZE - 1, posts_json_file);

/* Close the file after reading and putting the data to json_string. */
fclose(posts_json_file);

/* Parse the JSON. */
cJSON *json = cJSON_Parse(json_string);

if (json == NULL)
{
json_parse_error_msg();
goto END;
}

/* Get the posts array. */
cJSON *posts = cJSON_GetObjectItem(json, "posts");

if (!cJSON_IsArray(posts))
{
posts_not_array_error_msg();
goto END;
}

/* Counter for writing posts. */
size_t post_i = 0;

/* Write the posts and tags to windows. */
for (size_t i = 0; i < cJSON_GetArraySize(posts); i++)
{
/* Take the post with specified index. */
cJSON *post = cJSON_GetArrayItem(posts, i);

if (post == NULL)
{
post_error_msg();
goto END;
}

/* Take the file object of the specified post. */
cJSON *file_object = cJSON_GetObjectItem(post, "file");

if (file_object == NULL)
{
fprintf(stderr, "File object not found in post %d\n", i);
goto END;
}

/* Take the url of the of the specified post by parsing the data from file object. */
cJSON *url = cJSON_GetObjectItem(file_object, "url");

if (url != NULL && cJSON_IsString(url) && post_i != posts_panel_height - 2)
{
write_post(posts_window, post_i, url);

wrefresh(posts_window);

/* Increase post_i. */
post_i++;
}
}

wrefresh(posts_window);
refresh();

END:
cJSON_Delete(json);
getch();
endwin();
}
33 changes: 33 additions & 0 deletions src/search/write_post.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @file write_post.c
*
* @brief This file is used to write posts to the posts window.
*
* @author Mehmet Mert Gunduz ([email protected])
*
* @date 28/05/2024
*/

#include "yiffy_search.h"

/// @brief Writes posts to the posts window.
/// @param window Posts window.
/// @param post_i The index of the post.
/// @param url The url of the post with specified index.
void write_post(WINDOW *window, size_t post_i, cJSON *url)
{
/* Make the first post blinking. */
if (post_i == 0)
{
wattron(window, A_STANDOUT);
}

/* Write the post URL. */
mvwprintw(window, post_i + 1, 1, "%s", url->valuestring);

/* Close the post blinking. */
if (post_i == 0)
{
wattroff(window, A_STANDOUT);
}
}
6 changes: 5 additions & 1 deletion src/search/yiffy_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdlib.h>
#include <time.h>

#include "../fetch/yiffy_fetch.h"
#include "../msg/yiffy_messages.h"

/// @brief Stores the control name and key for displaying in the controls panel.
Expand All @@ -32,11 +33,14 @@ void init_ncurses();

/* User Interface creation functions. */
void create_top_window(WINDOW *window, bool is_nsfw);
void create_posts_window(WINDOW *window, int *posts_panel_height);
void create_posts_window(WINDOW **window, int *posts_panel_height);
void create_post_tags_window(WINDOW *window, int *posts_panel_height);
void create_controls_window(WINDOW *window, int *posts_panel_height, control *controls, int total_controls);

/* Function for writing the titles to windows. */
void set_window_title(WINDOW *window, const char *title);

/* Function for writing the posts to posts window. */
void write_post(WINDOW *window, size_t post_i, cJSON *url);

#endif /* YIFFY_UI_H */

0 comments on commit 920d7d3

Please sign in to comment.