/* # Created by Matteo Rattotti on 13/03/06. # Copyright (c) 2006 - 2007 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. */ #import "img2icsnController.h" #import "iconFamily.h" @implementation img2icsnController #define INVALID_IMG NSLocalizedStringFromTable(@"Invalid image file", @"img2icnsLOC", @"Warning when an invalid image is processed") #define STATE_READY NSLocalizedStringFromTable(@"Ready",@"img2icnsLOC",@"When program is ready to process image") #define STATE_BUSY NSLocalizedStringFromTable(@"Converting...",@"img2icnsLOC",@"When program is convering images") - (void)applicationWillFinishLaunching:(NSNotification *)aNotification{ //NSLog(@"%d", [self outputType]); } - (void)awakeFromNib { // Set export path from pref NSString* path = [self exportPath]; if(path != nil) [pathField setStringValue:path]; else [pathField setStringValue:@"Desktop"]; dropIcon = nil; /* Old now using bindings // set output file type from pref int output = [self outputType]; int i; for (i=0; i<[prefMatrix numberOfRows]; i++) if (i == output) [[prefMatrix cellAtRow:i column:0] setState:YES]; else [[prefMatrix cellAtRow:i column:0] setState:NO]; */ } -(void) convert:(NSArray*)files{ [self setBusy:1]; // OLD //int outType = 0; //if([[prefMatrix cellAtRow:0 column:0] state] != YES) // outType = 1; int outType = [self outputType]; NSString* exPath = [NSString stringWithFormat:@"%@/%@", NSHomeDirectory(), [pathField stringValue]]; NSString* tmpPath; NSString* outPath; NSEnumerator *pathsEnum = [files objectEnumerator]; while (tmpPath = [pathsEnum nextObject]) { /*NSLog(@"tmp -> %@", tmpPath); if(![[tmpPath lowercaseString]hasSuffix:@"jpg"] && ![[tmpPath lowercaseString] hasSuffix:@"gif"] && ![[tmpPath lowercaseString] hasSuffix:@"png"] && ![[tmpPath lowercaseString] hasSuffix:@"tif"] && ![[tmpPath lowercaseString] hasSuffix:@"tiff"] && ![[tmpPath lowercaseString] hasSuffix:@"bmp"] ) continue;*/ NSImage *image = [[[NSImage alloc] initWithContentsOfFile:tmpPath] autorelease]; IconFamily *icon = [IconFamily iconFamilyWithThumbnailsOfImage:image]; NSImageRep* imageRep = [image bestRepresentationForDevice:nil]; // Checking if the file is a supported image typpe if (![imageRep isKindOfClass:[NSBitmapImageRep class]]) { NSLog(INVALID_IMG); image = nil; //continue; } outPath = [NSString stringWithFormat:@"%@/%@", exPath, [tmpPath lastPathComponent]]; // User wants icns file if (image != nil && outType == 0) { NSString *icnsPath = [self safeNameForFile:[[outPath stringByDeletingPathExtension] stringByAppendingString:@".icns"]]; //NSLog(@"icnsPath -> %@", icnsPath); [icon writeToFile:icnsPath]; [icon setAsCustomIconForFile:icnsPath]; } // User wants folder icon else if (image != nil && outType == 1) { NSString *pathDir = [outPath stringByDeletingPathExtension]; //NSLog(@"pathDir -> %@", pathDir); [[NSFileManager defaultManager] createDirectoryAtPath:pathDir attributes:nil]; [icon setAsCustomIconForDirectory:pathDir]; } // User wants a droplets else if (image != nil && outType ==2){ //NSLog(@"in"); if(dropIcon != nil){ //[dropIcon release]; dropIcon = nil; } //NSLog(@"making drop icon"); dropIcon = [[IconFamily iconFamilyWithThumbnailsOfImage:image] retain]; //icon; //NSLog(@"icon -> %@", dropIcon); [image setScalesWhenResized:1]; [image setSize:NSMakeSize(128.0,128.0)]; [image retain]; [dropView setImage:image]; } else{ // Non valid image, or User want to drop somethings if(dropIcon != nil){ //NSLog(@"value icon -> %@", dropIcon); NSLog(tmpPath); BOOL isDir; // Setting icon if([[NSFileManager defaultManager] fileExistsAtPath:tmpPath isDirectory:&isDir] && isDir) [dropIcon setAsCustomIconForDirectory:tmpPath]; else [dropIcon setAsCustomIconForFile:tmpPath]; } } } [self setBusy:0]; } // 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; } - (IBAction)changeExpPath:(id)sender { /* Creating open panel and setting prefs */ int result; NSOpenPanel *oPanel = [NSOpenPanel openPanel]; [oPanel setCanChooseFiles:NO]; [oPanel setCanChooseDirectories:YES]; [oPanel setCanCreateDirectories:YES]; [oPanel setAllowsMultipleSelection:NO]; /*end open panel setting*/ result = [oPanel runModalForDirectory:NSHomeDirectory() file:nil types:nil]; if (result == NSOKButton) { /* Getting directory target and stripping HOME path */ NSArray *filesToOpen = [oPanel filenames]; NSString* path = [filesToOpen objectAtIndex:0]; NSRange home = [path rangeOfString:NSHomeDirectory()]; [pathField setStringValue:[path substringFromIndex:home.length+1]]; [pathField setToolTip:[path substringFromIndex:home.location]]; } } - (void)application:(NSApplication *)sender openFiles:(NSArray *)filePaths { [self convert:filePaths]; } - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag { [mainWin makeKeyAndOrderFront:self]; return 1; } - (void)applicationWillTerminate:(NSNotification *)aNotification { /* Older prefs int outType = 0; if([[prefMatrix cellAtRow:0 column:0] state] != YES) outType = 1; */ // Saving pref before the app quit //[[NSUserDefaults standardUserDefaults] setInteger:outType forKey:@"outputType"]; [[NSUserDefaults standardUserDefaults] setObject:[pathField stringValue] forKey:@"exportPath"]; } - (void) setBusy:(BOOL) busyState { if(busyState){ [progressWeel startAnimation:self]; [statusField setStringValue:STATE_BUSY]; [mainWin display]; } else{ [progressWeel stopAnimation:self]; [statusField setStringValue:STATE_READY]; } } - (IBAction)closeWin:(id)sender { //[mainWin close]; [[NSApp keyWindow]close]; } - (IBAction)minWin:(id)sender { [mainWin miniaturize:self]; } - (int)outputType { int i; for (i=0; i<[prefMatrix numberOfRows]; i++) if ([[prefMatrix cellAtRow:i column:0] state]) return i; return 0; //return [[NSUserDefaults standardUserDefaults] integerForKey:@"outputType"]; } - (NSString *) exportPath { return [[NSUserDefaults standardUserDefaults] stringForKey:@"exportPath"]; } - (void)applicationWillUpdate:(NSNotification *)aNotification{ if([mainWin viewsNeedDisplay]) [mainWin display]; } - (void)applicationWillBecomeActive:(NSNotification *)aNotification{ //NSLog(@"active"); [mainWin display]; } - (void)applicationDidResignActive:(NSNotification *)aNotification{ [mainWin display]; } - (void)applicationDidUpdate:(NSNotification *)aNotification{ //if([mainWin viewsNeedDisplay]) // [mainWin display]; } - (void)applicationDidBecomeActive:(NSNotification *)aNotification{ //NSLog(@"did active"); } @end