//
//  WebFrame.m
//  mySTEP
//
//  Created by Dr. H. Nikolaus Schaller on Mon Jan 05 2004.
//  Revised May 2006
//  Copyright (c) 2004 DSITRI. All rights reserved.
//

#import <WebKit/WebFrame.h>
#import <Foundation/NSXMLParser.h>
#import "Private.h"

@implementation WebFrame

- (id) initWithName:(NSString *) n webFrameView:(WebFrameView *) frameView webView:(WebView *) webView
{
    self=[super init];
    if(self)
		{ // If an error occurs here, send a [self release] message and return nil.
		_name=[n retain];
		_frameview=[frameView retain];
		_webview=[webView retain];
		_children=[[NSMutableArray alloc] initWithCapacity:5];
		}
    return self;
}

- (void) _setParentFrame:(WebFrame *) parent;
{
	if(_parent)
		[(NSMutableArray *) [_parent childFrames] removeObject:self];
	ASSIGN(_parent, parent);
	if(_parent)
		[(NSMutableArray *) [_parent childFrames] addObject:self];
}

- (void) dealloc;
{
	[self stopLoading];	// cancel any pending actions (i.e. _provisionalDataSource)
	[_dataSource release];
	if(_parent)
		[(NSMutableArray *) [_parent childFrames] addObject:self];
	[_name release];
	[_frameview release];
	[_webview release];
	[_children release];
	[_request release];
	[_dataSource release];
	[super dealloc];
}

- (void) loadRequest:(NSURLRequest *) req;
{
#if 1
	NSLog(@"%@ loadRequest:%@", self, req);
#endif
	[_request autorelease];
	_request=[req copy];	// make a copy so that we can reload any time
	[self reload];
}

- (void) stopLoading;
{
	[_provisionalDataSource release];
	_provisionalDataSource=nil;
}

- (void) reload;
{
#if 1
	NSLog(@"reload %@", self);
	NSLog(@"_request=%@", _request);
#endif
	_provisionalDataSource=[[WebDataSource alloc] initWithRequest:_request];
	[_provisionalDataSource _setWebFrame:self];
}

- (void) _finishedLoading;
{ // callback from data source
#if 1
	NSLog(@"WebFrame finishedLoading");
#endif
	[_dataSource autorelease];
	_dataSource=_provisionalDataSource;
	_provisionalDataSource=nil;
}

- (void) loadAlternateHTMLString:(NSString *) string baseURL:(NSURL *) url forUnreachableURL:(NSURL *) unreach;
{ // render HTML string
  // combine string with an error message for unreachable URL
	[self loadHTMLString:string baseURL:url];
}

- (void) loadArchive:(WebArchive *) archive;
{
	NIMP;
}

- (void) loadData:(NSData *) data MIMEType:(NSString *) mime textEncodingName:(NSString *) encoding baseURL:(NSURL *) url;
{ // NOTE: data might be incomplete, i.e. we will be called again as soon as new data arrives
	NSLog(@"MIME %@", mime);
	NSLog(@"textEncodingName %@", encoding);
	NSLog(@"baseURL %@", url);
}

- (void) loadHTMLString:(NSString *) string baseURL:(NSURL *) url;
{
	[self loadData:[string dataUsingEncoding:NSUnicodeStringEncoding]
		  MIMEType:@"Text/html"
  textEncodingName:@"utf-16"
		   baseURL:url];
}

- (WebFrame *) findFrameNamed:(NSString *) n;
{
	if([n isEqualToString:@"_self"] || [n isEqualToString:@"_current"])
		return self;
	if([n isEqualToString:@"_parent"])
		return _parent?_parent:self;
	if([n isEqualToString:@"_top"])
		{
		WebFrame *f=self;
		while([f parentFrame])
			f=[f parentFrame];	// search top element
		return f;
		}
	if([n isEqualToString:_name])
		return self;
	// FIXME: search descendents and parents
	return nil;
}

- (WebDataSource *) dataSource; { return _dataSource; }
- (WebDataSource *) provisionalDataSource; { return _provisionalDataSource; }
- (WebFrame *) parentFrame; { return _parent; }
- (NSArray *) childFrames; { return _children; }
- (WebFrameView *) frameView; { return _frameview; }
- (WebView *) webView; { return _webview; }
- (NSString *) name; { return _name; }

#ifndef __mySTEP__
- (DOMDocument *) DOMDocument; { return [[_dataSource representation] root]; }
#endif

// should be passed on creation

- (DOMHTMLElement *) frameElement; { return NIMP; }

@end