//
//	File:		ICeCoffEEController.m
//
//	Contains:	ICeCoffEE APE preference pane controller
//
//	Copyright:	Copyright (c) 2003, Nicholas Riley
//			All Rights Reserved.
//
//	Author(s):	Nicholas Riley (Sun Jan 19 2003)
//
// ===========================================================================

#import <ApplicationEnhancer/ApplicationEnhancer.h>
#import "ICeCoffEEController.h"
#import "ICeCoffEEShared.h"
#import "ICeCoffEEServicePrefController.h"

// Just a handy macro for localized strings from our bundle; unused in this project.
#define NSLocalizedString_(key, comment) [[self bundle] localizedStringForKey:(key) value:@"" table:nil]

@implementation ICeCoffEEController

// This method should return an id of a class that implements the protocol
// required by APE Manager.

+ (id)prefPaneHandlerForAPE:(NSBundle*)apeBundle andBundle:(NSBundle*)prefPaneBundle
{
    return [[self alloc] initWithAPE: apeBundle andBundle: prefPaneBundle];
}

// Init the instance of our class
- (id)initWithAPE:(NSBundle*)apeBundle andBundle:(NSBundle*)prefPaneBundle
{
    [self setAPEBundle: apeBundle prefPaneBundle: prefPaneBundle];
    
    [NSBundle loadNibNamed:@"APE Manager plugin" owner:self]; // load our nib file; it will call our awakeFromNib method
    
    return self;
}

// This method is called when the preference pane is about to be shown.
- (void)willLoad
{
    // we do nothing here
}

// This method is called when the preference pane will be dismissed.
- (void)willUnload
{
    [prefs release];
    prefs = nil;
}

// This method should return an NSView to be loaded into APE Manager preference
// area.
- (NSView *)mainView
{
    return [window contentView];
}

#define ICP(pref) (NSString *)kIC ## pref

// This method is called once our nib file is loaded (we load it in the init method)
- (void)awakeFromNib
{
    // load the preferences for our APE module
    prefs = [[CFPreferencesWrapper_ICeCoffEE preferencesWithApplication: (NSString *)kICBundleIdentifier] retain];

    CFBundleRef apeBundle = CFBundleCreate(NULL, (CFURLRef)[NSURL fileURLWithPath: [_apeBundle bundlePath]]);
    if ([prefs integerForKey: ICP(LastLoadedVersion)] != CFBundleGetVersionNumber(apeBundle)) {
        [tabView selectTabViewItemWithIdentifier: @"noPrefs"];
        CFRelease(apeBundle);
        return;
    }
    CFRelease(apeBundle);
    [tabView selectTabViewItemWithIdentifier: @"prefs"];

    [commandClickEnabled setState: [prefs boolForKey: ICP(CommandClickEnabled)]];
    [self commandClickEnabled: nil];
    [textBlinkEnabled setState: [prefs boolForKey: ICP(TextBlinkEnabled)]];
    [self textBlinkEnabled: nil];
    int blink = [prefs integerForKey: ICP(TextBlinkCount)];
    if (blink != -1 && (blink = [textBlinkCount indexOfItemWithTag: blink]) != -1)
        [textBlinkCount selectItemAtIndex: blink];
    [servicesInContextualMenu setState: [prefs boolForKey: ICP(ServicesInContextualMenu)]];
    [servicesInMenuBar setState: [prefs boolForKey: ICP(ServicesInMenuBar)]];
    [terminalRequireOptionForSelfDrag setState: [prefs boolForKey: ICP(TerminalRequireOptionForSelfDrag)]];
    [errorSoundEnabled setState: [prefs boolForKey: ICP(ErrorSoundEnabled)]];
    [errorDialogEnabled setState: [prefs boolForKey: ICP(ErrorDialogEnabled)]];
}

- (void)setAPEBundle:(NSBundle*)apeBundle prefPaneBundle:(NSBundle *)aBundle;
{
    [apeBundle retain];
    [_apeBundle release];
    _apeBundle = apeBundle;
    [aBundle retain];
    [_bundle release];
    _bundle = aBundle;
}

- (NSBundle *)bundle;
{
    [_bundle autorelease];
    return [_bundle retain];
}

- (void)prefsChanged;
{
    [prefs synchronize];
    APEMessageBroadcast(kICBundleIdentifier, kICPreferencesChanged, NULL);
}

- (IBAction)commandClickEnabled:(NSButton *)sender;
{
    BOOL enabled = [commandClickEnabled state];
    if (sender != nil) {
        [prefs setBool: enabled forKey: ICP(CommandClickEnabled)];
        [self prefsChanged];
    }
    [textBlinkEnabled setEnabled: enabled];
    [textBlinkCount setEnabled: enabled ? [textBlinkEnabled state] : NO];
}
- (IBAction)textBlinkEnabled:(NSButton *)sender;
{
    BOOL enabled = [textBlinkEnabled state];
    if (sender != nil) {
        [prefs setBool: enabled forKey: ICP(TextBlinkEnabled)];
        [self prefsChanged];
    }
    [textBlinkCount setEnabled: enabled];
}
- (IBAction)textBlinkCount:(NSPopUpButton *)sender;
{
    [prefs setInteger: [[textBlinkCount selectedItem] tag] forKey: ICP(TextBlinkCount)];
    [self prefsChanged];
}
- (IBAction)servicesInContextualMenu:(NSButton *)sender;
{
    [prefs setBool: [servicesInContextualMenu state] forKey: ICP(ServicesInContextualMenu)];
    [self prefsChanged];
}
- (IBAction)servicesInMenuBar:(NSButton *)sender;
{
    [prefs setBool: [servicesInMenuBar state] forKey: ICP(ServicesInMenuBar)];
    [self prefsChanged];
}
- (IBAction)editContextualServices:(NSButton *)sender;
{
    [[ICeCoffEEServicePrefController alloc] performSelector: @selector(initWithParentWindow:) withObject: [NSApp mainWindow] afterDelay: 0.01];
}
- (IBAction)terminalRequireOptionForSelfDrag:(NSButton *)sender;
{
    [prefs setBool: [terminalRequireOptionForSelfDrag state] forKey: ICP(TerminalRequireOptionForSelfDrag)];
    [self prefsChanged];
}
- (IBAction)errorSoundEnabled:(NSButton *)sender;
{
    [prefs setBool: [errorSoundEnabled state] forKey: ICP(ErrorSoundEnabled)];
    [self prefsChanged];
}
- (IBAction)errorDialogEnabled:(NSButton *)sender;
{
    [prefs setBool: [errorDialogEnabled state] forKey: ICP(ErrorDialogEnabled)];
    [self prefsChanged];
}

@end
