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. #import "STConstants.h"
  27.  
  28. typedef struct objc_method *Method;
  29.  
  30. struct objc_method {
  31.     SEL method_name;
  32.     char *method_types;
  33.     IMP method_imp;
  34. };
  35.  
  36. @implementation STSafariPlugin
  37.  
  38.  
  39. ////////////////////////////////////////////// *** renameSelector
  40.  
  41. + (BOOL)renameSelector:(Class)_class fromOldSelector:(SEL)_oldSelector toNewSelector:(SEL)_newSelector
  42. {
  43.     Method method = nil;
  44.    
  45.     method = class_getInstanceMethod(_class, _oldSelector);
  46.    
  47.     if (method == nil) {
  48.         return NO;
  49.     }
  50.    
  51.     method->method_name = _newSelector;
  52.    
  53.     return YES;
  54. }
  55.  
  56.  
  57. ////////////////////////////////////////////// *** load
  58.  
  59. + (void)load
  60. {
  61.     // A special method called by SIMBL once the application has started and all classes are initialized.
  62.    
  63.     // Exchange Safari's applicationWillTerminate: with custom one
  64.     [STSafariPlugin renameSelector:[AppController class] fromOldSelector:@selector(applicationShouldTerminate:) toNewSelector:@selector(_safari_applicationShouldTerminate:)];
  65.     [STSafariPlugin renameSelector:[AppController class] fromOldSelector:@selector(_tc_applicationShouldTerminate:) toNewSelector:@selector(applicationShouldTerminate:)];
  66.    
  67.     // Exchange Safari's windowShouldClose: with custom one
  68.     [STSafariPlugin renameSelector:[BrowserWindowController class] fromOldSelector:@selector(windowShouldClose:) toNewSelector:@selector(_safari_windowShouldClose:)];
  69.     [STSafariPlugin renameSelector:[BrowserWindowController class] fromOldSelector:@selector(_tc_windowShouldClose:) toNewSelector:@selector(windowShouldClose:)];
  70.    
  71.     // Load our custom menu items
  72.     [NSBundle loadNibNamed:@"MenuAdditions" owner:[STSafariPlugin sharedInstance]];
  73. }
  74.  
  75.  
  76. ////////////////////////////////////////////// *** sharedInstance
  77.  
  78. + (STSafariPlugin *)sharedInstance
  79. {
  80.     // Returns a single static instance of the plugin
  81.     static STSafariPlugin *plugin = nil;
  82.    
  83.     if (plugin == nil) {
  84.         plugin = [[STSafariPlugin alloc] init];
  85.     }
  86.    
  87.     return plugin;
  88. }
  89.  
  90.  
  91. ////////////////////////////////////////////// *** terminateWithoutWarning
  92.  
  93. - (BOOL)terminateWithoutWarning
  94. {
  95.     return terminateWithoutWarning;
  96. }
  97.  
  98.  
  99. ////////////////////////////////////////////// *** setTerminateWithoutWarning
  100.  
  101. - (void)setTerminateWithoutWarning:(BOOL)warning
  102. {
  103.     terminateWithoutWarning = warning;
  104. }
  105.  
  106.  
  107. ////////////////////////////////////////////// *** isLoaded
  108.  
  109. - (BOOL)isLoaded
  110. {
  111.     return YES;
  112. }
  113.  
  114.  
  115. ////////////////////////////////////////////// *** awakeFromNib
  116.  
  117. - (void)awakeFromNib
  118. {
  119.     NSMenu *safariMenuBar = [[NSApplication sharedApplication] mainMenu];
  120.     NSMenu *fileMenu = [[safariMenuBar itemAtIndex:1] submenu];
  121.    
  122.     NSMenuItem *closeWithoutWarningMenuItem = [menuAdditions itemAtIndex:0];
  123.     NSMenuItem *quitWithoutWarningMenuItem = [menuAdditions itemAtIndex:1];
  124.    
  125.     if ([[NSUserDefaults standardUserDefaults] boolForKey:STAddCloseNoWarningMenuItem]) {
  126.         NSEnumerator *itemEnumerator = [[fileMenu itemArray] objectEnumerator];
  127.         NSMenuItem *item;
  128.  
  129.         // Add the 'Close Window Without Warning' menu item
  130.         while(item = [itemEnumerator nextObject]) {
  131.             if ([item action] == @selector(performClose:)) {
  132.                 int index = [fileMenu indexOfItem:item];
  133.                 [fileMenu insertItem:[closeWithoutWarningMenuItem copy] atIndex:(index + 1)];
  134.                
  135.                 break;
  136.             }
  137.         }
  138.     }
  139.    
  140.     if ([[NSUserDefaults standardUserDefaults] boolForKey:STAddQuitNoWarningMenuItem]) {
  141.         // Add the 'Quit Safari Without Warning' menu item
  142.         [[[safariMenuBar itemAtIndex:0] submenu] addItem:[quitWithoutWarningMenuItem copy]];
  143.     }
  144.    
  145.     terminateWithoutWarning = NO;
  146. }
  147.  
  148.  
  149. ////////////////////////////////////////////// *** closeWindowWithoutWarning
  150.  
  151. - (IBAction)closeWindowWithoutWarning:(id)sender
  152. {
  153.     [[[NSApplication sharedApplication] mainWindow] close];
  154. }
  155.  
  156.  
  157. ////////////////////////////////////////////// *** quitWithoutWarning
  158.  
  159. - (IBAction)quitWithoutWarning:(id)sender
  160. {
  161.     terminateWithoutWarning = YES;
  162.     [[NSApplication sharedApplication] terminate:self];
  163. }
  164.  
  165.  
  166. ////////////////////////////////////////////// *** validateUserInterfaceItem
  167.  
  168. - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item
  169. {
  170.     if ([item action] == @selector(closeWindowWithoutWarning:)) {
  171.         if ([[NSApplication sharedApplication] mainWindow]) {
  172.             return YES;
  173.         }
  174.     }
  175.     else if ([item action] == @selector(quitWithoutWarning:)) {
  176.         return YES;
  177.     }
  178.  
  179.     // Otherwise ask Safari to validate the item
  180.     return NO;
  181. }
  182.  
  183. @end