From c4afba9d06f8e1f0189aa6f09738253e32d57a4b Mon Sep 17 00:00:00 2001 From: Antoine Fontaine <antoine.fontaine@epfl.ch> Date: Mon, 12 Aug 2019 16:06:21 +0200 Subject: [PATCH] Handles better filename outside of folders Closes #26 --- src/gui-resource.c | 3 +-- src/moodle-parser.c | 13 ++++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/gui-resource.c b/src/gui-resource.c index fb2cf75..9b7b31d 100644 --- a/src/gui-resource.c +++ b/src/gui-resource.c @@ -49,8 +49,7 @@ G_DEFINE_TYPE (GuiResource, gui_resource, HDY_TYPE_ACTION_ROW) GuiResource * gui_resource_new (struct Resource file_info) { - // if both or none are true, there is a problem - g_assert ((file_info.filename==NULL) ^ (file_info.type==RESOURCE_FOLDERFILE)); + g_assert_nonnull (file_info.name); return g_object_new (HDY_TYPE_GUI_RESOURCE, "resource_type", file_info.type, "resource_name", file_info.name, diff --git a/src/moodle-parser.c b/src/moodle-parser.c index 70ee930..5deb0f1 100644 --- a/src/moodle-parser.c +++ b/src/moodle-parser.c @@ -131,6 +131,10 @@ moodle_parse_folder_content (const gchar *html, gsize html_size, const gchar *mo if (node->child && node->child->next && node->child->next->child) { name = g_strdup (myhtml_node_text(node->child->next->child, NULL)); } + if (!name) { + g_warning ("Could not find the name of a file from a folder\n"); + name = filename; + } resources[count] = (struct Resource) {type, id, name, href, filename}; count++; } @@ -231,8 +235,7 @@ moodle_extract_week (gchar *week_name, myhtml_tree_t *tree, myhtml_tree_node_t * resources = malloc (sizeof (struct Resource) * 1000); for (li = element->child; li; li = li->next) { - gchar *name; - gchar *textname; + gchar *name, *textname, *filename; enum resource_type type; guint id; myhtml_collection_t *hrefs = myhtml_get_nodes_by_tag_id_in_scope (tree, NULL, @@ -246,12 +249,12 @@ moodle_extract_week (gchar *week_name, myhtml_tree_t *tree, myhtml_tree_node_t * } myhtml_tree_node_t *node = hrefs->list[0]; - name = textname = NULL; + name = textname = filename = NULL; id = 0; for (myhtml_tree_attr_t *attr = myhtml_node_attribute_first (node); attr; attr = attr->next) { if (strcmp (myhtml_attribute_key (attr, NULL), "href") == 0) { const char *href = myhtml_attribute_value (attr, NULL); - type = read_link (href, &id, NULL); + type = read_link (href, &id, &filename); name = g_strdup (/*g_path_get_basename (*/href/*)*/); if (node->child && node->child->next && node->child->next->child) { textname = g_strdup (myhtml_node_text(node->child->next->child, NULL)); @@ -262,7 +265,7 @@ moodle_extract_week (gchar *week_name, myhtml_tree_t *tree, myhtml_tree_node_t * if (id && textname && name) { assert(resource_count<1000); // there should be no more than 1000 files a week, right ? right ?? - resources[resource_count] = (struct Resource) {type, id, textname, name, NULL}; + resources[resource_count] = (struct Resource) {type, id, textname, name, filename}; resource_count++; } else { g_print ("a li doesn't have any link\n"); -- GitLab