/*
* $Id: STSessionHandler.m 225 2011-08-13 21:16:20Z stuart $
*
* SafariTabs
* http://stuconnolly.com/projects/safaritabs/
*
* Copyright (c) 2010 Stuart Connolly. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import <WebKit/WebKit.h>
#import "STSessionHandler.h"
#import "STSessionRestoreController.h"
#import "STBrowserWindowController.h"
#import "STSafariPluginHandler.h"
#import "BrowserWindowController.h"
#import "STUpdater.h"
#import "BrowserDocument.h"
#import "STConstants.h"
@interface STSessionHandler ()
@end
@implementation STSessionHandler
@synthesize isFirstWindow;
@synthesize launchedViaURLEvent;
/*!
* Initialises an instance of STSessionHanlder.
*
* @return The initialised instance
*/
- (id)init
{
if ((self = [super init])) {
isFirstWindow = YES;
launchedViaURLEvent = NO;
}
return self;
}
/*!
* Called just after the plugin has successfully been loaded to perform any necessary actions upon
* application startup.
*/
- (void)pluginLoaded
{
if (![self launchedViaURLEvent]) {
if ([defaults integerForKey:STTabRestoreQuitAction] == 0) {
if ([defaults integerForKey:STTabRestoreLaunchAction] == 0) {
[self restoreSavedTabs];
}
else if (([defaults integerForKey:STTabRestoreLaunchAction] == 1) && ([defaults objectForKey:STWindowsAndTabs])) {
[[[STSafariPluginHandler sharedInstance] sessionRestoreController] displaySessionRestorePanelUsingData:[defaults objectForKey:STWindowsAndTabs]];
}
}
}
// If required check for updates
if ([defaults boolForKey:STCheckForUpdates]) {
[[[STSafariPluginHandler sharedInstance] safariTabsUpdater] checkForUpdatesUsingDelegate:[STSafariPluginHandler sharedInstance] inBackground:YES];
}
}
/*!
* Saves Safari's currently open tabs. Depending on the user's preferences either all tabs in all open windows
* or only tabs within the active window will be saved.
*/
- (void)saveOpenTabs
{
if ([defaults integerForKey:STTabWindowSaveOption] == 0) {
id document;
while ((document = [e nextObject]))
{
id windowController = [[document windowControllers] objectAtIndex:0];
[windows addObject:[windowController currentlyOpenTabs]];
}
}
else if ([defaults integerForKey:STTabWindowSaveOption] == 1) {
id windowController
= [[[[NSDocumentController sharedDocumentController
] currentDocument
] windowControllers
] objectAtIndex
:0];
[windows addObject:[windowController currentlyOpenTabs]];
}
}
/*!
* Restores all the tabs the user had open during their last session. Depending on the user's preferences either
* all of their last sessions windows or just their active window at the time will be restored.
*/
- (void)restoreSavedTabs
{
NSData *windowData
= [defaults dataForKey
:STWindowsAndTabs
];
if (windowData != nil) {
if ([defaults integerForKey:STTabRestoreLaunchAction] == 0) {
[[[STSafariPluginHandler sharedInstance] sessionHandler] restoreTabsFromWindows:windows];
}
else if ([defaults integerForKey:STTabRestoreLaunchAction] == 1) {
BrowserDocument
*document
= [[[NSDocumentController sharedDocumentController
] documents
] objectAtIndex
:0];
[[[STSafariPluginHandler sharedInstance] sessionHandler] openBrowserWithTabs:[self _constructArrayOfTabURLsFromTabs:[windows objectAtIndex:0]] inDocument:document];
}
}
}
/*!
* Restores the previous session's tabs from the supplied array of windows.
*
* @param windows An array of windows for which the tabs belong to.
*/
- (void)restoreTabsFromWindows
:(NSArray *)windows;
{
NSEnumerator *windowEnumerator
= [windows objectEnumerator
];
while ((tabs = [windowEnumerator nextObject]))
{
if (isFirstWindow) {
BrowserDocument
*document
= [[[NSDocumentController sharedDocumentController
] documents
] objectAtIndex
:0];
[self openBrowserWithTabs:[self _constructArrayOfTabURLsFromTabs:tabs] inDocument:document];
isFirstWindow = NO;
}
else {
[self openBrowserWithTabs:[self _constructArrayOfTabURLsFromTabs:tabs] inDocument:nil];
}
}
}
/*!
* Opens a new Safari browser belonging to the supplied document containing the supplied tabs.
*
* @param tabs The array of tabs that the browser should open with
* @param document The document that the newly created tabs should belong to
*/
- (void)openBrowserWithTabs
:(NSArray *)tabs inDocument
:(BrowserDocument
*)document
{
if (document == nil) {
document
= [[NSDocumentController sharedDocumentController
] openUntitledDocumentAndDisplay
:YES error
:&error
];
}
BrowserWindowController *windowController = [[document windowControllers] objectAtIndex:0];
BOOL firstTab = YES;
for (NSURL *tabURL
in tabs
)
{
if (firstTab) {
[document goToURL:tabURL];
firstTab = NO;
}
else {
[[[windowController createTab
] mainFrame
] loadRequest
:[NSURLRequest requestWithURL
:tabURL
]];
}
}
}
/*!
* Constructs an array of NSURLs from the addresses of the supplied tabs array.
*
* @param tabs An array of STSessionRestoreTabs.
*
* @return A mutable array of NSURLs
*/
{
{
NSURL *tabURL
= [tab objectForKey
:STTabURLKey
];
if (tabURL) [tabURLs addObject:tabURL];
}
return tabURLs;
}
@end