2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2011-2022 Transmission authors and contributors.
|
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2011-02-10 00:51:25 +00:00
|
|
|
|
|
|
|
#import "URLSheetWindowController.h"
|
|
|
|
#import "Controller.h"
|
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface URLSheetWindowController ()
|
2011-02-11 03:17:36 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic) IBOutlet NSTextField* fLabelField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fTextField;
|
|
|
|
@property(nonatomic) IBOutlet NSButton* fOpenButton;
|
|
|
|
@property(nonatomic) IBOutlet NSButton* fCancelButton;
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateOpenButtonForURL:(NSString*)string;
|
2011-02-11 03:17:36 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2011-02-10 00:51:25 +00:00
|
|
|
@implementation URLSheetWindowController
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* urlString = nil;
|
2011-02-10 00:58:05 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
- (instancetype)init
|
2011-02-10 00:51:25 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self = [self initWithWindowNibName:@"URLSheetWindow"];
|
2011-02-10 00:51:25 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)awakeFromNib
|
2011-02-10 00:51:25 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fLabelField.stringValue = NSLocalizedString(@"Internet address of torrent file:", "URL sheet label");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.urlString)
|
2011-02-11 03:17:36 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTextField.stringValue = self.urlString;
|
|
|
|
[self.fTextField selectText:self];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self updateOpenButtonForURL:self.urlString];
|
2011-02-11 03:17:36 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fOpenButton.title = NSLocalizedString(@"Open", "URL sheet button");
|
|
|
|
self.fCancelButton.title = NSLocalizedString(@"Cancel", "URL sheet button");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fOpenButton sizeToFit];
|
|
|
|
[self.fCancelButton sizeToFit];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-11 03:05:57 +00:00
|
|
|
//size the two buttons the same
|
2022-02-22 16:04:20 +00:00
|
|
|
NSRect openFrame = self.fOpenButton.frame;
|
2011-02-11 03:05:57 +00:00
|
|
|
openFrame.size.width += 10.0;
|
2022-02-22 16:04:20 +00:00
|
|
|
NSRect cancelFrame = self.fCancelButton.frame;
|
2011-02-11 03:05:57 +00:00
|
|
|
cancelFrame.size.width += 10.0;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-11 03:05:57 +00:00
|
|
|
if (NSWidth(openFrame) > NSWidth(cancelFrame))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2011-02-11 03:05:57 +00:00
|
|
|
cancelFrame.size.width = NSWidth(openFrame);
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2011-02-11 03:05:57 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2011-02-11 03:05:57 +00:00
|
|
|
openFrame.size.width = NSWidth(cancelFrame);
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
openFrame.origin.x = NSWidth(self.window.frame) - NSWidth(openFrame) - 20.0 + 6.0; //I don't know why the extra 6.0 is needed
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fOpenButton.frame = openFrame;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-02-11 03:05:57 +00:00
|
|
|
cancelFrame.origin.x = NSMinX(openFrame) - NSWidth(cancelFrame);
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fCancelButton.frame = cancelFrame;
|
2011-02-10 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)openURLEndSheet:(id)sender
|
2011-02-10 00:51:25 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSApp endSheet:self.window returnCode:1];
|
2011-02-10 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)openURLCancelEndSheet:(id)sender
|
2011-02-10 00:51:25 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSApp endSheet:self.window returnCode:0];
|
2011-02-10 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)urlString
|
2011-02-10 00:51:25 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.fTextField.stringValue;
|
2011-02-10 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)controlTextDidChange:(NSNotification*)notification
|
2011-02-10 00:51:25 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self updateOpenButtonForURL:self.fTextField.stringValue];
|
2011-02-11 03:17:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateOpenButtonForURL:(NSString*)string
|
2011-02-11 03:17:36 +00:00
|
|
|
{
|
2011-02-10 00:51:25 +00:00
|
|
|
BOOL enable = YES;
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([string isEqualToString:@""])
|
|
|
|
{
|
2011-02-10 00:51:25 +00:00
|
|
|
enable = NO;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2011-02-10 00:51:25 +00:00
|
|
|
else
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRange prefixRange = [string rangeOfString:@"://"];
|
2021-08-07 07:27:56 +00:00
|
|
|
if (prefixRange.location != NSNotFound && string.length == NSMaxRange(prefixRange))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2011-02-10 00:51:25 +00:00
|
|
|
enable = NO;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2011-02-10 00:51:25 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fOpenButton.enabled = enable;
|
2011-02-10 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|