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