Ejemplo n.º 1
0
static bool save_complete_save_html_document(save_complete_ctx *ctx,
		hlcache_handle *c, bool index)
{
	nserror ret;
	FILE *fp;
	char *fname = NULL;
	dom_document *doc;
	lwc_string *mime_type;
	char filename[32];

	if (index) {
		snprintf(filename, sizeof filename, "index");
	} else {
		snprintf(filename, sizeof filename, "%p", c);
	}

	ret = netsurf_mkpath(&fname, NULL, 2, ctx->path, filename);
	if (ret != NSERROR_OK) {
		guit->misc->warning(messages_get_errorcode(ret), NULL);
		return false;
	}

	fp = fopen(fname, "wb");
	if (fp == NULL) {
		free(fname);
		LOG("fopen(): errno = %i", errno);
		guit->misc->warning("SaveError", strerror(errno));
		return false;
	}

	ctx->base = html_get_base_url(c);
	ctx->fp = fp;
	ctx->iter_state = STATE_NORMAL;

	doc = html_get_document(c);

	if (save_complete_libdom_treewalk((dom_node *) doc,
			save_complete_node_handler, ctx) == false) {
		free(fname);
		guit->misc->warning("NoMemory", 0);
		fclose(fp);
		return false;
	}

	fclose(fp);

	mime_type = content_get_mime_type(c);
	if (mime_type != NULL) {
		if (ctx->set_type != NULL)
			ctx->set_type(fname, mime_type);

		lwc_string_unref(mime_type);
	}
	free(fname);

	return true;
}
Ejemplo n.º 2
0
static bool save_complete_save_html_document(save_complete_ctx *ctx,
		hlcache_handle *c, bool index)
{
	bool error;
	FILE *fp;
	dom_document *doc;
	lwc_string *mime_type;
	char filename[32];
	char fullpath[PATH_MAX];

	strncpy(fullpath, ctx->path, sizeof fullpath);

	if (index)
		snprintf(filename, sizeof filename, "index");
	else 
		snprintf(filename, sizeof filename, "%p", c);

	error = path_add_part(fullpath, sizeof fullpath, filename);
	if (error == false) {
		warn_user("NoMemory", NULL);
		return false;
	}

	fp = fopen(fullpath, "wb");
	if (fp == NULL) {
		warn_user("NoMemory", NULL);
		return false;
	}

	ctx->base = html_get_base_url(c);
	ctx->fp = fp;
	ctx->iter_state = STATE_NORMAL;

	doc = html_get_document(c);

	if (save_complete_libdom_treewalk((dom_node *) doc,
			save_complete_node_handler, ctx) == false) {
		warn_user("NoMemory", 0);
		fclose(fp);
		return false;
	}

	fclose(fp);

	mime_type = content_get_mime_type(c);
	if (mime_type != NULL) {
		if (ctx->set_type != NULL)
			ctx->set_type(fullpath, mime_type);

		lwc_string_unref(mime_type);
	}

	return true;
}
Ejemplo n.º 3
0
bool save_complete_html(hlcache_handle *c, const char *path, bool index,
		struct save_complete_entry **list)
{
	struct html_stylesheet *sheets;
	struct content_html_object *objects;
	const char *base_url;
	char filename[256];
	unsigned int i, count;
	xmlDocPtr doc;
	bool res;

	if (content_get_type(c) != CONTENT_HTML)
		return false;

	if (save_complete_list_check(c, *list))
		return true;

	base_url = html_get_base_url(c);

	/* save stylesheets, ignoring the base and adblocking sheets */
	sheets = html_get_stylesheets(c, &count);

	for (i = STYLESHEET_START; i != count; i++) {
		hlcache_handle *css;
		const char *css_data;
		unsigned long css_size;
		char *source;
		int source_len;
		struct nscss_import *imports;
		uint32_t import_count;

		if (sheets[i].type == HTML_STYLESHEET_INTERNAL) {
			if (save_imported_sheets(
					sheets[i].data.internal->imports, 
					sheets[i].data.internal->import_count, 
					path, list) == false)
				return false;

			continue;
		}

		css = sheets[i].data.external;

		if (!css)
			continue;
		if (save_complete_list_check(css, *list))
			continue;

		if (!save_complete_list_add(css, list)) {
			warn_user("NoMemory", 0);
			return false;
		}

		imports = nscss_get_imports(css, &import_count);
		if (!save_imported_sheets(imports, import_count, path, list))
			return false;

		snprintf(filename, sizeof filename, "%p", css);

		css_data = content_get_source_data(css, &css_size);

		source = rewrite_stylesheet_urls(css_data, css_size, 
				&source_len, content_get_url(css),
				*list);
		if (!source) {
			warn_user("NoMemory", 0);
			return false;
		}
		res = save_complete_gui_save(path, filename, source_len,
				source, CONTENT_CSS);
		free(source);
		if (res == false)
			return false;
	}
	
	/* save objects */
	objects = html_get_objects(c, &count);

	for (i = 0; i != count; i++) {
		hlcache_handle *obj = objects[i].content;
		const char *obj_data;
		unsigned long obj_size;

		if (obj == NULL || content_get_type(obj) >= CONTENT_OTHER)
			continue;

		obj_data = content_get_source_data(obj, &obj_size);

		if (obj_data == NULL)
			continue;

		if (save_complete_list_check(obj, *list))
			continue;

		if (!save_complete_list_add(obj, list)) {
			warn_user("NoMemory", 0);
			return false;
		}

		if (content_get_type(obj) == CONTENT_HTML) {
			if (!save_complete_html(obj, path, false, list))
				return false;
			continue;
		}

		snprintf(filename, sizeof filename, "%p", obj);
		res = save_complete_gui_save(path, filename, 
				obj_size, obj_data, content_get_type(obj));
		if(res == false)
			return false;
	}

	/*save_complete_list_dump();*/

	/* copy document */
	doc = xmlCopyDoc(html_get_document(c), 1);
	if (doc == NULL) {
		warn_user("NoMemory", 0);
		return false;
	}

	/* rewrite all urls we know about */
	if (!rewrite_document_urls(doc, html_get_base_url(c), *list)) {
		xmlFreeDoc(doc);
		warn_user("NoMemory", 0);
		return false;
	}

	/* save the html file out last of all */
	if (index)
		snprintf(filename, sizeof filename, "index");
	else 
		snprintf(filename, sizeof filename, "%p", c);

	errno = 0;
	if (save_complete_htmlSaveFileFormat(path, filename, doc, 0, 0) == -1) {
		if (errno)
			warn_user("SaveError", strerror(errno));
		else
			warn_user("SaveError", "htmlSaveFileFormat failed");

		xmlFreeDoc(doc);
		return false;
	}	

	xmlFreeDoc(doc);

	return true;
}