dev.stuconnolly.com / svn / safaritabs

  1. /*
  2.  *  $Id: STSafariPlugin.m 224 2011-08-13 20:39:45Z stuart $
  3.  *
  4.  *  SafariTabs
  5.  *  http://stuconnolly.com/projects/safaritabs/
  6.  *
  7.  *  Copyright (c) 2010 Stuart Connolly. All rights reserved.
  8.  *
  9.  *  This program is free software: you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by                        
  11.  *  the Free Software Foundation, either version 3 of the License, or
  12.  *  (at your option) any later version.
  13.  *
  14.  *  This program is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17.  *  GNU General Public License for more details.
  18.  *
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with this program. If not, see <http://www.gnu.org/licenses/>.
  21.  */
  22.  
  23. #import "STSafariPlugin.h"
  24.  
  25. #import "STSafariPluginHandler.h"
  26. #import "STBrowserWindowController.h"
  27. #import "STSessionHandler.h"
  28. #import "STAppController.h"
  29. #import "STApplication.h"
  30. #import "STPreferences.h"
  31.  
  32. #import "STUpdater.h"
  33. #import "STRuntime.h"
  34. #import "STUtilities.h"
  35. #import "STConstants.h"
  36.  
  37. @interface STAppController (DeclaredAPI)
  38.  
  39. - (void)_setupUI;
  40. - (void)st_setupUI;
  41. - (void)safari_setupUI;
  42.  
  43. - (BOOL)st_applicationShouldTerminate:(NSNotification *)notification;
  44. - (BOOL)safari_applicationShouldTerminate:(NSNotification *)notification;
  45.  
  46. - (BOOL)st_application:(NSApplication *)sender openFile:(NSString *)filename;
  47. - (BOOL)safari_application:(NSApplication *)sender openFile:(NSString *)filename;
  48.  
  49. - (void)_handleURLEvent:(id)arg1 withReply:(id)arg2;
  50. - (void)st_handleURLEvent:(id)arg1 withReply:(id)arg2;
  51. - (void)safari_handleURLEvent:(id)arg1 withReply:(id)arg2;
  52.  
  53. @end
  54.  
  55. @implementation STSafariPlugin
  56.  
  57. /*!
  58.  * @brief Loads the SafariTabs plugin
  59.  *
  60.  * SafariTabs' bundle loading method. All runtime method swizzling is performed here.
  61.  */
  62. + (void)load
  63. {      
  64.     // Rename existing methods
  65.     STRenameMethodSelector(NSClassFromString(@"NSPreferences"), [STPreferences class], @selector(_setupUI), @selector(st_setupUI), @selector(safari_setupUI), YES);
  66.     STRenameMethodSelector(NSClassFromString(@"AppController"), [STAppController class], @selector(applicationShouldTerminate:), @selector(st_applicationShouldTerminate:), @selector(safari_applicationShouldTerminate:), YES);
  67.     STRenameMethodSelector(NSClassFromString(@"AppController"), [STAppController class], @selector(application:openFile:), @selector(st_application:openFile:), @selector(safari_application:openFile:), YES);
  68.     STRenameMethodSelector(NSClassFromString(@"AppController"), [STAppController class], @selector(_handleURLEvent:withReply:), @selector(st_handleURLEvent:withReply:), @selector(safari_handleURLEvent:withReply:), YES);
  69.        
  70.     // Add new methods
  71.     STAddMethodsToClassFromClass(NSClassFromString(@"BrowserWindowController"), [STBrowserWindowController class]);
  72.        
  73.     // Output plugin load message
  74.     NSLog(@"SafariTabs version %@ (%@) successfully loaded from '%@'", STPluginVersion(), STBundleVersion(), [STPluginBundle() bundlePath]);
  75.    
  76.     // Register plugin defaults
  77.     [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[STPluginBundle() pathForResource:STDefaultsPlistName ofType:@"plist"]]];
  78.    
  79.     [[[STSafariPluginHandler sharedInstance] sessionHandler] pluginLoaded];
  80. }
  81.  
  82. /*!
  83.  * @brief Application singleton
  84.  *
  85.  * Returns the application's shared instance of the SafariTabs plugin.
  86.  *
  87.  * @return The application's shared instance of the SafariTabs plugin.
  88.  */
  89. + (STSafariPlugin *)sharedInstance
  90. {
  91.     // Singleton instance
  92.     static STSafariPlugin *_sharedPlugin = nil;
  93.    
  94.     // Returns a single static instance of the plugin    
  95.     if (_sharedPlugin == nil) {
  96.         _sharedPlugin = [[STSafariPlugin alloc] init];
  97.     }
  98.    
  99.     return _sharedPlugin;  
  100. }
  101.  
  102. #pragma mark -
  103.  
  104. /*!
  105.  * @brief Standard initialization
  106.  *
  107.  * @return The initialized object
  108.  */
  109. - (id)init
  110. {
  111.     if ((self = [super init])) {
  112.         _pluginHandler = [[STSafariPluginHandler alloc] init];
  113.     }
  114.    
  115.     return self;
  116. }
  117.  
  118. /*!
  119.  * @brief Is the plugin loaded
  120.  *
  121.  * Indicates whether or not the SafariTabs plugin bundle is loaded into the targetted application.
  122.  *
  123.  * @return A boolean value indicating whether the plugin is loaded
  124.  */
  125. - (BOOL)isLoaded
  126. {
  127.     return [STPluginBundle() isLoaded];
  128. }
  129.  
  130. /*!
  131.  * @brief Dealloc
  132.  */
  133. - (void)dealloc
  134. {
  135.     [_pluginHandler release], _pluginHandler = nil;
  136.    
  137.     [super dealloc];
  138. }
  139.  
  140. @end
  141.