dev.stuconnolly.com / svn / safaritabs

  1. /*
  2.  *  $Id: STBrowserWindowController.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 <WebKit/WebKit.h>
  24.  
  25. #import "STSafariPlugin.h"
  26. #import "STBrowserWindowController.h"
  27. #import "STConstants.h"
  28.  
  29. #import "BrowserDocument.h"
  30. #import "BrowserWebView.h"
  31. #import "BrowserTabViewItem.h"
  32.  
  33. @interface STBrowserWindowController (DeclaredAPI)
  34.  
  35. - (id)createTab;
  36. - (id)orderedTabs;
  37. - (id)orderedTabViewItems;
  38.  
  39. - (void)closeCurrentTab:(id)arg;
  40.  
  41. @end
  42.  
  43. @implementation STBrowserWindowController
  44.  
  45. /*!
  46.  * Returns an array of dictionaries, each representing a tab of the window associated with this controller. Each
  47.  * dictionary contains the tab's title and URL.
  48.  *
  49.  * @return An array of dictionaries representing the currently open tabs
  50.  */
  51. - (NSArray *)currentlyOpenTabs
  52. {
  53.     NSMutableArray *tabs = [NSMutableArray array];
  54.    
  55.     for (id tab in [self orderedTabs])
  56.     {        
  57.         if (tab) {
  58.             NSURL    *tabURL  = [tab currentURL];
  59.             NSString *tabTile = [tab titleForLocationFieldURL];
  60.             NSImage  *tabIcon = [tab mainFrameIcon];
  61.            
  62.             if ((tabTile) && (tabURL) && (tabIcon)) {
  63.                 [tabs addObject:[NSDictionary dictionaryWithObjectsAndKeys:tabTile, STTabTitleKey, tabURL, STTabURLKey, tabIcon, STTabIconKey, nil]];
  64.             }
  65.         }
  66.     }
  67.    
  68.     return tabs;
  69. }
  70.  
  71. /*!
  72.  * Returns an array of strings representing the URLs of the currently open tabs on the window associated with
  73.  * this controller.
  74.  *
  75.  * @return An array of strings containing the tabs URL's
  76.  */
  77. - (NSArray *)currentlyOpenTabURLs
  78. {
  79.     NSMutableArray *tabs = [NSMutableArray array];
  80.        
  81.     for (id tab in [self orderedTabs])
  82.     {
  83.         if ([tab currentURL] != nil) {
  84.             [tabs addObject:[tab currentURL]];
  85.         }
  86.     }
  87.    
  88.     return tabs;
  89. }
  90.  
  91. /*!
  92.  * Gets the localized version of the string 'Close Tab' by extracting it from the menu item 'Close Tab'. This
  93.  * is used for setting the undo menu item when tab undo support is on.
  94.  *
  95.  * @return The localized 'Close Tab' string
  96.  */
  97. - (NSString *)getLocalizedCloseTabString
  98. {
  99.     NSString *closeTabString = @"";
  100.        
  101.     for (NSMenuItem *item in [[[[[NSApplication sharedApplication] mainMenu] itemAtIndex:1] submenu] itemArray])
  102.     {
  103.         if ([item action] == @selector(closeCurrentTab:)) {
  104.             closeTabString = [item title];
  105.             break;
  106.         }
  107.     }
  108.    
  109.     return closeTabString;
  110. }
  111.  
  112. /*!
  113.  * This is the re-implementation of Safari's swizzled newTabWithURL: method. Depending on the user's preferences
  114.  * the closed tab will be registered with the application's undo manager.
  115.  *
  116.  * @param url The object calling the method
  117.  */
  118. - (void)newTabWithURL:(NSString *)url
  119. {
  120.     NSUInteger i;
  121.     BrowserTabViewItem *newTabViewItem = nil;
  122.    
  123.     id tab = [self createTab];
  124.    
  125.     [[tab mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
  126.    
  127.     NSArray *tabViews = [self orderedTabs];
  128.     NSArray *tabs = [self orderedTabViewItems];
  129.    
  130.     for (i = 0; i < [tabViews count]; i++)
  131.     {
  132.         if ([[tabViews objectAtIndex:i] isEqualTo:tab]) {
  133.             newTabViewItem = [tabs objectAtIndex:i];
  134.             break;
  135.         }
  136.     }
  137. }
  138.  
  139. @end
  140.