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).
     1 /*
     2  Copyright (c) The Growl Project, 2004-2005
     3  All rights reserved.
     4 
     5  Redistribution and use in source and binary forms, with or without modification,
     6  are permitted provided that the following conditions are met:
     7 
     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.
    16 
    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.
    27 */
    28 //
    29 //  GrowlMailPreferences.m
    30 //  GrowlMail
    31 //
    32 //  Created by Ingmar Stein on 30.10.04.
    33 //
    34 
    35 #import "GrowlMailPreferences.h"
    36 #import "GrowlMailPreferencesModule.h"
    37 #import "GrowlMail.h"
    38 #import "GrowlMailNotifier.h"
    39 
    40 #import <objc/objc-runtime.h>
    41 
    42 static void GMExchangeMethodImplementations(Method a, Method b);
    43 
    44 @interface NSPreferences (GMSwizzleSticks)
    45 
    46 + (id) sharedPreferencesForGrowlMail;
    47 + (id) sharedPreferencesFromAppKitSwizzledByGrowlMail;
    48 
    49 @end
    50 
    51 //Make the swizzle more readable.
    52 #define sharedPreferencesFromAppKitSwizzledByGrowlMail sharedPreferencesForGrowlMail
    53 
    54 @implementation GrowlMailPreferences
    55 
    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.
    58 + (void) load {
    59 	Class NSPreferencesClass = NSClassFromString(@"NSPreferences");
    60 	if (!NSPreferencesClass)
    61 		GMShutDownGrowlMailAndWarn(@"Couldn't install GrowlMail prefpane: NSPreferences class missing");
    62 	else {
    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");
    67 		else {
    68 			//+[GrowlMailPreferences sharedPreferencesForGrowlMail]
    69 			Method sharedPreferencesForGrowlMail = class_getClassMethod(self, @selector(sharedPreferencesForGrowlMail));
    70 
    71 			GMExchangeMethodImplementations(sharedPreferencesFromAppKit, sharedPreferencesForGrowlMail);
    72 		}
    73 	}
    74 }
    75 
    76 @end
    77 
    78 @implementation NSPreferences (GMSwizzleSticks)
    79 
    80 + (id) sharedPreferencesForGrowlMail {
    81 	static BOOL	added = NO;
    82 	id preferences = [self sharedPreferencesFromAppKitSwizzledByGrowlMail];
    83 
    84 	if (preferences && !added) {
    85 		added = YES;
    86 		[preferences addPreferenceNamed:[GrowlMail preferencesPanelName] owner:[GrowlMailPreferencesModule sharedInstance]];
    87 	}
    88 
    89 	return preferences;
    90 }
    91 
    92 @end
    93 
    94 static void GMExchangeMethodImplementations(Method a, Method b)
    95 {
    96 	if (!(a && 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]);
    98 
    99 	method_exchangeImplementations(a, b);
   100 }