Multiple bug and leaks fixes.

This commit is contained in:
sergiotarxz 2021-11-07 17:59:43 +01:00
parent 5287d215dc
commit d91cd296a9
6 changed files with 60 additions and 15 deletions

View File

@ -28,11 +28,15 @@ G_DEFINE_TYPE (MgBackendReadmng, mg_backend_readmng, G_TYPE_OBJECT)
static GParamSpec *mg_backend_readmng_properties[MG_BACKEND_READMNG_N_PROPERTIES] = { NULL, }; static GParamSpec *mg_backend_readmng_properties[MG_BACKEND_READMNG_N_PROPERTIES] = { NULL, };
static void
mg_backend_readmng_dispose (GObject *object);
static void static void
mg_backend_readmng_class_init (MgBackendReadmngClass *class) { mg_backend_readmng_class_init (MgBackendReadmngClass *class) {
GObjectClass *object_class = G_OBJECT_CLASS (class); GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->set_property = mg_backend_readmng_set_property; object_class->set_property = mg_backend_readmng_set_property;
object_class->get_property = mg_backend_readmng_get_property; object_class->get_property = mg_backend_readmng_get_property;
object_class->dispose = mg_backend_readmng_dispose;
mg_backend_readmng_properties[MG_BACKEND_READMNG_BASE_URL] = g_param_spec_string ("base_url", mg_backend_readmng_properties[MG_BACKEND_READMNG_BASE_URL] = g_param_spec_string ("base_url",
"BaseURL", "BaseURL",
@ -89,6 +93,16 @@ mg_backend_readmng_init (MgBackendReadmng *self) {
} }
self->xml_utils = mg_util_xml_new (); self->xml_utils = mg_util_xml_new ();
} }
static void
mg_backend_readmng_dispose (GObject *object) {
MgBackendReadmng *self = MG_BACKEND_READMNG (object);
g_clear_object (&self->xml_utils);
if (self->main_page_html) {
g_free (self->main_page_html);
self->main_page_html = NULL;
}
G_OBJECT_CLASS (mg_backend_readmng_parent_class)->dispose (object);
}
char * char *
mg_backend_readmng_get_base_url (MgBackendReadmng *self) { mg_backend_readmng_get_base_url (MgBackendReadmng *self) {
@ -139,6 +153,8 @@ mg_backend_readmng_get_featured_manga (MgBackendReadmng *self) {
xmlDocPtr html_document; xmlDocPtr html_document;
html_document = mg_backend_readmng_fetch_xml_main_page (self); html_document = mg_backend_readmng_fetch_xml_main_page (self);
mangas = mg_backend_readmng_parse_main_page (self, html_document); mangas = mg_backend_readmng_parse_main_page (self, html_document);
xmlFreeDoc (html_document);
return mangas; return mangas;
} }
@ -157,7 +173,7 @@ mg_backend_readmng_retrieve_manga_details (MgBackendReadmng *self,
size_t movie_detail_len = 0; size_t movie_detail_len = 0;
if (mg_manga_has_details (manga)) { if (mg_manga_has_details (manga)) {
return; goto cleanup_mg_backend_readmng_retrieve_manga_details;
} }
xml_utils = self->xml_utils; xml_utils = self->xml_utils;
@ -168,7 +184,7 @@ mg_backend_readmng_retrieve_manga_details (MgBackendReadmng *self,
node_set = xpath_result->nodesetval; node_set = xpath_result->nodesetval;
if (!node_set) { if (!node_set) {
fprintf(stderr, "No match\n"); fprintf(stderr, "No match\n");
return; goto cleanup_mg_backend_readmng_retrieve_manga_details;
} }
for (int i = 0; i < node_set->nodeNr; i++) { for (int i = 0; i < node_set->nodeNr; i++) {
xmlNodePtr node = node_set->nodeTab[i]; xmlNodePtr node = node_set->nodeTab[i];
@ -182,6 +198,8 @@ mg_backend_readmng_retrieve_manga_details (MgBackendReadmng *self,
manga_chapters = mg_backend_readmng_recover_chapter_list (self, html_document); manga_chapters = mg_backend_readmng_recover_chapter_list (self, html_document);
mg_manga_set_chapter_list (manga, manga_chapters); mg_manga_set_chapter_list (manga, manga_chapters);
mg_manga_details_recovered (manga); mg_manga_details_recovered (manga);
cleanup_mg_backend_readmng_retrieve_manga_details:
g_free (movie_detail);
} }
static GListStore * static GListStore *
@ -201,7 +219,7 @@ mg_backend_readmng_recover_chapter_list (MgBackendReadmng *self,
node_set = xpath_result->nodesetval; node_set = xpath_result->nodesetval;
if (!node_set) { if (!node_set) {
fprintf(stderr, "No matching ul\n"); fprintf(stderr, "No matching ul\n");
return return_value; goto cleanup_mg_backend_readmng_recover_chapter_list;
} }
for (int i = 0; i < node_set->nodeNr; i++) { for (int i = 0; i < node_set->nodeNr; i++) {
xmlNodePtr node = node_set->nodeTab[i]; xmlNodePtr node = node_set->nodeTab[i];
@ -210,7 +228,7 @@ mg_backend_readmng_recover_chapter_list (MgBackendReadmng *self,
} }
if (!ul_len) { if (!ul_len) {
fprintf(stderr, "No matching chp_lst\n"); fprintf(stderr, "No matching chp_lst\n");
return return_value; goto cleanup_mg_backend_readmng_recover_chapter_list;
} }
ul = uls[0]; ul = uls[0];
for (xmlNodePtr li = ul->children; li; li = li->next) { for (xmlNodePtr li = ul->children; li; li = li->next) {
@ -221,6 +239,10 @@ mg_backend_readmng_recover_chapter_list (MgBackendReadmng *self,
} }
} }
} }
cleanup_mg_backend_readmng_recover_chapter_list:
if (uls) {
g_free (uls);
}
return return_value; return return_value;
} }
@ -282,6 +304,7 @@ mg_backend_readmng_fetch_xml_details (MgBackendReadmng *self,
char *html_response = mg_util_soup_get_request (util_soup, char *html_response = mg_util_soup_get_request (util_soup,
request_url, &response_len); request_url, &response_len);
g_object_unref (util_soup);
return htmlReadMemory (html_response, response_len, NULL, NULL, return htmlReadMemory (html_response, response_len, NULL, NULL,
HTML_PARSE_RECOVER | HTML_PARSE_NODEFDTD HTML_PARSE_RECOVER | HTML_PARSE_NODEFDTD
| HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING ); | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING );
@ -307,6 +330,7 @@ mg_backend_readmng_get_main_page (MgBackendReadmng *self, size_t *len) {
MgUtilSoup *util_soup = mg_util_soup_new (); MgUtilSoup *util_soup = mg_util_soup_new ();
self->main_page_html = mg_util_soup_get_request (util_soup, self->main_page_html = mg_util_soup_get_request (util_soup,
self->base_url, &self->main_page_html_len); self->base_url, &self->main_page_html_len);
g_object_unref (util_soup);
} }
if (len) { if (len) {
*len = self->main_page_html_len; *len = self->main_page_html_len;
@ -329,6 +353,7 @@ mg_backend_readmng_parse_main_page (MgBackendReadmng *self, const xmlDocPtr html
mangas, current_li); mangas, current_li);
} }
g_free (li);
return mangas; return mangas;
} }
@ -368,7 +393,7 @@ mg_backend_readmng_retrieve_slides (MgBackendReadmng *self, const xmlDocPtr html
node_set = xpath_result->nodesetval; node_set = xpath_result->nodesetval;
if (!node_set) { if (!node_set) {
fprintf(stderr, "No match\n"); fprintf(stderr, "No match\n");
return NULL; goto cleanup_mg_backend_readmng_retrieve_slides;
} }
for (int i = 0; i < node_set->nodeNr; i++) { for (int i = 0; i < node_set->nodeNr; i++) {
xmlNodePtr node = node_set->nodeTab[i]; xmlNodePtr node = node_set->nodeTab[i];
@ -381,6 +406,8 @@ mg_backend_readmng_retrieve_slides (MgBackendReadmng *self, const xmlDocPtr html
if (xpath_result) { if (xpath_result) {
xmlXPathFreeObject(xpath_result); xmlXPathFreeObject(xpath_result);
} }
cleanup_mg_backend_readmng_retrieve_slides:
g_free (nodes);
return slides; return slides;
} }

View File

@ -36,14 +36,15 @@ mg_util_xml_new () {
xmlNodePtr * xmlNodePtr *
mg_util_xml_find_class (MgUtilXML *self, xmlNodePtr node, char *class, mg_util_xml_find_class (MgUtilXML *self, xmlNodePtr node, char *class,
size_t *len, xmlNodePtr *nodes, int return_on_first) { size_t *len, xmlNodePtr *nodes, int return_on_first) {
char *attr = NULL;
for (xmlNodePtr child = node->children; child; child=child->next) { for (xmlNodePtr child = node->children; child; child=child->next) {
char *attr = mg_util_xml_get_attr (self, child, "class"); attr = mg_util_xml_get_attr (self, child, "class");
if (attr && mg_util_xml_has_class (self, attr, class)) { if (attr && mg_util_xml_has_class (self, attr, class)) {
(*len)++; (*len)++;
nodes = g_realloc (nodes, sizeof *nodes * *len); nodes = g_realloc (nodes, sizeof *nodes * *len);
nodes[*len-1] = child; nodes[*len-1] = child;
if (return_on_first) { if (return_on_first) {
return nodes; goto cleanup_mg_util_xml_find_class;
} }
} }
if (node->children) { if (node->children) {
@ -52,11 +53,15 @@ mg_util_xml_find_class (MgUtilXML *self, xmlNodePtr node, char *class,
nodes = mg_util_xml_find_class (self, child, class, nodes = mg_util_xml_find_class (self, child, class,
len, nodes, return_on_first); len, nodes, return_on_first);
if (*len) { if (*len) {
return nodes; goto cleanup_mg_util_xml_find_class;
} }
} }
} }
} }
cleanup_mg_util_xml_find_class:
if (attr) {
g_free (attr);
}
return nodes; return nodes;
} }

View File

@ -26,7 +26,9 @@ reverse_list (GtkButton *reverse_button,
g_list_store_append (new_model, MG_MANGA_CHAPTER g_list_store_append (new_model, MG_MANGA_CHAPTER
(g_list_model_get_item (G_LIST_MODEL (model), i))); (g_list_model_get_item (G_LIST_MODEL (model), i)));
} }
gtk_single_selection_set_model (selection, G_LIST_MODEL (new_model)); GtkSingleSelection *new_selection = gtk_single_selection_new
(G_LIST_MODEL (new_model));
gtk_list_view_set_model (list_view, GTK_SELECTION_MODEL (new_selection));
g_object_unref (G_OBJECT (model)); g_object_unref (G_OBJECT (model));
} }
@ -62,8 +64,9 @@ create_detail_view (MgManga *manga) {
GtkListView *chapter_list = NULL; GtkListView *chapter_list = NULL;
GtkPicture *manga_image = create_picture_from_url ( GtkPicture *manga_image = create_picture_from_url (
mg_manga_get_image_url(manga), 200); mg_manga_get_image_url(manga), 200);
char *manga_title_text = mg_manga_get_title (manga);
char *title_text = mg_util_xml_get_title_text ( char *title_text = mg_util_xml_get_title_text (
xml_util, mg_manga_get_title (manga)); xml_util, manga_title_text);
char *description_text; char *description_text;
scroll = gtk_scrolled_window_new (); scroll = gtk_scrolled_window_new ();
@ -98,8 +101,12 @@ create_detail_view (MgManga *manga) {
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_box_append (detail_view, GTK_WIDGET (toggle_folded_button)); gtk_box_append (detail_view, GTK_WIDGET (toggle_folded_button));
gtk_box_append (detail_view, GTK_WIDGET (reverse_list_button));
gtk_box_append (detail_view, scroll); gtk_box_append (detail_view, scroll);
gtk_box_append (detail_view, GTK_WIDGET (reverse_list_button));
g_clear_object (&readmng);
g_free (manga_title_text);
g_free (title_text);
g_free (description_text);
return detail_view; return detail_view;
} }

