/* # Created by Matteo Rattotti on 13/03/06. # Copyright (c) 2006 - 2008 Shiny Frog. All rights reserved. # Contact matteo.rattotti@shinyfrog.net for any problem # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ // // webImpressor.m // Netfixer // // Created by Matteo Rattotti on 10/07/06. // Copyright 2006 __MyCompanyName__. All rights reserved. // #import "webImpressor.h" #import "netFixerAppDelegate.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]; } // makes sure a file isn't written over - (NSString *)safeNameForFile:(NSString *)file { NSFileManager *fm = [NSFileManager defaultManager]; if (![fm fileExistsAtPath:file]) return file; int i; NSString *pathExtension = [file pathExtension]; NSString *pathWithoutExtension = [file stringByDeletingPathExtension]; NSString *checkPath; i = 1; do { checkPath = [NSString stringWithFormat:@"%@ %d.%@", pathWithoutExtension, i, pathExtension]; i++; } while ([fm fileExistsAtPath:checkPath]); return checkPath; } - (void)saveOutputFile:(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"];} } //Write output file with checked and safe filename. [[img representationUsingType:imgType properties:nil] writeToFile:[self safeNameForFile: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];*/ NSTimeInterval ti = ([[NSApp delegate] delayValue]); [self performSelector:@selector(saveImage:) withObject:frame afterDelay:ti]; //NSBitmapImageRep *image = [self view2bmp:view]; //[self saveImage:url imageRep:image]; //NSLog( @"%d", [[NSApp delegate] delayValue] ); } } - (void)saveImage:(WebFrame*)frame{ NSString *url = [[[[frame dataSource]initialRequest]URL]absoluteString]; NSView *view = [[frame frameView] documentView]; [self correctView:view]; NSBitmapImageRep *image = [self view2bmp:view]; [self saveOutputFile: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]; } } - (void)webView:(WebView *)webview willPerformClientRedirectToURL:(NSURL *)URL delay:(NSTimeInterval)seconds fireDate:(NSDate *)date forFrame:(WebFrame *)frame { [[NSApp delegate] updateAddrFieldAfterRedirect:[URL absoluteString]]; } @end