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/AIAppleScriptAdditions.h>
24 #import <AIUtilities/AIWindowAdditions.h>
25 #import <Adium/AIHTMLDecoder.h>
26 #import <Adium/NDRunLoopMessenger.h>
28 #define SAFARI_LINK_IDENTIFER @"SafariLink"
29 #define SAFARI_LINK_SCRIPT_PATH [[NSBundle bundleForClass:[self class]] pathForResource:@"Safari.scpt" ofType:nil]
32 * @class ESSafariLinkToolbarItemPlugin
33 * @brief Component to add a toolbar item which inserts a link to the active Safari web page
35 @implementation ESSafariLinkToolbarItemPlugin
42 CFURLRef urlToDefaultBrowser = NULL;
43 NSString *browserName = nil;
44 NSImage *browserImage = nil;
46 if (LSGetApplicationForURL((CFURLRef)[NSURL URLWithString:@"http://google.com"],
49 &urlToDefaultBrowser) != kLSApplicationNotFoundErr) {
50 NSString *defaultBrowserName;
51 NSString *defaultBrowserPath;
53 defaultBrowserPath = [(NSURL *)urlToDefaultBrowser path];
54 defaultBrowserName = [[NSFileManager defaultManager] displayNameAtPath:defaultBrowserPath];
56 //Is the default browser supported?
57 NSEnumerator *enumerator = [[NSArray arrayWithObjects:@"Safari", @"Firefox", @"OmniWeb", @"Camino", @"Shiira", @"NetNewsWire", nil] objectEnumerator];
58 NSString *aSupportedBrowser;
60 while ((aSupportedBrowser = [enumerator nextObject])) {
61 if ([defaultBrowserName rangeOfString:aSupportedBrowser
62 options:(NSCaseInsensitiveSearch | NSLiteralSearch)].location != NSNotFound) {
63 //Use the name and image provided by the system if possible
64 browserName = defaultBrowserName;
65 browserImage = [[NSWorkspace sharedWorkspace] iconForFile:defaultBrowserPath];
71 if (urlToDefaultBrowser) {
72 CFRelease(urlToDefaultBrowser);
75 if (!browserName || !browserImage) {
76 //Fall back on Safari and the image stored within our bundle if necessary
77 browserName = @"Safari";
78 browserImage = [NSImage imageNamed:@"Safari" forClass:[self class] loadLazily:YES];
81 //Remote the path extension if there is one (.app if the Finder is set to show extensions; no change otherwise)
82 browserName = [browserName stringByDeletingPathExtension];
84 NSToolbarItem *toolbarItem;
85 toolbarItem = [AIToolbarUtilities toolbarItemWithIdentifier:SAFARI_LINK_IDENTIFER
86 label:[NSString stringWithFormat:AILocalizedString(@"%@ Link",nil), browserName]
87 paletteLabel:[NSString stringWithFormat:AILocalizedString(@"Insert %@ Link",nil), browserName]
88 toolTip:[NSString stringWithFormat:AILocalizedString(@"Insert link to active page in %@",nil), browserName]
90 settingSelector:@selector(setImage:)
91 itemContent:browserImage
92 action:@selector(insertSafariLink:)
94 [[adium toolbarController] registerToolbarItem:toolbarItem forToolbarType:@"TextEntry"];
98 * @brief Insert a link to the active Safari page into the first responder if it is an NSTextView
100 - (IBAction)insertSafariLink:(id)sender
102 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
103 NSTextView *earliestTextView = (NSTextView *)[keyWindow earliestResponderOfClass:[NSTextView class]];
105 if (earliestTextView) {
106 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.")];
107 [[adium applescriptabilityController] runApplescriptAtPath:SAFARI_LINK_SCRIPT_PATH
108 function:@"substitute"
111 selector:@selector(applescriptDidRun:resultString:)
112 userInfo:earliestTextView];
119 * @brief A script finished running
121 - (void)applescriptDidRun:(id)userInfo resultString:(NSString *)resultString
123 NSTextView *earliestTextView = (NSTextView *)userInfo;
125 //If the script returns nil or fails, do nothing
126 if (resultString && [resultString length]) {
127 //Insert the script result - it should have returned an HTML link, so process it first
128 NSAttributedString *attributedScriptResult;
129 NSDictionary *attributes;
131 attributedScriptResult = [AIHTMLDecoder decodeHTML:resultString];
133 attributes = [[earliestTextView typingAttributes] copy];
134 [earliestTextView insertText:attributedScriptResult];
135 if (attributes) [earliestTextView setTypingAttributes:attributes];
136 [attributes release];