William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | /*
* Adium is the legal property of its developers, whose names are listed in the copyright file included
* with this source distribution.
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "ESApplescriptabilityController.h"
#import <Adium/AIContentControllerProtocol.h>
#import <Adium/AIToolbarControllerProtocol.h>
#import "ESSafariLinkToolbarItemPlugin.h"
#import <AIUtilities/AIToolbarUtilities.h>
#import <AIUtilities/AIImageAdditions.h>
#import <AIUtilities/AIWindowAdditions.h>
#import <Adium/AIHTMLDecoder.h>
#define SAFARI_LINK_IDENTIFER @"SafariLink"
#define SAFARI_LINK_SCRIPT_PATH [[NSBundle bundleForClass:[self class]] pathForResource:@"Safari.scpt" ofType:nil]
/*!
* @class ESSafariLinkToolbarItemPlugin
* @brief Component to add a toolbar item which inserts a link to the active Safari web page
*/
@implementation ESSafariLinkToolbarItemPlugin
/*!
* @brief Install
*/
- (void)installPlugin
{
CFURLRef urlToDefaultBrowser = NULL;
NSString *browserName = nil;
NSImage *browserImage = nil;
if (LSGetApplicationForURL((CFURLRef)[NSURL URLWithString:@"http://google.com"],
kLSRolesViewer,
NULL /*outAppRef*/,
&urlToDefaultBrowser) != kLSApplicationNotFoundErr) {
NSString *defaultBrowserName;
NSString *defaultBrowserPath;
defaultBrowserPath = [(NSURL *)urlToDefaultBrowser path];
defaultBrowserName = [[NSFileManager defaultManager] displayNameAtPath:defaultBrowserPath];
//Is the default browser supported?
NSEnumerator *enumerator = [[NSArray arrayWithObjects:@"Safari", @"Firefox", @"OmniWeb", @"Camino", @"Shiira", @"NetNewsWire", nil] objectEnumerator];
NSString *aSupportedBrowser;
while ((aSupportedBrowser = [enumerator nextObject])) {
if ([defaultBrowserName rangeOfString:aSupportedBrowser
options:(NSCaseInsensitiveSearch | NSLiteralSearch)].location != NSNotFound) {
//Use the name and image provided by the system if possible
browserName = defaultBrowserName;
browserImage = [[NSWorkspace sharedWorkspace] iconForFile:defaultBrowserPath];
break;
}
}
}
if (urlToDefaultBrowser) {
CFRelease(urlToDefaultBrowser);
}
if (!browserName || !browserImage) {
//Fall back on Safari and the image stored within our bundle if necessary
browserName = @"Safari";
browserImage = [NSImage imageNamed:@"Safari" forClass:[self class] loadLazily:YES];
}
//Remote the path extension if there is one (.app if the Finder is set to show extensions; no change otherwise)
browserName = [browserName stringByDeletingPathExtension];
NSToolbarItem *toolbarItem;
toolbarItem = [AIToolbarUtilities toolbarItemWithIdentifier:SAFARI_LINK_IDENTIFER
label:[NSString stringWithFormat:AILocalizedString(@"%@ Link",nil), browserName]
paletteLabel:[NSString stringWithFormat:AILocalizedString(@"Insert %@ Link",nil), browserName]
toolTip:[NSString stringWithFormat:AILocalizedString(@"Insert link to active page in %@",nil), browserName]
target:self
settingSelector:@selector(setImage:)
itemContent:browserImage
action:@selector(insertSafariLink:)
menu:nil];
[adium.toolbarController registerToolbarItem:toolbarItem forToolbarType:@"TextEntry"];
}
/*!
* @brief Insert a link to the active Safari page into the first responder if it is an NSTextView
*/
- (IBAction)insertSafariLink:(id)sender
{
NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
NSTextView *earliestTextView = (NSTextView *)[keyWindow earliestResponderOfClass:[NSTextView class]];
if (earliestTextView) {
NSArray *arguments = [NSArray arrayWithObject:AILocalizedString(@"Multiple browsers are open. Please select one link:", "Prompt when more than one web browser is available when inserting a link from the active browser.")];
[adium.applescriptabilityController runApplescriptAtPath:SAFARI_LINK_SCRIPT_PATH
function:@"substitute"
arguments:arguments
notifyingTarget:self
selector:@selector(applescriptDidRun:resultString:)
userInfo:earliestTextView];
} else {
NSBeep();
}
}
/*!
* @brief A script finished running
*/
- (void)applescriptDidRun:(id)userInfo resultString:(NSString *)resultString
{
NSTextView *earliestTextView = (NSTextView *)userInfo;
//If the script returns nil or fails, do nothing
if (resultString && [resultString length]) {
//Insert the script result - it should have returned an HTML link, so process it first
NSAttributedString *attributedScriptResult;
NSDictionary *attributes;
attributedScriptResult = [AIHTMLDecoder decodeHTML:resultString];
attributes = [[earliestTextView typingAttributes] copy];
[earliestTextView insertText:attributedScriptResult];
if (attributes) [earliestTextView setTypingAttributes:attributes];
[attributes release];
} else {
NSBeep();
}
}
@end
|