1/******************************************************************************
2 * $Id: URLSheetWindowController.m 13254 2012-03-13 03:39:56Z livings124 $
3 *
4 * Copyright (c) 2011-2012 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "URLSheetWindowController.h"
26#import "Controller.h"
27
28@interface URLSheetWindowController (Private)
29
30- (void) updateOpenButtonForURL: (NSString *) string;
31
32@end
33
34@implementation URLSheetWindowController
35
36NSString * urlString = nil;
37
38- (id) initWithController: (Controller *) controller
39{
40    if ((self = [self initWithWindowNibName: @"URLSheetWindow"]))
41    {
42        fController = controller;
43    }
44    return self;
45}
46
47- (void) awakeFromNib
48{
49    [fLabelField setStringValue: NSLocalizedString(@"Internet address of torrent file:", "URL sheet label")];
50    
51    if (urlString)
52    {
53        [fTextField setStringValue: urlString];
54        [fTextField selectText: self];
55        
56        [self updateOpenButtonForURL: urlString];
57    }
58    
59    [fOpenButton setTitle: NSLocalizedString(@"Open", "URL sheet button")];
60    [fCancelButton setTitle: NSLocalizedString(@"Cancel", "URL sheet button")];
61    
62    [fOpenButton sizeToFit];
63    [fCancelButton sizeToFit];
64    
65    //size the two buttons the same
66    NSRect openFrame = [fOpenButton frame];
67    openFrame.size.width += 10.0;
68    NSRect cancelFrame = [fCancelButton frame];
69    cancelFrame.size.width += 10.0;
70    
71    if (NSWidth(openFrame) > NSWidth(cancelFrame))
72        cancelFrame.size.width = NSWidth(openFrame);
73    else
74        openFrame.size.width = NSWidth(cancelFrame);
75    
76    openFrame.origin.x = NSWidth([[self window] frame]) - NSWidth(openFrame) - 20.0 + 6.0; //I don't know why the extra 6.0 is needed
77    [fOpenButton setFrame: openFrame];
78    
79    cancelFrame.origin.x = NSMinX(openFrame) - NSWidth(cancelFrame);
80    [fCancelButton setFrame: cancelFrame];
81}
82
83- (void) openURLEndSheet: (id) sender
84{
85    [[self window] orderOut: sender];
86    [NSApp endSheet: [self window] returnCode: 1];
87}
88
89- (void) openURLCancelEndSheet: (id) sender
90{
91    [[self window] orderOut: sender];
92    [NSApp endSheet: [self window] returnCode: 0];
93}
94
95- (NSString *) urlString
96{
97    return [[[fTextField stringValue] retain] autorelease];
98}
99
100- (void) controlTextDidChange: (NSNotification *) notification
101{
102    [self updateOpenButtonForURL: [fTextField stringValue]];
103}
104
105@end
106
107@implementation URLSheetWindowController (Private)
108
109- (void) updateOpenButtonForURL: (NSString *) string
110{
111    BOOL enable = YES;
112    if ([string isEqualToString: @""])
113        enable = NO;
114    else
115    {
116        NSRange prefixRange = [string rangeOfString: @"://"];
117        if (prefixRange.location != NSNotFound && [string length] == NSMaxRange(prefixRange))
118            enable = NO;
119    }
120    
121    [fOpenButton setEnabled: enable];
122}
123
124@end
125