diff options
author | Vincent Sanders <vince@kyllikki.org> | 2014-01-25 23:00:22 +0000 |
---|---|---|
committer | Vincent Sanders <vince@kyllikki.org> | 2014-01-25 23:01:32 +0000 |
commit | 8ce0a10670e655d9e3a4f31fedd34baf1a3189b9 (patch) | |
tree | c1aa73bf937d097a6d2cb7efe3777cb6c720e7f7 /gtk | |
parent | 46b8fbaeac4dd1e35945ae1338056156e5b3b86b (diff) | |
download | netsurf-8ce0a10670e655d9e3a4f31fedd34baf1a3189b9.tar.gz netsurf-8ce0a10670e655d9e3a4f31fedd34baf1a3189b9.tar.bz2 |
move path_to_url and url_to_path to fetch operation table
Diffstat (limited to 'gtk')
-rw-r--r-- | gtk/Makefile.target | 2 | ||||
-rw-r--r-- | gtk/dialogs/source.c | 17 | ||||
-rw-r--r-- | gtk/fetch.c (renamed from gtk/filetype.c) | 187 | ||||
-rw-r--r-- | gtk/fetch.h (renamed from gtk/filetype.h) | 12 | ||||
-rw-r--r-- | gtk/gui.c | 120 | ||||
-rw-r--r-- | gtk/gui.h | 2 |
6 files changed, 175 insertions, 165 deletions
diff --git a/gtk/Makefile.target b/gtk/Makefile.target index 426c4affd..ec19d1b36 100644 --- a/gtk/Makefile.target +++ b/gtk/Makefile.target @@ -108,7 +108,7 @@ $(eval $(foreach V,$(filter GTK_IMAGE_%,$(.VARIABLES)),$(call convert_image,$($( # S_GTK are sources purely for the GTK build S_GTK := font_pango.c bitmap.c gui.c schedule.c thumbnail.c plotters.c \ treeview.c scaffolding.c gdk.c completion.c login.c throbber.c \ - selection.c history.c window.c filetype.c download.c menu.c \ + selection.c history.c window.c fetch.c download.c menu.c \ print.c search.c tabs.c theme.c toolbar.c gettext.c \ compat.c cookies.c hotlist.c \ $(addprefix dialogs/,preferences.c about.c source.c) diff --git a/gtk/dialogs/source.c b/gtk/dialogs/source.c index 5306bdc16..a7c98152e 100644 --- a/gtk/dialogs/source.c +++ b/gtk/dialogs/source.c @@ -23,13 +23,6 @@ #include <unistd.h> #include <gtk/gtk.h> -#include "gtk/compat.h" -#include "gtk/dialogs/source.h" -#include "gtk/dialogs/about.h" -#include "gtk/window.h" -#include "gtk/gui.h" -#include "gtk/print.h" -#include "gtk/selection.h" #include "desktop/browser_private.h" #include "desktop/netsurf.h" #include "desktop/print.h" @@ -42,9 +35,17 @@ #include "render/font.h" #include "content/content.h" #include "content/content_type.h" - #include "utils/log.h" +#include "gtk/compat.h" +#include "gtk/dialogs/source.h" +#include "gtk/dialogs/about.h" +#include "gtk/window.h" +#include "gtk/gui.h" +#include "gtk/print.h" +#include "gtk/selection.h" +#include "gtk/fetch.h" + struct nsgtk_source_window { gchar *url; char *data; diff --git a/gtk/filetype.c b/gtk/fetch.c index a949b14d5..f4a42b5f8 100644 --- a/gtk/filetype.c +++ b/gtk/fetch.c @@ -1,6 +1,5 @@ /* - * Copyright 2007 Rob Kendrick <rjek@netsurf-browser.org> - * Copyright 2007 Vincent Sanders <vince@debian.org> + * Copyright 2014 vincent Sanders <vince@netsurf-browser.org> * * This file is part of NetSurf, http://www.netsurf-browser.org/ * @@ -18,18 +17,18 @@ */ #include <stdio.h> -#include <stdbool.h> -#include <string.h> -#include <stdlib.h> -#include <ctype.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> -#include "gtk/filetype.h" -#include "content/fetch.h" -#include "utils/log.h" #include "utils/hashtable.h" +#include "utils/url.h" +#include "utils/log.h" +#include "utils/filepath.h" +#include "desktop/gui.h" + +#include "gtk/gui.h" +#include "gtk/fetch.h" static struct hash_table *mime_hash = NULL; @@ -43,9 +42,8 @@ void gtk_fetch_filetype_init(const char *mimefile) /* first, check to see if /etc/mime.types in preference */ if ((stat("/etc/mime.types", &statbuf) == 0) && - S_ISREG(statbuf.st_mode)) { + S_ISREG(statbuf.st_mode)) { mimefile = "/etc/mime.types"; - } fh = fopen(mimefile, "r"); @@ -75,25 +73,30 @@ void gtk_fetch_filetype_init(const char *mimefile) while (!feof(fh)) { char line[256], *ptr, *type, *ext; + if (fgets(line, 256, fh) == NULL) - break; + break; + if (!feof(fh) && line[0] != '#') { ptr = line; /* search for the first non-whitespace character */ - while (isspace(*ptr)) + while (isspace(*ptr)) { ptr++; + } /* is this line empty other than leading whitespace? */ - if (*ptr == '\n' || *ptr == '\0') + if (*ptr == '\n' || *ptr == '\0') { continue; + } type = ptr; /* search for the first non-whitespace char or NUL or * NL */ - while (*ptr && (!isspace(*ptr)) && *ptr != '\n') + while (*ptr && (!isspace(*ptr)) && *ptr != '\n') { ptr++; + } if (*ptr == '\0' || *ptr == '\n') { /* this mimetype has no extensions - read next @@ -106,8 +109,9 @@ void gtk_fetch_filetype_init(const char *mimefile) /* search for the first non-whitespace character which * will be the first filename extenion */ - while (isspace(*ptr)) + while (isspace(*ptr)) { ptr++; + } while(true) { ext = ptr; @@ -115,9 +119,11 @@ void gtk_fetch_filetype_init(const char *mimefile) /* search for the first whitespace char or * NUL or NL which is the end of the ext. */ - while (*ptr && (!isspace(*ptr)) && - *ptr != '\n') + while (*ptr && + (!isspace(*ptr)) && + *ptr != '\n') { ptr++; + } if (*ptr == '\0' || *ptr == '\n') { /* special case for last extension on @@ -134,8 +140,11 @@ void gtk_fetch_filetype_init(const char *mimefile) /* search for the first non-whitespace char or * NUL or NL, to find start of next ext. */ - while (*ptr && (isspace(*ptr)) && *ptr != '\n') + while (*ptr && + (isspace(*ptr)) && + *ptr != '\n') { ptr++; + } } } } @@ -162,8 +171,9 @@ const char *fetch_filetype(const char *unix_path) return "text/plain"; } - if (S_ISDIR(statbuf.st_mode)) + if (S_ISDIR(statbuf.st_mode)) { return "application/x-netsurf-directory"; + } l = strlen(unix_path); @@ -184,11 +194,13 @@ const char *fetch_filetype(const char *unix_path) } ptr = unix_path + strlen(unix_path); - while (*ptr != '.' && *ptr != '/') + while (*ptr != '.' && *ptr != '/') { ptr--; + } - if (*ptr != '.') + if (*ptr != '.') { return "text/plain"; + } ext = strdup(ptr + 1); /* skip the . */ @@ -196,7 +208,7 @@ const char *fetch_filetype(const char *unix_path) * copy is lower case too. */ lowerchar = ext; - while(*lowerchar) { + while (*lowerchar) { *lowerchar = tolower(*lowerchar); lowerchar++; } @@ -204,31 +216,132 @@ const char *fetch_filetype(const char *unix_path) type = hash_get(mime_hash, ext); free(ext); - return type != NULL ? type : "text/plain"; + if (type == NULL) { + type = "text/plain"; + } + + return type; } -#ifdef TEST_RIG +/** + * Return the filename part of a full path + * + * \param path full path and filename + * \return filename (will be freed with free()) + */ +static char *filename_from_path(char *path) +{ + char *leafname; + + leafname = strrchr(path, '/'); + if (!leafname) { + leafname = path; + } else { + leafname += 1; + } + + return strdup(leafname); +} -int main(int argc, char *argv[]) +/** + * Add a path component/filename to an existing path + * + * \param path buffer containing path + free space + * \param length length of buffer "path" + * \param newpart string containing path component to add to path + * \return true on success + */ +static bool path_add_part(char *path, int length, const char *newpart) { - unsigned int c1, *c2; - const char *key; + if (path[strlen(path) - 1] != '/') { + strncat(path, "/", length); + } - gtk_fetch_filetype_init("./mime.types"); + strncat(path, newpart, length); - c1 = 0; c2 = 0; + return true; +} - while ( (key = hash_iterate(mime_hash, &c1, &c2)) != NULL) { - printf("%s ", key); +char *path_to_url(const char *path) +{ + int urllen; + char *url; + + if (path == NULL) { + return NULL; + } + + urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1; + + url = malloc(urllen); + if (url == NULL) { + return NULL; + } + + if (*path == '/') { + path++; /* file: paths are already absolute */ } - printf("\n"); + snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, path); + + return url; +} + + +static char *url_to_path(const char *url) +{ + char *path; + char *respath; + url_func_result res; /* result from url routines */ + + res = url_path(url, &path); + if (res != URL_FUNC_OK) { + return NULL; + } - if (argc > 1) { - printf("%s maps to %s\n", argv[1], fetch_filetype(argv[1])); + res = url_unescape(path, &respath); + free(path); + if (res != URL_FUNC_OK) { + return NULL; } - gtk_fetch_filetype_fin(); + return respath; } -#endif +static nsurl *gui_get_resource_url(const char *path) +{ + char buf[PATH_MAX]; + char *raw; + nsurl *url = NULL; + + /* default.css -> gtkdefault.css */ + if (strcmp(path, "default.css") == 0) { + path = "gtkdefault.css"; + } + + /* favicon.ico -> favicon.png */ + if (strcmp(path, "favicon.ico") == 0) { + path = "favicon.png"; + } + + raw = path_to_url(filepath_sfind(respaths, buf, path)); + if (raw != NULL) { + nsurl_create(raw, &url); + free(raw); + } + + return url; +} + +static struct gui_fetch_table fetch_table = { + .filename_from_path = filename_from_path, + .path_add_part = path_add_part, + .filetype = fetch_filetype, + .path_to_url = path_to_url, + .url_to_path = url_to_path, + + .get_resource_url = gui_get_resource_url, + +}; + +struct gui_fetch_table *nsgtk_fetch_table = &fetch_table; diff --git a/gtk/filetype.h b/gtk/fetch.h index 68bb9c0ff..400a06a35 100644 --- a/gtk/filetype.h +++ b/gtk/fetch.h @@ -1,6 +1,5 @@ /* - * Copyright 2007 Rob Kendrick <rjek@netsurf-browser.org> - * Copyright 2007 Vincent Sanders <vince@debian.org> + * Copyright 2014 Vincent Sanders <vince@netsurf-browser.org> * * This file is part of NetSurf, http://www.netsurf-browser.org/ * @@ -17,6 +16,15 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#ifndef NETSURF_GTK_FETCH_H +#define NETSURF_GTK_FETCH_H + +struct gui_fetch_table *nsgtk_fetch_table; + void gtk_fetch_filetype_init(const char *mimefile); void gtk_fetch_filetype_fin(void); const char *fetch_filetype(const char *unix_path); + +char *path_to_url(const char *path); + +#endif @@ -60,7 +60,7 @@ #include "gtk/completion.h" #include "gtk/cookies.h" #include "gtk/download.h" -#include "gtk/filetype.h" +#include "gtk/fetch.h" #include "gtk/gui.h" #include "gtk/history.h" #include "gtk/hotlist.h" @@ -109,7 +109,7 @@ static void nsgtk_PDF_no_pass(GtkButton *w, gpointer data); #define THROBBER_FRAMES 9 -static char **respaths; /** resource search path vector */ +char **respaths; /** resource search path vector */ /** Create an array of valid paths to search for resources. * @@ -324,28 +324,6 @@ static void check_options(char **respath) } -static nsurl *gui_get_resource_url(const char *path) -{ - char buf[PATH_MAX]; - char *raw; - nsurl *url = NULL; - - /* default.css -> gtkdefault.css */ - if (strcmp(path, "default.css") == 0) - path = "gtkdefault.css"; - - /* favicon.ico -> favicon.png */ - if (strcmp(path, "favicon.ico") == 0) - path = "favicon.png"; - - raw = path_to_url(filepath_sfind(respaths, buf, path)); - if (raw != NULL) { - nsurl_create(raw, &url); - free(raw); - } - - return url; -} /** @@ -842,51 +820,6 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, } -char *path_to_url(const char *path) -{ - int urllen; - char *url; - - if (path == NULL) { - return NULL; - } - - urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1; - - url = malloc(urllen); - if (url == NULL) { - return NULL; - } - - if (*path == '/') { - path++; /* file: paths are already absolute */ - } - - snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, path); - - return url; -} - - -char *url_to_path(const char *url) -{ - char *path; - char *respath; - url_func_result res; /* result from url routines */ - - res = url_path(url, &path); - if (res != URL_FUNC_OK) { - return NULL; - } - - res = url_unescape(path, &respath); - free(path); - if (res != URL_FUNC_OK) { - return NULL; - } - - return respath; -} #ifdef WITH_PDF_EXPORT @@ -1089,59 +1022,12 @@ uint32_t gtk_gui_gdkkey_to_nskey(GdkEventKey *key) } } -/** - * Return the filename part of a full path - * - * \param path full path and filename - * \return filename (will be freed with free()) - */ - -static char *filename_from_path(char *path) -{ - char *leafname; - - leafname = strrchr(path, '/'); - if (!leafname) - leafname = path; - else - leafname += 1; - - return strdup(leafname); -} - -/** - * Add a path component/filename to an existing path - * - * \param path buffer containing path + free space - * \param length length of buffer "path" - * \param newpart string containing path component to add to path - * \return true on success - */ - -static bool path_add_part(char *path, int length, const char *newpart) -{ - if(path[strlen(path) - 1] != '/') - strncat(path, "/", length); - - strncat(path, newpart, length); - - return true; -} static struct gui_clipboard_table nsgtk_clipboard_table = { .get = gui_get_clipboard, .set = gui_set_clipboard, }; -static struct gui_fetch_table nsgtk_fetch_table = { - .filename_from_path = filename_from_path, - .path_add_part = path_add_part, - .filetype = fetch_filetype, - - .get_resource_url = gui_get_resource_url, - -}; - static struct gui_browser_table nsgtk_browser_table = { .poll = gui_poll, @@ -1166,7 +1052,7 @@ int main(int argc, char** argv) .window = nsgtk_window_table, .clipboard = &nsgtk_clipboard_table, .download = nsgtk_download_table, - .fetch = &nsgtk_fetch_table, + .fetch = nsgtk_fetch_table, }; /* check home directory is available */ @@ -59,6 +59,8 @@ extern char *themelist_file_location; extern GdkPixbuf *favicon_pixbuf; /* favicon default pixbuf */ +extern char **respaths; /** resource search path vector */ + uint32_t gtk_gui_gdkkey_to_nskey(GdkEventKey *); extern void gui_401login_open(nsurl *url, const char *realm, |