dev.stuconnolly.com / svn / safaritabs

  1. /*
  2.  *  $Id: STAppController.m 206 2010-01-30 22:43:48Z 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 "STAppController.h"
  24.  
  25. #import "STSafariPlugin.h"
  26. #import "STSessionHandler.h"
  27. #import "STSafariPluginHandler.h"
  28.  
  29. #import "BrowserDocument.h"
  30. #import "STBrowserWindowController.h"
  31.  
  32. #import "STUtilities.h"
  33. #import "STConstants.h"
  34.  
  35. @interface STAppController (DeclaredAPI)
  36.  
  37. - (NSApplicationTerminateReply)safari_applicationShouldTerminate:(id)sender;
  38. - (BOOL)safari_application:(NSApplication *)application openFile:(NSString *)filename;
  39. - (void)safari_handleURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)reply;
  40.  
  41. @end
  42.  
  43. @implementation STAppController
  44.  
  45. /*!
  46.  * This is the re-implementation of Safari's swizzled applicationShouldTerminate: method. If specified in the
  47.  * user's preferences then the currently open tabs will be saved.
  48.  *
  49.  * @param sender The object calling the method (instance of NSApplication)
  50.  *
  51.  * @return An integer (one of NSApplicationTerminateReply constants) indicating whether the application should
  52.  *         terminate.
  53.  */
  54. - (NSApplicationTerminateReply)st_applicationShouldTerminate:(id)sender
  55. {                  
  56.     NSMenuItem *privateBrowsingMenuItem = [[[[NSApp mainMenu] itemAtIndex:0] submenu] itemAtIndex:7];
  57.        
  58.     // If required save the currently open tabs
  59.     if (([[NSUserDefaults standardUserDefaults] integerForKey:STTabRestoreQuitAction] == 0) && ([privateBrowsingMenuItem state] != NSOnState)) {       
  60.         [[[STSafariPluginHandler sharedInstance] sessionHandler] saveOpenTabs];
  61.     }
  62.    
  63.     return [self safari_applicationShouldTerminate:sender];
  64. }
  65.  
  66. /*!
  67.  * This is the re-implementation of Safari's swizzled application:OpenFile: method.
  68.  */
  69. - (BOOL)st_application:(NSApplication *)application openFile:(NSString *)filename
  70. {
  71.     STSessionHandler *sessionHandler = [[STSafariPluginHandler sharedInstance] sessionHandler];
  72.    
  73.     // Safari's attempting to open a file, so don't overwrite the first window
  74.     [sessionHandler setIsFirstWindow:NO];
  75.     [sessionHandler setLaunchedViaURLEvent:YES];
  76.    
  77.     return [self safari_application:application openFile:filename];
  78. }
  79.  
  80. /*!
  81.  * This is the re-implementation of Safari's swizzled handleURLEvent:withReplyEvent: method.
  82.  */
  83. - (void)st_handleURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)reply
  84. {
  85.     STSessionHandler *sessionHandler = [[STSafariPluginHandler sharedInstance] sessionHandler];
  86.    
  87.     // Safari's attempting to open a URL passed in from an event, so don't overwrite the first window
  88.     [sessionHandler setIsFirstWindow:NO];
  89.     [sessionHandler setLaunchedViaURLEvent:YES];
  90.    
  91.     [self safari_handleURLEvent:event withReplyEvent:reply];
  92. }
  93.  
  94. @end
  95.