dev.stuconnolly.com / svn / safaritabs

  1. /*
  2.  *  STAppController.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 "STAppController.h"
  24. #import "STSafariPlugin.h"
  25. #import "BrowserWindowController.h"
  26. #import "STConstants.h"
  27.  
  28. @implementation AppController (STAppController)
  29.  
  30.  
  31. ////////////////////////////////////////////// *** _tc_applicationShouldTerminate
  32.  
  33. - (NSApplicationTerminateReply)_tc_applicationShouldTerminate:(id)sender;
  34. {  
  35.     if ([[NSUserDefaults standardUserDefaults] boolForKey:STEnableQuitSafariWarning] == YES) {
  36.         // If terminateWithoutWarning is YES then there is no need to continue, just terminate Safari without a warning regardless.
  37.         if ([[STSafariPlugin sharedInstance] terminateWithoutWarning]) {
  38.             return NSTerminateNow;
  39.         }
  40.        
  41.         int tabCount;
  42.         NSString *message; 
  43.         NSArray *windows = [[NSDocumentController sharedDocumentController] documents];
  44.        
  45.         NSEnumerator* e = [windows objectEnumerator];
  46.         id document;
  47.        
  48.         while(document = [e nextObject]) {
  49.             BrowserWindowController *windowController = [[document windowControllers] objectAtIndex:0];
  50.            
  51.             tabCount = [[windowController orderedTabs] count];
  52.         }
  53.        
  54.         if (([windows count] == 0) || ([windows count] == 1) && (tabCount == 1)) {
  55.             return NSTerminateNow;
  56.         }
  57.         else if ([windows count] > 1) {
  58.             message = [NSString stringWithFormat:@"You currently have %d windows open. Are you sure you want quit?", [windows count]];
  59.         }
  60.         else if (([windows count] == 1) && (tabCount > 1)) {       
  61.             message = [NSString stringWithFormat:@"You currently have %d tabs open. Are you sure you want quit?", tabCount];
  62.         }
  63.        
  64.         NSAlert *alert = [NSAlert alertWithMessageText:message defaultButton:@"Quit" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@""];
  65.        
  66.         if ([alert runModal] == NSAlertAlternateReturn) {
  67.             return NSTerminateCancel;
  68.         }
  69.     }
  70.    
  71.     return NSTerminateNow;
  72. }
  73.  
  74. @end