dev.stuconnolly.com / svn / safaritabs

  1. /*
  2.  *  $Id: STSessionHandler.m 225 2011-08-13 21:16:20Z 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 "STSessionHandler.h"
  26. #import "STSessionRestoreController.h"
  27. #import "STBrowserWindowController.h"
  28. #import "STSafariPluginHandler.h"
  29.  
  30. #import "BrowserWindowController.h"
  31.  
  32. #import "STUpdater.h"
  33. #import "BrowserDocument.h"
  34. #import "STConstants.h"
  35.  
  36. @interface STSessionHandler ()
  37.  
  38. - (NSMutableArray *)_constructArrayOfTabURLsFromTabs:(NSArray *)tabs;
  39.  
  40. @end
  41.  
  42. @implementation STSessionHandler
  43.  
  44. @synthesize isFirstWindow;
  45. @synthesize launchedViaURLEvent;
  46.  
  47. /*!
  48.  * Initialises an instance of STSessionHanlder.
  49.  *
  50.  * @return The initialised instance
  51.  */
  52. - (id)init
  53. {
  54.     if ((self = [super init])) {
  55.         isFirstWindow = YES;
  56.         launchedViaURLEvent = NO;
  57.     }
  58.    
  59.     return self;
  60. }
  61.  
  62. /*!
  63.  * Called just after the plugin has successfully been loaded to perform any necessary actions upon
  64.  * application startup.
  65.  */
  66. - (void)pluginLoaded
  67. {
  68.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  69.    
  70.     if (![self launchedViaURLEvent]) {
  71.         if ([defaults integerForKey:STTabRestoreQuitAction] == 0) {
  72.             if ([defaults integerForKey:STTabRestoreLaunchAction] == 0) {
  73.                 [self restoreSavedTabs];
  74.             }
  75.             else if (([defaults integerForKey:STTabRestoreLaunchAction] == 1) && ([defaults objectForKey:STWindowsAndTabs])) {
  76.                 [[[STSafariPluginHandler sharedInstance] sessionRestoreController] displaySessionRestorePanelUsingData:[defaults objectForKey:STWindowsAndTabs]];
  77.             }
  78.         }
  79.     }
  80.    
  81.     // If required check for updates
  82.     if ([defaults boolForKey:STCheckForUpdates]) {  
  83.         [[[STSafariPluginHandler sharedInstance] safariTabsUpdater] checkForUpdatesUsingDelegate:[STSafariPluginHandler sharedInstance] inBackground:YES];
  84.     }
  85. }
  86.  
  87. /*!
  88.  * Saves Safari's currently open tabs. Depending on the user's preferences either all tabs in all open windows
  89.  * or only tabs within the active window will be saved.
  90.  */
  91. - (void)saveOpenTabs
  92. {
  93.     NSMutableArray *windows = [NSMutableArray array];
  94.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  95.    
  96.     if ([defaults integerForKey:STTabWindowSaveOption] == 0) {
  97.         id document;
  98.         NSEnumerator *e = [[[NSDocumentController sharedDocumentController] documents] objectEnumerator];
  99.        
  100.         while ((document = [e nextObject]))
  101.         {
  102.             id windowController = [[document windowControllers] objectAtIndex:0];
  103.            
  104.             [windows addObject:[windowController currentlyOpenTabs]];
  105.         }
  106.     }
  107.     else if ([defaults integerForKey:STTabWindowSaveOption] == 1) {
  108.         id windowController = [[[[NSDocumentController sharedDocumentController] currentDocument] windowControllers] objectAtIndex:0];
  109.        
  110.         [windows addObject:[windowController currentlyOpenTabs]];
  111.     }
  112.    
  113.     [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:windows] forKey:STWindowsAndTabs];
  114. }
  115.  
  116. /*!
  117.  * Restores all the tabs the user had open during their last session. Depending on the user's preferences either
  118.  * all of their last sessions windows or just their active window at the time will be restored.
  119.  */
  120. - (void)restoreSavedTabs
  121. {  
  122.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  123.     NSData *windowData = [defaults dataForKey:STWindowsAndTabs];
  124.    
  125.     if (windowData != nil) {
  126.         NSMutableArray *windows = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:windowData];
  127.        
  128.         if ([defaults integerForKey:STTabRestoreLaunchAction] == 0) {
  129.             [[[STSafariPluginHandler sharedInstance] sessionHandler] restoreTabsFromWindows:windows];
  130.         }
  131.         else if ([defaults integerForKey:STTabRestoreLaunchAction] == 1) {
  132.             BrowserDocument *document = [[[NSDocumentController sharedDocumentController] documents] objectAtIndex:0];
  133.             [[[STSafariPluginHandler sharedInstance] sessionHandler] openBrowserWithTabs:[self _constructArrayOfTabURLsFromTabs:[windows objectAtIndex:0]] inDocument:document];
  134.         }
  135.     }
  136. }
  137.  
  138. /*!
  139.  * Restores the previous session's tabs from the supplied array of windows.
  140.  *
  141.  * @param windows An array of windows for which the tabs belong to.
  142.  */
  143. - (void)restoreTabsFromWindows:(NSArray *)windows;
  144. {
  145.     NSArray *tabs;
  146.     NSEnumerator *windowEnumerator = [windows objectEnumerator];
  147.    
  148.     while ((tabs = [windowEnumerator nextObject]))
  149.     {
  150.         if (isFirstWindow) {
  151.             BrowserDocument *document = [[[NSDocumentController sharedDocumentController] documents] objectAtIndex:0];
  152.             [self openBrowserWithTabs:[self _constructArrayOfTabURLsFromTabs:tabs] inDocument:document];
  153.            
  154.             isFirstWindow = NO;
  155.         }
  156.         else {
  157.             [self openBrowserWithTabs:[self _constructArrayOfTabURLsFromTabs:tabs] inDocument:nil];
  158.         }
  159.     }
  160. }
  161.  
  162. /*!
  163.  * Opens a new Safari browser belonging to the supplied document containing the supplied tabs.
  164.  *
  165.  * @param tabs     The array of tabs that the browser should open with
  166.  * @param document The document that the newly created tabs should belong to
  167.  */
  168. - (void)openBrowserWithTabs:(NSArray *)tabs inDocument:(BrowserDocument *)document
  169. {
  170.     if (document == nil) {
  171.         NSError *error;
  172.         document = [[NSDocumentController sharedDocumentController] openUntitledDocumentAndDisplay:YES error:&error];
  173.     }
  174.    
  175.     BrowserWindowController *windowController = [[document windowControllers] objectAtIndex:0];
  176.        
  177.     BOOL firstTab = YES;
  178.    
  179.     for (NSURL *tabURL in tabs)
  180.     {        
  181.         if (firstTab) {
  182.             [document goToURL:tabURL];
  183.             firstTab = NO;
  184.         }
  185.         else {
  186.             [[[windowController createTab] mainFrame] loadRequest:[NSURLRequest requestWithURL:tabURL]];
  187.         }
  188.     }
  189. }
  190.  
  191. /*!
  192.  * Constructs an array of NSURLs from the addresses of the supplied tabs array.
  193.  *
  194.  * @param tabs An array of STSessionRestoreTabs.
  195.  *
  196.  * @return A mutable array of NSURLs
  197.  */
  198. - (NSMutableArray *)_constructArrayOfTabURLsFromTabs:(NSArray *)tabs
  199. {
  200.     NSMutableArray *tabURLs = [NSMutableArray array];
  201.    
  202.     for (NSDictionary *tab in tabs)
  203.     {
  204.         NSURL *tabURL = [tab objectForKey:STTabURLKey];
  205.        
  206.         if (tabURL) [tabURLs addObject:tabURL];
  207.     }
  208.    
  209.     return tabURLs;
  210. }
  211.  
  212. @end
  213.