NetSurf
viewsource.c
Go to the documentation of this file.
1/*
2 * Copyright 2009 Mark Benjamin <MarkBenjamin@dfgh.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdlib.h>
20#include <string.h>
21#include <gtk/gtk.h>
22
23#include "utils/utils.h"
24#include "utils/utf8.h"
25#include "utils/nsurl.h"
26#include "utils/messages.h"
28#include "netsurf/content.h"
29
30#include "gtk/viewdata.h"
31#include "gtk/viewsource.h"
32
34{
35 nserror ret;
36 struct hlcache_handle *hlcontent;
37 const uint8_t *source_data;
38 size_t source_size;
39 char *ndata = NULL;
40 size_t ndata_len;
41 char *filename;
42 char *title;
43
44 hlcontent = browser_window_get_content(bw);
45 if (hlcontent == NULL) {
47 }
48
49 if (content_get_type(hlcontent) != CONTENT_HTML) {
51 }
52
53 source_data = content_get_source_data(hlcontent, &source_size);
54
55 ret = nsurl_nice(browser_window_access_url(bw), &filename, false);
56 if (ret != NSERROR_OK) {
57 filename = strdup(messages_get("SaveSource"));
58 if (filename == NULL) {
59 return NSERROR_NOMEM;
60 }
61 }
62
63 title = malloc(strlen(nsurl_access(browser_window_access_url(bw))) + SLEN("Source of - NetSurf") + 1);
64 if (title == NULL) {
65 free(filename);
66 return NSERROR_NOMEM;
67 }
68 sprintf(title, "Source of %s - NetSurf", nsurl_access(browser_window_access_url(bw)));
69
70 ret = utf8_from_enc((const char *)source_data,
71 content_get_encoding(hlcontent,
73 source_size,
74 &ndata,
75 &ndata_len);
76 if (ret == NSERROR_OK) {
77 ret = nsgtk_viewdata(title, filename, ndata, ndata_len);
78 }
79
80 free(filename);
81 free(title);
82
83 return ret;
84}
Browser window creation and manipulation interface.
struct nsurl * browser_window_access_url(const struct browser_window *bw)
Access a browser window's URL.
struct hlcache_handle * browser_window_get_content(struct browser_window *bw)
Get a cache handle for the content within a browser window.
@ CONTENT_HTML
content is HTML
Definition: content_type.h:58
@ CONTENT_ENCODING_NORMAL
The content encoding.
Definition: content_type.h:45
wimp_w parent
Definition: dialog.c:88
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_BAD_CONTENT
Bad Content.
Definition: errors.h:56
@ NSERROR_BAD_PARAMETER
Bad Parameter.
Definition: errors.h:48
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
Public content interface.
const uint8_t * content_get_source_data(struct hlcache_handle *h, size_t *size)
Retrieve source of content.
Definition: content.c:1209
const char * content_get_encoding(struct hlcache_handle *h, enum content_encoding_type op)
Retrieve the encoding of a content.
Definition: content.c:1321
content_type content_get_type(struct hlcache_handle *h)
Retrieve computed type of content.
Definition: content.c:1061
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:241
Localised message support (interface).
NetSurf URL handling (interface).
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
nserror nsurl_nice(const nsurl *url, char **result, bool remove_extensions)
Attempt to find a nice filename for a URL.
Interface to utility string handling.
Browser window data.
High-level cache handle.
Definition: hlcache.c:66
nserror utf8_from_enc(const char *string, const char *encname, size_t len, char **result, size_t *result_len)
Convert a string in the named encoding into a UTF-8 string.
Definition: utf8.c:321
UTF-8 manipulation functions (interface).
Interface to a number of general purpose functionality.
#define SLEN(x)
Calculate length of constant C string.
Definition: utils.h:84
nserror nsgtk_viewdata(const char *title, const char *filename, char *ndata, size_t ndata_len)
Display text to a user.
Definition: viewdata.c:931
nserror nsgtk_viewsource(GtkWindow *parent, struct browser_window *bw)
Definition: viewsource.c:33