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).
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 //Make the swizzle more readable.
52 #define sharedPreferencesFromAppKitSwizzledByGrowlMail sharedPreferencesForGrowlMail
54 @implementation GrowlMailPreferences
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.
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.
59 Class NSPreferencesClass = NSClassFromString(@"NSPreferences");
60 if (!NSPreferencesClass)
61 GMShutDownGrowlMailAndWarn(@"Couldn't install GrowlMail prefpane: NSPreferences class missing");
63 //+[NSPreferences sharedPreferences]
64 Method sharedPreferencesFromAppKit = class_getClassMethod(NSPreferencesClass, @selector(sharedPreferences));
65 if (!sharedPreferencesFromAppKit)
66 GMShutDownGrowlMailAndWarn(@"Couldn't install GrowlMail prefpane: +[NSPreferences sharedPreferences] method missing");
68 //+[GrowlMailPreferences sharedPreferencesForGrowlMail]
69 Method sharedPreferencesForGrowlMail = class_getClassMethod(self, @selector(sharedPreferencesForGrowlMail));
71 GMExchangeMethodImplementations(sharedPreferencesFromAppKit, sharedPreferencesForGrowlMail);
78 @implementation NSPreferences (GMSwizzleSticks)
80 + (id) sharedPreferencesForGrowlMail {
81 static BOOL added = NO;
82 id preferences = [self sharedPreferencesFromAppKitSwizzledByGrowlMail];
84 if (preferences && !added) {
86 [preferences addPreferenceNamed:[GrowlMail preferencesPanelName] owner:[GrowlMailPreferencesModule sharedInstance]];
94 static void GMExchangeMethodImplementations(Method a, Method b)
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]);
99 method_exchangeImplementations(a, b);