1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * Copyright (C) 2014 Igalia S.L.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program 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 GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "yelp-uri-builder.h"
23 #define BOGUS_PREFIX "bogus-"
24 #define BOGUS_PREFIX_LEN 6
27 build_network_uri (const gchar
*uri
)
30 gchar
*bogus_scheme
, *path
, *retval
;
32 soup_uri
= soup_uri_new (uri
);
34 /* Build the URI that will be passed to WebKit. Relative URIs will be
35 * automatically resolved by WebKit, so we need to add a leading slash to
36 * help: and ghelp: URIs to be considered as absolute by WebKit.
38 if (g_str_equal (soup_uri
->scheme
, "ghelp") || g_str_equal (soup_uri
->scheme
, "gnome-help") ||
39 g_str_equal (soup_uri
->scheme
, "help") || g_str_equal (soup_uri
->scheme
, "help-list") ||
40 g_str_equal (soup_uri
->scheme
, "info") || g_str_equal (soup_uri
->scheme
, "man")) {
42 if (g_str_equal (soup_uri
->scheme
, "info") && soup_uri
->fragment
) {
43 path
= g_strdup_printf ("/%s/%s", soup_uri
->path
, soup_uri
->fragment
);
44 soup_uri_set_fragment (soup_uri
, NULL
);
46 path
= g_strdup_printf ("/%s", soup_uri
->path
);
48 soup_uri_set_path (soup_uri
, path
);
52 /* We need to use a different scheme from help or ghelp to be able to deal
53 with absolute uris in the HTML. Help uri schemes are help:gnome-help/...
54 they dont have a slash after the colon so WebKit resolves them as a relative
55 url when they are not. This doesn't happen if the current page URI has a different
56 scheme from absolute uri scheme.
58 bogus_scheme
= build_network_scheme (soup_uri
->scheme
);
59 soup_uri_set_scheme (soup_uri
, bogus_scheme
);
61 retval
= soup_uri_to_string (soup_uri
, FALSE
);
62 soup_uri_free (soup_uri
);
63 g_free (bogus_scheme
);
69 build_yelp_uri (const gchar
*uri_str
)
73 gchar
*uri
= g_strdup (uri_str
);
75 if (!g_str_has_prefix (uri
, BOGUS_PREFIX
))
78 memmove (uri
, uri
+ BOGUS_PREFIX_LEN
, strlen (uri
) - BOGUS_PREFIX_LEN
+ 1);
80 /* Remove the leading slash */
81 resource
= strstr (uri
, ":");
83 memmove (resource
, resource
+ 1, strlen (resource
));
85 /* Remove the trailing slash if any */
86 path_len
= strlen (uri
);
87 if (uri
[path_len
- 1] == '/')
88 uri
[path_len
- 1] = '\0';
90 if (g_str_has_prefix (uri
, "info:")) {
93 frag
= g_strrstr (uri
, "/");
102 build_network_scheme (const gchar
*scheme
)
104 return g_strdup_printf (BOGUS_PREFIX
"%s", scheme
);