dev.stuconnolly.com / svn / safaritabs

  1. /*
  2.  *  STSafariPlugin.m
  3.  *
  4.  *  SafariTabs
  5.  *
  6.  *  Copyright (c) 2007 Stuart Connolly. All rights reserved.
  7.  *
  8.  *  This program is free software; you can redistribute it and/or
  9.  *  modify it under the terms of the GNU General Public License
  10.  *  as published by the Free Software Foundation; either version 2
  11.  *  of the License, or (at your option) any later version.
  12.  *
  13.  *  This program is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.  *  GNU General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with this program; if not, write to the Free Software
  20.  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  21.  */
  22.  
  23. #import "STSafariPlugin.h"
  24. #import "STAppController.h"
  25. #import "STBrowserWindowController.h"
  26.  
  27. typedef struct objc_method *Method;
  28.  
  29. struct objc_method {
  30.     SEL method_name;
  31.     char *method_types;
  32.     IMP method_imp;
  33. };
  34.  
  35. ////////////////////////////////////////////// *** DTRenameSelector
  36.  
  37. BOOL STRenameSelector(Class _class, SEL _oldSelector, SEL _newSelector)
  38. {
  39.     Method method = nil;
  40.    
  41.     method = class_getInstanceMethod(_class, _oldSelector);
  42.    
  43.     if (method == nil) {
  44.         return NO;
  45.     }
  46.    
  47.     method->method_name = _newSelector;
  48.    
  49.     return YES;
  50. }
  51.  
  52.  
  53. @implementation STSafariPlugin
  54.  
  55. ////////////////////////////////////////////// *** load
  56.  
  57. + (void)load
  58. {
  59.     // A special method called by SIMBL once the application has started and all classes are initialized.
  60.    
  61.     // Exchange Safari's applicationWillTerminate: with custom one
  62.     STRenameSelector([AppController class], @selector(applicationShouldTerminate:), @selector(_safari_applicationShouldTerminate:));
  63.     STRenameSelector([AppController class], @selector(_tc_applicationShouldTerminate:), @selector(applicationShouldTerminate:));
  64.    
  65.     // Exchange Safari's windowShouldClose: with custom one
  66.     STRenameSelector([BrowserWindowController class], @selector(windowShouldClose:), @selector(_safari_windowShouldClose:));
  67.     STRenameSelector([BrowserWindowController class], @selector(_tc_windowShouldClose:), @selector(windowShouldClose:));
  68. }
  69.  
  70.  
  71. ////////////////////////////////////////////// *** sharedInstance
  72.  
  73. + (STSafariPlugin *)sharedInstance
  74. {
  75.     // Returns a single static instance of the plugin
  76.     static STSafariPlugin *plugin = nil;
  77.    
  78.     if (plugin == nil) {
  79.         plugin = [[STSafariPlugin alloc] init];
  80.     }
  81.    
  82.     return plugin;
  83. }
  84.  
  85. @end