Implement methods to get bookmarks

This commit is contained in:
anthony 2021-01-05 20:07:24 +00:00
parent 87ed8db872
commit 3f29c76540
5 changed files with 140 additions and 9 deletions

View File

@ -1,7 +1,11 @@
#import <Cocoa/Cocoa.h>
#define BOOKMARKS_PATH @"/.config/NetSurf"
#define UNSORTED_NAME @"Unsorted"
@interface BookmarkFolder: NSObject {
NSString *name;
NSString *path;
NSArray *children;
BookmarkFolder *parentFolder;
}
@ -10,6 +14,7 @@
-(NSArray*)children;
-(NSString*)name;
-(BOOL)isRootFolder;
-(BOOL)isUnsortedFolder;
-(void)deleteFolder;
-(void)addChild: (id)child;
-(void)removeChild: (id)child;

View File

@ -1,5 +1,20 @@
#import <Cocoa/Cocoa.h>
#import "BookmarkFolder.h"
#import "Website.h"
/*
Since there will be considerably less bookmarks than history entries, performance is less of
an issue here. So bookmarks are simply saved from NSDictionary into folders. One file
per website. Bookmark folders just mirror the directory structure. Child items are
lazy-loaded when requested.
*/
static BookmarkFolder *cachedRootFolder;
@interface BookmarkFolder(Private)
-(NSString*)path;
-(void)setPath: (NSString*)aPath;
-(void)setChildren: (NSArray*)children;
@end
@implementation BookmarkFolder
@ -7,18 +22,56 @@
if (self = [super init]) {
[aFolderName retain];
name = aFolderName;
children = [[NSArray alloc] init];
children = nil;
path = [[[aParent path] stringByAppendingPathComponent: aFolderName] retain];
}
return self;
}
-(void)dealloc {
if ([self isRootFolder]) {
cachedRootFolder = nil;
}
[children release];
[name release];
[path release];
[super dealloc];
}
-(NSArray*)children {
if (children != nil) {
return children;
}
NSMutableArray *newChildren = [[NSMutableArray alloc] init];
NSError *err = nil;
NSArray *fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
path error: &err];
if (err != nil) {
NSLog(@"Error reading bookmarks root directory");
return nil;
}
NSString *fileName;
NSString *chPath;
NSDictionary *chDict;
id child;
BOOL isDir;
for (NSUInteger i = 0; i < [fileNames count]; i++) {
fileName = [fileNames objectAtIndex: i];
chPath = [path stringByAppendingPathComponent: fileName];
isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath: chPath
isDirectory: &isDir];
if (isDir) {
child = [[BookmarkFolder alloc] initWithName: fileName parent: self];
[newChildren addObject: [child autorelease]];
} else {
chDict = [NSDictionary contentsOfFileAtPath: chPath];
child = [[Website alloc] initWithDictionary: chDict];
[newChildren addObject: [child autorelease]];
}
}
children = newChildren;
return children;
}
@ -27,15 +80,72 @@
}
-(BOOL)isRootFolder {
return NO;
return parentFolder == nil;
}
-(void)save {
-(BOOL)isUnsortedFolder {
return [name isEqual: UNSORTED_NAME];
}
-(void)deleteFolder {
}
-(void)addChild: (id)child {
if ([child isKindOfClass: [Website class]]) {
} else if ([child isKindOfClass: [BookmarkFolder class]]) {
}
}
-(void)removeChild: (id)child {
if ([child isKindOfClass: [Website class]]) {
} else if ([child isKindOfClass: [BookmarkFolder class]]) {
}
}
+(BookmarkFolder*)rootBookmarkFolder {
return nil;
if (cachedRootFolder != nil) {
return [cachedRootFolder autorelease];
}
NSString *rootPath = [NSHomeDirectory() stringByAppendingPathComponent:
BOOKMARKS_PATH];
NSString *unsortedPath = [rootPath stringByAppendingPathComponent: UNSORTED_NAME];
[[NSFileManager defaultManager] createDirectoryAtPath: rootPath attributes: nil];
[[NSFileManager defaultManager] createDirectoryAtPath: unsortedPath attributes: nil];
BookmarkFolder *rootFolder = [[BookmarkFolder alloc] initWithName: @"" parent: nil];
[rootFolder setPath: rootPath];
cachedRootFolder = rootFolder;
return [rootFolder autorelease];
}
+(BookmarkFolder*)unsortedBookmarkFolder {
id child;
NSArray *rootChildren = [[BookmarkFolder rootBookmarkFolder] children];
for (NSUInteger i = 0; i < [rootChildren count]; i++) {
child = [rootChildren objectAtIndex: i];
if ([child isKindOfClass: [BookmarkFolder class]]
&& [child isUnsortedFolder]) {
return child;
}
}
}
-(NSString*)path {
return path;
}
-(void)setPath: (NSString*)aPath {
[path release];
path = [aPath retain];
}
-(void)setChildren: (NSArray*)someChildren {
[children release];
children = [someChildren retain];
}
@end

View File

@ -1,6 +1,6 @@
#include <AppKit/AppKit.h>
#include "BookmarksWindowController.h"
#include "BookmarkFolder.h"
#import <AppKit/AppKit.h>
#import "BookmarksWindowController.h"
#import "BookmarkFolder.h"
@implementation BookmarksWindowController

View File

@ -1,7 +1,7 @@
#import <Cocoa/Cocoa.h>
#define WebsiteHistoryUpdatedNotificationName @"WebsiteHistoryUpdatedNotification"
#define HISTORY_PATH @"/.cache/NetSurf"
#define HISTORY_PATH @"/.config/NetSurf"
struct website_data {
int len_name;
@ -20,10 +20,13 @@ struct website_data {
-(id)initWithName: (NSString*)aName url: (NSString*)aUrl;
-(id)initWithData: (struct website_data*)someData atFileOffset: (long)aFileOffset;
-(id)initWithDictionary: (NSDictionary*)aDictionary;
-(NSString*)name;
-(NSString*)url;
-(long)fileOffset;
-(NSDictionary*)asDictionary;
-(void)open;
-(void)addToHistory;
@end

View File

@ -30,6 +30,12 @@
return self;
}
-(id)initWithDictionary: (NSDictionary*)aDictionary {
NSString *aName = [aDictionary objectForKey: @"name"];
NSString *aUrl = [aDictionary objectForKey: @"url"];
return [self initWithName: aName url: aUrl];
}
-(void)dealloc {
free(data);
[super dealloc];
@ -44,6 +50,13 @@
data->len_url];
}
-(NSDictionary*)asDictionary {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject: [self name] forKey: @"name"];
[dict setObject: [self url] forKey: @"url"];
return dict;
}
-(long)fileOffset {
return fileOffset;
}