summaryrefslogtreecommitdiff
path: root/frontends/cocoa/BookmarksController.m
blob: d7918d9fc72fb9697de375b5e60323cd38a3e4e7 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * 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 "utils/messages.h"
#import "utils/utils.h"
#import "utils/nsurl.h"
#import "netsurf/browser_window.h"
#import "netsurf/keypress.h"
#import "desktop/hotlist.h"

#import "cocoa/desktop-tree.h"
#import "cocoa/BookmarksController.h"
#import "cocoa/Tree.h"
#import "cocoa/TreeView.h"
#import "cocoa/NetsurfApp.h"
#import "cocoa/BrowserViewController.h"
#import "cocoa/gui.h"


@interface BookmarksController ()
- (void) noteAppWillTerminate: (NSNotification *) note;
- (void) save;
@end

@implementation BookmarksController

@synthesize defaultMenu;
@synthesize view;

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

- (id)init
{
        if ((self = [super initWithWindowNibName: @"BookmarksWindow"]) == nil) {
                return nil;
        }
        tree_hotlist_path = cocoa_hotlist_path();
        tree = [[Tree alloc] initWithFlags: TREE_HOTLIST];
        nodeForMenu = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks,
                                       NSNonOwnedPointerMapValueCallBacks,
                                       0);

        [[NSNotificationCenter defaultCenter]
          addObserver:self
             selector:@selector(noteAppWillTerminate:)
                 name:NSApplicationWillTerminateNotification
               object:NSApp];

        return self;
}

- (void) noteAppWillTerminate: (NSNotification *)note
{
        [self save];
}

- (void) save
{
        hotlist_export( cocoa_hotlist_path(), NULL );
}

- (void) dealloc
{
        [self setView: nil];
        NSFreeMapTable( nodeForMenu );
        [tree release];

        [[NSNotificationCenter defaultCenter] removeObserver: self];

        [super dealloc];
}

- (void) menuNeedsUpdate: (NSMenu *)menu
{
#if 0
        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;
        }

        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: )];
                }
        }
#endif
}

- (IBAction) openBookmarkURL: (id)sender
{
        const char *urltxt = [[sender representedObject] UTF8String];
        NSParameterAssert( urltxt != NULL );

        nsurl *url;
        nserror error;

        error = nsurl_create(urltxt, &url);
        if (error == NSERROR_OK) {
                BrowserViewController *tab = [(NetSurfApp *)NSApp frontTab];
                if (tab != nil) {
                        error = browser_window_navigate([tab browser],
                                                        url,
                                                        NULL,
                                                        BW_NAVIGATE_HISTORY,
                                                        NULL,
                                                        NULL,
                                                        NULL);
                } else {
                        error = browser_window_create(BW_CREATE_HISTORY,
                                                      url,
                                                      NULL,
                                                      NULL,
                                                      NULL);
                }
                nsurl_unref(url);
        }
        if (error != NSERROR_OK) {
                cocoa_warning(messages_get_errorcode(error), 0);
        }
}

- (IBAction) addBookmark: (id)sender
{
        struct browser_window *bw = [[(NetSurfApp *)NSApp frontTab] browser];
        if (bw != NULL) {
                hotlist_add_url(browser_window_get_url(bw));
        }
}

- (BOOL) validateUserInterfaceItem: (id)item
{
        SEL action = [item action];

        if (action == @selector( addBookmark: )) {
                return [(NetSurfApp *)NSApp frontTab] != nil;
        }

        return YES;
}

- (void) windowDidLoad
{
        hotlist_expand(false);
        hotlist_contract(true);

        [view setTree: tree];
}


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

- (IBAction) editSelected: (id)sender
{
        hotlist_edit_selection();
}

- (IBAction) deleteSelected: (id)sender
{
        hotlist_keypress(NS_KEY_DELETE_LEFT);
}

- (IBAction) addFolder: (id)sender
{
        hotlist_add_folder(NULL, false, 0);
}

@end