Add Google Chrome support for link insertion.
2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3 * with this source distribution.
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.
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.
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.
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>
26 #define SAFARI_LINK_IDENTIFER @"SafariLink"
27 #define SAFARI_LINK_SCRIPT_PATH [[NSBundle bundleForClass:[self class]] pathForResource:@"Safari.scpt" ofType:nil]
29 @interface ESSafariLinkToolbarItemPlugin ()
30 - (void)insertSafariLink:(id)sender;
31 - (void)applescriptDidRun:(id)userInfo resultString:(NSString *)resultString;
35 * @class ESSafariLinkToolbarItemPlugin
36 * @brief Component to add a toolbar item which inserts a link to the active Safari web page
38 @implementation ESSafariLinkToolbarItemPlugin
45 CFURLRef urlToDefaultBrowser = NULL;
46 NSString *browserName = nil;
47 NSImage *browserImage = nil;
49 if (LSGetApplicationForURL((CFURLRef)[NSURL URLWithString:@"http://google.com"],
52 &urlToDefaultBrowser) != kLSApplicationNotFoundErr) {
53 NSString *defaultBrowserName;
54 NSString *defaultBrowserPath;
56 defaultBrowserPath = [(NSURL *)urlToDefaultBrowser path];
57 defaultBrowserName = [[NSFileManager defaultManager] displayNameAtPath:defaultBrowserPath];
59 //Is the default browser supported?
60 NSEnumerator *enumerator = [[NSArray arrayWithObjects:@"Safari", @"Firefox", @"OmniWeb", @"Camino", @"Shiira", @"NetNewsWire", @"Google Chrome", nil] objectEnumerator];
61 NSString *aSupportedBrowser;
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];
74 if (urlToDefaultBrowser) {
75 CFRelease(urlToDefaultBrowser);
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];
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];
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]
93 settingSelector:@selector(setImage:)
94 itemContent:browserImage
95 action:@selector(insertSafariLink:)
97 [adium.toolbarController registerToolbarItem:toolbarItem forToolbarType:@"TextEntry"];
101 * @brief Insert a link to the active Safari page into the first responder if it is an NSTextView
103 - (IBAction)insertSafariLink:(id)sender
105 NSWindow *keyWin = [[NSApplication sharedApplication] keyWindow];
106 NSTextView *earliestTextView = (NSTextView *)[keyWin earliestResponderOfClass:[NSTextView class]];
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"
114 selector:@selector(applescriptDidRun:resultString:)
115 userInfo:earliestTextView];
122 * @brief A script finished running
124 - (void)applescriptDidRun:(id)userInfo resultString:(NSString *)resultString
126 NSTextView *earliestTextView = (NSTextView *)userInfo;
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;
134 attributedScriptResult = [AIHTMLDecoder decodeHTML:resultString];
136 attributes = [[earliestTextView typingAttributes] copy];
137 [earliestTextView insertText:attributedScriptResult];
138 if (attributes) [earliestTextView setTypingAttributes:attributes];
139 [attributes release];