2011-01-19 23:19:43 +03:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-01-31 22:12:00 +03:00
|
|
|
#import "cocoa/BrowserViewController.h"
|
|
|
|
#import "cocoa/BrowserView.h"
|
|
|
|
#import "cocoa/BrowserWindowController.h"
|
2011-01-19 23:19:43 +03:00
|
|
|
|
2012-08-22 15:38:16 +04:00
|
|
|
#import "desktop/browser_private.h"
|
2011-01-19 23:19:43 +03:00
|
|
|
#import "desktop/history_core.h"
|
|
|
|
#import "desktop/textinput.h"
|
2013-05-26 01:46:27 +04:00
|
|
|
#import "utils/nsoption.h"
|
2011-01-19 23:19:43 +03:00
|
|
|
|
2012-10-11 15:48:15 +04:00
|
|
|
#import "utils/corestrings.h"
|
2011-05-12 21:31:55 +04:00
|
|
|
#import "utils/filename.h"
|
2013-02-22 18:44:13 +04:00
|
|
|
#import "utils/messages.h"
|
2011-05-12 21:31:55 +04:00
|
|
|
#import "utils/url.h"
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
|
|
|
|
@implementation BrowserViewController
|
|
|
|
|
|
|
|
@synthesize browser;
|
|
|
|
@synthesize url;
|
|
|
|
@synthesize browserView;
|
|
|
|
@synthesize windowController;
|
|
|
|
@synthesize title;
|
|
|
|
@synthesize status;
|
2011-01-20 16:22:39 +03:00
|
|
|
@synthesize isProcessing;
|
2011-01-25 20:16:17 +03:00
|
|
|
@synthesize favicon;
|
2011-01-26 16:35:07 +03:00
|
|
|
@synthesize canGoBack;
|
|
|
|
@synthesize canGoForward;
|
2011-01-19 23:19:43 +03:00
|
|
|
|
2011-01-20 14:38:10 +03:00
|
|
|
- (void) dealloc;
|
|
|
|
{
|
|
|
|
[self setUrl: nil];
|
|
|
|
[self setBrowserView: nil];
|
|
|
|
[self setWindowController: nil];
|
|
|
|
[self setTitle: nil];
|
|
|
|
[self setStatus: nil];
|
2011-02-26 13:35:44 +03:00
|
|
|
[self setFavicon: nil];
|
2011-01-20 14:38:10 +03:00
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
- initWithBrowser: (struct browser_window *) bw;
|
|
|
|
{
|
|
|
|
if ((self = [super initWithNibName: @"Browser" bundle: nil]) == nil) return nil;
|
|
|
|
|
|
|
|
browser = bw;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) navigate: (id) sender;
|
|
|
|
{
|
2013-02-18 16:24:49 +04:00
|
|
|
nsurl *urlns;
|
2013-02-14 19:09:28 +04:00
|
|
|
nserror error;
|
|
|
|
|
2013-02-18 16:24:49 +04:00
|
|
|
error = nsurl_create([url UTF8String], &urlns);
|
2013-02-14 19:09:28 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
warn_user(messages_get_errorcode(error), 0);
|
|
|
|
} else {
|
|
|
|
browser_window_navigate(browser,
|
2013-02-18 16:24:49 +04:00
|
|
|
urlns,
|
2013-02-14 19:09:28 +04:00
|
|
|
NULL,
|
2013-02-18 15:51:42 +04:00
|
|
|
BROWSER_WINDOW_HISTORY |
|
|
|
|
BROWSER_WINDOW_VERIFIABLE,
|
2013-02-14 19:09:28 +04:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
2013-02-18 16:24:49 +04:00
|
|
|
nsurl_unref(urlns);
|
2013-02-14 19:09:28 +04:00
|
|
|
}
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) awakeFromNib;
|
|
|
|
{
|
|
|
|
[browserView setBrowser: browser];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (IBAction) zoomIn: (id) sender;
|
|
|
|
{
|
|
|
|
browser_window_set_scale( browser, browser->scale * 1.1, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) zoomOut: (id) sender;
|
|
|
|
{
|
|
|
|
browser_window_set_scale( browser, browser->scale * 0.9, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) zoomOriginal: (id) sender;
|
|
|
|
{
|
2012-03-22 13:34:34 +04:00
|
|
|
browser_window_set_scale( browser, (float)nsoption_int(scale) / 100.0, true );
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2011-01-24 12:12:22 +03:00
|
|
|
- (IBAction) backForwardSelected: (id) sender;
|
|
|
|
{
|
|
|
|
if ([sender selectedSegment] == 0) [self goBack: sender];
|
|
|
|
else [self goForward: sender];
|
|
|
|
}
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
- (IBAction) goBack: (id) sender;
|
|
|
|
{
|
|
|
|
if (browser && history_back_available( browser->history )) {
|
|
|
|
history_back(browser, browser->history);
|
2011-01-26 16:35:07 +03:00
|
|
|
[self updateBackForward];
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) goForward: (id) sender;
|
|
|
|
{
|
|
|
|
if (browser && history_forward_available( browser->history )) {
|
|
|
|
history_forward(browser, browser->history);
|
2011-01-26 16:35:07 +03:00
|
|
|
[self updateBackForward];
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-31 21:48:09 +03:00
|
|
|
- (IBAction) goHome: (id) sender;
|
|
|
|
{
|
2013-02-18 16:24:49 +04:00
|
|
|
nsurl *urlns;
|
|
|
|
nserror error;
|
|
|
|
|
|
|
|
error = nsurl_create(nsoption_charp(homepage_url), &urlns);
|
|
|
|
if (error == NSERROR_OK) {
|
|
|
|
error = browser_window_navigate(browser,
|
|
|
|
urlns,
|
|
|
|
NULL,
|
|
|
|
BROWSER_WINDOW_HISTORY |
|
|
|
|
BROWSER_WINDOW_VERIFIABLE,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
nsurl_unref(urlns);
|
|
|
|
}
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
warn_user(messages_get_errorcode(error), 0);
|
|
|
|
}
|
2011-01-31 21:48:09 +03:00
|
|
|
}
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
- (IBAction) reloadPage: (id) sender;
|
|
|
|
{
|
|
|
|
browser_window_reload( browser, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) stopLoading: (id) sender;
|
|
|
|
{
|
|
|
|
browser_window_stop( browser );
|
|
|
|
}
|
|
|
|
|
2011-05-12 21:31:55 +04:00
|
|
|
- (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. */
|
2012-10-11 15:48:15 +04:00
|
|
|
lwc_string *scheme = nsurl_get_component(hlcache_handle_get_url(content), NSURL_SCHEME);
|
|
|
|
if (scheme == NULL)
|
2011-05-12 21:31:55 +04:00
|
|
|
return;
|
2012-10-11 15:48:15 +04:00
|
|
|
|
2012-10-11 15:52:47 +04:00
|
|
|
bool match;
|
|
|
|
if (lwc_string_isequal(scheme, corestring_lwc_file, &match) == lwc_error_ok && match == true)
|
2012-10-11 15:48:15 +04:00
|
|
|
path = url_to_path(nsurl_access(hlcache_handle_get_url(content)));
|
|
|
|
lwc_string_unref(scheme);
|
2011-05-12 21:31:55 +04:00
|
|
|
|
|
|
|
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"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
static inline bool compare_float( float a, float b )
|
|
|
|
{
|
|
|
|
const float epsilon = 0.00001;
|
|
|
|
|
|
|
|
if (a == b) return true;
|
|
|
|
|
|
|
|
return fabs( (a - b) / b ) <= epsilon;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) validateUserInterfaceItem: (id) item;
|
|
|
|
{
|
|
|
|
SEL action = [item action];
|
|
|
|
|
|
|
|
if (action == @selector(copy:)) {
|
2013-02-22 18:44:13 +04:00
|
|
|
return browser_window_get_editor_flags( browser ) & BW_EDITOR_CAN_COPY;
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector(cut:)) {
|
2013-02-22 18:44:13 +04:00
|
|
|
return browser_window_get_editor_flags( browser ) & BW_EDITOR_CAN_CUT;
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector(paste:)) {
|
2013-02-22 18:44:13 +04:00
|
|
|
return browser_window_get_editor_flags( browser ) & BW_EDITOR_CAN_PASTE;
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector( stopLoading: )) {
|
|
|
|
return browser->loading_content != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector( zoomOriginal: )) {
|
2012-03-22 13:34:34 +04:00
|
|
|
return !compare_float( browser->scale, (float)nsoption_int(scale) / 100.0 );
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2011-01-28 21:26:34 +03:00
|
|
|
if (action == @selector( goBack: )) {
|
|
|
|
return canGoBack;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector( goForward: )) {
|
|
|
|
return canGoForward;
|
|
|
|
}
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-26 16:35:07 +03:00
|
|
|
- (void) updateBackForward;
|
|
|
|
{
|
2011-02-27 12:46:54 +03:00
|
|
|
[browserView updateHistory];
|
2011-01-26 16:35:07 +03:00
|
|
|
[self setCanGoBack: browser != NULL && history_back_available( browser->history )];
|
|
|
|
[self setCanGoForward: browser != NULL && history_forward_available( browser->history )];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) contentUpdated;
|
|
|
|
{
|
2011-02-27 12:46:54 +03:00
|
|
|
[browserView updateHistory];
|
2011-01-26 16:35:07 +03:00
|
|
|
}
|
|
|
|
|
2011-02-28 18:24:38 +03:00
|
|
|
struct history_add_menu_item_data {
|
|
|
|
NSInteger index;
|
|
|
|
NSMenu *menu;
|
|
|
|
id target;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool history_add_menu_item_cb( const struct history *history, int x0, int y0, int x1, int y1,
|
|
|
|
const struct history_entry *page, void *user_data )
|
|
|
|
{
|
|
|
|
struct history_add_menu_item_data *data = user_data;
|
|
|
|
|
|
|
|
NSMenuItem *item = nil;
|
|
|
|
if (data->index < [data->menu numberOfItems]) {
|
|
|
|
item = [data->menu itemAtIndex: data->index];
|
|
|
|
} else {
|
|
|
|
item = [[NSMenuItem alloc] initWithTitle: @""
|
|
|
|
action: @selector( historyItemSelected: )
|
|
|
|
keyEquivalent: @""];
|
|
|
|
[data->menu addItem: item];
|
|
|
|
[item release];
|
|
|
|
}
|
|
|
|
++data->index;
|
|
|
|
|
|
|
|
[item setTarget: data->target];
|
|
|
|
[item setTitle: [NSString stringWithUTF8String: history_entry_get_title( page )]];
|
|
|
|
[item setRepresentedObject: [NSValue valueWithPointer: page]];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) historyItemSelected: (id) sender;
|
|
|
|
{
|
|
|
|
struct history_entry *entry = [[sender representedObject] pointerValue];
|
|
|
|
history_go( browser, browser->history, entry, false );
|
|
|
|
[self updateBackForward];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) buildBackMenu: (NSMenu *)menu;
|
|
|
|
{
|
|
|
|
struct history_add_menu_item_data data = {
|
|
|
|
.index = 0,
|
|
|
|
.menu = menu,
|
|
|
|
.target = self
|
|
|
|
};
|
|
|
|
history_enumerate_back( browser->history, history_add_menu_item_cb, &data );
|
|
|
|
while (data.index < [menu numberOfItems]) [menu removeItemAtIndex: data.index];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) buildForwardMenu: (NSMenu *)menu;
|
|
|
|
{
|
|
|
|
struct history_add_menu_item_data data = {
|
|
|
|
.index = 0,
|
|
|
|
.menu = menu,
|
|
|
|
.target = self
|
|
|
|
};
|
|
|
|
history_enumerate_forward( browser->history, history_add_menu_item_cb, &data );
|
|
|
|
while (data.index < [menu numberOfItems]) [menu removeItemAtIndex: data.index];
|
|
|
|
}
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
@end
|