From 092c62f0e01ae489baa50fa66a2045b680e15f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revel?= Date: Thu, 12 May 2011 17:31:55 +0000 Subject: [PATCH] Add code to open the source file into Xcode. Currently called by the context menu because I didn't want to dig the .xib files, feel free to move it to the View main menu. svn path=/trunk/netsurf/; revision=12387 --- cocoa/BrowserView.m | 2 ++ cocoa/BrowserViewController.h | 2 ++ cocoa/BrowserViewController.m | 65 ++++++++++++++++++++++++++++++++++- cocoa/NetsurfApp.m | 4 +++ 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/cocoa/BrowserView.m b/cocoa/BrowserView.m index 2e0b0b87f..8cb16d799 100644 --- a/cocoa/BrowserView.m +++ b/cocoa/BrowserView.m @@ -559,6 +559,8 @@ static browser_mouse_state cocoa_mouse_flags_for_event( NSEvent *evt ) action: @selector(reloadPage:) keyEquivalent: @""]; [popupMenu addItemWithTitle: NSLocalizedString( @"Forward", @"Context menu" ) action: @selector(goForward:) keyEquivalent: @""]; + [popupMenu addItemWithTitle: NSLocalizedString( @"View Source", @"Context menu" ) + action: @selector(viewSource:) keyEquivalent: @""]; [NSMenu popUpContextMenu: popupMenu withEvent: event forView: self]; diff --git a/cocoa/BrowserViewController.h b/cocoa/BrowserViewController.h index 81ac74249..6c498766e 100644 --- a/cocoa/BrowserViewController.h +++ b/cocoa/BrowserViewController.h @@ -67,6 +67,8 @@ struct browser_window; - (IBAction) zoomOut: (id) sender; - (IBAction) zoomOriginal: (id) sender; +- (IBAction) viewSource: (id) sender; + - (void) buildBackMenu: (NSMenu *)menu; - (void) buildForwardMenu: (NSMenu *)menu; diff --git a/cocoa/BrowserViewController.m b/cocoa/BrowserViewController.m index 387227de0..2ae354d71 100644 --- a/cocoa/BrowserViewController.m +++ b/cocoa/BrowserViewController.m @@ -16,7 +16,6 @@ * along with this program. If not, see . */ - #import "cocoa/BrowserViewController.h" #import "cocoa/BrowserView.h" #import "cocoa/BrowserWindowController.h" @@ -27,6 +26,9 @@ #import "desktop/options.h" #import "desktop/selection.h" +#import "utils/filename.h" +#import "utils/url.h" + @implementation BrowserViewController @@ -125,6 +127,67 @@ browser_window_stop( browser ); } +- (IBAction) viewSource: (id) sender; +{ + struct hlcache_handle *content; + size_t size; + const char *source; + const char *path = NULL; + + if (browser == NULL) + return; + content = browser->current_content; + if (content == NULL) + return; + source = content_get_source_data(content, &size); + if (source == NULL) + return; + + /* try to load local files directly. */ + char *scheme; + if (url_scheme(content_get_url(content), &scheme) != URL_FUNC_OK) + return; + if (strcmp(scheme, "file") == 0) + path = url_to_path(content_get_url(content)); + free(scheme); + + if (path == NULL) { + /* We cannot release the requested filename until after it + * has finished being used. As we can't easily find out when + * this is, we simply don't bother releasing it and simply + * allow it to be re-used next time NetSurf is started. The + * memory overhead from doing this is under 1 byte per + * filename. */ + const char *filename = filename_request(); + const char *extension = "txt"; + fprintf(stderr, "filename '%p'\n", filename); + if (filename == NULL) + return; + lwc_string *str = content_get_mime_type(content); + if (str) { + NSString *mime = [NSString stringWithUTF8String:lwc_string_data(str)]; + NSString *uti = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (CFStringRef)mime, NULL); + NSString *ext = (NSString *)UTTypeCopyPreferredTagWithClass((CFStringRef)uti, kUTTagClassFilenameExtension); + extension = [ext UTF8String]; + lwc_string_unref(str); + } + + NSURL *dataUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%s.%s", filename, extension] + relativeToURL:[NSURL fileURLWithPath:@TEMP_FILENAME_PREFIX]]; + + + NSData *data = [NSData dataWithBytes:source length:size]; + [data writeToURL:dataUrl atomically:NO]; + path = [[dataUrl path] UTF8String]; + } + + if (path) { + NSString * p = [NSString stringWithUTF8String: path]; + NSWorkspace * ws = [NSWorkspace sharedWorkspace]; + [ws openFile:p withApplication:@"Xcode"]; + } +} + static inline bool compare_float( float a, float b ) { const float epsilon = 0.00001; diff --git a/cocoa/NetsurfApp.m b/cocoa/NetsurfApp.m index 631d56b6c..8caa9f849 100644 --- a/cocoa/NetsurfApp.m +++ b/cocoa/NetsurfApp.m @@ -37,6 +37,7 @@ #import "desktop/tree.h" #import "render/html.h" #import "utils/url.h" +#import "utils/filename.h" #import "utils/log.h" #import "utils/messages.h" #import "utils/utils.h" @@ -180,6 +181,9 @@ int main( int argc, char **argv ) netsurf_init(&argc, &argv, options, messages); + /* Initialise filename allocator */ + filename_initialise(); + apple_image_init(); NSApplication *app = cocoa_prepare_app();