// // webImpressor.m // Netfixer // // Created by Matteo Rattotti on 10/07/06. // Copyright 2006 __MyCompanyName__. All rights reserved. // #import "webImpressor.h" #import @implementation webImpressor - (id) init{ [super init]; imgType = NSPNGFileType; rect = NSMakeRect(0,0,800,600); size = NSMakeSize(800,600); return self; } - (void) dealloc{ [super dealloc]; } - (void) setProgWell: (NSProgressIndicator *)p{ pwell = p; } - (void) setFileName: (NSString *) filename{ fName = [[NSString alloc]initWithString:filename]; } - (void) setFormat: (NSBitmapImageFileType) type{ imgType = type; } - (void) setCanvas:(NSRect) imgRect canvasSize: (NSSize) cSize{ rect = imgRect; size = cSize; } - (NSBitmapImageRep *) view2bmp: (NSView *) view{ [view lockFocus]; NSBitmapImageRep *image = [NSBitmapImageRep alloc]; [image initWithFocusedViewRect:[view bounds]]; [view unlockFocus]; return image; } - (void) correctView: (NSView *) view{ NSRect bound = [view bounds]; NSSize s = bound.size; [[view window] display]; [[view window] setContentSize:s]; [view setFrame:[view bounds]]; } - (void) resetWebView: (WebView *) webview{ //NSRect rect = NSMakeRect(0,0,800,600); //NSSize size = NSMakeSize(800,600); [[webview window] setContentSize:size]; [webview setFrame:rect]; } - (void) saveImage: (NSString *) name imageRep: (NSBitmapImageRep *) img{ if([[fName pathExtension] isEqualToString:@""]){ if(imgType == NSPNGFileType){fName = [fName stringByAppendingString:@".png"];} else if(imgType == NSTIFFFileType){fName = [fName stringByAppendingString:@".tif"];} else if(imgType == NSGIFFileType){fName = [fName stringByAppendingString:@".gif"];} else if(imgType == NSJPEGFileType){fName = [fName stringByAppendingString:@".jpg"];} else if(imgType == NSBMPFileType){fName = [fName stringByAppendingString:@".bmp"];} } [[img representationUsingType:imgType properties:nil] writeToFile:fName atomically:YES]; /* Stopping animation well*/ [pwell stopAnimation:self]; } - (void)webView:(WebView *)webview didFinishLoadForFrame:(WebFrame *)frame{ if(frame == [webview mainFrame]){ [self resetWebView:webview]; NSString* url = [[[[frame dataSource]initialRequest]URL]absoluteString]; NSView *view = [[frame frameView] documentView]; [self correctView:view]; NSBitmapImageRep *image = [self view2bmp:view]; [self saveImage:url imageRep:image]; } } - (void)webView:(WebView *)webview didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame{ if(frame == [webview mainFrame]){ NSAlert *a = [NSAlert alertWithMessageText:@"Error loading this url" defaultButton:@"ok" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Please check address for typo, or maybe your connection..."]; [a runModal]; /* Stopping animation well*/ [pwell stopAnimation:self]; } } - (void)webView:(WebView *)webview didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame{ if(frame == [webview mainFrame]){ NSAlert *a = [NSAlert alertWithMessageText:@"Error loading this url" defaultButton:@"ok" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Please check address for typo, or maybe your connection..."]; [a runModal]; /* Stopping animation well*/ [pwell stopAnimation:self]; } } @end