diff --git a/frontends/gnustep/BrowserWindowController.m b/frontends/gnustep/BrowserWindowController.m index 25fd3d366..d635da3ca 100644 --- a/frontends/gnustep/BrowserWindowController.m +++ b/frontends/gnustep/BrowserWindowController.m @@ -3,6 +3,7 @@ #import #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"]; diff --git a/frontends/gnustep/HistoryWindowController.h b/frontends/gnustep/HistoryWindowController.h index 9a3b44527..0a685052f 100644 --- a/frontends/gnustep/HistoryWindowController.h +++ b/frontends/gnustep/HistoryWindowController.h @@ -2,6 +2,7 @@ @interface HistoryWindowController: NSWindowController { id outlineView; + NSMutableDictionary *historyItems; } @end \ No newline at end of file diff --git a/frontends/gnustep/HistoryWindowController.m b/frontends/gnustep/HistoryWindowController.m index 01d1f32bb..3c4ffe483 100644 --- a/frontends/gnustep/HistoryWindowController.m +++ b/frontends/gnustep/HistoryWindowController.m @@ -1,18 +1,68 @@ #import #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 \ No newline at end of file diff --git a/frontends/gnustep/Website.h b/frontends/gnustep/Website.h index 80e58c1b5..64526da99 100644 --- a/frontends/gnustep/Website.h +++ b/frontends/gnustep/Website.h @@ -11,7 +11,7 @@ -(NSString*)name; -(NSURL*)url; --(void)setVisited; +-(void)addToHistory; +(NSArray*)historicWebsites; @end \ No newline at end of file diff --git a/frontends/gnustep/Website.m b/frontends/gnustep/Website.m index fdca7d199..82f9c9d0d 100644 --- a/frontends/gnustep/Website.m +++ b/frontends/gnustep/Website.m @@ -1,5 +1,10 @@ #import #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 \ No newline at end of file