Migrating to ListView.

This commit is contained in:
sergiotarxz 2021-10-28 22:26:59 +02:00
parent 0ac7affa93
commit d2ecdddf44
4 changed files with 112 additions and 72 deletions

View File

@ -1,5 +1,5 @@
CC := gcc CC := gcc
LIBS := libadwaita-1 gtk4 libsoup-2.4 libxml-2.0 libpcre2-8 LIBS := libadwaita-1 gtk4 libsoup-2.4 libxml-2.0 libpcre2-8 gio-2.0
INCDIR := -I ./include INCDIR := -I ./include
CFLAGS := $(shell pkg-config --cflags ${LIBS}) -Wall CFLAGS := $(shell pkg-config --cflags ${LIBS}) -Wall
LDFLAGS := $(shell pkg-config --libs ${LIBS}) LDFLAGS := $(shell pkg-config --libs ${LIBS})

View File

@ -2,6 +2,8 @@
#include <glib-object.h> #include <glib-object.h>
#include <gio/gio.h>
#include <openmg/manga.h> #include <openmg/manga.h>
G_BEGIN_DECLS; G_BEGIN_DECLS;
@ -24,7 +26,7 @@ mg_backend_readmng_get_property (GObject *object, guint property_id,
MgBackendReadmng * MgBackendReadmng *
mg_backend_readmng_new (void); mg_backend_readmng_new (void);
MgManga ** GListStore *
mg_backend_readmng_get_featured_manga (MgBackendReadmng *self, size_t *len); mg_backend_readmng_get_featured_manga (MgBackendReadmng *self);
G_END_DECLS G_END_DECLS

136
main.c
View File

@ -17,9 +17,10 @@ GtkListBox *
create_list_box (GtkBox *box); create_list_box (GtkBox *box);
void void
g_object_set_property_int(GObject *object, char *property_key, int value); g_object_set_property_int(GObject *object, char *property_key, int value);
static void
void setup_list_view_mangas(GtkSignalListItemFactory *factory,
fill_list_of_mangas (GtkListBox *list); GtkListItem *list_item,
gpointer user_data);
void void
activate (AdwApplication *app, activate (AdwApplication *app,
@ -30,59 +31,98 @@ activate (AdwApplication *app,
GtkBox *box = create_main_box( GtkBox *box = create_main_box(
ADW_APPLICATION_WINDOW ADW_APPLICATION_WINDOW
(window)); (window));
GtkListBox *list;
create_headerbar (box); create_headerbar (box);
list = create_list_box (box); GListStore *mangas;
gtk_widget_set_vexpand (GTK_WIDGET (list), 1);
fill_list_of_mangas (list); MgBackendReadmng *readmng = mg_backend_readmng_new ();
mangas = mg_backend_readmng_get_featured_manga (readmng);
GtkSingleSelection *selection = gtk_single_selection_new (G_LIST_MODEL (mangas));
GtkListItemFactory *factory = gtk_signal_list_item_factory_new ();
g_signal_connect (G_OBJECT (factory), "bind",
G_CALLBACK (setup_list_view_mangas),
NULL);
GtkWidget *list_view = gtk_list_view_new (GTK_SELECTION_MODEL (selection),
factory);
gtk_box_append (box, list_view);
gtk_widget_show (window); gtk_widget_show (window);
} }
static void
setup_list_view_mangas(GtkSignalListItemFactory *factory,
GtkListItem *list_item,
gpointer user_data) {
MgManga *manga = gtk_list_item_get_item (list_item);
GtkBox *box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
GtkWidget *label = gtk_label_new (mg_manga_get_title (manga));
GtkWidget *picture;
GFileIOStream *iostream;
GFile *tmp_image;
GError *error = NULL;
size_t size_downloaded_image = 0;
char *downloaded_image;
downloaded_image = get_request (mg_manga_get_image_url(manga), &size_downloaded_image);
tmp_image = g_file_new_tmp ("mangareadertmpfileXXXXXX",
&iostream,
&error
);
if (error) {
fprintf (stderr, "Unable to read file: %s\n", error->message);
return;
}
error = NULL;
g_output_stream_write (g_io_stream_get_output_stream (G_IO_STREAM (iostream)),
downloaded_image, size_downloaded_image, NULL, &error);
if (error) {
fprintf (stderr, "Unable to write file: %s\n", error->message);
return;
}
picture = gtk_picture_new_for_file (tmp_image);
g_object_set_property_int (G_OBJECT(picture), "height-request", 200);
gtk_box_append (box, picture);
gtk_box_append (box, label);
gtk_list_item_set_child (list_item, GTK_WIDGET (box));
}
void void
fill_list_of_mangas (GtkListBox *list) { fill_list_of_mangas (GtkListBox *list) {
MgManga **mangas; // for (int i = 0; i<len_mangas; i++) {
MgManga *manga; // GtkWidget *picture;
GtkWidget *row; // GFileIOStream *iostream;
size_t len_mangas = 0; // GFile *tmp_image;
// GError *error = NULL;
//
MgBackendReadmng *readmng = mg_backend_readmng_new (); // size_t size_downloaded_image = 0;
mangas = mg_backend_readmng_get_featured_manga (readmng, &len_mangas); // char *downloaded_image;
for (int i = 0; i<len_mangas; i++) { //
GtkWidget *picture; // manga = mangas[i];
GFileIOStream *iostream; //
GFile *tmp_image; // downloaded_image = get_request (mg_manga_get_image_url(manga), &size_downloaded_image);
GError *error = NULL; // tmp_image = g_file_new_tmp ("mangareadertmpfileXXXXXX",
// &iostream,
size_t size_downloaded_image = 0; // &error
char *downloaded_image; // );
// if (error) {
manga = mangas[i]; // fprintf (stderr, "Unable to read file: %s\n", error->message);
// return;
downloaded_image = get_request (mg_manga_get_image_url(manga), &size_downloaded_image); // }
tmp_image = g_file_new_tmp ("mangareadertmpfileXXXXXX", // error = NULL;
&iostream, // g_output_stream_write (g_io_stream_get_output_stream (G_IO_STREAM (iostream)),
&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 read file: %s\n", error->message); // return;
return; // }
} // picture = gtk_picture_new_for_file (tmp_image);
error = NULL; // g_object_set_property_int (G_OBJECT(picture), "height-request", 200);
g_output_stream_write (g_io_stream_get_output_stream (G_IO_STREAM (iostream)), // row = gtk_list_box_row_new ();
downloaded_image, size_downloaded_image, NULL, &error); // gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), picture);
if (error) { // gtk_list_box_append (list, row);
fprintf (stderr, "Unable to write file: %s\n", error->message); // }
return;
}
picture = gtk_picture_new_for_file (tmp_image);
g_object_set_property_int (G_OBJECT(picture), "height-request", 200);
row = gtk_list_box_row_new ();
gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), picture);
gtk_list_box_append (list, row);
}
} }
void void

