Merged [15040]: Trying some magic: 5 seconds after the last unreachable host is repor...
[adiumx.git] / Source / ESSafariLinkToolbarItemPlugin.m
blob87672cf7aca30f471629a071014a17d6fda1378b
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 "AIContentController.h"
18 #import "AIToolbarController.h"
19 #import "ESSafariLinkToolbarItemPlugin.h"
20 #import <AIUtilities/AIToolbarUtilities.h>
21 #import <AIUtilities/ESImageAdditions.h>
22 #import <AIUtilities/AIAppleScriptAdditions.h>
23 #import <AIUtilities/AIWindowAdditions.h>
24 #import <Adium/AIHTMLDecoder.h>
25 #import <Adium/NDRunLoopMessenger.h>
27 #define SAFARI_LINK_IDENTIFER   @"SafariLink"
28 #define SAFARI_LINK_SCRIPT_PATH [[NSBundle bundleForClass:[self class]] pathForResource:@"Safari.scpt" ofType:nil]
30 @interface ESSafariLinkToolbarItemPlugin (PRIVATE)
31 - (NSString *)_executeSafariLinkScript;
32 @end
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
40 /*!
41  * @brief Install
42  */
43 - (void)installPlugin
45         safariLinkScript = nil;
47         //SafariLink
48         NSToolbarItem   *toolbarItem;
49         toolbarItem = [AIToolbarUtilities toolbarItemWithIdentifier:SAFARI_LINK_IDENTIFER
50                                                                                                                   label:AILocalizedString(@"Safari Link",nil)
51                                                                                                    paletteLabel:AILocalizedString(@"Insert Safari Link",nil)
52                                                                                                                 toolTip:AILocalizedString(@"Insert link to active page in Safari",nil)
53                                                                                                                  target:self
54                                                                                                 settingSelector:@selector(setImage:)
55                                                                                                         itemContent:[NSImage imageNamed:@"Safari" forClass:[self class]]
56                                                                                                                  action:@selector(insertSafariLink:)
57                                                                                                                    menu:nil];
58         [[adium toolbarController] registerToolbarItem:toolbarItem forToolbarType:@"TextEntry"];
61 /*!
62  * @brief Deallocate
63  */
64 - (void)dealloc
66         [safariLinkScript release]; safariLinkScript = nil;
67         [super dealloc];
70 /*!
71  * @brief Insert a link to the active Safari page into the first responder if it is an NSTextView
72  */
73 - (IBAction)insertSafariLink:(id)sender
75         NSWindow        *keyWindow = [[NSApplication sharedApplication] keyWindow];
76         NSTextView      *earliestTextView = (NSTextView *)[keyWindow earliestResponderOfClass:[NSTextView class]];
77         
78         if (earliestTextView) {
79                 NSTask                  *scriptTask;
80                 NSMutableArray  *applescriptRunnerArguments = [NSArray arrayWithObjects:SAFARI_LINK_SCRIPT_PATH, @"substitute", nil];
81                 NSString                *applescriptRunnerPath;
82                 NSPipe                  *standardOuptut = [NSPipe pipe];
83                 NSString                *scriptResult;
85                 //Find the path to the ApplescriptRunner application
86                 applescriptRunnerPath = [[NSBundle mainBundle] pathForResource:@"AdiumApplescriptRunner"
87                                                                                                                                 ofType:nil
88                                                                                                                    inDirectory:nil];
89                 //Set up our task
90                 scriptTask = [[NSTask alloc] init];
91                 [scriptTask setLaunchPath:applescriptRunnerPath];
92                 [scriptTask setArguments:applescriptRunnerArguments];
93                 [scriptTask setStandardOutput:standardOuptut];
95                 //Launch, then block until it's finished
96                 [scriptTask launch];
97                 [scriptTask waitUntilExit];
99                 //Retrieve the HTML returned by the script via standardOutput
100                 scriptResult = [[NSString alloc] initWithData:[[standardOuptut fileHandleForReading] readDataToEndOfFile]
101                                                                                          encoding:NSUTF8StringEncoding];
103                 //If the script returns nil or fails, do nothing
104                 if (scriptResult && [scriptResult length]) {
105                         //Insert the script result - it should have returned an HTML link, so process it first
106                         NSAttributedString      *attributedScriptResult;
107                         NSDictionary            *attributes;
109                         attributedScriptResult = [AIHTMLDecoder decodeHTML:scriptResult];
111                         attributes = [[earliestTextView typingAttributes] copy];
112                         [earliestTextView insertText:attributedScriptResult];
113                         if (attributes) [earliestTextView setTypingAttributes:attributes];
114                         [attributes release];
115                 }
117                 [scriptResult release];
118                 [scriptTask release];
119         }
122 @end