// ===========================================================================
//
//	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 <mach-o/dyld.h>
#import <objc/objc-runtime.h>
#import "ICeCoffEE.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;

// With APE 1.3, if we're in the exclude list, APEBundleMainEarlyLoad doesn't get invoked; don't need to use APETools.  But we need to do our own management to avoid loading in background-only applications.
static Boolean ICCF_shouldLoad;

static Boolean ICCF_IsOne(CFTypeRef value) {
    if (value == NULL)
	return false;
    CFTypeID typeID = CFGetTypeID(value);
    if (typeID == CFBooleanGetTypeID())
	return CFBooleanGetValue((CFBooleanRef)value);
    else if (typeID == CFNumberGetTypeID()) {
	static const int one = 1;
	static CFNumberRef oneRef = NULL;
	if (oneRef == NULL) oneRef = CFNumberCreate(NULL, kCFNumberIntType, &one);
	return CFNumberCompare((CFNumberRef)value, oneRef, NULL) == kCFCompareEqualTo;
    } else if (typeID == CFStringGetTypeID()) {
	return CFStringCompare((CFStringRef)value, CFSTR("1"), 0) == kCFCompareEqualTo;
    }
    return false;
}

void APEBundleMainEarlyLoad(CFBundleRef inBundle, CFStringRef inAPEToolsApplicationID)
{
    ICCF_MigratePrefs();

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

    ICCF_shouldLoad = false;
    
    CFBundleRef appBundle = CFBundleGetMainBundle();
    if (appBundle == NULL) {
	apeprintf("can't get CFBundle for current process; not loading\n");
	return;
    }
    
    if (ICCF_IsOne(CFBundleGetValueForInfoDictionaryKey(appBundle, CFSTR("LSUIElement"))) ||
	ICCF_IsOne(CFBundleGetValueForInfoDictionaryKey(appBundle, CFSTR("NSUIElement"))) ||
	ICCF_IsOne(CFBundleGetValueForInfoDictionaryKey(appBundle, CFSTR("LSBackgroundOnly"))) ||
	ICCF_IsOne(CFBundleGetValueForInfoDictionaryKey(appBundle, CFSTR("NSBackgroundOnly")))) {
	ICapeprintf("not loading as this application is background-only\n");
	return;
    }
    ICCF_shouldLoad = true;
}

static Boolean ICCF_CFBundleIDMatches(CFStringRef bundleID, CFStringRef test) {
    return CFStringCompare(bundleID, test, kCFCompareCaseInsensitive) == kCFCompareEqualTo;
}

static Boolean ICCF_tryLoading = false;
static Boolean ICCF_loadedInWebKit = false;
static Boolean ICCF_loadedInPDFKit = false;

static void ICCF_OnAddImage(const struct mach_header *mh, intptr_t vmaddr_slide) {
    if (!ICCF_tryLoading) // when initially registered, called a lot
	return;
    if (!ICCF_loadedInWebKit &&
	ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "mouseUp:") &&
	ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "mouseDown:") &&
	ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "menuForEvent:") &&
	ICCF_PatchMethod("WebPDFView", "ICeCoffEEWebPDFView", "ICeCoffEEWebPDFViewSuper", "menuForEvent:")) {
	ICapeprintf("loaded in WebHTMLView/WebPDFView for WebKit/Safari 3\n");
	ICCF_loadedInWebKit = true;
    }
    if (!ICCF_loadedInPDFKit &&
	ICCF_PatchMethod("PDFView", "ICeCoffEEPDFView", "ICeCoffEEPDFViewSuper", "menuForEvent:")) {
	ICapeprintf("loaded in PDFView for PDFKit\n");
	ICCF_loadedInPDFKit = true;
    }
}

