Source/ESSafariLinkToolbarItemPlugin.m
author Frank Dowsett <wixardy@adium.im>
Sun Aug 29 20:05:34 2010 -0400 (21 months ago)
changeset 3278 2dfee308da03
parent 3092 ffb42621b742
child 3679 f4294bb53b0f
permissions -rw-r--r--
Add Google Chrome support for link insertion.
David@0
     1
/*
David@0
     2
 * Adium is the legal property of its developers, whose names are listed in the copyright file included
David@0
     3
 * with this source distribution.
David@0
     4
 *
David@0
     5
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
David@0
     6
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
David@0
     7
 * or (at your option) any later version.
David@0
     8
 *
David@0
     9
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
David@0
    10
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
David@0
    11
 * Public License for more details.
David@0
    12
 *
David@0
    13
 * You should have received a copy of the GNU General Public License along with this program; if not,
David@0
    14
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
David@0
    15
 */
David@0
    16
David@0
    17
#import "ESApplescriptabilityController.h"
David@0
    18
#import <Adium/AIContentControllerProtocol.h>
David@0
    19
#import <Adium/AIToolbarControllerProtocol.h>
David@0
    20
#import "ESSafariLinkToolbarItemPlugin.h"
David@0
    21
#import <AIUtilities/AIToolbarUtilities.h>
David@0
    22
#import <AIUtilities/AIImageAdditions.h>
David@0
    23
#import <AIUtilities/AIWindowAdditions.h>
David@0
    24
#import <Adium/AIHTMLDecoder.h>
David@0
    25
David@0
    26
#define SAFARI_LINK_IDENTIFER	@"SafariLink"
David@0
    27
#define SAFARI_LINK_SCRIPT_PATH	[[NSBundle bundleForClass:[self class]] pathForResource:@"Safari.scpt" ofType:nil]
David@0
    28
sholt@3092
    29
@interface ESSafariLinkToolbarItemPlugin ()
sholt@3092
    30
- (void)insertSafariLink:(id)sender;
sholt@3092
    31
- (void)applescriptDidRun:(id)userInfo resultString:(NSString *)resultString;
sholt@3092
    32
@end
sholt@3092
    33
David@0
    34
/*!
David@0
    35
 * @class ESSafariLinkToolbarItemPlugin
David@0
    36
 * @brief Component to add a toolbar item which inserts a link to the active Safari web page
David@0
    37
 */
David@0
    38
@implementation ESSafariLinkToolbarItemPlugin
David@0
    39
David@0
    40
/*!
David@0
    41
 * @brief Install
David@0
    42
 */
David@0
    43
