Extras/GrowlMail/GrowlMailPreferences.m
author Peter Hosey <hg@boredzo.org>
Sat Jun 06 21:30:17 2009 -0700 (2009-06-06)
changeset 4211 3be8b67aac72
parent 1905 fb0c631f0b53
child 4397 289e1a952af2
permissions -rw-r--r--
Since poseAsClass: is doomed anyway and appears to cause a crash on PowerPC[1], replace it with method-swizzling. This definitely contributes to our 64-bit cleanliness; hopefully, it also fixes that crash.

[1]: <http://groups.google.com/group/growldiscuss/msg/2b85001f4c6c0d4a>
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
ingmarstein@565
    51
@implementation GrowlMailPreferences
hg@4211
    52
hg@4211
    53
//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
    54
//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
    55
+ (void) load {
hg@4211
    56
	Class NSPreferencesClass = NSClassFromString(@"NSPreferences");
hg@4211
    57
	if (!NSPreferencesClass)
hg@4211
    58
		GMShutDownGrowlMailAndWarn(@"Couldn't install GrowlMail prefpane: NSPreferences class missing");
hg@4211
    59
	else {
hg@4211
    60
		//+[NSPreferences sharedPreferences]
hg@4211
    61
		Method sharedPreferencesFromAppKit = class_getClassMethod(NSPreferencesClass, @selector(sharedPreferences));
hg@4211
    62
		if (!sharedPreferencesFromAppKit)
hg@4211
    63
			GMShutDownGrowlMailAndWarn(@"Couldn't install GrowlMail prefpane: +[NSPreferences sharedPreferences] method missing");
hg@4211
    64
		else {
hg@4211
    65
			//+[GrowlMailPreferences sharedPreferencesFromAppKitSwizzledByGrowlMail]
hg@4211
    66
			Method sharedPreferencesFromAppKitFromGrowlMail = class_getClassMethod(self, @selector(sharedPreferencesFromAppKitSwizzledByGrowlMail));
hg@4211
    67
			//+[GrowlMailPreferences sharedPreferencesForGrowlMail]
hg@4211
    68
			Method sharedPreferencesForGrowlMail = class_getClassMethod(self, @selector(sharedPreferencesForGrowlMail));
hg@4211
    69
hg@4211
    70
			//Follow the lady!
hg@4211
    71
			GMExchangeMethodImplementations(sharedPreferencesFromAppKit, sharedPreferencesFromAppKitFromGrowlMail);
hg@4211
    72
			GMExchangeMethodImplementations(sharedPreferencesFromAppKit, sharedPreferencesForGrowlMail);
hg@4211
    73
			/*Results of the swizzling:
hg@4211
    74
			 *
hg@4211
    75
			 *+[NSPreferences sharedPreferences]
hg@4211
    76
			 *	implemented by former +[NSPreferences sharedPreferencesForGrowlMail]
hg@4211
    77
			 *
hg@4211
    78
			 *+[NSPreferences sharedPreferencesForGrowlMail]
hg@4211
    79
			 *	implemented by former +[NSPreferences sharedPreferencesFromAppKitSwizzledByGrowlMail] (the stub)
hg@4211
    80
			 *
hg@4211
    81
			 *+[NSPreferences sharedPreferencesFromAppKitSwizzledByGrowlMail]
hg@4211
    82
			 *	implemented by former +[NSPreferences sharedPreferences]
hg@4211
    83
			 */
hg@4211
    84
		}
hg@4211
    85
	}
ingmarstein@565
    86
}
ingmarstein@565
    87
hg@4211
    88
@end
hg@4211
    89
hg@4211
    90
@implementation NSPreferences (GMSwizzleSticks)
hg@4211
    91
hg@4211
    92
+ (id) sharedPreferencesForGrowlMail {
ingmarstein@565
    93
	static BOOL	added = NO;
hg@4211
    94
	id preferences = [self sharedPreferencesFromAppKitSwizzledByGrowlMail];
ingmarstein@565
    95
ingmarstein@1156
    96
	if (preferences && !added) {
ingmarstein@565
    97
		added = YES;
ingmarstein@565
    98
		[preferences addPreferenceNamed:[GrowlMail preferencesPanelName] owner:[GrowlMailPreferencesModule sharedInstance]];
ingmarstein@565
    99
	}
ingmarstein@565
   100
ingmarstein@1141
   101
	return preferences;
ingmarstein@565
   102
}
hg@4211
   103
+ (id) sharedPreferencesFromAppKitSwizzledByGrowlMail {
hg@4211
   104
	//Stub implementation to swizzle out.
hg@4211
   105
	return nil;
hg@4211
   106
}
hg@4211
   107
ingmarstein@565
   108
@end
hg@4211
   109
hg@4211
   110
static void GMExchangeMethodImplementations(Method a, Method b)
hg@4211
   111
{
hg@4211
   112
#if __OBJC2__
hg@4211
   113
	method_exchangeImplementations(a, b);
hg@4211
   114
#else
hg@4211
   115
	NSCAssert(a && b, @"Attempt to swizzle fewer than two method implementations");
hg@4211
   116
hg@4211
   117
	//Adapted from ObjC 1 implementation written by an unknown author and posted to http://www.cocoadev.com/index.pl?MethodSwizzling
hg@4211
   118
	char *tempTypes;
hg@4211
   119
	IMP tempImp;
hg@4211
   120
hg@4211
   121
	tempTypes = a->method_types;
hg@4211
   122
	a->method_types = b->method_types;
hg@4211
   123
	b->method_types = tempTypes;
hg@4211
   124
hg@4211
   125
	tempImp = a->method_imp;
hg@4211
   126
	a->method_imp = b->method_imp;
hg@4211
   127
	b->method_imp = tempImp;
hg@4211
   128
#endif
hg@4211
   129
}