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