/*
* STSafariPlugin.m
*
* SafariTabs
*
* Copyright (c) 2007 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#import "STSafariPlugin.h"
#import "STAppController.h"
#import "STBrowserWindowController.h"
#import "STConstants.h"
typedef struct objc_method *Method;
struct objc_method {
SEL method_name;
char *method_types;
IMP method_imp;
};
@implementation STSafariPlugin
////////////////////////////////////////////// *** renameSelector
+ (BOOL)renameSelector:(Class)_class fromOldSelector:(SEL)_oldSelector toNewSelector:(SEL)_newSelector
{
Method method = nil;
method = class_getInstanceMethod(_class, _oldSelector);
if (method == nil) {
return NO;
}
method->method_name = _newSelector;
return YES;
}
////////////////////////////////////////////// *** load
+ (void)load
{
// A special method called by SIMBL once the application has started and all classes are initialized.
// Exchange Safari's applicationWillTerminate: with custom one
[STSafariPlugin renameSelector:[AppController class] fromOldSelector:@selector(applicationShouldTerminate:) toNewSelector:@selector(_safari_applicationShouldTerminate:)];
[STSafariPlugin renameSelector:[AppController class] fromOldSelector:@selector(_st_applicationShouldTerminate:) toNewSelector:@selector(applicationShouldTerminate:)];
// Exchange Safari's windowShouldClose: with custom one
[STSafariPlugin renameSelector:[BrowserWindowController class] fromOldSelector:@selector(windowShouldClose:) toNewSelector:@selector(_safari_windowShouldClose:)];
[STSafariPlugin renameSelector:[BrowserWindowController class] fromOldSelector:@selector(_st_windowShouldClose:) toNewSelector:@selector(windowShouldClose:)];
// Exchange Safari's createTab: with custom one
[STSafariPlugin renameSelector:[BrowserWindowController class] fromOldSelector:@selector(createTab) toNewSelector:@selector(_safari_createTab)];
[STSafariPlugin renameSelector:[BrowserWindowController class] fromOldSelector:@selector(_st_createTab) toNewSelector:@selector(createTab)];
// Exchange Safari's performQuickSearch: with custom one
[STSafariPlugin renameSelector:[BrowserWindowController class] fromOldSelector:@selector(performQuickSearch:) toNewSelector:@selector(_safari_performQuickSearch:)];
[STSafariPlugin renameSelector:[BrowserWindowController class] fromOldSelector:@selector(_st_performQuickSearch:) toNewSelector:@selector(performQuickSearch:)];
// Load our custom menu items
[NSBundle loadNibNamed
:@"MenuAdditions" owner
:[STSafariPlugin sharedInstance
]];
}
////////////////////////////////////////////// *** sharedInstance
+ (STSafariPlugin *)sharedInstance
{
// Returns a single static instance of the plugin
static STSafariPlugin *plugin = nil;
if (plugin == nil) {
plugin = [[STSafariPlugin alloc] init];
}
return plugin;
}
////////////////////////////////////////////// *** terminateWithoutWarning
- (BOOL)terminateWithoutWarning
{
return terminateWithoutWarning;
}
////////////////////////////////////////////// *** setTerminateWithoutWarning
- (void)setTerminateWithoutWarning:(BOOL)warning
{
terminateWithoutWarning = warning;
}
////////////////////////////////////////////// *** isLoaded
- (BOOL)isLoaded
{
return YES;
}
////////////////////////////////////////////// *** awakeFromNib
- (void)awakeFromNib
{
NSMenu *fileMenu
= [[safariMenuBar itemAtIndex
:1] submenu
];
NSMenuItem *closeWithoutWarningMenuItem
= [menuAdditions itemAtIndex
:0];
NSMenuItem *quitWithoutWarningMenuItem
= [menuAdditions itemAtIndex
:1];
if ([[NSUserDefaults standardUserDefaults
] boolForKey
:STAddCloseNoWarningMenuItem
]) {
NSEnumerator *itemEnumerator
= [[fileMenu itemArray
] objectEnumerator
];
// Add the 'Close Window Without Warning' menu item
while(item = [itemEnumerator nextObject]) {
if ([item action] == @selector(performClose:)) {
int index = [fileMenu indexOfItem:item];
[fileMenu insertItem:[closeWithoutWarningMenuItem copy] atIndex:(index + 1)];
break;
}
}
}
if ([[NSUserDefaults standardUserDefaults
] boolForKey
:STAddQuitNoWarningMenuItem
]) {
// Add the 'Quit Safari Without Warning' menu item
[[[safariMenuBar itemAtIndex:0] submenu] addItem:[quitWithoutWarningMenuItem copy]];
}
terminateWithoutWarning = NO;
}
////////////////////////////////////////////// *** closeWindowWithoutWarning
- (IBAction)closeWindowWithoutWarning:(id)sender
{
}
////////////////////////////////////////////// *** quitWithoutWarning
- (IBAction)quitWithoutWarning:(id)sender
{
terminateWithoutWarning = YES;
}
////////////////////////////////////////////// *** validateUserInterfaceItem
- (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item
{
if ([item action] == @selector(closeWindowWithoutWarning:)) {
return YES;
}
}
else if ([item action] == @selector(quitWithoutWarning:)) {
return YES;
}
// Otherwise ask Safari to validate the item
return NO;
}
@end