summaryrefslogtreecommitdiff
path: root/cocoa/BookmarksController.m
blob: e78d765292f69cb79e8331fe6ce13ddfc58da8cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * 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/BookmarksController.h"
#import "cocoa/Tree.h"
#import "cocoa/NetsurfApp.h"
#import "cocoa/BrowserViewController.h"
#import "cocoa/gui.h"

#import "desktop/hotlist.h"
#import "desktop/tree.h"
#import "desktop/tree_url_node.h"

@implementation BookmarksController

@synthesize defaultMenu;

static const char *cocoa_hotlist_path( void )
{
	NSString *path = [[NSUserDefaults standardUserDefaults] stringForKey: kHotlistFileOption];
	return [path UTF8String];
}

- init;
{
	if ((self = [super init]) == nil) return nil;
	
	tree = [[Tree alloc] initWithFlags: hotlist_get_tree_flags()];
	hotlist_initialise( [tree tree], cocoa_hotlist_path(), "" );
	nodeForMenu = NSCreateMapTable( NSNonOwnedPointerMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, 0 );
	
	return self;
}

- (void) dealloc;
{
	NSFreeMapTable( nodeForMenu );
	hotlist_cleanup( cocoa_hotlist_path() );
	[tree release];
	[super dealloc];
}

- (void) menuNeedsUpdate: (NSMenu *)menu
{
	for (NSMenuItem *item in [menu itemArray]) {
		if ([item hasSubmenu]) NSMapRemove( nodeForMenu, [item submenu] );
		[menu removeItem: item];
	}

	bool hasSeparator = true;
	struct node *node = (struct node *)NSMapGet( nodeForMenu, menu );
	if (node == NULL) {
		for (NSMenuItem *item in [defaultMenu itemArray]) {
			[menu addItem: [[item copy] autorelease]];
		}
		hasSeparator = false;
		node = [tree rootNode];
	}
	
	for (struct node *child = tree_node_get_child( node ); 
		 child != NULL; 
		 child = tree_node_get_next( child )) {
		
		if (tree_node_is_deleted( child )) continue;
		
		if (!hasSeparator) {
			[menu addItem: [NSMenuItem separatorItem]];
			hasSeparator = true;
		}
		
		NSString *title = [NSString stringWithUTF8String: tree_url_node_get_title( child )];
		
		NSMenuItem *item = [menu addItemWithTitle: title action: NULL keyEquivalent: @""];
		if (tree_node_is_folder( child )) {
			NSMenu *subMenu = [[[NSMenu alloc] initWithTitle: title] autorelease];
			NSMapInsert( nodeForMenu, subMenu, child );
			[subMenu setDelegate: self];
			[menu setSubmenu: subMenu forItem: item];
		} else {
			[item setRepresentedObject: [NSString stringWithUTF8String: tree_url_node_get_url( child )]];
			[item setTarget: self];
			[item setAction: @selector( openBookmarkURL: )];
		}
	}
}

- (IBAction) openBookmarkURL: (id) sender;
{
	const char *url = [[sender representedObject] UTF8String];
	NSParameterAssert( url != NULL );
	
	BrowserViewController *tab = [(NetSurfApp *)NSApp frontTab];
	if (tab != nil) {
		browser_window_go( [tab browser], url, NULL, true );
	} else {
		browser_window_create( url, NULL, NULL, true, false );
	}
}

- (IBAction) showBookmarksWindow: (id) sender;
{
	NSLog( @"TODO: show bookmarks window" );
}

- (IBAction) addBookmark: (id) sender;
{
	NSLog( @"TODO: add bookmark" );
}

- (BOOL) validateUserInterfaceItem: (id) item;
{
	SEL action = [item action];
	
	if (action == @selector( addBookmark: )) {
		return [(NetSurfApp *)NSApp frontTab] != nil;
	}
	
	return YES;
}


+ (void) initialize;
{
	[[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithObjectsAndKeys:
															  cocoa_get_user_path( @"Bookmarks.html" ), kHotlistFileOption,
															  nil]];
}

@end