History - WIP basic history impl

This commit is contained in:
anthony 2020-12-24 22:11:41 +00:00
parent 4a696cf84a
commit e46bb36669
5 changed files with 142 additions and 5 deletions

View File

@ -3,6 +3,7 @@
#import <AppKit/AppKit.h>
#import "BrowserWindowController.h"
#import "PlotView.h"
#import "Website.h"
#import "netsurf/browser_window.h"
#import "utils/nsurl.h"
#import "desktop/browser_history.h"
@ -122,7 +123,17 @@
}
-(void)newContent {
NSLog(@"New content");
struct nsurl *url = browser_window_access_url(browser);
const char *title = browser_window_get_title(browser);
if (title == NULL) {
title = "";
}
NSString *name = [NSString stringWithCString: title];
NSString *urlStr = [NSString stringWithCString: nsurl_access(url)];
Website *website = [[Website alloc] initWithName: name
url: [NSURL URLWithString: urlStr]];
[website addToHistory];
[website release];
}
-(void)startThrobber {
[refreshButton setTitle: @"Stop"];

View File

@ -2,6 +2,7 @@
@interface HistoryWindowController: NSWindowController {
id outlineView;
NSMutableDictionary *historyItems;
}
@end

View File

@ -1,18 +1,68 @@
#import <Cocoa/Cocoa.h>
#import "HistoryWindowController.h"
#import "Website.h"
@implementation HistoryWindowController
-(id)init {
if (self = [super initWithWindowNibName: @"History"]) {
// .....
NSArray *allHistory = [Website historicWebsites];
historyItems = [[NSMutableDictionary alloc] init];
[historyItems setObject: allHistory forKey: @"recent"];
}
return self;
}
-(void)dealloc {
[historyItems release];
[super dealloc];
}
-(void)awakeFromNib {
NSLog(@"Awoke from nib...");
[[self window] makeKeyAndOrderFront: self];
[outlineView expandItem: [[historyItems allValues] firstObject] expandChildren: NO];
}
-(id)outlineView: (NSOutlineView*)outlineView child: (NSInteger)index ofItem: (id)item {
if (item == nil) {
return [[historyItems allValues] firstObject];
} else {
return [item objectAtIndex: index];
}
}
-(BOOL)outlineView: (NSOutlineView*)outlineView isItemExpandable: (id)item {
if ([item isKindOfClass: [NSArray class]]) {
return YES;
} else {
return NO;
}
}
-(NSInteger)outlineView: (NSOutlineView*)outlineView numberOfChildrenOfItem: (id)item {
if (item == nil) {
return 1;
}
return [item count];
}
-(id)outlineView: (NSOutlineView*)outlineView objectValueForTableColumn: (NSTableColumn*)tableColumn byItem: (id)item {
if ([item isKindOfClass: [NSArray class]]) {
return @"Recent History";
} else if ([item isKindOfClass: [Website class]]) {
return [item name];
} else {
NSLog(@"clas: %@", [item class]);
return @"Error";
}
}
-(BOOL)outlineView: (NSOutlineView*)outlineView shouldSelectItem: (id)item {
NSLog(@"clicked item %@", item);
return YES;
}
@end

View File

@ -11,7 +11,7 @@
-(NSString*)name;
-(NSURL*)url;
-(void)setVisited;
-(void)addToHistory;
+(NSArray*)historicWebsites;
@end

View File

@ -1,5 +1,10 @@
#import <Cocoa/Cocoa.h>
#import "Website.h"
#define HISTORY_PATH @".cache/NetSurf"
static NSMutableArray *history;
@implementation Website
-(id)initWithName: (NSString*)aName url: (NSURL*)aUrl {
@ -28,13 +33,83 @@
return url;
}
-(void)setVisited {
// MARK: - History implementation
+(id)websiteWithDictionary: (NSDictionary*)dictionary {
Website *ret = [[[Website alloc] init] autorelease];
if (ret != nil) {
ret->name = [dictionary objectForKey: @"name"];
[ret->name retain];
ret->url = [NSURL URLWithString: [dictionary objectForKey: @"url"]];
[ret->url retain];
ret->lastVisited = [NSDate dateWithTimeIntervalSince1970: [[dictionary
objectForKey: @"date"] doubleValue]];
[ret->lastVisited retain];
}
return ret;
}
-(NSDictionary*)toDictionary {
return [NSDictionary dictionaryWithObjectsAndKeys: name, @"name",
[url absoluteString], @"url",
[NSNumber numberWithDouble: [lastVisited timeIntervalSince1970]], @"date",
nil];
}
+(void)saveHistoryToDisk {
NSLog(@"Save history to disk");
if (history == nil) {
return;
}
NSError *error = nil;
NSDictionary *attrs = [NSDictionary dictionary];
BOOL ok = [[NSFileManager defaultManager] createDirectoryAtPath: [NSString
pathWithComponents: [NSArray arrayWithObjects: NSHomeDirectory(),
HISTORY_PATH, nil]] withIntermediateDirectories: YES attributes: attrs
error: &error];
if (!ok) {
NSLog(@"Error creating cache dir!");
}
NSMutableArray *toSave = [NSMutableArray array];
for (NSUInteger i = 0; i < [history count]; i++) {
[toSave addObject: [[history objectAtIndex: i] toDictionary]];
}
ok = [toSave writeToFile: [NSString pathWithComponents: [NSArray
arrayWithObjects: NSHomeDirectory(), HISTORY_PATH, @"history", nil]]
atomically: YES];
if (!ok) {
NSLog(@"Failed to save latest history to file");
}
}
+(void)initHistoryIfNeeded {
if (history == nil) {
NSArray *historyDicts = [NSMutableArray arrayWithContentsOfFile:
[NSString pathWithComponents: [NSArray arrayWithObjects:
NSHomeDirectory(), HISTORY_PATH, @"history", nil]]];
history = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < [historyDicts count]; i++) {
[history addObject: [Website websiteWithDictionary: [historyDicts
objectAtIndex: i]]];
}
[[NSNotificationCenter defaultCenter] addObserver: [self class]
selector: @selector(saveHistoryToDisk)
name: NSApplicationWillTerminateNotification
object: nil];
}
}
-(void)addToHistory {
[Website initHistoryIfNeeded];
[lastVisited release];
lastVisited = [[NSDate alloc] init];
[history addObject: self];
NSLog(@"Added %@ , %@ to history!", [self name], [self url]);
}
+(NSArray*)historicWebsites {
return [NSArray array];
[Website initHistoryIfNeeded];
return history;
}
@end