- (void)installPlugin
David@0
    44
{
David@0
    45
	CFURLRef	urlToDefaultBrowser = NULL;
David@0
    46
	NSString	*browserName = nil;
David@0
    47
	NSImage		*browserImage = nil;
David@0
    48
David@0
    49
	if (LSGetApplicationForURL((CFURLRef)[NSURL URLWithString:@"http://google.com"],
David@0
    50
							   kLSRolesViewer,
David@0
    51
							   NULL /*outAppRef*/,
David@0
    52
							   &urlToDefaultBrowser) != kLSApplicationNotFoundErr) {
David@0
    53
		NSString	*defaultBrowserName;
David@0
    54
		NSString	*defaultBrowserPath;
David@0
    55
David@0
    56
		defaultBrowserPath = [(NSURL *)urlToDefaultBrowser path];
David@0
    57
		defaultBrowserName = [[NSFileManager defaultManager] displayNameAtPath:defaultBrowserPath];
David@0
    58
David@0
    59
		//Is the default browser supported?
wixardy@3278
    60
		NSEnumerator *enumerator = [[NSArray arrayWithObjects:@"Safari", @"Firefox", @"OmniWeb", @"Camino", @"Shiira", @"NetNewsWire", @"Google Chrome", nil] objectEnumerator];
David@0
    61
		NSString	 *aSupportedBrowser;
David@0
    62
David@0
    63
		while ((aSupportedBrowser = [enumerator nextObject])) {
David@0
    64
			if ([defaultBrowserName rangeOfString:aSupportedBrowser
David@0
    65
										  options:(NSCaseInsensitiveSearch | NSLiteralSearch)].location != NSNotFound) {
David@0
    66
				//Use the name and image provided by the system if possible
David@0
    67
				browserName = defaultBrowserName;
David@0
    68
				browserImage = [[NSWorkspace sharedWorkspace] iconForFile:defaultBrowserPath];
David@0
    69
				break;
David@0
    70
			}
David@0
    71
		}
David@0
    72
	}
David@0
    73
	
David@0
    74
	if (urlToDefaultBrowser) {
David@0
    75
		CFRelease(urlToDefaultBrowser);
David@0
    76
	}
David@0
    77
	
David@0
    78
	if (!browserName || !browserImage) {
David@0
    79
		//Fall back on Safari and the image stored within our bundle if necessary
David@0
    80
		browserName = @"Safari";
David@0
    81
		browserImage = [NSImage imageNamed:@"Safari" forClass:[self class] loadLazily:YES];
David@0
    82
	}	
David@0
    83
David@0
    84
	//Remote the path extension if there is one (.app if the Finder is set to show extensions; no change otherwise)
David@0
    85
	browserName = [browserName stringByDeletingPathExtension];
David@0
    86
David@0
    87
	NSToolbarItem	*toolbarItem;
David@0
    88
	toolbarItem = [AIToolbarUtilities toolbarItemWithIdentifier:SAFARI_LINK_IDENTIFER
David@0
    89
														  label:[NSString stringWithFormat:AILocalizedString(@"%@ Link",nil), browserName]
David@0
    90
												   paletteLabel:[NSString stringWithFormat:AILocalizedString(@"Insert %@ Link",nil), browserName]
David@0
    91
														toolTip:[NSString stringWithFormat:AILocalizedString(@"Insert link to active page in %@",nil), browserName]
David@0
    92
														 target:self
David@0
    93
												settingSelector:@selector(setImage:)
David@0
    94
													itemContent:browserImage
David@0
    95
														 action:@selector(insertSafariLink:)
David@0
    96
														   menu:nil];
David@100
    97
	[adium.toolbarController registerToolbarItem:toolbarItem forToolbarType:@"TextEntry"];
David@0
    98
}
David@0
    99
David@0
   100
/*!
David@0
   101
 * @brief Insert a link to the active Safari page into the first responder if it is an NSTextView
David@0
   102
 */
David@0
   103
- (IBAction)insertSafariLink:(id)sender
David@0
   104
{
sholt@3088
   105
	NSWindow	*keyWin = [[NSApplication sharedApplication] keyWindow];
sholt@3088
   106
	NSTextView	*earliestTextView = (NSTextView *)[keyWin earliestResponderOfClass:[NSTextView class]];
David@0
   107
David@0
   108
	if (earliestTextView) {
David@0
   109
		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.")];
David@100
   110
		[adium.applescriptabilityController runApplescriptAtPath:SAFARI_LINK_SCRIPT_PATH
David@0
   111
														  function:@"substitute"
David@0
   112
														 arguments:arguments
David@0
   113
												   notifyingTarget:self
David@0
   114
														  selector:@selector(applescriptDidRun:resultString:)
David@0
   115
														  userInfo:earliestTextView];
David@0
   116
	} else {
David@0
   117
		NSBeep();
David@0
   118
	}
David@0
   119
}
David@0
   120
David@0
   121
/*!
David@0
   122
 * @brief A script finished running
David@0
   123
 */
David@0
   124
- (void)applescriptDidRun:(id)userInfo resultString:(NSString *)resultString
David@0
   125
{
David@0
   126
	NSTextView	*earliestTextView = (NSTextView *)userInfo;
David@0
   127
David@0
   128
	//If the script returns nil or fails, do nothing
David@0
   129
	if (resultString && [resultString length]) {
David@0
   130
		//Insert the script result - it should have returned an HTML link, so process it first
David@0
   131
		NSAttributedString	*attributedScriptResult;
David@0
   132
		NSDictionary		*attributes;
David@0
   133
		
David@0
   134
		attributedScriptResult = [AIHTMLDecoder decodeHTML:resultString];
David@0
   135
		
David@0
   136
		attributes = [[earliestTextView typingAttributes] copy];
David@0
   137
		[earliestTextView insertText:attributedScriptResult];
David@0
   138
		if (attributes) [earliestTextView setTypingAttributes:attributes];
David@0
   139
		[attributes release];
David@0
   140
David@0
   141
	} else {
David@0
   142
		NSBeep();		
David@0
   143
	}
David@0
   144
}
David@0
   145
David@0
   146
@end