/*******************************************************************************
 Copyright (c) 2004-2006 Jordy Rose
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 
 Except as contained in this notice, the name(s) of the above copyright holders
 shall not be used in advertising or otherwise to promote the sale, use or other
 dealings in this Software without prior authorization.
*******************************************************************************/

#import "NewDockController.h"

// Taken from NSApplication.h -- needed for pre-Tiger systems
#ifndef NSAppKitVersionNumber10_3
	#define NSAppKitVersionNumber10_3 743
#endif

static BOOL userHasTiger;

/*!
 * Controls the panel used to create new docks. Part of Dockyard's Controller layer in the
 * MVC paradigm, as all decisions are relayed from the controls on the panel to the
 * <code>DockyardPreferences</code> instance.
 */
@implementation NewDockController
+ (void)initialize
{
	userHasTiger = floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_3;
}

- (void)windowDidLoad
{
//	preferences = [menuExtra preferences];
	
	NSWindow *window = [self window];
	[window setLevel:NSStatusWindowLevel];
	[window center];
	
	if (!userHasTiger) {
		[dashboardItemsMatrix setEnabled:NO];
	}
}

/*!
 * This function is mostly a call to createDockWithName:fromFile:dashboardFile:,
 * but includes a switch statement to decide where to find the dock's plist
 * file. The choices are Apple's default dock, an empty dock, and the current dock.
 * The same choices are given for the dock's Dashboard contents on a 10.4 or later
 * system. Also, if the name chosen is invalid, the window will not close, but will
 * instead erase the name and wait for a different one.
 *
 * @see DockyardPreferences#createDockWithName:fromFile:dashboardFile:
 */
- (IBAction)confirm:(id)sender
{
	static NSString *appleDockPath = nil;
	static NSString *emptyDockPath = nil;
	static NSString *currentDockPath = nil;

	static NSString *appleDashboardPath = nil;
	static NSString *emptyDashboardPath = nil;
	static NSString *currentDashboardPath = nil;
	
	static NSString *localizedInvalidNameString = nil;

	NSString *name, *dockPath, *dashboardPath;
	BOOL loadNewDock = NO;
	
	name = [nameField stringValue];
	[name retain];
	loadNewDock = ([switchBox state] == NSOnState);
		
	switch ([dockItemsMatrix selectedRow]) {
	case 1: // Apple default dock
		if (appleDockPath == nil)
			appleDockPath = [[[NSBundle bundleWithPath:@"/System/Library/CoreServices/Dock.app"] pathForResource:@"default" ofType:@"plist"] retain];
		dockPath = appleDockPath;
		break;
	case 2: // Empty dock
		if (emptyDockPath == nil)
			emptyDockPath = [[[NSBundle bundleForClass:[self class]] pathForResource:@"emptyDock" ofType:@"plist"] retain];
		dockPath = emptyDockPath;
		break;
	case 0: // Same as current
	default: // Default to same as current - should never happen
		if (currentDockPath == nil)
			currentDockPath = [[@"~/Library/Preferences/com.apple.dock.plist" stringByStandardizingPath] retain];
		dockPath = currentDockPath;
	}
	
	if (userHasTiger) {
		switch ([dashboardItemsMatrix selectedRow]) {
		case 1: // Apple default dashboard -- location unknown. For now use the in-bundle copy.
			if (appleDashboardPath == nil)
//				appleDashboardPath = [[NSBundle bundleWithPath:@"/System/Library/CoreServices/Dock.app"] pathForResource:@"default" ofType:@"plist"];
				appleDashboardPath = [[[NSBundle bundleForClass:[self class]] pathForResource:@"defaultDashboard" ofType:@"plist"] retain];
			dashboardPath = appleDashboardPath;
			break;
		case 2: // Empty dashboard
			if (emptyDashboardPath == nil)
				emptyDashboardPath = [[[NSBundle bundleForClass:[self class]] pathForResource:@"emptyDashboard" ofType:@"plist"] retain];
			dashboardPath = emptyDashboardPath;
			break;
		case 0: // Same as current
		default: // Default to same as current - should never happen
			if (currentDashboardPath == nil)
				currentDashboardPath = [[@"~/Library/Preferences/com.apple.dashboard.plist" stringByStandardizingPath] retain];
			dashboardPath = currentDashboardPath;
		}
		
	} else {
		dashboardPath = nil;
	}
	
//	if ([menuExtra createDockWithFile:dockPath withName:name changeToIt:loadNewDock])
	if ([preferences createDockWithName:name fromFile:dockPath dashboardFile:dashboardPath]) {
		if (loadNewDock)
			[preferences changeDock:name];
		[self close];
//		[newDockWindow performClose:sender];
	} else {
		NSBeep();
		[nameField setStringValue:@""];
		if (localizedInvalidNameString == nil)
			localizedInvalidNameString = [NSLocalizedStringFromTableInBundle(@"Invalid Name!", nil, [NSBundle bundleForClass:[self class]], @"Error message given when a name is taken") retain];
		[((NSTextFieldCell *)[nameField cell]) setPlaceholderString:localizedInvalidNameString];
	}
	
	[name release];
}

@end
