/*
* $Id: STSessionRestoreController.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 "STSessionRestoreController.h"
#import "STSessionRestoreTextFieldCell.h"
#import "STSafariPluginHandler.h"
#import "STSessionHandler.h"
#import "STSessionRestoreTab.h"
#import "BrowserDocument.h"
#import "STConstants.h"
#import "STUtilities.h"
@interface STSessionRestoreController ()
- (NSUInteger)_numberOfSelectedTabs;
@end
@implementation STSessionRestoreController
/*!
* Initializes an instance of STSessionRestoreController with the supplied delegate.
*
* @return The initialized object
*/
- (id)initWithDelegate:(id)delegate
{
if ((self = [super initWithWindowNibName:@"STSessionRestore"])) {
_delegate = delegate;
}
return self;
}
/*!
* Setup various interface controls.
*/
- (void)awakeFromNib
{
[[tabRestoreTableView tableColumnWithIdentifier:STTableViewTabTitleColumnIdentifier] setDataCell:[[[STSessionRestoreTextFieldCell alloc] init] autorelease]];
[[NSGraphicsContext currentContext
] setImageInterpolation
:NSImageInterpolationHigh
];
// Make pretty icons
[self updateUserInterface];
tabRestoreTableViewBoxIsVisible = YES;
[self toggleTabRestoreTableView:self];
}
/*!
* Sets the tabs to restore for the last session.
*
* @param tabs The array of tabs
*/
{
if (_sessionRestoreTabs != tabs) {
[_sessionRestoreTabs release];
_sessionRestoreTabs = [tabs retain];
}
}
#pragma mark -
#pragma mark IB Action Methods
/*!
* Restores all tabs.
*
* @param sender The object calling the method
*/
- (IBAction)restoreAllTabs:(id)sender
{
[[[STSafariPluginHandler sharedInstance
] sessionHandler
] restoreTabsFromWindows
:(NSArray *)[NSKeyedUnarchiver unarchiveObjectWithData
:[[NSUserDefaults standardUserDefaults
] dataForKey
:STWindowsAndTabs
]]];
[self close];
[NSApp abortModal];
}
/*!
* Cancels tab restoration.
*
* @param sender The object calling the method
*/
- (IBAction)dontRestoreTabs:(id)sender
{
[self close];
[NSApp abortModal];
}
/*!
* Restores the selected tabs.
*
* @param sender The object calling the method
*/
- (IBAction)restoreSelectedTabs:(id)sender
{
STSessionHandler *sessionHandler = [[STSafariPluginHandler sharedInstance] sessionHandler];
if ([_sessionRestoreTabs count] == [self _numberOfSelectedTabs]) {
}
else {
[sessionHandler openBrowserWithTabs
:[self _constructArrayOfSelectedTabURLs
:_sessionRestoreTabs
] inDocument
:[[[NSDocumentController sharedDocumentController
] documents
] objectAtIndex
:0]];
}
[self close];
[NSApp abortModal];
}
/*!
* Toggles (shows/hides) the tab restore table view.
*
* @param sender The object calling the method
*/
- (IBAction)toggleTabRestoreTableView:(id)sender
{
// Performs the calculations necessary for the showing and hiding and subsequent automatic window
// resizing whenever the user clicks the sender.
//
// Adapted from Larry L. Fransson at http://homepage.mac.com/lfransson/Cocoa/FileSharing1.html
CGFloat newHeight;
NSView *view
= [[showTabRestoreTableViewButton superview
] superview
];
NSRect frame
= [NSWindow contentRectForFrameRect
:[[self window
] frame
] styleMask
:[[self window
] styleMask
]];
tableViewBoxHeight = [tabRestoreTableViewBox bounds].size.height;
NSUInteger showButtonMask = [showTabRestoreTableViewButton autoresizingMask];
// Set views not to autoresize
[view setAutoresizingMask:0];
[showTabRestoreTableViewButton setAutoresizingMask:0];
[dontRestoreTabsButton setAutoresizingMask:0];
[restoreTabsButton setAutoresizingMask:0];
if (tabRestoreTableViewBoxIsVisible) {
newHeight = (frame.size.height - tableViewBoxHeight);
frame.origin.y += frame.size.height;
frame.origin.y -= newHeight;
frame.size.height = newHeight;
[selectedTabCountTextField setHidden:YES];
[tabRestoreTableViewBox setHidden:YES];
frame
= [NSWindow frameRectForContentRect
:frame styleMask
:[[tabRestoreTableViewBox window
] styleMask
]];
[[tabRestoreTableViewBox window] setFrame:frame display:YES animate:YES];
[showTabRestoreTableViewButton setHidden:NO];
[dontRestoreTabsButton setTitle:STLocalizedString(@"Don't restore")];
[dontRestoreTabsButton setAction:@selector(dontRestoreTabs:)];
[restoreTabsButton setTitle:STLocalizedString(@"Restore All Tabs")];
[restoreTabsButton setAction:@selector(restoreAllTabs:)];
[restoreTabsButton setEnabled:YES];
// Re-enable autoresizing
[view setAutoresizingMask:showButtonMask];
[showTabRestoreTableViewButton setAutoresizingMask:showButtonMask];
[dontRestoreTabsButton setAutoresizingMask:showButtonMask];
[restoreTabsButton setAutoresizingMask:showButtonMask];
tabRestoreTableViewBoxIsVisible = NO;
}
else {
newHeight = (frame.size.height + tableViewBoxHeight);
frame.origin.y += frame.size.height;
frame.origin.y -= newHeight;
frame.size.height = newHeight;
frame
= [NSWindow frameRectForContentRect
:frame styleMask
:[[tabRestoreTableViewBox window
] styleMask
]];
[[tabRestoreTableViewBox window] setFrame:frame display:YES animate:YES];
[showTabRestoreTableViewButton setHidden:YES];
[tabRestoreTableViewBox setHidden:NO];
[selectedTabCountTextField setHidden:NO];
[dontRestoreTabsButton setTitle:STLocalizedString(@"Cancel")];
[dontRestoreTabsButton setAction:@selector(toggleTabRestoreTableView:)];
[restoreTabsButton setTitle:STLocalizedString(@"Restore Tabs")];
[restoreTabsButton setAction:@selector(restoreSelectedTabs:)];
[view setAutoresizingMask:showButtonMask];
[showTabRestoreTableViewButton setAutoresizingMask:showButtonMask];
[dontRestoreTabsButton setAutoresizingMask:showButtonMask];
[restoreTabsButton setAutoresizingMask:showButtonMask];
tabRestoreTableViewBoxIsVisible = YES;
}
[self updateUserInterface];
}
/*!
* Toggles (enables/disables) the 'Dont ask to restore again' checkbox.
*
* @param sender The object calling the method
*/
- (IBAction)toggleDontAskToRestoreAgain:(id)sender
{
[[NSUserDefaults standardUserDefaults
] setInteger
:(![sender state
]) forKey
:STTabRestoreLaunchAction
];
}
/*!
* Displays the session restore panel with the supplied archived window data.
*
* @param data The archived window data
*/
- (void)displaySessionRestorePanelUsingData
:(NSData *)data
{
[self setSessionRestoreTabs:[self _constructArrayOfTabsFromArray:windows]];
if ([_sessionRestoreTabs count] != 0) {
[NSApp runModalForWindow:[self window]];
}
}
/*!
* Updates the user interface with respect to the tab restore table view and number of selected tabs.
*/
- (void)updateUserInterface
{
[tabRestoreTableView reloadData];
if (tabRestoreTableViewBoxIsVisible) {
[restoreTabsButton setEnabled:([self _numberOfSelectedTabs] > 0)];
}
[selectedTabCountTextField setStringValue
:[NSString stringWithFormat
:STLocalizedString
(@"Selected tab count"),
(unsigned long)[self _numberOfSelectedTabs
],
(unsigned long)[_sessionRestoreTabs count
]]];
}
#pragma mark -
/*!
* @brief Dealloc.
*/
- (void)dealloc
{
[_sessionRestoreTabs release], _sessionRestoreTabs = nil;
[super dealloc];
}
/*!
* Returns the number of selected tabs within the session restore table view.
*
* @return An unsigned integer indicating the number of selected tabs.
*/
- (NSUInteger)_numberOfSelectedTabs
{
NSUInteger selectedCount = 0;
for (STSessionRestoreTab *tab in _sessionRestoreTabs)
{
if ([[tab tabEnabled] boolValue]) selectedCount++;
}
return selectedCount;
}
/*!
* Constructs an array of NSURLs of the currently selected tabs within the session restore table view.
*
* @param tabs An array of all the tabs within the table view.
*
* @return An array of NUSRLs
*/
{
for (STSessionRestoreTab *tab in tabs)
{
if ([[tab tabEnabled] boolValue]) {
[tabURLs addObject:[tab tabURL]];
}
}
return tabURLs;
}
/*!
* Constructs an array of STSessionRestoreTabs from the supplied tab array.
*
* @param array An array of saved tabs
*
* @return An array of STSessionRestoreTabs
*/
{
{
while ((tabDictionary = [tabEnumerator nextObject]))
{
NSString *tabTile
= [tabDictionary objectForKey
:STTabTitleKey
];
NSURL *tabURL
= [tabDictionary objectForKey
:STTabURLKey
];
NSImage *tabIcon
= [tabDictionary objectForKey
:STTabIconKey
];
if ((tabTile) && (tabURL) && (tabIcon)) {
[tabs addObject:[STSessionRestoreTab tabWithTitle:tabTile url:tabURL icon:tabIcon]];
}
}
}
return tabs;
}
@end