// ===========================================================================
//
//	File:		APEMain.m
//
//	Contains:	ICeCoffEE APE Module code
//
//	Copyright:	Copyright (c) 2003, Nicholas Riley
//			All Rights Reserved.
//
//	Author(s):	Nicholas Riley (Sun Jan 19 2003)
//
// ===========================================================================

#import <Carbon/Carbon.h>
#import <ApplicationEnhancer/ApplicationEnhancer.h>
#import <ApplicationEnhancer/APETools.h>
#import <objc/objc-runtime.h>
#import "ICeCoffEE.h"
#import "ICeCoffEETextEdit.h"
#import "CPS.h"

//ĄĄĄ Our settings

//ĄĄĄ Function prototypes
static void ICCF_ReloadPrefs();				// reloads our preferences
static void ICCF_MigratePrefs();			// migrates prefs from 1.0Đ1.2

//ĄĄĄ Enter sandman, the code begins --

#define ICCF_GET_PATCHCLASS(patchclass) \
    struct objc_class *patchclass = objc_getClass(patchclass ## Name); \
    if (patchclass == NULL) { \
        ICapeprintf("can't get %s\n", patchclass ## Name); \
        return NO; \
    }

#define ICCF_GET_METHOD(name, patchclass, sel) \
    Method name = class_getInstanceMethod(patchclass, sel); \
    if (name == NULL) { \
        ICapeprintf("can't get %s\n", patchclass ## Name); \
        return NO; \
    }

BOOL ICCF_PatchMethod(char *patcheeClassName,
                      char *patchClassName,
                      char *patchSuperclassName,
                      char *selectorString) {

    ICCF_GET_PATCHCLASS(patcheeClass);
    ICCF_GET_PATCHCLASS(patchClass);
    ICCF_GET_PATCHCLASS(patchSuperclass);

    SEL selector = sel_getUid(selectorString);

    ICCF_GET_METHOD(patcheeMethod, patcheeClass, selector);
    ICCF_GET_METHOD(patchMethod, patchClass, selector);
    ICCF_GET_METHOD(patchSuperMethod, patchSuperclass, selector);

    if (APEPatchCreate(patchSuperMethod->method_imp,
                       APEPatchCreate(patcheeMethod->method_imp, patchMethod->method_imp)) == NULL) {
        ICapeprintf("can't patch class %s with [%s %s] super %s", patcheeClassName, patchClassName, selectorString, patchSuperclassName);
        return NO;
    }
    return YES;
}

CFBundleRef ICCF_bundle;

void APEBundleMain(CFBundleRef inBundle)
{
    ICCF_MigratePrefs();

    UInt32 icVersion = CFBundleGetVersionNumber(inBundle);
    ICapeprintf("ICeCoffEE APE: bundle version is %ld (0x%x)\n", icVersion, icVersion);
    CFNumberRef icVersionRef = CFNumberCreate(NULL, kCFNumberLongType, &icVersion);
    CFPreferencesSetAppValue(kICLastLoadedVersion, icVersionRef, kICBundleIdentifier);
    SAFE_RELEASE(icVersionRef);

    // first check if this application is in the exclude list;
    // if it is, simply return and do not apply any patches.
    // APETools will help us with that; APE Manager will take care of
    // exclude list management for us.
    if (APEToolsIsInExcludeList(kICBundleIdentifier, NULL)) {
        ICapeprintf("ICeCoffEE APE: not loading as this application is excluded.\n");
        return;
    }

    CPSProcessSerNum psn;
    OSStatus err = CPSGetCurrentProcess(&psn);
    if (err != noErr) {
        apeprintf("ICeCoffEE APE: Can't get process serial number for current process (error %ld); not loading in this application\n", err);
        return;
    }
    
    CPSProcessInfoRec info;
    err = CPSGetProcessInfo(&psn, &info, NULL, 0, NULL, NULL, 0);
    if (err != noErr) {
        apeprintf("ICeCoffEE APE: Can't get process information (error %ld); not loading in this application\n", err);
        return;
    }
    ICapeprintf("ICeCoffEE APE: got process attributes = 0x%lx\n", info.Attributes);
    if (info.Attributes & (kCPSBGOnlyAttr | kCPSUIElementAttr | kCPSFullScreenAttr)) {
        ICapeprintf("ICeCoffEE APE: not loading as this application is background-only\n");
        return;
    }

    ICCF_bundle = CFBundleGetBundleWithIdentifier(kICBundleIdentifier);
    CFStringRef bundleID = CFBundleGetIdentifier(CFBundleGetMainBundle());
    BOOL shouldLoadInNSTextView = YES;

    // XXX handle patching error return from ICCF_PatchMethod
    if (bundleID != NULL) {
        if (CFStringCompare(bundleID, CFSTR("com.apple.projectbuilder"), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
            ICCF_PatchMethod("PBXTextView", "ICeCoffEEMenuOnly", "ICeCoffEEMenuSuper", "menuForEvent:");
            ICapeprintf("ICeCoffEE APE: loaded in PBXTextView for PB!\n");
            shouldLoadInNSTextView = NO;
        } else if (CFStringCompare(bundleID, CFSTR("com.apple.terminal"), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
            ICCF_PatchMethod("TermSubview", "ICeCoffEEMenuOnly", "ICeCoffEEMenuSuper", "menuForEvent:") &&
            ICCF_PatchMethod("TermSubview", "ICeCoffEETerminal", "ICECoffEETermSubviewSuper", "selectedRange") &&
            ICCF_PatchMethod("TermSubview", "ICeCoffEETerminal", "ICECoffEETermSubviewSuper", "attributedSubstringFromRange:") &&
            ICCF_PatchMethod("TermSubview", "ICeCoffEETerminal", "ICECoffEETermSubviewSuper", "mouseDown:") &&
            ICCF_PatchMethod("TermSubview", "ICeCoffEETerminal", "ICECoffEETermSubviewSuper", "mouseUp:") &&
            ICCF_PatchMethod("TermSubview", "ICeCoffEETerminal", "ICECoffEETermSubviewSuper", "_optionClickEvent:::") ,
            ICapeprintf("ICeCoffEE APE: loaded in TermSubview for Terminal!\n");
        } else if (CFStringCompare(bundleID, CFSTR("com.apple.safari"), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
            ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "mouseUp:") &&
            ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "mouseDown:") &&
            ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "menuForEvent:") , 
            ICapeprintf("ICeCoffEE APE: loaded in WebHTMLView for Safari!\n");
        }
    }

    if (shouldLoadInNSTextView) {
        ICCF_PatchMethod("NSTextView", "ICeCoffEE", "ICeCoffEESuper", "mouseDown:") &&
        ICCF_PatchMethod("NSTextView", "ICeCoffEE", "ICeCoffEESuper", "menuForEvent:") ,
        ICapeprintf("ICeCoffEE APE: loaded generic NSTextView support\n");
    }

    gTEClick = APEPatchCreate(&TEClick, &ICCF_TEClick);
    if (gTEClick != NULL) {
        ICapeprintf("ICeCoffEE APE: patched TEClick\n");
    }

    ICCF_ReloadPrefs();
        
    return;
}

// We define APEBundleMessage so we can receive messages from our preference pane.  We actually only know the Refresh message that instructs our APE module to reload the preferences.  Why do we reload preferences here and not do it in our patch? Well, although you can poll CFPreferences in every patch call, that'll result in some performance loss - we better cache the settings in a static variable and refresh them from prefs only when needed.
OSStatus APEBundleMessage(CFStringRef message,CFDataRef inData,CFDataRef *outData)
{
    ICapeprintf("ICeCoffEE APE: message '%@' (inData = %@)\n", message, inData);
    
    if (CFStringCompare(message, kICPreferencesChanged, NULL) == kCFCompareEqualTo)		
    {	// request to reload prefs from our preference pane
        ICCF_ReloadPrefs();
    }
    
    return noErr;
}

Boolean ICCF_GetBooleanPref(CFStringRef prefKey, Boolean defaultValue) {
    Boolean keyExists;
    Boolean value = CFPreferencesGetAppBooleanValue(prefKey, kICBundleIdentifier, &keyExists);
    if (keyExists) return value;
    CFPreferencesSetAppValue(prefKey, defaultValue ? kCFBooleanTrue : kCFBooleanFalse, kICBundleIdentifier);
    return defaultValue;
}

CFIndex ICCF_GetCFIndexPref(CFStringRef prefKey, CFIndex defaultValue) {
    Boolean keyExists;
    CFIndex value = CFPreferencesGetAppIntegerValue(prefKey, kICBundleIdentifier, &keyExists);
    if (keyExists) return value;
    CFNumberRef defaultValueNumber = CFNumberCreate(NULL, kCFNumberCFIndexType, &defaultValue);
    CFPreferencesSetAppValue(prefKey, defaultValueNumber, kICBundleIdentifier);
    CFRelease(defaultValueNumber);
    return defaultValue;
}

// to test: defaults write net.sabi.ICeCoffEE "Excluded Applications" -array '{CFBundleID = "com.apple.projectbuilder"; }' '{CFBundleID = "net.sabi.Pester"; }' '{CFBundleID = "com.apple.foobar"; }'
static void ICCF_MigratePrefs() {
    
    CFArrayRef prefExcludedApps = CFPreferencesCopyAppValue(kIC12PrefExcluded, kICBundleIdentifier);

    if (prefExcludedApps == NULL) return;

    if (CFGetTypeID(prefExcludedApps) != CFArrayGetTypeID()) {
        CFRelease(prefExcludedApps);
        return;
    }
    
    CFMutableArrayRef excludedApps = CFArrayCreateMutableCopy(NULL, 0, prefExcludedApps);
    CFRelease(prefExcludedApps); prefExcludedApps = NULL;

    ICapeprintf("Excluded apps: %@\n", excludedApps);

    CFIndex excludedAppCount = CFArrayGetCount(excludedApps);
    CFIndex excludedAppIndex;

    CFDictionaryRef excludedAppSpecifiers = NULL;
    CFStringRef excludedAppBundleID = NULL;
    CFURLRef excludedAppURL = NULL;
    CFBundleRef excludedAppBundle = NULL;

    BOOL postponeMigrationForApp;

    for (excludedAppIndex = excludedAppCount - 1 ; excludedAppIndex >= 0 ; excludedAppIndex--) {
        if ( (excludedAppSpecifiers = CFArrayGetValueAtIndex(excludedApps, excludedAppIndex)) == NULL || CFGetTypeID(excludedAppSpecifiers) != CFDictionaryGetTypeID()) {
            CFArrayRemoveValueAtIndex(excludedApps, excludedAppIndex);
            continue;
        }

        postponeMigrationForApp = NO;
        if ( (excludedAppBundleID = CFDictionaryGetValue(excludedAppSpecifiers, kIC12PrefExcludedAppSpecifierBundleID)) != NULL
             && CFGetTypeID(excludedAppBundleID) == CFStringGetTypeID()
             && CFStringCompare(excludedAppBundleID, CFSTR("com.apple.projectbuilder"), kCFCompareCaseInsensitive) != kCFCompareEqualTo) {
            if (LSFindApplicationForInfo(kLSUnknownCreator, excludedAppBundleID, NULL, NULL, &excludedAppURL) == noErr) {
                excludedAppBundle = CFBundleCreate(NULL, excludedAppURL);
                if (excludedAppBundle != NULL) {
                    APEToolsAddToExcludeList(kICBundleIdentifier, excludedAppBundle);
                } else postponeMigrationForApp = YES; // can't create bundle
            } else postponeMigrationForApp = YES; // can't find app
        }
        // don't release excludedAppSpecifiers, used Get
        // don't release excludedAppBundleID, used Get
        if (excludedAppURL != NULL) { CFRelease(excludedAppURL); excludedAppURL = NULL; }
        if (excludedAppBundle != NULL) { CFRelease(excludedAppBundle); excludedAppBundle = NULL; }
        if (!postponeMigrationForApp) CFArrayRemoveValueAtIndex(excludedApps, excludedAppIndex);
    }

    ICapeprintf("Excluded apps remaining: %@\n", excludedApps);
    CFPreferencesSetAppValue(kIC12PrefExcluded, (CFArrayGetCount(excludedApps) == 0 ? NULL : excludedApps), kICBundleIdentifier);
    CFRelease(excludedApps);

    CFPreferencesAppSynchronize(kICBundleIdentifier);
}

static void ICCF_ReloadPrefs() {
    CFPreferencesAppSynchronize(kICBundleIdentifier);

    ICCF_prefs.commandClickEnabled = ICCF_GetBooleanPref(kICCommandClickEnabled, YES);
    ICCF_prefs.textBlinkEnabled = ICCF_GetBooleanPref(kICTextBlinkEnabled, YES);
    ICCF_prefs.textBlinkCount = ICCF_GetCFIndexPref(kICTextBlinkCount, 3);
    ICCF_prefs.servicesInContextualMenu = ICCF_GetBooleanPref(kICServicesInContextualMenu, YES);
    ICCF_prefs.servicesInMenuBar = ICCF_GetBooleanPref(kICServicesInMenuBar, NO);
    ICCF_prefs.errorSoundEnabled = ICCF_GetBooleanPref(kICErrorSoundEnabled, NO);
    ICCF_prefs.errorDialogEnabled = ICCF_GetBooleanPref(kICErrorDialogEnabled, YES);

    CFPreferencesAppSynchronize(kICBundleIdentifier);

    ICCF_AddRemoveServicesMenu();
}