View File

@ -50,14 +50,16 @@ setup_list_view_mangas (GtkSignalListItemFactory *factory,
gpointer user_data) { gpointer user_data) {
MgManga *manga = gtk_list_item_get_item (list_item); MgManga *manga = gtk_list_item_get_item (list_item);
GtkBox *box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0)); GtkBox *box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
char *manga_title = mg_manga_get_title (manga);
GtkWidget *label = gtk_label_new (mg_manga_get_title (manga)); GtkWidget *label = gtk_label_new (manga_title);
GtkWidget *picture = GTK_WIDGET ( GtkWidget *picture = GTK_WIDGET (
create_picture_from_url (mg_manga_get_image_url(manga), 200)); create_picture_from_url (mg_manga_get_image_url(manga), 200));
gtk_box_append (box, picture); gtk_box_append (box, picture);
gtk_box_append (box, label); gtk_box_append (box, label);
gtk_list_item_set_child (list_item, GTK_WIDGET (box)); gtk_list_item_set_child (list_item, GTK_WIDGET (box));
g_free (manga_title);
} }
GtkListView * GtkListView *

View File

@ -44,6 +44,7 @@ activate (AdwApplication *app,
gtk_box_append (box, GTK_WIDGET (views_leaflet)); gtk_box_append (box, GTK_WIDGET (views_leaflet));
gtk_widget_show (window); gtk_widget_show (window);
g_clear_object (&readmng);
} }
static GtkBox * static GtkBox *

