Extras/GrowlMail/GrowlMailPreferences.m
author Peter Hosey <hg@boredzo.org>
Thu Sep 24 23:30:02 2009 -0700 (2009-09-24)
changeset 4431 6b117acf2fe9
parent 4398 9d356863dcc5
child 4468 37889e96cb40
permissions -rw-r--r--
Use a simple swap, not a three-point swizzle, in the hope of interacting better with other Mail bundles that have preferences (such as MailActOn).
tick@796
     1
/*
ingmarstein@1156
     2
 Copyright (c) The Growl Project, 2004-2005
tick@796
     3
 All rights reserved.
ingmarstein@1905
     4
ingmarstein@1156
     5
 Redistribution and use in source and binary forms, with or without modification,
ingmarstein@1156
     6
 are permitted provided that the following conditions are met:
ingmarstein@1905
     7
tick@796
     8
 1. Redistributions of source code must retain the above copyright
tick@796
     9
 notice, this list of conditions and the following disclaimer.
tick@796
    10
 2. Redistributions in binary form must reproduce the above copyright
tick@796
    11
 notice, this list of conditions and the following disclaimer in the
tick@796
    12
 documentation and/or other materials provided with the distribution.
tick@796
    13
 3. Neither the name of Growl nor the names of its contributors
tick@796
    14
 may be used to endorse or promote products derived from this software
tick@796
    15
 without specific prior written permission.
ingmarstein@1905
    16
ingmarstein@1156
    17
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ingmarstein@1156
    18
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
ingmarstein@1156
    19
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ingmarstein@1156
    20
 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
ingmarstein@1156
    21
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
ingmarstein@1156
    22
 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
ingmarstein@1156
    23
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
ingmarstein@1156
    24
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
ingmarstein@1156
    25
 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
ingmarstein@1156
    26
 OF THE POSSIBILITY OF SUCH DAMAGE.
ingmarstein@1156
    27
*/
ingmarstein@565
    28
//
ingmarstein@565
    29
//  GrowlMailPreferences.m
ingmarstein@565
    30
//  GrowlMail
ingmarstein@565
    31
//
ingmarstein@565
    32
//  Created by Ingmar Stein on 30.10.04.
ingmarstein@565
    33
//
ingmarstein@565
    34
ingmarstein@565
    35
#import "GrowlMailPreferences.h"
ingmarstein@565
    36
#import "GrowlMailPreferencesModule.h"
ingmarstein@565
    37
#import "GrowlMail.h"
hg@4211
    38
#import "GrowlMailNotifier.h"
hg@4211
    39
hg@4211
    40
#import <objc/objc-runtime.h>
hg@4211
    41
hg@4211
    42
static void GMExchangeMethodImplementations(Method a, Method b);
hg@4211
    43
hg@4211
    44
@interface NSPreferences (GMSwizzleSticks)
hg@4211
    45
hg@4211
    46
+ (id) sharedPreferencesForGrowlMail;
hg@4211
    47
+ (id) sharedPreferencesFromAppKitSwizzledByGrowlMail;
hg@4211
    48
hg@4211
    49
@end
ingmarstein@565
    50
hg@4431
    51
//Make the swizzle more readable.
hg@4431
    52
#define sharedPreferencesFromAppKitSwizzledByGrowlMail sharedPreferencesForGrowlMail
hg@4431
    53
ingmarstein@565
    54
@implementation GrowlMailPreferences
hg@4211
    55
hg@4211
    56
//As of Mac OS X 10.5.6, Mail creates the +sharedPreferences object lazily, so the simplest way to install our prefpane is to swizzle the +sharedPreferences method.
hg@4211
    57
//We used to install our prefpane by posing as NSPreferences, but class-posing doesn't exist in 64-bit, and seemed to cause at least one crash on PowerPC machines in GrowlMail 1.1.5b1.
ingmarstein@1713
    58
+ (void) load {
hg@4211
    59
	Class NSPreferencesClass = NSClassFromString(@"NSPreferences");
hg@4211
    60
	if (!NSPreferencesClass)
hg@4211
    61
		GMShutDownGrowlMailAndWarn(@"Couldn't install GrowlMail prefpane: NSPreferences class missing");
hg@4211
    62
	else {
hg@4211
    63
		//+[NSPreferences sharedPreferences]
hg@4211
    64
		Method sharedPreferencesFromAppKit = class_getClassMethod(NSPreferencesClass, @selector(sharedPreferences));
hg@4211
    65
		if (!sharedPreferencesFromAppKit)
hg@4211
    66
			GMShutDownGrowlMailAndWarn(@"Couldn't install GrowlMail prefpane: +[NSPreferences sharedPreferences] method missing");
hg@4211
    67
		else {
hg@4211
    68
			//+[GrowlMailPreferences sharedPreferencesForGrowlMail]
hg@4211
    69
			Method sharedPreferencesForGrowlMail = class_getClassMethod(self, @selector(sharedPreferencesForGrowlMail));
hg@4211
    70
hg@4211
    71
			GMExchangeMethodImplementations(sharedPreferencesFromAppKit, sharedPreferencesForGrowlMail);
hg@4211
    72
		}
hg@4211
    73
	}
ingmarstein@565
    74
}
ingmarstein@565
    75
hg@4211
    76
@end
hg@4211
    77
hg@4211
    78
@implementation NSPreferences (GMSwizzleSticks)
hg@4211
    79
hg@4211
    80
+ (id) sharedPreferencesForGrowlMail {
ingmarstein@565
    81
	static BOOL	added = NO;
hg@4211
    82
	id preferences = [self sharedPreferencesFromAppKitSwizzledByGrowlMail];
ingmarstein@565
    83
ingmarstein@1156
    84
	if (preferences && !added) {
ingmarstein@565
    85
		added = YES;
ingmarstein@565
    86
		[preferences addPreferenceNamed:[GrowlMail preferencesPanelName] owner:[GrowlMailPreferencesModule sharedInstance]];
ingmarstein@565
    87
	}
ingmarstein@565
    88
ingmarstein@1141
    89
	return preferences;
ingmarstein@565
    90
}
hg@4211
    91
ingmarstein@565
    92
@end
hg@4211
    93
hg@4211
    94
static void GMExchangeMethodImplementations(Method a, Method b)
hg@4211
    95
{
hg@4398
    96
	if (!(a && b))
hg@4398
    97
		GMShutDownGrowlMailAndWarn([NSString stringWithFormat:@"Attempt to swizzle fewer than two method implementations: %s and %s", a ? sel_getName(method_getName(a)) : NULL, b ? sel_getName(method_getName(b)) : NULL]);
hg@4398
    98
hg@4211
    99
	method_exchangeImplementations(a, b);
hg@4211
   100
}