dev.stuconnolly.com / svn / safaritabs

  1. /*
  2.  *  STBrowserWindowController.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 "STBrowserWindowController.h"
  24. #import "STConstants.h"
  25. #import "BrowserDocument.h"
  26. #import <WebKit/WebKit.h>
  27.  
  28. @implementation BrowserWindowController (STBrowserWindowController)
  29.  
  30.  
  31. ////////////////////////////////////////////// *** _st_windowShouldClose
  32.  
  33. - (BOOL)_st_windowShouldClose:(id)sender
  34. {  
  35.     if ([[NSUserDefaults standardUserDefaults] boolForKey:STEnableCloseWindowWarning] == YES) {
  36.         if ([self moreThanOneTabShowing]) {
  37.             NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:@"You currently have %d tabs open. Are you sure you want to close them?", [[self orderedTabs] count]] defaultButton:@"Close Tabs" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@""];
  38.        
  39.             [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(tabWarningDidEnd:returnCode:contextInfo:) contextInfo:(void *)sender];
  40.        
  41.             return NO;
  42.         }
  43.     }
  44.  
  45.     return YES;
  46. }
  47.  
  48.  
  49. ////////////////////////////////////////////// *** _st_createTab
  50.  
  51. - (id)_st_createTab
  52. {  
  53.     NSURL *currentURL = [[self browserDocument] currentURL];
  54.     id tab = [self _safari_createTab];
  55.    
  56.     int option = [[NSUserDefaults standardUserDefaults] integerForKey:STNewTabsLoadOption];
  57.    
  58.     if (option == 1) {
  59.         [self goHome:self];
  60.     }
  61.     else if (option == 2) {
  62.         [[tab mainFrame] loadRequest:[NSURLRequest requestWithURL:currentURL]];
  63.     }
  64.    
  65.     return tab;
  66. }
  67.  
  68.  
  69. ////////////////////////////////////////////// *** tabWarningDidEnd
  70.  
  71. - (void)tabWarningDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo
  72. {
  73.     if (returnCode == NSAlertDefaultReturn) {
  74.         // We don't want the window being closed before the sheet is dismissed (disappears from view), so we call a method after half
  75.         // second to close the window.
  76.         [self performSelector:@selector(_st_closeWindow) withObject:nil afterDelay:0.5];
  77.     }
  78. }
  79.  
  80.  
  81. ////////////////////////////////////////////// *** _tc_closeWindow
  82.  
  83. - (void)_st_closeWindow
  84. {
  85.     [self close];
  86. }
  87.  
  88. @end