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