diff options
Diffstat (limited to 'frontends/cocoa/fetch.m')
-rw-r--r-- | frontends/cocoa/fetch.m | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/frontends/cocoa/fetch.m b/frontends/cocoa/fetch.m new file mode 100644 index 000000000..764f25438 --- /dev/null +++ b/frontends/cocoa/fetch.m @@ -0,0 +1,131 @@ +/* + * Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com> + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#import <Cocoa/Cocoa.h> + +#import "utils/log.h" +#import "utils/nsurl.h" +#import "netsurf/fetch.h" + +#import "cocoa/fetch.h" + +static char cocoafiletype[200]; + +static const char *fetch_filetype(const char *unix_path) +{ + NSString *path = @(unix_path); + NSString *uti = [[NSWorkspace sharedWorkspace] typeOfFile:path error:NULL]; + if (uti == nil) { + // If the file was not found look up the UTI from the file extension. + uti = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)path.pathExtension, nil); + } + + if (uti == nil) { + // We can’t determine the UTI, let’s pretend this is HTML. + return "text/html"; + } + + NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)uti, kUTTagClassMIMEType); + + strncpy(cocoafiletype, [mimeType cStringUsingEncoding: NSUTF8StringEncoding], sizeof cocoafiletype); + + NSLOG(netsurf, INFO, "\tMIME type for '%s' is '%s'", unix_path, cocoafiletype); + + return cocoafiletype; +} + +static NSString *findResource(const char *path) { + NSString *pathString = @(path); + NSString *nspath = [[NSBundle mainBundle] pathForResource:pathString ofType:@""]; + + if (nspath == nil) { + nspath = [[NSBundle mainBundle] pathForResource: pathString ofType:@"" inDirectory:nil forLocalization:@"en"]; + } + + return nspath; +} + +static nsurl *gui_get_resource_url(const char *path) +{ + NSString *nspath = findResource(path); + if (nspath == nil) { + return NULL; + } + + nsurl *url = NULL; + nsurl_create([[[NSURL fileURLWithPath:nspath] absoluteString] UTF8String], &url); + return url; +} + +static nserror cocoa_get_resource_data(const char *path, const uint8_t **data, size_t *data_len) +{ + NSString *resourcePath = findResource(path); + if (resourcePath == nil) { + return NSERROR_NOT_FOUND; + } + + int file = open(resourcePath.fileSystemRepresentation, O_RDONLY); + if (file < 0) { + return NSERROR_NOT_FOUND; + } + + int size = lseek(file, 0, SEEK_END); + if (size < 0) { + close(file); + return NSERROR_INVALID; + } + + void *buffer = malloc(size); + if (buffer == NULL) { + close(file); + return NSERROR_NOMEM; + } + + lseek(file, 0, SEEK_SET); + int readAmount = read(file, buffer, size); + + if (size != readAmount) { + free(buffer); + close(file); + return NSERROR_INVALID; + } + + *data = buffer; + *data_len = size; + + close(file); + + return NSERROR_OK; +} + +static nserror cocoa_release_resource_data(const uint8_t *data) +{ + free((void *)data); + return NSERROR_OK; +} + +static struct gui_fetch_table fetch_table = { + .filetype = fetch_filetype, + + .get_resource_url = gui_get_resource_url, + + .get_resource_data = cocoa_get_resource_data, + .release_resource_data = cocoa_release_resource_data, +}; + +struct gui_fetch_table *cocoa_fetch_table = &fetch_table; |