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
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
CFLAGS := $(shell pkg-config --cflags ${LIBS}) -Wall
LDFLAGS := $(shell pkg-config --libs ${LIBS})

View File

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

136
main.c
View File

@ -17,9 +17,10 @@ GtkListBox *
create_list_box (GtkBox *box);
void
g_object_set_property_int(GObject *object, char *property_key, int value);
void
fill_list_of_mangas (GtkListBox *list);
static void
setup_list_view_mangas(GtkSignalListItemFactory *factory,
GtkListItem *list_item,
gpointer user_data);
void
activate (AdwApplication *app,
@ -30,59 +31,98 @@ activate (AdwApplication *app,
GtkBox *box = create_main_box(
ADW_APPLICATION_WINDOW
(window));
GtkListBox *list;
create_headerbar (box);
list = create_list_box (box);
gtk_widget_set_vexpand (GTK_WIDGET (list), 1);
fill_list_of_mangas (list);
GListStore *mangas;
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);
}
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
fill_list_of_mangas (GtkListBox *list) {
MgManga **mangas;
MgManga *manga;
GtkWidget *row;
size_t len_mangas = 0;
MgBackendReadmng *readmng = mg_backend_readmng_new ();
mangas = mg_backend_readmng_get_featured_manga (readmng, &len_mangas);
for (int i = 0; i<len_mangas; i++) {
GtkWidget *picture;
GFileIOStream *iostream;
GFile *tmp_image;
GError *error = NULL;
size_t size_downloaded_image = 0;
char *downloaded_image;
manga = mangas[i];
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);
row = gtk_list_box_row_new ();
gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), picture);
gtk_list_box_append (list, row);
}
// for (int i = 0; i<len_mangas; i++) {
// GtkWidget *picture;
// GFileIOStream *iostream;
// GFile *tmp_image;
// GError *error = NULL;
//
// size_t size_downloaded_image = 0;
// char *downloaded_image;
//
// manga = mangas[i];
//
// 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);
// 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

View File

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