View File

@ -15,7 +15,7 @@ struct _MgBackendReadmng {
char *base_url; char *base_url;
size_t main_page_html_len; size_t main_page_html_len;
char *main_page_html; char *main_page_html;
MgManga *(*get_featured_manga) (); GListStore *(*get_featured_manga) ();
}; };
G_DEFINE_TYPE (MgBackendReadmng, mg_backend_readmng, G_TYPE_OBJECT) G_DEFINE_TYPE (MgBackendReadmng, mg_backend_readmng, G_TYPE_OBJECT)
@ -43,17 +43,17 @@ static xmlNodePtr
mg_backend_readmng_retrieve_img_from_thumbnail (MgBackendReadmng *self, xmlNodePtr thumbnail); mg_backend_readmng_retrieve_img_from_thumbnail (MgBackendReadmng *self, xmlNodePtr thumbnail);
static xmlNodePtr static xmlNodePtr
mg_backend_readmng_retrieve_ul_slides(MgBackendReadmng *self, xmlNodePtr slides) ; mg_backend_readmng_retrieve_ul_slides(MgBackendReadmng *self, xmlNodePtr slides) ;
static MgManga ** static void
mg_backend_readmng_extract_manga_info_from_current_li (MgBackendReadmng *self, mg_backend_readmng_extract_manga_info_from_current_li (MgBackendReadmng *self,
MgManga **mangas, xmlNodePtr current_li, size_t *len); GListStore *mangas, xmlNodePtr current_li);
static xmlNodePtr * static xmlNodePtr *
mg_backend_readmng_retrieve_li_slides (MgBackendReadmng *self, const xmlNodePtr slides, size_t *li_len); mg_backend_readmng_retrieve_li_slides (MgBackendReadmng *self, const xmlNodePtr slides, size_t *li_len);
static xmlNodePtr static xmlNodePtr
mg_backend_readmng_retrieve_slides (MgBackendReadmng *self, const xmlDocPtr html_document); mg_backend_readmng_retrieve_slides (MgBackendReadmng *self, const xmlDocPtr html_document);
static const char * static const char *
mg_backend_readmng_get_main_page (MgBackendReadmng *self, size_t *len); mg_backend_readmng_get_main_page (MgBackendReadmng *self, size_t *len);
static MgManga ** static GListStore *
mg_backend_readmng_parse_main_page (MgBackendReadmng *self, size_t *len, const xmlDocPtr html_document); mg_backend_readmng_parse_main_page (MgBackendReadmng *self, const xmlDocPtr html_document);
static xmlDocPtr static xmlDocPtr
mg_backend_readmng_fetch_xml_main_page (MgBackendReadmng *self); mg_backend_readmng_fetch_xml_main_page (MgBackendReadmng *self);
@ -107,12 +107,12 @@ mg_backend_readmng_get_property (GObject *object,
} }
} }
MgManga ** GListStore *
mg_backend_readmng_get_featured_manga (MgBackendReadmng *self, size_t *len) { mg_backend_readmng_get_featured_manga (MgBackendReadmng *self) {
MgManga **mangas; GListStore *mangas;
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, len, html_document); mangas = mg_backend_readmng_parse_main_page (self, html_document);
return mangas; return mangas;
} }
@ -141,20 +141,20 @@ mg_backend_readmng_get_main_page (MgBackendReadmng *self, size_t *len) {
return self->main_page_html; return self->main_page_html;
} }
static MgManga ** static GListStore *
mg_backend_readmng_parse_main_page (MgBackendReadmng *self, size_t *len, const xmlDocPtr html_document) { mg_backend_readmng_parse_main_page (MgBackendReadmng *self, const xmlDocPtr html_document) {
MgManga **mangas = NULL; GListStore *mangas = g_list_store_new(MG_TYPE_MANGA);
xmlNodePtr *li; xmlNodePtr *li;
*len = 0;
xmlNodePtr slides = mg_backend_readmng_retrieve_slides (self, html_document); xmlNodePtr slides = mg_backend_readmng_retrieve_slides (self, html_document);
size_t li_len = 0; size_t li_len = 0;
li = mg_backend_readmng_retrieve_li_slides (self, slides, &li_len); li = mg_backend_readmng_retrieve_li_slides (self, slides, &li_len);
for (int i = 0; i<li_len; i++) { for (int i = 0; i<li_len; i++) {
xmlNodePtr current_li = li[i]; xmlNodePtr current_li = li[i];
mangas = mg_backend_readmng_extract_manga_info_from_current_li (self, mangas, current_li, mg_backend_readmng_extract_manga_info_from_current_li (self,
len); mangas, current_li);
} }
return mangas; return mangas;
} }
@ -226,20 +226,18 @@ mg_backend_readmng_retrieve_title_from_li (MgBackendReadmng *self, xmlNodePtr li
return NULL; return NULL;
} }
static MgManga ** static void
mg_backend_readmng_extract_manga_info_from_current_li (MgBackendReadmng *self, mg_backend_readmng_extract_manga_info_from_current_li (MgBackendReadmng *self,
MgManga **mangas, xmlNodePtr current_li, size_t *len) { GListStore *mangas, xmlNodePtr current_li) {
xmlNodePtr thumbnail = mg_backend_readmng_retrieve_thumbnail_from_li (self, current_li); xmlNodePtr thumbnail = mg_backend_readmng_retrieve_thumbnail_from_li (self, current_li);
xmlNodePtr title = mg_backend_readmng_retrieve_title_from_li (self, current_li); xmlNodePtr title = mg_backend_readmng_retrieve_title_from_li (self, current_li);
xmlNodePtr img; xmlNodePtr img;
if (thumbnail && title && (img = mg_backend_readmng_retrieve_img_from_thumbnail (self, thumbnail))) { if (thumbnail && title && (img = mg_backend_readmng_retrieve_img_from_thumbnail (self, thumbnail))) {
(*len)++; g_list_store_append (mangas, mg_manga_new (get_attr (img, "src"),
mangas = g_realloc(mangas, sizeof *mangas * *len); (char *)xmlNodeGetContent (title)));
mangas[*len-1] = mg_manga_new (get_attr (img, "src"), (char *)xmlNodeGetContent (title));
} }
return mangas;
} }
static xmlNodePtr static xmlNodePtr