Unescape the HREF attribute's text before passing it to NSURL which does not expect...
[adiumx.git] / Source / ESSafariLinkToolbarItemPlugin.m
blob2a4e0a2c4e8889e0d852bd2097d7edf76f03b0b4
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  */
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]
31 /*!
32  * @class ESSafariLinkToolbarItemPlugin
33  * @brief Component to add a toolbar item which inserts a link to the active Safari web page
34  */
35 @implementation ESSafariLinkToolbarItemPlugin
37 /*!
38  * @brief Install
39  */
40 - (void)installPlugin
42         CFURLRef        urlToDefaultBrowser = NULL;
43         NSString        *browserName = nil;
44         NSImage         *browserImage = nil;
46         if (LSGetApplicationForURL((CFURLRef)[NSURL URLWithString:@"http://google.com"],
47                                                            kLSRolesViewer,
48                                                            NULL /*outAppRef*/,
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];
66                                 break;
67                         }
68                 }
69         }
70         
71         if (urlToDefaultBrowser) {
72                 CFRelease(urlToDefaultBrowser);
73         }
74         
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];
79         }       
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]
89                                                                                                                  target:self
90                                                                                                 settingSelector:@selector(setImage:)
91                                                                                                         itemContent:browserImage
92                                                                                                                  action:@selector(insertSafariLink:)
93                                                                                                                    menu:nil];
94         [[adium toolbarController] registerToolbarItem:toolbarItem forToolbarType:@"TextEntry"];
97 /*!
98  * @brief Insert a link to the active Safari page into the first responder if it is an NSTextView
99  */
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"
109                                                                                                                  arguments:arguments
110                                                                                                    notifyingTarget:self
111                                                                                                                   selector:@selector(applescriptDidRun:resultString:)
112                                                                                                                   userInfo:earliestTextView];
113         } else {
114                 NSBeep();
115         }
119  * @brief A script finished running
120  */
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;
130                 
131                 attributedScriptResult = [AIHTMLDecoder decodeHTML:resultString];
132                 
133                 attributes = [[earliestTextView typingAttributes] copy];
134                 [earliestTextView insertText:attributedScriptResult];
135                 if (attributes) [earliestTextView setTypingAttributes:attributes];
136                 [attributes release];
138         } else {
139                 NSBeep();               
140         }
143 @end