void APEBundleMainLateLoad(CFBundleRef inBundle, CFStringRef inAPEToolsApplicationID)
{
    if (!ICCF_shouldLoad) return;

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

    // XXX handle patching error return from ICCF_PatchMethod
    if (bundleID != NULL) {
        if (ICCF_CFBundleIDMatches(bundleID, CFSTR("com.apple.xcode"))) {
	    if (ICCF_PatchMethod("XCSourceCodeTextView", "ICeCoffEEMenuOnly", "ICeCoffEEMenuSuper", "menuForEvent:")) {
		// Xcode 3.x: XCSourceCodeTextView < XCTextView < NSTextView for source
		//            XCDiffTextView < PBXTextView < NSTextView for rich text
		ICCF_PatchMethod("NSTextView", "ICeCoffEE", "ICeCoffEESuper", "mouseDown:");
		ICCF_PatchMethod("XCDiffTextView", "ICeCoffEE", "ICeCoffEESuper", "menuForEvent:");
		    // subclass of PBXTextView; patching both is bad
		ICCF_PatchMethod("NSTextView", "ICeCoffEE", "ICeCoffEESuper", "clickedOnLink:atIndex:");
	    } else {
		// Xcode 2.x: both source and rich text views are PBXTextView
		ICCF_PatchMethod("PBXTextView", "ICeCoffEEMenuOnly", "ICeCoffEEMenuSuper", "menuForEvent:");
	    }
            ICapeprintf("loaded in PBXTextView / XCTextView for Xcode\n");
            shouldLoadInNSTextView = NO;
        } else if (ICCF_CFBundleIDMatches(bundleID, CFSTR("com.apple.terminal"))) {
	    if (ICCF_PatchMethod("TTView", "ICeCoffEEMenuOnly", "ICeCoffEEMenuSuper", "menuForEvent:")) {
		ICCF_PatchMethod("TTView", "ICeCoffEETTView", "ICeCoffEETTViewSuper", "mouseDown:") &&
		ICCF_PatchMethod("TTView", "ICeCoffEETTView", "ICeCoffEETTViewSuper", "mouseUp:") &&
		ICCF_PatchMethod("TTView", "ICeCoffEETTView", "ICeCoffEETTViewSuper", "draggingEntered:");
		ICapeprintf("loaded in TTView for Terminal\n");
	    } else {
		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", "draggingEntered:") &&
		ICCF_PatchMethod("TermSubview", "ICeCoffEETerminal", "ICeCoffEETermSubviewSuper", "_optionClickEvent:::");
		ICapeprintf("loaded in TermSubview for Terminal\n");
	    }
	} else if (ICCF_CFBundleIDMatches(bundleID, CFSTR("org.mozilla.camino"))) {
            ICCF_PatchMethod("ChildView", "ICeCoffEEMenuOnly", "ICeCoffEEMenuSuper", "menuForEvent:");
	    ICapeprintf("loaded in ChildView for Camino\n");
        }
    }

    if (shouldLoadInNSTextView) {
        ICCF_PatchMethod("NSTextView", "ICeCoffEE", "ICeCoffEESuper", "mouseDown:") &&
	ICCF_PatchMethod("NSTextView", "ICeCoffEE", "ICeCoffEESuper", "clickedOnLink:atIndex:") &&
        ICCF_PatchMethod("NSTextView", "ICeCoffEE", "ICeCoffEESuper", "menuForEvent:");
        ICapeprintf("loaded generic NSTextView support\n");
    }
    
    _dyld_register_func_for_add_image(ICCF_OnAddImage);
    ICCF_tryLoading = true;
    ICCF_OnAddImage(NULL, 0L);

    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("message '%@' (inData = %@)\n", message, inData);
    
    if (CFStringCompare(message, kICPreferencesChanged, 0) == 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;
}

void ICCF_GetCFTypePref(CFStringRef prefKey, CFTypeRef *val, CFTypeID type) {
    if (*val != NULL) {
        CFRelease(*val);
        *val = NULL;
    }
    
    *val = CFPreferencesCopyAppValue(prefKey, kICBundleIdentifier);

    if (*val == NULL) return;

    if (CFGetTypeID(*val) != type) {
        CFRelease(*val);
        *val = NULL;
        return;
    }
}

// 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, CFSTR("Migrated from ICeCoffEE 1.0-1.2"), NO);
                } 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_GetCFTypePref(kICServiceOptions, (CFTypeRef *)&ICCF_prefs.serviceOptions, CFDictionaryGetTypeID());
    ICCF_prefs.terminalRequireOptionForSelfDrag = ICCF_GetBooleanPref(kICTerminalRequireOptionForSelfDrag, NO);
    ICCF_prefs.errorSoundEnabled = ICCF_GetBooleanPref(kICErrorSoundEnabled, NO);
    ICCF_prefs.errorDialogEnabled = ICCF_GetBooleanPref(kICErrorDialogEnabled, YES);

    CFPreferencesAppSynchronize(kICBundleIdentifier);

    ICCF_AddRemoveServicesMenu();
}