View File

@ -7,7 +7,7 @@
GtkPicture * GtkPicture *
create_picture_from_url (char *url, gint picture_height) { create_picture_from_url (char *url, gint picture_height) {
GtkPicture *picture; GtkPicture *picture = NULL;
GFileIOStream *iostream; GFileIOStream *iostream;
GFile *tmp_image; GFile *tmp_image;
GError *error = NULL; GError *error = NULL;
@ -21,16 +21,19 @@ create_picture_from_url (char *url, gint picture_height) {
tmp_image = g_file_new_tmp ("mangareadertmpfileXXXXXX", &iostream, &error); tmp_image = g_file_new_tmp ("mangareadertmpfileXXXXXX", &iostream, &error);
if (error) { if (error) {
fprintf (stderr, "Unable to read file: %s\n", error->message); fprintf (stderr, "Unable to read file: %s\n", error->message);
return NULL; goto cleanup_create_picture_from_url;
} }
error = NULL; error = NULL;
g_output_stream_write (g_io_stream_get_output_stream (G_IO_STREAM (iostream)), g_output_stream_write (g_io_stream_get_output_stream (G_IO_STREAM (iostream)),
downloaded_image, size_downloaded_image, NULL, &error); downloaded_image, size_downloaded_image, NULL, &error);
if (error) { if (error) {
fprintf (stderr, "Unable to write file: %s\n", error->message); fprintf (stderr, "Unable to write file: %s\n", error->message);
return NULL; goto cleanup_create_picture_from_url;
} }
picture = GTK_PICTURE (gtk_picture_new_for_file (tmp_image)); picture = GTK_PICTURE (gtk_picture_new_for_file (tmp_image));
g_object_set_property_int (G_OBJECT(picture), "height-request", picture_height); g_object_set_property_int (G_OBJECT(picture), "height-request", picture_height);
cleanup_create_picture_from_url:
g_free (downloaded_image);
return picture; return picture;
} }