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>
2 Copyright (c) The Growl Project, 2004-2005
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. Neither the name of Growl nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 OF THE POSSIBILITY OF SUCH DAMAGE.
29 // GrowlMailPreferences.m
32 // Created by Ingmar Stein on 30.10.04.
35 #import "GrowlMailPreferences.h"
36 #import "GrowlMailPreferencesModule.h"
38 #import "GrowlMailNotifier.h"
40 #import <objc/objc-runtime.h>
42 static void GMExchangeMethodImplementations(Method a, Method b);
44 @interface NSPreferences (GMSwizzleSticks)
46 + (id) sharedPreferencesForGrowlMail;
47 + (id) sharedPreferencesFromAppKitSwizzledByGrowlMail;
51 @implementation GrowlMailPreferences
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.
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.
56 Class NSPreferencesClass = NSClassFromString(@"NSPreferences");
57 if (!NSPreferencesClass)
58 GMShutDownGrowlMailAndWarn(@"Couldn't install GrowlMail prefpane: NSPreferences class missing");
60 //+[NSPreferences sharedPreferences]
61 Method sharedPreferencesFromAppKit = class_getClassMethod(NSPreferencesClass, @selector(sharedPreferences));
62 if (!sharedPreferencesFromAppKit)
63 GMShutDownGrowlMailAndWarn(@"Couldn't install GrowlMail prefpane: +[NSPreferences sharedPreferences] method missing");
65 //+[GrowlMailPreferences sharedPreferencesFromAppKitSwizzledByGrowlMail]
66 Method sharedPreferencesFromAppKitFromGrowlMail = class_getClassMethod(self, @selector(sharedPreferencesFromAppKitSwizzledByGrowlMail));
67 //+[GrowlMailPreferences sharedPreferencesForGrowlMail]
68 Method sharedPreferencesForGrowlMail = class_getClassMethod(self, @selector(sharedPreferencesForGrowlMail));
71 GMExchangeMethodImplementations(sharedPreferencesFromAppKit, sharedPreferencesFromAppKitFromGrowlMail);
72 GMExchangeMethodImplementations(sharedPreferencesFromAppKit, sharedPreferencesForGrowlMail);
73 /*Results of the swizzling:
75 *+[NSPreferences sharedPreferences]
76 * implemented by former +[NSPreferences sharedPreferencesForGrowlMail]
78 *+[NSPreferences sharedPreferencesForGrowlMail]
79 * implemented by former +[NSPreferences sharedPreferencesFromAppKitSwizzledByGrowlMail] (the stub)
81 *+[NSPreferences sharedPreferencesFromAppKitSwizzledByGrowlMail]
82 * implemented by former +[NSPreferences sharedPreferences]
90 @implementation NSPreferences (GMSwizzleSticks)
92 + (id) sharedPreferencesForGrowlMail {
93 static BOOL added = NO;
94 id preferences = [self sharedPreferencesFromAppKitSwizzledByGrowlMail];
96 if (preferences && !added) {
98 [preferences addPreferenceNamed:[GrowlMail preferencesPanelName] owner:[GrowlMailPreferencesModule sharedInstance]];
103 + (id) sharedPreferencesFromAppKitSwizzledByGrowlMail {
104 //Stub implementation to swizzle out.
110 static void GMExchangeMethodImplementations(Method a, Method b)
113 method_exchangeImplementations(a, b);
115 NSCAssert(a && b, @"Attempt to swizzle fewer than two method implementations");
117 //Adapted from ObjC 1 implementation written by an unknown author and posted to http://www.cocoadev.com/index.pl?MethodSwizzling
121 tempTypes = a->method_types;
122 a->method_types = b->method_types;
123 b->method_types = tempTypes;
125 tempImp = a->method_imp;
126 a->method_imp = b->method_imp;
127 b->method_imp = tempImp;